razeh
razeh

Reputation: 2765

Finding files in bazel unit tests as an external dependency

I have a bazel project, call it foo, with Catch C++ unit tests that read files from a relative path.

The unit tests run fine in the foo project.

I'm using foo as an external dependency for another project, call it bar. I want the unit tests to work in bar, but in bar they cannot find the files.

What I do not understand is how to find the files in a project independent fashion. In project foo, the files are placed in:

~/.cache/<blah>/<blah>/<testname.runfiles>/__main__/source/

But in project bar, the files are placed in

  ~/.cache/<blah>/<blah>/<testname.runfiles>/__main__/external/bars_archive_name/source

The TEST_SRCDIR environment variable provided by bazel points to the directory, but that doesn't help because of the two different directory structures.

The unit test can not know bar's archive name (because that's specified in foo's WORKSPACE file), so it cannot find the files. The unit test also doesn't know if it should append external or not.

This seems like something that should be simple to do, so I hope that I'm missing something obvious.

Upvotes: 2

Views: 1229

Answers (1)

Christopher Parsons
Christopher Parsons

Reputation: 337

How does your unit test depend on the files of project bar? Does the unit test require that the files are present under a relative path?

(Also, what language are your tests in? The more specific you can be about your project setup, the better I can help :))

Have you considered passing the location of test dependencies via flags / arguments to your test? If this is a possibility, Bazel can help resolve these paths as absolute paths if you use $(location) substitution.

For example, your test target might look like:

my_unit_test(
  name = "test",
  srcs = ["mysrc.file"],
  deps = ["@bar//barproj/other.file"],
  args = [
    "$(location @bar//barproj/other.file)",
  ],
)

Please let me know if this is insufficient!

Upvotes: 2

Related Questions