diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2018-02-09 07:10:22 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-09 07:12:07 -0800 |
commit | fb014f8e7b352c24ef6c8857eb0c9fdb3a171f9a (patch) | |
tree | b68967a2ea89bc8e57b490b65b6002c4b366a159 /src/test/py | |
parent | 220d650b3f47712a5b82748141f3381075c35c80 (diff) |
Java,runfiles,test: test finding runfiles
Add a test to //src/test/py/bazel:runfiles_test
that ensures that Java binaries using the Java
runfiles library in @bazel_tools can find their
runfiles, even if JAVA_RUNFILES and the RUNFILES_*
envvars are undefined.
There's already a shell script test for this in
//src/test/shell/bazel:java_launcher_test, but we
don't run that on Windows, shell tests are slower
on Windows than on Linux anyway, and adding this
test was very little work. Plus I'll need the new
Bar.java file for other tests that are coming.
See https://github.com/bazelbuild/bazel/issues/4460
Change-Id: Ie0fa51046c3f3a1e5ece7a6206131261a5a5b1f8
PiperOrigin-RevId: 185137988
Diffstat (limited to 'src/test/py')
4 files changed, 67 insertions, 30 deletions
diff --git a/src/test/py/bazel/runfiles_test.py b/src/test/py/bazel/runfiles_test.py index f416904241..cda4bf11be 100644 --- a/src/test/py/bazel/runfiles_test.py +++ b/src/test/py/bazel/runfiles_test.py @@ -122,12 +122,14 @@ class RunfilesTest(test_base.TestBase): self.fail("lines: %s" % lines) self.assertEqual(lines[0], "data for bar.py") - def testPythonRunfilesLibraryFindsRunfilesWithoutEnvvars(self): + def testRunfilesLibrariesFindRunfilesWithoutEnvvars(self): for s, t in [ ("WORKSPACE.mock", "WORKSPACE"), ("bar/BUILD.mock", "bar/BUILD"), ("bar/bar.py", "bar/bar.py"), ("bar/bar-py-data.txt", "bar/bar-py-data.txt"), + ("bar/Bar.java", "bar/Bar.java"), + ("bar/bar-java-data.txt", "bar/bar-java-data.txt"), ]: self.CopyFile( self.Rlocation( @@ -140,34 +142,35 @@ class RunfilesTest(test_base.TestBase): exit_code, _, stderr = self.RunBazel(["build", "//bar:all"]) self.AssertExitCode(exit_code, 0, stderr) - if test_base.TestBase.IsWindows(): - bin_path = os.path.join(bazel_bin, "bar/bar-py.exe") - else: - bin_path = os.path.join(bazel_bin, "bar/bar-py") - - self.assertTrue(os.path.exists(bin_path)) - - exit_code, stdout, stderr = self.RunProgram( - [bin_path], - env_remove=set([ - "RUNFILES_MANIFEST_FILE", - "RUNFILES_MANIFEST_ONLY", - "RUNFILES_DIR", - "JAVA_RUNFILES", - ]), - env_add={"TEST_SRCDIR": "__ignore_me__"}) - self.AssertExitCode(exit_code, 0, stderr) - if len(stdout) < 2: - self.fail("stdout: %s" % stdout) - self.assertEqual(stdout[0], "Hello Python Bar!") - six.assertRegex(self, stdout[1], "^rloc=.*/bar/bar-py-data.txt") - self.assertNotIn("__ignore_me__", stdout[1]) - - with open(stdout[1].split("=", 1)[1], "r") as f: - lines = [l.strip() for l in f.readlines()] - if len(lines) != 1: - self.fail("lines: %s" % lines) - self.assertEqual(lines[0], "data for bar.py") + for lang in [("py", "Python", "bar.py"), ("java", "Java", "Bar.java")]: + if test_base.TestBase.IsWindows(): + bin_path = os.path.join(bazel_bin, "bar/bar-%s.exe" % lang[0]) + else: + bin_path = os.path.join(bazel_bin, "bar/bar-" + lang[0]) + + self.assertTrue(os.path.exists(bin_path)) + + exit_code, stdout, stderr = self.RunProgram( + [bin_path], + env_remove=set([ + "RUNFILES_MANIFEST_FILE", + "RUNFILES_MANIFEST_ONLY", + "RUNFILES_DIR", + "JAVA_RUNFILES", + ]), + env_add={"TEST_SRCDIR": "__ignore_me__"}) + self.AssertExitCode(exit_code, 0, stderr) + if len(stdout) < 2: + self.fail("stdout(%s): %s" % (lang[0], stdout)) + self.assertEqual(stdout[0], "Hello %s Bar!" % lang[1]) + six.assertRegex(self, stdout[1], "^rloc=.*/bar/bar-%s-data.txt" % lang[0]) + self.assertNotIn("__ignore_me__", stdout[1]) + + with open(stdout[1].split("=", 1)[1], "r") as f: + lines = [l.strip() for l in f.readlines()] + if len(lines) != 1: + self.fail("lines(%s): %s" % (lang[0], lines)) + self.assertEqual(lines[0], "data for " + lang[2]) if __name__ == "__main__": diff --git a/src/test/py/bazel/testdata/runfiles_test/bar/BUILD.mock b/src/test/py/bazel/testdata/runfiles_test/bar/BUILD.mock index 0405d57cf4..92098bce73 100644 --- a/src/test/py/bazel/testdata/runfiles_test/bar/BUILD.mock +++ b/src/test/py/bazel/testdata/runfiles_test/bar/BUILD.mock @@ -3,7 +3,15 @@ package(default_visibility = ["//visibility:public"]) py_binary( name = "bar-py", srcs = ["bar.py"], + data = ["bar-py-data.txt"], main = "bar.py", deps = ["@bazel_tools//tools/runfiles:py-runfiles"], - data = ["bar-py-data.txt"], +) + +java_binary( + name = "bar-java", + srcs = ["Bar.java"], + data = ["bar-java-data.txt"], + main_class = "Bar", + deps = ["@bazel_tools//tools/runfiles:java-runfiles"], ) diff --git a/src/test/py/bazel/testdata/runfiles_test/bar/Bar.java b/src/test/py/bazel/testdata/runfiles_test/bar/Bar.java new file mode 100644 index 0000000000..30e77bc261 --- /dev/null +++ b/src/test/py/bazel/testdata/runfiles_test/bar/Bar.java @@ -0,0 +1,25 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.devtools.build.runfiles.Runfiles; +import java.io.IOException; + +/** A mock Java binary only used in tests, to exercise the Java Runfiles library. */ +public class Bar { + public static void main(String[] args) throws IOException { + System.out.println("Hello Java Bar!"); + Runfiles r = Runfiles.create(); + System.out.println("rloc=" + r.rlocation("foo_ws/bar/bar-java-data.txt")); + } +} diff --git a/src/test/py/bazel/testdata/runfiles_test/bar/bar-java-data.txt b/src/test/py/bazel/testdata/runfiles_test/bar/bar-java-data.txt new file mode 100644 index 0000000000..5b29b00473 --- /dev/null +++ b/src/test/py/bazel/testdata/runfiles_test/bar/bar-java-data.txt @@ -0,0 +1 @@ +data for Bar.java |