-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
cfg = _test_arg_transition
in configurations/cc_test/defs.bzl (#…
…367) Question for the bazel experts: Does configurations/cc_test work as intended without the added `cfg = _test_arg_transition` in defs.bzl? This PR changes mytest.cc to send `argv` to stdout. Without the added `cfg = _test_arg_transition` in defs.bzl, `new arg` does not appear in the output. See below. The other changes are optional, they just make this example more complete. Not being very familiar with the bazel mechanisms, it took me a while to figure out the two tricks to 1. ensure that `args` are handled correctly, and 2. the non-transitioned test is not run. ________ **WITHOUT** the added `cfg = _test_arg_transition` in defs.bzl ``` bazel test :all --test_output=all ``` ``` MYTEST ARGV[0]: /usr/local/google/home/rwgk/.cache/bazel/_bazel_rwgk/bfd421ff56c7e52ce2de56579817e14d/sandbox/linux-sandbox/9/execroot/__main__/bazel-out/k8-fastbuild/bin/my-test.runfiles/__main__/my-test MYTEST ARGV[1]: x MYTEST ARGV[2]: y MYTEST ARGV[3]: z ``` ________ **WITH** the added `cfg = _test_arg_transition` in defs.bzl (i.e. this PR as is) ``` MYTEST ARGV[0]: /usr/local/google/home/rwgk/.cache/bazel/_bazel_rwgk/bfd421ff56c7e52ce2de56579817e14d/sandbox/linux-sandbox/4/execroot/__main__/bazel-out/k8-fastbuild-ST-54535d7cadf4/bin/my-test.runfiles/__main__/my-test MYTEST ARGV[1]: x MYTEST ARGV[2]: y MYTEST ARGV[3]: z MYTEST ARGV[4]: new arg ```
- Loading branch information
Showing
4 changed files
with
57 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,33 @@ | ||
### Example showing how to use [Starlark configuration](https://bazel.build/extending/config) to write a | ||
`cc_test` wrapper with a starlark transition | ||
|
||
the `test_arg_cc_test` macro in `defs.bzl` defines a wrapper for basically a cc_test that has been transitioned. | ||
the `test_arg_cc_test` macro in `defs.bzl` defines a wrapper for basically a `cc_test` that has been transitioned. | ||
This allows, e.g., the test itself to select attribute values based on the value of that transition. There is some | ||
light magic in the `transition_rule` implementation that allows dependents of the `test_arg_cc_test` macros to | ||
treat the targets it creates the exact same as a regular cc test. | ||
treat the targets it creates the exact same as a regular `cc_test`. | ||
|
||
To run this example: | ||
|
||
``` | ||
$ bazel test :all --test_output=all | ||
``` | ||
|
||
``` | ||
==================== Test output for //:my-test: | ||
################################################################################ | ||
MYTEST ARGV[0]: .../bazel-out/k8-fastbuild-ST-54535d7cadf4/bin/my-test.runfiles/__main__/my-test | ||
MYTEST ARGV[1]: x | ||
MYTEST ARGV[2]: y | ||
MYTEST ARGV[3]: z | ||
MYTEST ARGV[4]: new arg | ||
################################################################################ | ||
================================================================================ | ||
``` | ||
|
||
Known limitation: | ||
|
||
`bazel test :all --test_output=all --test_arg=a` does not work as expected (`--test_arg=a` is ignored). | ||
This could be fixed with the approach shown under in the ../read_attr_in_transition example, | ||
but it is omitted here to not make this example overly complex. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
// a trivially passing test | ||
#include <iostream> | ||
#include <string> | ||
|
||
int main() { | ||
return 0; | ||
} | ||
int main(int argc, const char* argv[]) { | ||
// Send argv to stdout. | ||
std::string divider(80, '#'); | ||
std::cout << '\n' << divider << '\n'; | ||
for (int i = 0; i < argc; i++) { | ||
std::cout << "MYTEST ARGV[" << i << "]: " << argv[i] << '\n'; | ||
} | ||
std::cout << divider << '\n' << '\n'; | ||
|
||
return 0; // This test always passes. | ||
} |