aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/py
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2018-04-16 03:29:37 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-04-16 03:30:37 -0700
commitef247efab4526547e12161b1d74cb624c0e0bd71 (patch)
tree6c9ef98446496c05f8f918fa87387ca8161ac784 /src/test/py
parentfa135cf600a957cbcef0ca45f97d5a9009d40859 (diff)
Java,runfiles: add Runfiles.getEnvVars()
The new method lets Java programs export the RUNFILES_* environment variables for subprocesses, in case those subprocesses are other Bazel-built binaries that also need runfiles. See https://github.com/bazelbuild/bazel/issues/4460 Change-Id: I05c0b84db357128bc15f21a167a0d92e8d17599c Closes #5016. Change-Id: I66ca5c44067a7353b8de180a64f20c8352e3c9ec PiperOrigin-RevId: 193013289
Diffstat (limited to 'src/test/py')
-rw-r--r--src/test/py/bazel/runfiles_test.py47
-rw-r--r--src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock2
-rw-r--r--src/test/py/bazel/testdata/runfiles_test/foo/Foo.java55
3 files changed, 62 insertions, 42 deletions
diff --git a/src/test/py/bazel/runfiles_test.py b/src/test/py/bazel/runfiles_test.py
index 21ed5fd855..fb2b0e180f 100644
--- a/src/test/py/bazel/runfiles_test.py
+++ b/src/test/py/bazel/runfiles_test.py
@@ -31,50 +31,12 @@ class RunfilesTest(test_base.TestBase):
self.assertIn("building runfiles is not supported on Windows",
"\n".join(stderr))
- def testJavaRunfilesLibraryInBazelToolsRepo(self):
- for s, t in [
- ("WORKSPACE.mock", "WORKSPACE"),
- ("foo/BUILD.mock", "foo/BUILD"),
- ("foo/Foo.java", "foo/Foo.java"),
- ("foo/datadep/hello.txt", "foo/datadep/hello.txt"),
- ]:
- self.CopyFile(
- self.Rlocation(
- "io_bazel/src/test/py/bazel/testdata/runfiles_test/" + s), t)
-
- exit_code, stdout, stderr = self.RunBazel(["info", "bazel-bin"])
- self.AssertExitCode(exit_code, 0, stderr)
- bazel_bin = stdout[0]
-
- exit_code, _, stderr = self.RunBazel(["build", "//foo:runfiles-java"])
- self.AssertExitCode(exit_code, 0, stderr)
-
- if test_base.TestBase.IsWindows():
- bin_path = os.path.join(bazel_bin, "foo/runfiles-java.exe")
- else:
- bin_path = os.path.join(bazel_bin, "foo/runfiles-java")
-
- self.assertTrue(os.path.exists(bin_path))
-
- exit_code, stdout, stderr = self.RunProgram(
- [bin_path], 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 Java Foo!")
- six.assertRegex(self, stdout[1], "^rloc=.*/foo/datadep/hello.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], "world")
-
- def _AssertPythonRunfilesLibraryInBazelToolsRepo(self, family, lang_name):
+ def _AssertRunfilesLibraryInBazelToolsRepo(self, family, lang_name):
for s, t in [
("WORKSPACE.mock", "WORKSPACE"),
("foo/BUILD.mock", "foo/BUILD"),
("foo/foo.py", "foo/foo.py"),
+ ("foo/Foo.java", "foo/Foo.java"),
("foo/datadep/hello.txt", "foo/datadep/hello.txt"),
("bar/BUILD.mock", "bar/BUILD"),
("bar/bar.py", "bar/bar.py"),
@@ -132,7 +94,10 @@ class RunfilesTest(test_base.TestBase):
i += 2
def testPythonRunfilesLibraryInBazelToolsRepo(self):
- self._AssertPythonRunfilesLibraryInBazelToolsRepo("py", "Python")
+ self._AssertRunfilesLibraryInBazelToolsRepo("py", "Python")
+
+ def testJavaRunfilesLibraryInBazelToolsRepo(self):
+ self._AssertRunfilesLibraryInBazelToolsRepo("java", "Java")
def testRunfilesLibrariesFindRunfilesWithoutEnvvars(self):
for s, t in [
diff --git a/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock b/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock
index 04fc23ebac..48d3108f67 100644
--- a/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock
+++ b/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock
@@ -15,6 +15,8 @@ java_binary(
srcs = ["Foo.java"],
data = [
"datadep/hello.txt",
+ "//bar:bar-py",
+ "//bar:bar-java",
],
main_class = "Foo",
deps = ["@bazel_tools//tools/runfiles:java-runfiles"],
diff --git a/src/test/py/bazel/testdata/runfiles_test/foo/Foo.java b/src/test/py/bazel/testdata/runfiles_test/foo/Foo.java
index 0e9f5cfcd0..44a01847ce 100644
--- a/src/test/py/bazel/testdata/runfiles_test/foo/Foo.java
+++ b/src/test/py/bazel/testdata/runfiles_test/foo/Foo.java
@@ -13,13 +13,66 @@
// limitations under the License.
import com.google.devtools.build.runfiles.Runfiles;
+import java.io.BufferedReader;
+import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.TimeUnit;
/** A mock Java binary only used in tests, to exercise the Java Runfiles library. */
public class Foo {
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("Hello Java Foo!");
Runfiles r = Runfiles.create();
System.out.println("rloc=" + r.rlocation("foo_ws/foo/datadep/hello.txt"));
+
+ for (String lang : new String[] {"py", "java"}) {
+ String path = r.rlocation(childBinaryName(lang));
+ if (path == null || path.isEmpty()) {
+ throw new IOException("cannot find child binary for " + lang);
+ }
+
+ ProcessBuilder pb = new ProcessBuilder(path);
+ pb.environment().putAll(r.getEnvVars());
+ if (isWindows()) {
+ pb.environment().put("SYSTEMROOT", System.getenv("SYSTEMROOT"));
+ }
+ Process p = pb.start();
+ if (!p.waitFor(3, TimeUnit.SECONDS)) {
+ throw new IOException("child process for " + lang + " timed out");
+ }
+ if (p.exitValue() != 0) {
+ throw new IOException(
+ "child process for " + lang + " failed: " + readStream(p.getErrorStream()));
+ }
+ System.out.printf(readStream(p.getInputStream()));
+ }
+ }
+
+ private static boolean isWindows() {
+ return File.separatorChar == '\\';
+ }
+
+ private static String childBinaryName(String lang) {
+ if (isWindows()) {
+ return "foo_ws/bar/bar-" + lang + ".exe";
+ } else {
+ return "foo_ws/bar/bar-" + lang;
+ }
+ }
+
+ private static String readStream(InputStream stm) throws IOException {
+ StringBuilder result = new StringBuilder();
+ try (BufferedReader r =
+ new BufferedReader(new InputStreamReader(stm, StandardCharsets.UTF_8))) {
+ String line = null;
+ while ((line = r.readLine()) != null) {
+ line = line.trim(); // trim CRLF on Windows, LF on Linux
+ result.append(line).append("\n"); // ensure uniform line ending
+ }
+ }
+ return result.toString();
}
}