-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build
rust_test
targets using a crate name different from the under…
…lying lib (#2828) This is a rollforward of #2803, but behind the `incompatible_change_rust_test_compilation_output_directory` incompatible flag. This PR also makes `rust_test` put its compilation outputs in the same directory as the `rust_library` rule (i.e. not in a `test-{hash}` subdirectory anymore). After this change both the `rust_library` and `rust_test` rules will put all its compilation outputs in the same directory, but there won't be any name collisions in non-sandboxed environments (see #1427 for more context). Issue with context: #2827
- Loading branch information
Showing
8 changed files
with
142 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
test/unit/rust_test_outputs_are_in_same_directory/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
load(":rust_test_outputs.bzl", "rust_test_outputs_test_suite") | ||
|
||
############################ UNIT TESTS ############################# | ||
rust_test_outputs_test_suite(name = "rust_test_outputs_test_suite") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub fn main() {} |
92 changes: 92 additions & 0 deletions
92
test/unit/rust_test_outputs_are_in_same_directory/rust_test_outputs.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
"""Tests for rust_test outputs directory.""" | ||
|
||
load("@bazel_skylib//lib:paths.bzl", "paths") | ||
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") | ||
load("//rust:defs.bzl", "rust_binary", "rust_common", "rust_library", "rust_test") | ||
|
||
def _rust_test_outputs_test(ctx): | ||
env = analysistest.begin(ctx) | ||
tut = analysistest.target_under_test(env) | ||
|
||
output = tut[rust_common.crate_info].output | ||
|
||
# Check compilation output is in directory with same name as package | ||
test_target_label = ctx.attr.target_under_test[0].label | ||
asserts.true(env, output.dirname.split("/")[-1] == test_target_label.package.split("/")[-1]) | ||
|
||
# Check compilation output has same name as crate name, ignoring possible binary extension | ||
output_filename_without_ext = paths.split_extension(output.basename)[0] | ||
asserts.true(env, output_filename_without_ext == tut[rust_common.crate_info].name) | ||
|
||
return analysistest.end(env) | ||
|
||
rust_test_outputs_test = analysistest.make( | ||
_rust_test_outputs_test, | ||
config_settings = { | ||
str(Label("//rust/settings:incompatible_change_rust_test_compilation_output_directory")): True, | ||
}, | ||
) | ||
|
||
def _rust_test_outputs_targets(): | ||
rust_binary( | ||
name = "bin_outputs", | ||
srcs = ["foo.rs"], | ||
edition = "2018", | ||
) | ||
|
||
rust_library( | ||
name = "lib_outputs", | ||
srcs = ["foo.rs"], | ||
edition = "2018", | ||
) | ||
|
||
rust_test( | ||
name = "test_outputs_with_srcs", | ||
srcs = ["foo.rs"], | ||
edition = "2018", | ||
) | ||
|
||
rust_test_outputs_test( | ||
name = "rust_test_outputs_using_srcs_attr", | ||
target_under_test = ":test_outputs_with_srcs", | ||
) | ||
|
||
rust_test( | ||
name = "test_outputs_with_crate_from_bin", | ||
crate = "bin_outputs", | ||
edition = "2018", | ||
) | ||
|
||
rust_test_outputs_test( | ||
name = "rust_test_outputs_using_crate_attr_from_bin", | ||
target_under_test = ":test_outputs_with_crate_from_bin", | ||
) | ||
|
||
rust_test( | ||
name = "test_outputs_with_crate_from_lib", | ||
crate = "lib_outputs", | ||
edition = "2018", | ||
) | ||
|
||
rust_test_outputs_test( | ||
name = "rust_test_outputs_using_crate_attr_from_lib", | ||
target_under_test = ":test_outputs_with_crate_from_lib", | ||
) | ||
|
||
def rust_test_outputs_test_suite(name): | ||
"""Entry-point macro called from the BUILD file. | ||
Args: | ||
name: Name of the macro. | ||
""" | ||
|
||
_rust_test_outputs_targets() | ||
|
||
native.test_suite( | ||
name = name, | ||
tests = [ | ||
":rust_test_outputs_using_srcs_attr", | ||
":rust_test_outputs_using_crate_attr_from_bin", | ||
":rust_test_outputs_using_crate_attr_from_lib", | ||
], | ||
) |