aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/py
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2018-02-05 05:24:34 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-02-05 05:25:54 -0800
commita610a2b77893ed9edd3038cffe803bce68f83a80 (patch)
treee24dddc2d457ffdb908b3326f05748876c9955ef /src/test/py
parent17830c25a816d67ba79e783acaa6a085b516bd43 (diff)
python,runfiles: runfiles library in @bazel_tools
Also update the Python stub script template to set $RUNFILES_MANIFEST_FILE or $RUNFILES_DIR so the runfiles library only needs to look for those. See https://github.com/bazelbuild/bazel/issues/4460 RELNOTES[NEW]: python,runfiles: You can now depend on `@bazel_tools//tools/runfiles:py-runfiles` to get a platform-independent runfiles library for Python. See DocString of https://github.com/bazelbuild/bazel/blob/master/src/tools/runfiles/runfiles.py for usage information. Change-Id: I4f68a11cb59f2782e5203e39fe60cc66b46023a2 PiperOrigin-RevId: 184515490
Diffstat (limited to 'src/test/py')
-rw-r--r--src/test/py/bazel/BUILD6
-rw-r--r--src/test/py/bazel/runfiles_test.py44
-rw-r--r--src/test/py/bazel/test_base.py27
-rw-r--r--src/test/py/bazel/testdata/runfiles_test/WORKSPACE.mock1
-rw-r--r--src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock7
-rw-r--r--src/test/py/bazel/testdata/runfiles_test/foo/datadep/hello.txt1
-rw-r--r--src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py21
7 files changed, 103 insertions, 4 deletions
diff --git a/src/test/py/bazel/BUILD b/src/test/py/bazel/BUILD
index cfaf121925..87f3eef26f 100644
--- a/src/test/py/bazel/BUILD
+++ b/src/test/py/bazel/BUILD
@@ -95,7 +95,11 @@ py_test(
py_test(
name = "runfiles_test",
srcs = ["runfiles_test.py"],
- deps = [":test_base"],
+ data = glob(["testdata/runfiles_test/**"]),
+ deps = [
+ ":test_base",
+ "//third_party/py/six",
+ ],
)
py_test(
diff --git a/src/test/py/bazel/runfiles_test.py b/src/test/py/bazel/runfiles_test.py
index da6a143be4..c965f38fba 100644
--- a/src/test/py/bazel/runfiles_test.py
+++ b/src/test/py/bazel/runfiles_test.py
@@ -15,6 +15,7 @@
import os
import unittest
+import six
from src.test.py.bazel import test_base
@@ -70,13 +71,50 @@ class RunfilesTest(test_base.TestBase):
exit_code, stdout, stderr = self.RunProgram([bin_path])
self.AssertExitCode(exit_code, 0, stderr)
if len(stdout) != 2:
- self.fail("stdout: " + stdout)
+ self.fail("stdout: %s" % stdout)
self.assertEqual(stdout[0], "Hello Foo!")
- self.assertRegexpMatches(stdout[1], "^rloc=.*/foo/bar/hello.txt")
+ six.assertRegex(self, stdout[1], "^rloc=.*/foo/bar/hello.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: " + lines)
+ self.fail("lines: %s" % lines)
+ self.assertEqual(lines[0], "world")
+
+ def testPythonRunfilesLibraryInBazelToolsRepo(self):
+ for s, t in [
+ ("WORKSPACE.mock", "WORKSPACE"),
+ ("foo/BUILD.mock", "foo/BUILD"),
+ ("foo/runfiles.py", "foo/runfiles.py"),
+ ("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-py"])
+ self.AssertExitCode(exit_code, 0, stderr)
+
+ if test_base.TestBase.IsWindows():
+ bin_path = os.path.join(bazel_bin, "foo/runfiles-py.exe")
+ else:
+ bin_path = os.path.join(bazel_bin, "foo/runfiles-py")
+
+ self.assertTrue(os.path.exists(bin_path))
+
+ exit_code, stdout, stderr = self.RunProgram([bin_path])
+ self.AssertExitCode(exit_code, 0, stderr)
+ if len(stdout) != 2:
+ self.fail("stdout: %s" % stdout)
+ self.assertEqual(stdout[0], "Hello Foo!")
+ six.assertRegex(self, stdout[1], "^rloc=.*/foo/datadep/hello.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")
diff --git a/src/test/py/bazel/test_base.py b/src/test/py/bazel/test_base.py
index be8a0c4eb9..27d1898ab7 100644
--- a/src/test/py/bazel/test_base.py
+++ b/src/test/py/bazel/test_base.py
@@ -174,6 +174,33 @@ class TestBase(unittest.TestCase):
os.chmod(abspath, stat.S_IRWXU)
return abspath
+ def CopyFile(self, src_path, dst_path, executable=False):
+ """Copy a file to a path under the test's scratch directory.
+
+ Args:
+ src_path: string; a path, the file to copy
+ dst_path: string; a path, relative to the test's scratch directory, the
+ destination to copy the file to, e.g. "foo/bar/BUILD"
+ executable: bool; whether to make the destination file executable
+ Returns:
+ The absolute path of the destination file.
+ Raises:
+ ArgumentError: if `dst_path` is absolute or contains uplevel references
+ IOError: if an I/O error occurs
+ """
+ if not src_path or not dst_path:
+ return
+ abspath = self.Path(dst_path)
+ if os.path.exists(abspath) and not os.path.isfile(abspath):
+ raise IOError('"%s" (%s) exists and is not a file' % (dst_path, abspath))
+ self.ScratchDir(os.path.dirname(dst_path))
+ with open(src_path, 'r') as s:
+ with open(abspath, 'w') as d:
+ d.write(s.read())
+ if executable:
+ os.chmod(abspath, stat.S_IRWXU)
+ return abspath
+
def RunBazel(self, args, env_remove=None, env_add=None):
"""Runs "bazel <args>", waits for it to exit.
diff --git a/src/test/py/bazel/testdata/runfiles_test/WORKSPACE.mock b/src/test/py/bazel/testdata/runfiles_test/WORKSPACE.mock
new file mode 100644
index 0000000000..e7c87450e1
--- /dev/null
+++ b/src/test/py/bazel/testdata/runfiles_test/WORKSPACE.mock
@@ -0,0 +1 @@
+workspace(name = "foo_ws")
diff --git a/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock b/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock
new file mode 100644
index 0000000000..db28159133
--- /dev/null
+++ b/src/test/py/bazel/testdata/runfiles_test/foo/BUILD.mock
@@ -0,0 +1,7 @@
+py_binary(
+ name = "runfiles-py",
+ srcs = ["runfiles.py"],
+ data = ["datadep/hello.txt"],
+ main = "runfiles.py",
+ deps = ["@bazel_tools//tools/runfiles:py-runfiles"],
+)
diff --git a/src/test/py/bazel/testdata/runfiles_test/foo/datadep/hello.txt b/src/test/py/bazel/testdata/runfiles_test/foo/datadep/hello.txt
new file mode 100644
index 0000000000..cc628ccd10
--- /dev/null
+++ b/src/test/py/bazel/testdata/runfiles_test/foo/datadep/hello.txt
@@ -0,0 +1 @@
+world
diff --git a/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py b/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py
new file mode 100644
index 0000000000..c4f3def874
--- /dev/null
+++ b/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.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 Foo!')
+r = runfiles.Create()
+print('rloc=%s' % r.Rlocation('foo_ws/foo/datadep/hello.txt'))