diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2018-02-14 01:37:59 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-14 01:41:02 -0800 |
commit | b4545ba2b1aa4079b09a346a6d441ffa1e1b7d20 (patch) | |
tree | 848acb70c03d5cbccd1c30234bef513a4a871f40 /src/test/py | |
parent | 9bdb4d83e41170b573b602a8c2d3571a838fd153 (diff) |
windows,launcher: now picks up RUNFILES_* envvars
The Windows native {java,py,sh}_binary launcher
now picks up the RUNFILES_MANIFEST_FILE and
RUNFILES_DIR envvars from the environment. This
enables running such binaries even when they are
data-dependencies of other binaries and thus have
no runfiles (manifest or directory) of their own.
See https://github.com/bazelbuild/bazel/issues/4460
Change-Id: I1bc8e30f81b9932ea7c96ded2717ac9d0600403d
PiperOrigin-RevId: 185656004
Diffstat (limited to 'src/test/py')
-rw-r--r-- | src/test/py/bazel/runfiles_test.py | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/test/py/bazel/runfiles_test.py b/src/test/py/bazel/runfiles_test.py index cda4bf11be..e3f697cc55 100644 --- a/src/test/py/bazel/runfiles_test.py +++ b/src/test/py/bazel/runfiles_test.py @@ -172,6 +172,94 @@ class RunfilesTest(test_base.TestBase): self.fail("lines(%s): %s" % (lang[0], lines)) self.assertEqual(lines[0], "data for " + lang[2]) + def testRunfilesLibrariesFindRunfilesWithRunfilesManifestEnvvar(self): + for s, t in [ + ("WORKSPACE.mock", "WORKSPACE"), + ("bar/BUILD.mock", "bar/BUILD"), + # Note: do not test Python here, because py_binary always needs a + # runfiles tree, even on Windows, because it needs __init__.py files in + # every directory where there may be importable modules, so Bazel always + # needs to create a runfiles tree for py_binary. + ("bar/Bar.java", "bar/Bar.java"), + ("bar/bar-java-data.txt", "bar/bar-java-data.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", "--experimental_enable_runfiles=no", "//bar:bar-java"]) + self.AssertExitCode(exit_code, 0, stderr) + + if test_base.TestBase.IsWindows(): + bin_path = os.path.join(bazel_bin, "bar/bar-java.exe") + else: + bin_path = os.path.join(bazel_bin, "bar/bar-java") + + manifest_path = bin_path + ".runfiles_manifest" + self.assertTrue(os.path.exists(bin_path)) + self.assertTrue(os.path.exists(manifest_path)) + + # Create a copy of the runfiles manifest, replacing + # "bar/bar-java-data.txt" with a custom file. + mock_bar_dep = self.ScratchFile("bar-java-mockdata.txt", ["mock java data"]) + if test_base.TestBase.IsWindows(): + # Runfiles manifests use forward slashes as path separators, even on + # Windows. + mock_bar_dep = mock_bar_dep.replace("\\", "/") + manifest_key = "foo_ws/bar/bar-java-data.txt" + mock_manifest_line = manifest_key + " " + mock_bar_dep + with open(manifest_path, "rt") as f: + # Only rstrip newlines. Do not rstrip() completely, because that would + # remove spaces too. This is necessary in order to have at least one + # space in every manifest line. + # Some manifest entries don't have any path after this space, namely the + # "__init__.py" entries. (Bazel writes such manifests on every + # platform). The reason is that these files are never symlinks in the + # runfiles tree, Bazel actually creates empty __init__.py files (again + # on every platform). However to keep these manifest entries correct, + # they need to have a space character. + # We could probably strip thses lines completely, but this test doesn't + # aim to exercise what would happen in that case. + mock_manifest_data = [ + mock_manifest_line + if line.split(" ", 1)[0] == manifest_key else line.rstrip("\n\r") + for line in f + ] + + substitute_manifest = self.ScratchFile("mock-java.runfiles/MANIFEST", + mock_manifest_data) + + exit_code, stdout, stderr = self.RunProgram( + [bin_path], + env_remove=set(["RUNFILES_DIR"]), + env_add={ + # On Linux/macOS, the Java launcher picks up JAVA_RUNFILES and + # ignores RUNFILES_MANIFEST_FILE. + "JAVA_RUNFILES": substitute_manifest[:-len("/MANIFEST")], + # On Windows, the Java launcher picks up RUNFILES_MANIFEST_FILE. + "RUNFILES_MANIFEST_FILE": substitute_manifest, + "RUNFILES_MANIFEST_ONLY": "1", + "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 Bar!") + six.assertRegex(self, stdout[1], "^rloc=" + mock_bar_dep) + 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], "mock java data") + if __name__ == "__main__": unittest.main() |