falsekeel
falsekeel

Reputation: 177

bazel share files between different test targets

I know that bazel runs all commands in isolated sandboxes, however, I want to break them deliberately. Suppose I've got a project with the following layout:

.
├── BUILD
├── cpp
│   ├── BUILD
│   ├── foo.cpp
│   └── test_foo.cpp
├── python
│   ├── BUILD
│   ├── foo.py
│   └── test_foo.py
└── WORKSPACE

The file python/BUILD contains a py_test rule that runs some python tests that leave some artifacts (let's say some binary files). The file cpp/BUILD contains a cc_test rule that runs some cpp tests that require the artifacts left by the python tests.

The question is how I can do this? It would be better if the possible solution would provide all advantages of the incremental build system e.g. not running the python tests if they are older than artifacts and so on.

Upvotes: 0

Views: 918

Answers (2)

Anton Miroshnichenko
Anton Miroshnichenko

Reputation: 21

Maybe my case helps you.

I was able to extract py_test artifacts with the following genrule:

genrule(
    name = "target_name",
    outs = [
        "target_output.tar",
    ],
    cmd = "./$(location //dependent_target) ; \
echo Writing generated file: ; find ./bazel-out/ -name 'my_archive.tar' ; echo To this output: ; \
echo $@ ; find ./bazel-out/ -name 'target_output.tar' -exec cat {} > $@ \\;",
    tools = [
        "//dependent_target",
    ],
)

After a while I have found simple and correct way: just use TEST_UNDECLARED_OUTPUTS_DIR bazel variable, see https://bazel.build/reference/test-encyclopedia

Upvotes: 0

slsy
slsy

Reputation: 1304

The standard way for generating code for testing is a genrule, see example for the python script. After you have your genrule target (let say //path/to:foo_gen) all you have to do is to add the label to the cc_test data attribute:

cc_test(
  data = ['/path/to:foo_gen'],
...
)

File specified in the outs attibute of the genrule (see example above) will be available at path ./path/to/file_specified_in_outs

Upvotes: 1

Related Questions