diff options
author | 2018-02-06 01:51:40 -0800 | |
---|---|---|
committer | 2018-02-06 01:53:33 -0800 | |
commit | 5af519ac126a3df4371b3e8a8bb3cec6ed69c73c (patch) | |
tree | d225c3c36f5f08a48f8e35d5c6089a7b1a14a48a /src/test/py | |
parent | 2c98fffd44555b616da0449c73ab6b5cafde344d (diff) |
runfiles,py: add test to run a data-dependency
Update the runfiles test's mock Python binary to
run another mock Python binary and assert that the
outer binary propagates the runfiles information
to the inner one.
See https://github.com/bazelbuild/bazel/issues/4460
Change-Id: I3bba799a376642196777f7bc988837084a53bc93
PiperOrigin-RevId: 184650962
Diffstat (limited to 'src/test/py')
6 files changed, 88 insertions, 6 deletions
diff --git a/src/test/py/bazel/runfiles_test.py b/src/test/py/bazel/runfiles_test.py index c965f38fba..2f9f193170 100644 --- a/src/test/py/bazel/runfiles_test.py +++ b/src/test/py/bazel/runfiles_test.py @@ -86,6 +86,9 @@ class RunfilesTest(test_base.TestBase): ("foo/BUILD.mock", "foo/BUILD"), ("foo/runfiles.py", "foo/runfiles.py"), ("foo/datadep/hello.txt", "foo/datadep/hello.txt"), + ("bar/BUILD.mock", "bar/BUILD"), + ("bar/bar.py", "bar/bar.py"), + ("bar/bar-py-data.txt", "bar/bar-py-data.txt"), ]: self.CopyFile( self.Rlocation( @@ -107,16 +110,25 @@ class RunfilesTest(test_base.TestBase): exit_code, stdout, stderr = self.RunProgram([bin_path]) self.AssertExitCode(exit_code, 0, stderr) - if len(stdout) != 2: + if len(stdout) < 4: self.fail("stdout: %s" % stdout) - self.assertEqual(stdout[0], "Hello Foo!") + self.assertEqual(stdout[0], "Hello Python Foo!") six.assertRegex(self, stdout[1], "^rloc=.*/foo/datadep/hello.txt") + self.assertEqual(stdout[2], "Hello Python Bar!") + six.assertRegex(self, stdout[3], "^rloc=.*/bar/bar-py-data.txt") + 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") + with open(stdout[3].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") + if __name__ == "__main__": unittest.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 new file mode 100644 index 0000000000..0405d57cf4 --- /dev/null +++ b/src/test/py/bazel/testdata/runfiles_test/bar/BUILD.mock @@ -0,0 +1,9 @@ +package(default_visibility = ["//visibility:public"]) + +py_binary( + name = "bar-py", + srcs = ["bar.py"], + main = "bar.py", + deps = ["@bazel_tools//tools/runfiles:py-runfiles"], + data = ["bar-py-data.txt"], +) diff --git a/src/test/py/bazel/testdata/runfiles_test/bar/bar-py-data.txt b/src/test/py/bazel/testdata/runfiles_test/bar/bar-py-data.txt new file mode 100644 index 0000000000..1f71930893 --- /dev/null +++ b/src/test/py/bazel/testdata/runfiles_test/bar/bar-py-data.txt @@ -0,0 +1 @@ +data for bar.py diff --git a/src/test/py/bazel/testdata/runfiles_test/bar/bar.py b/src/test/py/bazel/testdata/runfiles_test/bar/bar.py new file mode 100644 index 0000000000..a88b10b28b --- /dev/null +++ b/src/test/py/bazel/testdata/runfiles_test/bar/bar.py @@ -0,0 +1,21 @@ +# 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. +"""Mock Python binary, only used in tests.""" + +from __future__ import print_function +from bazel_tools.tools.runfiles import runfiles + +print('Hello Python Bar!') +r = runfiles.Create() +print('rloc=%s' % r.Rlocation('foo_ws/bar/bar-py-data.txt')) 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 db28159133..63d1d4e33a 100644 --- a/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock +++ b/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock @@ -1,7 +1,10 @@ py_binary( name = "runfiles-py", srcs = ["runfiles.py"], - data = ["datadep/hello.txt"], + data = [ + "datadep/hello.txt", + "//bar:bar-py", + ], main = "runfiles.py", deps = ["@bazel_tools//tools/runfiles:py-runfiles"], ) diff --git a/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py b/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py index c4f3def874..6b2ba94c4e 100644 --- a/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py +++ b/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py @@ -14,8 +14,44 @@ """Mock Python binary, only used in tests.""" from __future__ import print_function + +import os +import subprocess + from bazel_tools.tools.runfiles import runfiles -print('Hello Foo!') -r = runfiles.Create() -print('rloc=%s' % r.Rlocation('foo_ws/foo/datadep/hello.txt')) + +def IsWindows(): + return os.name == "nt" + + +def ChildBinaryName(lang): + if IsWindows(): + return "foo_ws/bar/bar-%s.exe" % lang + else: + return "foo_ws/bar/bar-%s" % lang + + +def main(): + print("Hello Python Foo!") + r = runfiles.Create() + print("rloc=%s" % r.Rlocation("foo_ws/foo/datadep/hello.txt")) + + # Run a subprocess, propagate the runfiles envvar to it. The subprocess will + # use this process's runfiles manifest or runfiles directory. + if IsWindows(): + env = {"SYSTEMROOT": os.environ["SYSTEMROOT"]} + else: + env = {} + env.update(r.EnvVar()) + p = subprocess.Popen( + [r.Rlocation(ChildBinaryName("py"))], + env=env, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + for e in p.communicate(): + print(e) + + +if __name__ == "__main__": + main() |