3

I have a py_binary rule like this:

py_binary(
  name = "testInputs",
  srcs = ["testInputs.py"],
)

and a cc_test like this:

cc_test(
  name = "test",
  src = ["test.cc"],
  data = [":testInputs"],
)

test.cc needs an input file next to it (say input.txt) that is generated by testInputs.py. I want testInputs to get run and provide the input file to the test.

As mentioned here, I tried to to depend on testInputs in the data section. But the test doesn't find the input file nearby. Result of tree bazel-out | grep -F input.txt shows that even testInput rule has not run at all - since input.txt file not exists at all.

1 Answer 1

3

data = [":testInputs"] on the cc_test will make the py_binary itself available to the cc_test, not anything the py_binary might produce when run.

You'll want something like this:

cc_test(
  name = "test",
  src = ["test.cc"],
  data = [":test_input.txt"],
)

genrule(
  name = "gen_test_inputs",
  tools = [":test_input_generator"],
  outs = ["test_input.txt"],
  cmd = "$(location :test_input_generator) $@"
)

py_binary(
  name = "test_input_generator",
  srcs = ["test_input_generator.py"],
)
Sign up to request clarification or add additional context in comments.

5 Comments

It should work. But according to documentation, this situation is exactly mentioned! "If you want to run a py_binary from within another binary or test (for example, running a python binary to set up some mock resource from within a java_test) then the correct approach is to make the other binary or test depend on the py_binary in its data section." see the link I mentioned above
I guess that my mentioned scenario may not work as dependencies are not run, but just build! But so what does the doc above say?!
In cmd section, what is $@ for?
The documentation describes making a binary available to another binary so that one can run the other. But what you're asking about is making the output of one binary available to another. To do that you'd need to either depend directly on the binary as you did in your post, and then manually run the binary in test.cc, or ask bazel to do it by putting a genrule between the two binary rules.
$@ is a shortcut for "the path to the output file, if there is only one output". See docs.bazel.build/versions/master/be/general.html#genrule.cmd and docs.bazel.build/versions/master/be/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.