Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/defs.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ When building the whole binary in Bazel, use `rust_library` instead.
<pre>
rust_test(<a href="#rust_test-name">name</a>, <a href="#rust_test-aliases">aliases</a>, <a href="#rust_test-compile_data">compile_data</a>, <a href="#rust_test-crate">crate</a>, <a href="#rust_test-crate_features">crate_features</a>, <a href="#rust_test-crate_name">crate_name</a>, <a href="#rust_test-crate_root">crate_root</a>, <a href="#rust_test-data">data</a>, <a href="#rust_test-deps">deps</a>,
<a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-out_dir_tar">out_dir_tar</a>, <a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>, <a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-srcs">srcs</a>,
<a href="#rust_test-version">version</a>)
<a href="#rust_test-use_libtest_harness">use_libtest_harness</a>, <a href="#rust_test-version">version</a>)
</pre>

Builds a Rust test crate.
Expand Down Expand Up @@ -619,6 +619,7 @@ Run the test with `bazel build //hello_lib:hello_lib_test`.
| <a id="rust_test-rustc_env_files"></a>rustc_env_files | Files containing additional environment variables to set for rustc.<br><br>These files should contain a single variable per line, of format <code>NAME=value</code>, and newlines may be included in a value by ending a line with a trailing back-slash (<code>\</code>).<br><br>The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="rust_test-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>. | List of strings | optional | [] |
| <a id="rust_test-srcs"></a>srcs | List of Rust <code>.rs</code> source files used to build the library.<br><br>If <code>srcs</code> contains more than one file, then there must be a file either named <code>lib.rs</code>. Otherwise, <code>crate_root</code> must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="rust_test-use_libtest_harness"></a>use_libtest_harness | Whether to use libtest. | Boolean | optional | True |
| <a id="rust_test-version"></a>version | A version to inject in the cargo environment variable. | String | optional | "0.0.0" |


Expand Down
3 changes: 2 additions & 1 deletion docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ When building the whole binary in Bazel, use `rust_library` instead.
<pre>
rust_test(<a href="#rust_test-name">name</a>, <a href="#rust_test-aliases">aliases</a>, <a href="#rust_test-compile_data">compile_data</a>, <a href="#rust_test-crate">crate</a>, <a href="#rust_test-crate_features">crate_features</a>, <a href="#rust_test-crate_name">crate_name</a>, <a href="#rust_test-crate_root">crate_root</a>, <a href="#rust_test-data">data</a>, <a href="#rust_test-deps">deps</a>,
<a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-out_dir_tar">out_dir_tar</a>, <a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>, <a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-srcs">srcs</a>,
<a href="#rust_test-version">version</a>)
<a href="#rust_test-use_libtest_harness">use_libtest_harness</a>, <a href="#rust_test-version">version</a>)
</pre>

Builds a Rust test crate.
Expand Down Expand Up @@ -1059,6 +1059,7 @@ Run the test with `bazel build //hello_lib:hello_lib_test`.
| <a id="rust_test-rustc_env_files"></a>rustc_env_files | Files containing additional environment variables to set for rustc.<br><br>These files should contain a single variable per line, of format <code>NAME=value</code>, and newlines may be included in a value by ending a line with a trailing back-slash (<code>\</code>).<br><br>The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="rust_test-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>. | List of strings | optional | [] |
| <a id="rust_test-srcs"></a>srcs | List of Rust <code>.rs</code> source files used to build the library.<br><br>If <code>srcs</code> contains more than one file, then there must be a file either named <code>lib.rs</code>. Otherwise, <code>crate_root</code> must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="rust_test-use_libtest_harness"></a>use_libtest_harness | Whether to use libtest. | Boolean | optional | True |
| <a id="rust_test-version"></a>version | A version to inject in the cargo environment variable. | String | optional | "0.0.0" |


Expand Down
9 changes: 8 additions & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.use_libtest_harness else ["--cfg", "test"],
)

return _create_test_launcher(ctx, toolchain, output, providers)
Expand Down Expand Up @@ -703,6 +703,13 @@ _rust_test_attrs = {
["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.
"""),
),
"use_libtest_harness": attr.bool(
mandatory = False,
default = True,
doc = _tidy("""\
Whether to use libtest.
"""),
),
"_launcher": attr.label(
executable = True,
default = Label("//util/launcher:launcher"),
Expand Down
57 changes: 57 additions & 0 deletions test/unit/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,60 @@ 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_list_contains_adjacent_elements(env, list_under_test, adjacent_elements):
"""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):
return

unittest.fail(
env,
"Expected the to find '{expected}' within '{actual}'".format(
expected = adjacent_elements,
actual = list_under_test,
),
)

def assert_list_contains_adjacent_elements_not(env, list_under_test, adjacent_elements):
"""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):
unittest.fail(
env,
"Expected not the to find '{expected}' within '{actual}'".format(
expected = adjacent_elements,
actual = list_under_test,
),
)
4 changes: 4 additions & 0 deletions test/unit/use_libtest_harness/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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")
Empty file.
1 change: 1 addition & 0 deletions test/unit/use_libtest_harness/mytest_noharness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
66 changes: 66 additions & 0 deletions test/unit/use_libtest_harness/use_libtest_harness_test.bzl
Original file line number Diff line number Diff line change
@@ -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_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)
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_list_contains_adjacent_elements_not(env, action.argv, ["--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_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)
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",
],
)