Trouble with java_test and data files

1,824 views
Skip to first unread message

Paul Gross

unread,
Jun 7, 2015, 4:25:33 PM6/7/15
to bazel-...@googlegroups.com
I'm having trouble finding and reading files from java tests that I defined in the "data" section.

My project is using a maven directory structure, so my file is: src/test/resources/foo.txt.

I include this in the java_test with: data = glob(["src/test/resources/**"]),

I can see this file in the bazel output at: bazel-bin/app/test.runfiles/app/src/test/resources/foo.txt.

And then I try to use it from code using Guava Resources: Resources.getResource("src/test/resources/foo.txt").

I also tried "app/src/test/resources/foo.txt" but that didn't work, either. How can I find and read this file?

I have an example app with a commit illustrating the problem here: https://github.com/pgr0ss/bazel_java_example/commit/9764ff91a5b2d25e878c40fa1101374f5e0e6694

I run it with: bazel test --test_output=errors //app:test

Thanks,
Paul

Kristina Chodorow

unread,
Jun 8, 2015, 11:30:10 AM6/8/15
to Paul Gross, bazel-...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAMXKpGBM5VS3xwckPHa8oYoDcNjJdewp7shNso_yTz1X9Xpnpg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Paul Gross

unread,
Jun 8, 2015, 11:33:46 PM6/8/15
to Kristina Chodorow, bazel-...@googlegroups.com
I tried /foo.txt, but that doesn't seem to work, either.

It looks like the java example is using "resources" and putting the file into the jar. I'm trying to use "data" to keep the file separate. I have to keep it separate because I'm using an external library (dropwizard) that expects to be able to grab a path to the given file:

https://github.com/dropwizard/dropwizard/blob/2a08d3db1a0cd8aa49293b7fc5172ff6ec3d1022/dropwizard-testing/src/main/java/io/dropwizard/testing/ResourceHelpers.java#L21

Thanks,
Paul

Damien Martin-guillerez

unread,
Jun 9, 2015, 7:01:38 AM6/9/15
to Paul Gross, Kristina Chodorow, bazel-...@googlegroups.com
AFAIR, data is put in the runfiles whereas resources are put in the resources of the jar file. The resources package is computed relatively to the outermost enclosing folder inside the workspace who's name is either "resources", "java" or "javatests" meaning that for your case if you put the file in the resources attributes it should be accessible at /foo.txt

I don't really understand what's the problem with using resources instead of data but data is on the file system and you should ensure that you launch your java binary with the java launcher (so bazel-bin/app/test for yourcase) so the JAVA_RUNFILES environment variable is defined (see https://github.com/google/bazel/blob/master/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt#L121). Then you can read the file with:
InputStream stream = new FileInputStream(System.getenv("JAVA_RUNFILES") + "/app/src/test/resources/foo.txt").

I hope that helps.


For more options, visit https://groups.google.com/d/optout.
--
Damien

Paul Gross

unread,
Jun 10, 2015, 12:41:59 AM6/10/15
to Damien Martin-guillerez, Kristina Chodorow, bazel-...@googlegroups.com
Thanks, that was helpful. I still don't quite understand why this file isn't available via the class loader (e.g. Resources.getResource), but at least I can find it on disk using an environment variable.

Normally, i would just use resources and put these files in the jar. In this case, I'm using a DropwizardAppRule for testing which expects the path of the config file on disk. I can keep investigating if this can use a resource instead:

https://dropwizard.github.io/dropwizard/manual/testing.html#integration-testing

Thanks,
Paul

Damien Martin-guillerez

unread,
Jun 10, 2015, 8:21:51 AM6/10/15
to Paul Gross, Kristina Chodorow, bazel-...@googlegroups.com
I don't think this can be make to work with resources as resources are packed inside the jar file. Anyway, runfiles should work prefectly ok with java_test so using data is the correct way to go.


For more options, visit https://groups.google.com/d/optout.
--
Damien
Reply all
Reply to author
Forward
0 new messages