From 46c478dbf3c198573160bb363c4b3c3322c61025 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Thu, 27 May 2021 12:18:10 +0200 Subject: [PATCH 1/8] Add `harness` attribute to rust_test When set to False, rustc will not include libtest for the test, therefore the test author is responsible for implementing `main` function and running the tests. --- rust/private/rust.bzl | 9 ++- test/unit/test_harness/BUILD.bazel | 4 ++ test/unit/test_harness/mytest.rs | 0 test/unit/test_harness/mytest_noharness.rs | 1 + test/unit/test_harness/test_harness_test.bzl | 68 ++++++++++++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 test/unit/test_harness/BUILD.bazel create mode 100644 test/unit/test_harness/mytest.rs create mode 100644 test/unit/test_harness/mytest_noharness.rs create mode 100644 test/unit/test_harness/test_harness_test.bzl diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index a7223c46b2..e06b23b89b 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -437,7 +437,7 @@ def _rust_test_common(ctx, toolchain, output): toolchain = toolchain, crate_type = crate_type, crate_info = crate_info, - rust_flags = ["--test"], + rust_flags = ["--test"] if ctx.attr.harness else ["--cfg", "test"], ) return _create_test_launcher(ctx, toolchain, output, providers) @@ -703,6 +703,13 @@ _rust_test_attrs = { ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. """), ), + "harness": attr.bool( + mandatory = False, + default = True, + doc = _tidy("""\ + Whether to use libtest (defaults to True). + """), + ), "_launcher": attr.label( executable = True, default = Label("//util/launcher:launcher"), diff --git a/test/unit/test_harness/BUILD.bazel b/test/unit/test_harness/BUILD.bazel new file mode 100644 index 0000000000..fd1d44960f --- /dev/null +++ b/test/unit/test_harness/BUILD.bazel @@ -0,0 +1,4 @@ +load(":test_harness_test.bzl", "test_harness_test_suite") + +############################ UNIT TESTS ############################# +test_harness_test_suite(name = "test_harness_test_suite") diff --git a/test/unit/test_harness/mytest.rs b/test/unit/test_harness/mytest.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/unit/test_harness/mytest_noharness.rs b/test/unit/test_harness/mytest_noharness.rs new file mode 100644 index 0000000000..e71fdf5542 --- /dev/null +++ b/test/unit/test_harness/mytest_noharness.rs @@ -0,0 +1 @@ +fn main() {} \ No newline at end of file diff --git a/test/unit/test_harness/test_harness_test.bzl b/test/unit/test_harness/test_harness_test.bzl new file mode 100644 index 0000000000..966997e395 --- /dev/null +++ b/test/unit/test_harness/test_harness_test.bzl @@ -0,0 +1,68 @@ +"""Unittest to verify ordering of rust stdlib in rust_library() CcInfo""" + +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("//rust:defs.bzl", "rust_test") + +def _test_harness_rustc_flags_test_impl(ctx): + env = analysistest.begin(ctx) + tut = analysistest.target_under_test(env) + action = tut.actions[0] + argv = action.argv + asserts.true(env, action.mnemonic == "Rustc", "expected the action to be the rustc invocation, got %s" % action.mnemonic) + asserts.true(env, "test/unit/test_harness/mytest.rs" in argv, "expected the action to build mytest.rs") + asserts.true(env, "--test" in argv, "expected rustc invocation to contain --test, got %s" % len(argv)) + asserts.false(env, "--cfg" in argv, "expected rustc invocation not to contain --cfg, got %s" % len(argv)) + asserts.false(env, "test" in argv, "expected rustc invocation not to contain test, got %s" % len(argv)) + return analysistest.end(env) + +def _test_harness_rustc_noharness_flags_test_impl(ctx): + env = analysistest.begin(ctx) + tut = analysistest.target_under_test(env) + action = tut.actions[0] + argv = action.argv + asserts.true(env, action.mnemonic == "Rustc", "expected the action to be the rustc invocation, got %s" % action.mnemonic) + asserts.true(env, "test/unit/test_harness/mytest_noharness.rs" in argv, "expected the action to build mytest.rs") + asserts.false(env, "--test" in argv, "expected rustc invocation not to contain --test, got %s" % len(argv)) + asserts.true(env, "--cfg" in argv, "expected rustc invocation to contain --cfg, got %s" % len(argv)) + asserts.true(env, "test" in argv, "expected rustc invocation to contain test, got %s" % len(argv)) + return analysistest.end(env) + +test_harness_rustc_flags_test = analysistest.make(_test_harness_rustc_flags_test_impl) +test_harness_rustc_noharness_flags_test = analysistest.make(_test_harness_rustc_noharness_flags_test_impl) + +def _test_harness_test(): + rust_test( + name = "mytest", + srcs = ["mytest.rs"], + ) + + rust_test( + name = "mytest_noharness", + srcs = ["mytest_noharness.rs"], + harness = False, + ) + + test_harness_rustc_flags_test( + name = "test_harness_rustc_flags_test", + target_under_test = ":mytest", + ) + + test_harness_rustc_noharness_flags_test( + name = "test_harness_rustc_noharness_flags_test", + target_under_test = ":mytest_noharness", + ) + +def test_harness_test_suite(name): + """Entry-point macro called from the BUILD file. + + Args: + name: Name of the macro. + """ + _test_harness_test() + + native.test_suite( + name = name, + tests = [ + ":test_harness_rustc_flags_test", + ], + ) From d642ce74bd82c6bbffe95516ddff4cd4f9cd29d1 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Thu, 27 May 2021 12:25:40 +0200 Subject: [PATCH 2/8] Regenerate documentation --- docs/defs.md | 5 +++-- docs/flatten.md | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/defs.md b/docs/defs.md index 19784ac84f..92b1aa9858 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -465,8 +465,8 @@ When building the whole binary in Bazel, use `rust_library` instead.
 rust_test(name, aliases, compile_data, crate, crate_features, crate_name, crate_root, data, deps,
-          edition, env, out_dir_tar, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs,
-          version)
+          edition, env, harness, out_dir_tar, proc_macro_deps, rustc_env, rustc_env_files,
+          rustc_flags, srcs, version)
 
Builds a Rust test crate. @@ -613,6 +613,7 @@ Run the test with `bazel build //hello_lib:hello_lib_test`. | deps | List of other libraries to be linked to this library target.

These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] | | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" | | env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to $(execpath) and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | Dictionary: String -> String | optional | {} | +| harness | Whether to use libtest (defaults to True). | Boolean | optional | True | | out_dir_tar | __Deprecated__, do not use, see [#cargo_build_script] instead. | Label | optional | None | | proc_macro_deps | List of rust_library targets with kind proc-macro used to help build this library target. | List of labels | optional | [] | | rustc_env | Dictionary of additional "key": "value" environment variables to set for rustc.

rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | {} | diff --git a/docs/flatten.md b/docs/flatten.md index 90075793e2..0a423d154a 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -905,8 +905,8 @@ When building the whole binary in Bazel, use `rust_library` instead.
 rust_test(name, aliases, compile_data, crate, crate_features, crate_name, crate_root, data, deps,
-          edition, env, out_dir_tar, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs,
-          version)
+          edition, env, harness, out_dir_tar, proc_macro_deps, rustc_env, rustc_env_files,
+          rustc_flags, srcs, version)
 
Builds a Rust test crate. @@ -1053,6 +1053,7 @@ Run the test with `bazel build //hello_lib:hello_lib_test`. | deps | List of other libraries to be linked to this library target.

These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] | | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" | | env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to $(execpath) and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | Dictionary: String -> String | optional | {} | +| harness | Whether to use libtest (defaults to True). | Boolean | optional | True | | out_dir_tar | __Deprecated__, do not use, see [#cargo_build_script] instead. | Label | optional | None | | proc_macro_deps | List of rust_library targets with kind proc-macro used to help build this library target. | List of labels | optional | [] | | rustc_env | Dictionary of additional "key": "value" environment variables to set for rustc.

rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | {} | From d8f09c063f6dfbfec1738ea46838b45feebb3164 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Thu, 27 May 2021 20:30:47 +0200 Subject: [PATCH 3/8] Fix doc --- rust/private/rust.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index e06b23b89b..d7180aabae 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -707,7 +707,7 @@ _rust_test_attrs = { mandatory = False, default = True, doc = _tidy("""\ - Whether to use libtest (defaults to True). + Whether to use libtest. """), ), "_launcher": attr.label( From 1e8b854cf4d4569d3615da3841af4c3fd40766a2 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Thu, 27 May 2021 20:30:53 +0200 Subject: [PATCH 4/8] Regenerate documentation --- docs/defs.md | 2 +- docs/flatten.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/defs.md b/docs/defs.md index 92b1aa9858..57a6b0200a 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -613,7 +613,7 @@ Run the test with `bazel build //hello_lib:hello_lib_test`. | deps | List of other libraries to be linked to this library target.

These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] | | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" | | env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to $(execpath) and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | Dictionary: String -> String | optional | {} | -| harness | Whether to use libtest (defaults to True). | Boolean | optional | True | +| harness | Whether to use libtest. | Boolean | optional | True | | out_dir_tar | __Deprecated__, do not use, see [#cargo_build_script] instead. | Label | optional | None | | proc_macro_deps | List of rust_library targets with kind proc-macro used to help build this library target. | List of labels | optional | [] | | rustc_env | Dictionary of additional "key": "value" environment variables to set for rustc.

rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | {} | diff --git a/docs/flatten.md b/docs/flatten.md index 0a423d154a..a5562dadfd 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1053,7 +1053,7 @@ Run the test with `bazel build //hello_lib:hello_lib_test`. | deps | List of other libraries to be linked to this library target.

These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] | | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" | | env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to $(execpath) and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | Dictionary: String -> String | optional | {} | -| harness | Whether to use libtest (defaults to True). | Boolean | optional | True | +| harness | Whether to use libtest. | Boolean | optional | True | | out_dir_tar | __Deprecated__, do not use, see [#cargo_build_script] instead. | Label | optional | None | | proc_macro_deps | List of rust_library targets with kind proc-macro used to help build this library target. | List of labels | optional | [] | | rustc_env | Dictionary of additional "key": "value" environment variables to set for rustc.

rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | {} | From 9dca6e45a35d228390d104f75c9377b48743d257 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Fri, 28 May 2021 10:10:45 +0200 Subject: [PATCH 5/8] Rename to use_test_harness, fix tests --- rust/private/rust.bzl | 4 +- test/unit/common.bzl | 46 +++++++++++++ test/unit/test_harness/BUILD.bazel | 4 -- test/unit/test_harness/test_harness_test.bzl | 68 ------------------- test/unit/use_libtest_harness/BUILD.bazel | 4 ++ .../mytest.rs | 0 .../mytest_noharness.rs | 0 .../use_libtest_harness_test.bzl | 66 ++++++++++++++++++ 8 files changed, 118 insertions(+), 74 deletions(-) delete mode 100644 test/unit/test_harness/BUILD.bazel delete mode 100644 test/unit/test_harness/test_harness_test.bzl create mode 100644 test/unit/use_libtest_harness/BUILD.bazel rename test/unit/{test_harness => use_libtest_harness}/mytest.rs (100%) rename test/unit/{test_harness => use_libtest_harness}/mytest_noharness.rs (100%) create mode 100644 test/unit/use_libtest_harness/use_libtest_harness_test.bzl diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index d7180aabae..c1fb0fa967 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -437,7 +437,7 @@ def _rust_test_common(ctx, toolchain, output): toolchain = toolchain, crate_type = crate_type, crate_info = crate_info, - rust_flags = ["--test"] if ctx.attr.harness else ["--cfg", "test"], + rust_flags = ["--test"] if ctx.attr.use_libtest_harness else ["--cfg", "test"], ) return _create_test_launcher(ctx, toolchain, output, providers) @@ -703,7 +703,7 @@ _rust_test_attrs = { ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. """), ), - "harness": attr.bool( + "use_libtest_harness": attr.bool( mandatory = False, default = True, doc = _tidy("""\ diff --git a/test/unit/common.bzl b/test/unit/common.bzl index 17692c2900..0163cc2f11 100644 --- a/test/unit/common.bzl +++ b/test/unit/common.bzl @@ -28,3 +28,49 @@ def assert_argv_contains_prefix_suffix(env, action, prefix, suffix): args = action.argv, ), ) + +def assert_action_mnemonic(env, action, mnemonic): + if not action.mnemonic == mnemonic: + unittest.fail( + env, + "Expected the action to have the mnemonic '{expected}', but got '{actual}'".format( + expected = mnemonic, + actual = action.mnemonic, + ), + ) + +def _startswith(list, prefix): + if len(list) < len(prefix): + return False + for pair in zip(list[:len(prefix) + 1], prefix): + if pair[0] != pair[1]: + return False + return True + +def assert_argv_contains_in_order(env, action, flags): + argv = action.argv + for idx in range(len(argv)): + if argv[idx] == flags[0]: + if _startswith(argv[idx:], flags): + return + + unittest.fail( + env, + "Expected the to find '{expected}' within '{actual}'".format( + expected = flags, + actual = argv, + ), + ) + +def assert_argv_contains_in_order_not(env, action, flags): + argv = action.argv + for idx in range(len(argv)): + if argv[idx] == flags[0]: + if _startswith(argv[idx:], flags): + unittest.fail( + env, + "Expected not the to find '{expected}' within '{actual}'".format( + expected = flags, + actual = argv, + ), + ) diff --git a/test/unit/test_harness/BUILD.bazel b/test/unit/test_harness/BUILD.bazel deleted file mode 100644 index fd1d44960f..0000000000 --- a/test/unit/test_harness/BUILD.bazel +++ /dev/null @@ -1,4 +0,0 @@ -load(":test_harness_test.bzl", "test_harness_test_suite") - -############################ UNIT TESTS ############################# -test_harness_test_suite(name = "test_harness_test_suite") diff --git a/test/unit/test_harness/test_harness_test.bzl b/test/unit/test_harness/test_harness_test.bzl deleted file mode 100644 index 966997e395..0000000000 --- a/test/unit/test_harness/test_harness_test.bzl +++ /dev/null @@ -1,68 +0,0 @@ -"""Unittest to verify ordering of rust stdlib in rust_library() CcInfo""" - -load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") -load("//rust:defs.bzl", "rust_test") - -def _test_harness_rustc_flags_test_impl(ctx): - env = analysistest.begin(ctx) - tut = analysistest.target_under_test(env) - action = tut.actions[0] - argv = action.argv - asserts.true(env, action.mnemonic == "Rustc", "expected the action to be the rustc invocation, got %s" % action.mnemonic) - asserts.true(env, "test/unit/test_harness/mytest.rs" in argv, "expected the action to build mytest.rs") - asserts.true(env, "--test" in argv, "expected rustc invocation to contain --test, got %s" % len(argv)) - asserts.false(env, "--cfg" in argv, "expected rustc invocation not to contain --cfg, got %s" % len(argv)) - asserts.false(env, "test" in argv, "expected rustc invocation not to contain test, got %s" % len(argv)) - return analysistest.end(env) - -def _test_harness_rustc_noharness_flags_test_impl(ctx): - env = analysistest.begin(ctx) - tut = analysistest.target_under_test(env) - action = tut.actions[0] - argv = action.argv - asserts.true(env, action.mnemonic == "Rustc", "expected the action to be the rustc invocation, got %s" % action.mnemonic) - asserts.true(env, "test/unit/test_harness/mytest_noharness.rs" in argv, "expected the action to build mytest.rs") - asserts.false(env, "--test" in argv, "expected rustc invocation not to contain --test, got %s" % len(argv)) - asserts.true(env, "--cfg" in argv, "expected rustc invocation to contain --cfg, got %s" % len(argv)) - asserts.true(env, "test" in argv, "expected rustc invocation to contain test, got %s" % len(argv)) - return analysistest.end(env) - -test_harness_rustc_flags_test = analysistest.make(_test_harness_rustc_flags_test_impl) -test_harness_rustc_noharness_flags_test = analysistest.make(_test_harness_rustc_noharness_flags_test_impl) - -def _test_harness_test(): - rust_test( - name = "mytest", - srcs = ["mytest.rs"], - ) - - rust_test( - name = "mytest_noharness", - srcs = ["mytest_noharness.rs"], - harness = False, - ) - - test_harness_rustc_flags_test( - name = "test_harness_rustc_flags_test", - target_under_test = ":mytest", - ) - - test_harness_rustc_noharness_flags_test( - name = "test_harness_rustc_noharness_flags_test", - target_under_test = ":mytest_noharness", - ) - -def test_harness_test_suite(name): - """Entry-point macro called from the BUILD file. - - Args: - name: Name of the macro. - """ - _test_harness_test() - - native.test_suite( - name = name, - tests = [ - ":test_harness_rustc_flags_test", - ], - ) diff --git a/test/unit/use_libtest_harness/BUILD.bazel b/test/unit/use_libtest_harness/BUILD.bazel new file mode 100644 index 0000000000..eb9195123f --- /dev/null +++ b/test/unit/use_libtest_harness/BUILD.bazel @@ -0,0 +1,4 @@ +load(":use_libtest_harness_test.bzl", "use_libtest_harness_test_suite") + +############################ UNIT TESTS ############################# +use_libtest_harness_test_suite(name = "use_libtest_harness_test_suite") diff --git a/test/unit/test_harness/mytest.rs b/test/unit/use_libtest_harness/mytest.rs similarity index 100% rename from test/unit/test_harness/mytest.rs rename to test/unit/use_libtest_harness/mytest.rs diff --git a/test/unit/test_harness/mytest_noharness.rs b/test/unit/use_libtest_harness/mytest_noharness.rs similarity index 100% rename from test/unit/test_harness/mytest_noharness.rs rename to test/unit/use_libtest_harness/mytest_noharness.rs diff --git a/test/unit/use_libtest_harness/use_libtest_harness_test.bzl b/test/unit/use_libtest_harness/use_libtest_harness_test.bzl new file mode 100644 index 0000000000..174b24d6af --- /dev/null +++ b/test/unit/use_libtest_harness/use_libtest_harness_test.bzl @@ -0,0 +1,66 @@ +"""Unittest to verify ordering of rust stdlib in rust_library() CcInfo""" + +load("@bazel_skylib//lib:unittest.bzl", "analysistest") +load("//rust:defs.bzl", "rust_test") +load("//test/unit:common.bzl", "assert_action_mnemonic", "assert_argv_contains", "assert_argv_contains_in_order", "assert_argv_contains_in_order_not", "assert_argv_contains_not") + +def _use_libtest_harness_rustc_flags_test_impl(ctx): + env = analysistest.begin(ctx) + tut = analysistest.target_under_test(env) + action = tut.actions[0] + argv = action.argv + assert_action_mnemonic(env, action, "Rustc") + assert_argv_contains(env, action, "test/unit/use_libtest_harness/mytest.rs") + assert_argv_contains(env, action, "--test") + assert_argv_contains_in_order_not(env, action, ["--cfg", "test"]) + return analysistest.end(env) + +def _use_libtest_harness_rustc_noharness_flags_test_impl(ctx): + env = analysistest.begin(ctx) + tut = analysistest.target_under_test(env) + action = tut.actions[0] + assert_action_mnemonic(env, action, "Rustc") + assert_argv_contains(env, action, "test/unit/use_libtest_harness/mytest_noharness.rs") + assert_argv_contains_not(env, action, "--test") + assert_argv_contains_in_order(env, action, ["--cfg", "test"]) + return analysistest.end(env) + +use_libtest_harness_rustc_flags_test = analysistest.make(_use_libtest_harness_rustc_flags_test_impl) +use_libtest_harness_rustc_noharness_flags_test = analysistest.make(_use_libtest_harness_rustc_noharness_flags_test_impl) + +def _use_libtest_harness_test(): + rust_test( + name = "mytest", + srcs = ["mytest.rs"], + ) + + rust_test( + name = "mytest_noharness", + srcs = ["mytest_noharness.rs"], + use_libtest_harness = False, + ) + + use_libtest_harness_rustc_flags_test( + name = "use_libtest_harness_rustc_flags_test", + target_under_test = ":mytest", + ) + + use_libtest_harness_rustc_noharness_flags_test( + name = "use_libtest_harness_rustc_noharness_flags_test", + target_under_test = ":mytest_noharness", + ) + +def use_libtest_harness_test_suite(name): + """Entry-point macro called from the BUILD file. + + Args: + name: Name of the macro. + """ + _use_libtest_harness_test() + + native.test_suite( + name = name, + tests = [ + ":use_libtest_harness_rustc_flags_test", + ], + ) From c3980f2408f2a69620b62fd148d94654141e3625 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Fri, 28 May 2021 10:10:57 +0200 Subject: [PATCH 6/8] Regenerate documentation --- docs/defs.md | 6 +++--- docs/flatten.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/defs.md b/docs/defs.md index 57a6b0200a..15f0435165 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -465,8 +465,8 @@ When building the whole binary in Bazel, use `rust_library` instead.
 rust_test(name, aliases, compile_data, crate, crate_features, crate_name, crate_root, data, deps,
-          edition, env, harness, out_dir_tar, proc_macro_deps, rustc_env, rustc_env_files,
-          rustc_flags, srcs, version)
+          edition, env, out_dir_tar, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs,
+          use_libtest_harness, version)
 
Builds a Rust test crate. @@ -613,13 +613,13 @@ Run the test with `bazel build //hello_lib:hello_lib_test`. | deps | List of other libraries to be linked to this library target.

These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] | | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" | | env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to $(execpath) and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | Dictionary: String -> String | optional | {} | -| harness | Whether to use libtest. | Boolean | optional | True | | out_dir_tar | __Deprecated__, do not use, see [#cargo_build_script] instead. | Label | optional | None | | proc_macro_deps | List of rust_library targets with kind proc-macro used to help build this library target. | List of labels | optional | [] | | rustc_env | Dictionary of additional "key": "value" environment variables to set for rustc.

rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | {} | | rustc_env_files | Files containing additional environment variables to set for rustc.

These files should contain a single variable per line, of format NAME=value, and newlines may be included in a value by ending a line with a trailing back-slash (\).

The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged. | List of labels | optional | [] | | rustc_flags | List of compiler flags passed to rustc. | List of strings | optional | [] | | srcs | List of Rust .rs source files used to build the library.

If srcs contains more than one file, then there must be a file either named lib.rs. Otherwise, crate_root must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | [] | +| use_libtest_harness | Whether to use libtest. | Boolean | optional | True | | version | A version to inject in the cargo environment variable. | String | optional | "0.0.0" | diff --git a/docs/flatten.md b/docs/flatten.md index a5562dadfd..9606a49f68 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -905,8 +905,8 @@ When building the whole binary in Bazel, use `rust_library` instead.
 rust_test(name, aliases, compile_data, crate, crate_features, crate_name, crate_root, data, deps,
-          edition, env, harness, out_dir_tar, proc_macro_deps, rustc_env, rustc_env_files,
-          rustc_flags, srcs, version)
+          edition, env, out_dir_tar, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs,
+          use_libtest_harness, version)
 
Builds a Rust test crate. @@ -1053,13 +1053,13 @@ Run the test with `bazel build //hello_lib:hello_lib_test`. | deps | List of other libraries to be linked to this library target.

These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] | | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" | | env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to $(execpath) and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | Dictionary: String -> String | optional | {} | -| harness | Whether to use libtest. | Boolean | optional | True | | out_dir_tar | __Deprecated__, do not use, see [#cargo_build_script] instead. | Label | optional | None | | proc_macro_deps | List of rust_library targets with kind proc-macro used to help build this library target. | List of labels | optional | [] | | rustc_env | Dictionary of additional "key": "value" environment variables to set for rustc.

rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | Dictionary: String -> String | optional | {} | | rustc_env_files | Files containing additional environment variables to set for rustc.

These files should contain a single variable per line, of format NAME=value, and newlines may be included in a value by ending a line with a trailing back-slash (\).

The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged. | List of labels | optional | [] | | rustc_flags | List of compiler flags passed to rustc. | List of strings | optional | [] | | srcs | List of Rust .rs source files used to build the library.

If srcs contains more than one file, then there must be a file either named lib.rs. Otherwise, crate_root must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | [] | +| use_libtest_harness | Whether to use libtest. | Boolean | optional | True | | version | A version to inject in the cargo environment variable. | String | optional | "0.0.0" | From 1ca3651a33663ef1f4549e4d8e8dbb06d387e527 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Fri, 28 May 2021 10:23:24 +0200 Subject: [PATCH 7/8] renaming --- test/unit/common.bzl | 28 +++++++++---------- .../use_libtest_harness_test.bzl | 6 ++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/test/unit/common.bzl b/test/unit/common.bzl index 0163cc2f11..7466971d4a 100644 --- a/test/unit/common.bzl +++ b/test/unit/common.bzl @@ -47,30 +47,30 @@ def _startswith(list, prefix): return False return True -def assert_argv_contains_in_order(env, action, flags): - argv = action.argv - for idx in range(len(argv)): - if argv[idx] == flags[0]: - if _startswith(argv[idx:], flags): +def assert_list_contains_adjacent_elements(env, list_under_test, adjacent_elements): + """Assert that list_under_test contains given adjacent flags.""" + for idx in range(len(list_under_test)): + if list_under_test[idx] == adjacent_elements[0]: + if _startswith(list_under_test[idx:], adjacent_elements): return unittest.fail( env, "Expected the to find '{expected}' within '{actual}'".format( - expected = flags, - actual = argv, + expected = adjacent_elements, + actual = list_under_test, ), ) -def assert_argv_contains_in_order_not(env, action, flags): - argv = action.argv - for idx in range(len(argv)): - if argv[idx] == flags[0]: - if _startswith(argv[idx:], flags): +def assert_list_contains_adjacent_elements_not(env, list_under_test, adjacent_elements): + """Assert that list_under_test does not contains given adjacent flags.""" + for idx in range(len(list_under_test)): + if list_under_test[idx] == adjacent_elements[0]: + if _startswith(list_under_test[idx:], adjacent_elements): unittest.fail( env, "Expected not the to find '{expected}' within '{actual}'".format( - expected = flags, - actual = argv, + expected = adjacent_elements, + actual = list_under_test, ), ) diff --git a/test/unit/use_libtest_harness/use_libtest_harness_test.bzl b/test/unit/use_libtest_harness/use_libtest_harness_test.bzl index 174b24d6af..72ec2b7770 100644 --- a/test/unit/use_libtest_harness/use_libtest_harness_test.bzl +++ b/test/unit/use_libtest_harness/use_libtest_harness_test.bzl @@ -2,7 +2,7 @@ load("@bazel_skylib//lib:unittest.bzl", "analysistest") load("//rust:defs.bzl", "rust_test") -load("//test/unit:common.bzl", "assert_action_mnemonic", "assert_argv_contains", "assert_argv_contains_in_order", "assert_argv_contains_in_order_not", "assert_argv_contains_not") +load("//test/unit:common.bzl", "assert_action_mnemonic", "assert_argv_contains", "assert_argv_contains_not", "assert_list_contains_adjacent_elements", "assert_list_contains_adjacent_elements_not") def _use_libtest_harness_rustc_flags_test_impl(ctx): env = analysistest.begin(ctx) @@ -12,7 +12,7 @@ def _use_libtest_harness_rustc_flags_test_impl(ctx): assert_action_mnemonic(env, action, "Rustc") assert_argv_contains(env, action, "test/unit/use_libtest_harness/mytest.rs") assert_argv_contains(env, action, "--test") - assert_argv_contains_in_order_not(env, action, ["--cfg", "test"]) + assert_list_contains_adjacent_elements_not(env, action.argv, ["--cfg", "test"]) return analysistest.end(env) def _use_libtest_harness_rustc_noharness_flags_test_impl(ctx): @@ -22,7 +22,7 @@ def _use_libtest_harness_rustc_noharness_flags_test_impl(ctx): assert_action_mnemonic(env, action, "Rustc") assert_argv_contains(env, action, "test/unit/use_libtest_harness/mytest_noharness.rs") assert_argv_contains_not(env, action, "--test") - assert_argv_contains_in_order(env, action, ["--cfg", "test"]) + assert_list_contains_adjacent_elements(env, action.argv, ["--cfg", "test"]) return analysistest.end(env) use_libtest_harness_rustc_flags_test = analysistest.make(_use_libtest_harness_rustc_flags_test_impl) From 04b77cc216341c670e9a2efc586a9ecee016830a Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Fri, 28 May 2021 10:28:13 +0200 Subject: [PATCH 8/8] Buildifier --- test/unit/common.bzl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/test/unit/common.bzl b/test/unit/common.bzl index 7466971d4a..549931b331 100644 --- a/test/unit/common.bzl +++ b/test/unit/common.bzl @@ -48,7 +48,13 @@ def _startswith(list, prefix): return True def assert_list_contains_adjacent_elements(env, list_under_test, adjacent_elements): - """Assert that list_under_test contains given adjacent flags.""" + """Assert that list_under_test contains given adjacent flags. + + Args: + env: env from analysistest.begin(ctx). + list_under_test: list supposed to contain adjacent elements. + adjacent_elements: list of elements to be found inside list_under_test. + """ for idx in range(len(list_under_test)): if list_under_test[idx] == adjacent_elements[0]: if _startswith(list_under_test[idx:], adjacent_elements): @@ -63,7 +69,12 @@ def assert_list_contains_adjacent_elements(env, list_under_test, adjacent_elemen ) def assert_list_contains_adjacent_elements_not(env, list_under_test, adjacent_elements): - """Assert that list_under_test does not contains given adjacent flags.""" + """Assert that list_under_test does not contains given adjacent flags. + + Args: + env: env from analysistest.begin(ctx). + list_under_test: list supposed not to contain adjacent elements. + adjacent_elements: list of elements not to be found inside list_under_test.""" for idx in range(len(list_under_test)): if list_under_test[idx] == adjacent_elements[0]: if _startswith(list_under_test[idx:], adjacent_elements):