From 2d9f6acfac34c33cc22405f153b45bb4c0d9059e Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 25 Oct 2017 14:54:20 -0700 Subject: [PATCH] Add a test_fixtures GN rule that allows unittests to reference fixtures. (#4280) --- testing/BUILD.gn | 6 +- testing/build/gen_fixtures_location_symbol.py | 27 +++++++++ testing/testing.cc | 11 ++++ testing/testing.gni | 55 +++++++++++++++++++ testing/testing.h | 19 +++++++ 5 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 testing/build/gen_fixtures_location_symbol.py create mode 100644 testing/testing.cc create mode 100644 testing/testing.gni create mode 100644 testing/testing.h diff --git a/testing/BUILD.gn b/testing/BUILD.gn index f95b9d685..32a34254a 100644 --- a/testing/BUILD.gn +++ b/testing/BUILD.gn @@ -7,13 +7,13 @@ source_set("testing") { sources = [ "$flutter_root/testing/run_all_unittests.cc", + "$flutter_root/testing/testing.cc", + "$flutter_root/testing/testing.h", ] public_deps = [ "//third_party/gtest", ] - public_configs = [ - "$flutter_root:config", - ] + public_configs = [ "$flutter_root:config" ] } diff --git a/testing/build/gen_fixtures_location_symbol.py b/testing/build/gen_fixtures_location_symbol.py new file mode 100644 index 000000000..c2c49d087 --- /dev/null +++ b/testing/build/gen_fixtures_location_symbol.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# Copyright 2017 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import argparse +import subprocess +import sys +import os + + +def main(): + parser = argparse.ArgumentParser( + description='Create the symbol specifying the location of test fixtures.') + + parser.add_argument('--fixtures_location_file', type=str, required=True) + parser.add_argument('--fixtures_location', type=str, required=True) + + args = parser.parse_args() + + with open(args.fixtures_location_file, 'w') as file: + file.write('namespace testing {const char* GetFixturesPath() {return "%s";}}' + % args.fixtures_location) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/testing/testing.cc b/testing/testing.cc new file mode 100644 index 000000000..835ce4fe0 --- /dev/null +++ b/testing/testing.cc @@ -0,0 +1,11 @@ +// Copyright 2017 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "testing.h" + +namespace testing { + +// + +} // namespace testing diff --git a/testing/testing.gni b/testing/testing.gni new file mode 100644 index 000000000..a58876c38 --- /dev/null +++ b/testing/testing.gni @@ -0,0 +1,55 @@ +# Copyright 2016 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +template("test_fixtures") { + testonly = true + + assert(defined(invoker.fixtures), "Test fixtures must be specified.") + + fixtures_name_target_name = target_name + "_gen_fixtures_name" + fixtures_source_set_target_name = target_name + "_gen_fixtures_source_set" + fixtures_copy_target_name = target_name + "_copy_fixtures" + + fixtures_location = "$target_gen_dir/fixtures" + fixtures_location_file = "$target_gen_dir/test_fixtures_location.cc" + + action(fixtures_name_target_name) { + script = "$flutter_root/testing/build/gen_fixtures_location_symbol.py" + + outputs = [ + fixtures_location_file, + ] + + args = [ + "--fixtures_location_file", + rebase_path(fixtures_location_file), + "--fixtures_location", + rebase_path(fixtures_location), + ] + } + + source_set(fixtures_source_set_target_name) { + sources = [ + fixtures_location_file, + ] + + deps = [ + ":$fixtures_name_target_name", + ] + } + + copy(fixtures_copy_target_name) { + sources = invoker.fixtures + outputs = [ + "$fixtures_location/{{source_file_part}}", + ] + } + + group(target_name) { + deps = [ + ":$fixtures_copy_target_name", + ":$fixtures_source_set_target_name", + ] + } +} diff --git a/testing/testing.h b/testing/testing.h new file mode 100644 index 000000000..2377ce511 --- /dev/null +++ b/testing/testing.h @@ -0,0 +1,19 @@ +// Copyright 2017 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef TESTING_TESTING_H_ +#define TESTING_TESTING_H_ + +#include "gtest/gtest.h" + +namespace testing { + +// Returns the directory containing the test fixture for the target if this +// target has fixtures configured. If there are no fixtures, this is a link +// error. +const char* GetFixturesPath(); + +} // namespace testing + +#endif // TESTING_TESTING_H_ -- GitLab