aboutsummaryrefslogtreecommitdiff
path: root/absl/testing
diff options
context:
space:
mode:
authorYilei Yang <yileiyang@google.com>2018-01-16 14:43:19 -0800
committerCopybara-Service <copybara-piper@google.com>2018-01-16 14:43:39 -0800
commitbf431a057393a6a1d83cebfb83d7aaef2c530a6d (patch)
treef425654ab9559594ce8688c1f794e7ea730647a3 /absl/testing
parent809bfced716a811307d7a0a74d2737b135986542 (diff)
downloadabsl-py-bf431a057393a6a1d83cebfb83d7aaef2c530a6d.tar.gz
Windows tests: Use the bazel MANIFEST file to locate the .exe of the py_binary.
Added a workaround for https://github.com/bazelbuild/bazel/issues/4001 in absltest_test.py. PiperOrigin-RevId: 182114298
Diffstat (limited to 'absl/testing')
-rw-r--r--absl/testing/BUILD1
-rw-r--r--absl/testing/_bazelize_command.py43
-rw-r--r--absl/testing/tests/absltest_filtering_test.py5
-rw-r--r--absl/testing/tests/absltest_randomization_test.py5
-rwxr-xr-xabsl/testing/tests/absltest_sharding_test.py5
-rw-r--r--absl/testing/tests/absltest_test.py2
-rw-r--r--absl/testing/tests/absltest_test_helper.py26
-rwxr-xr-xabsl/testing/tests/xml_reporter_test.py8
8 files changed, 60 insertions, 35 deletions
diff --git a/absl/testing/BUILD b/absl/testing/BUILD
index 878886a..5420686 100644
--- a/absl/testing/BUILD
+++ b/absl/testing/BUILD
@@ -56,6 +56,7 @@ py_library(
srcs = ["_bazelize_command.py"],
srcs_version = "PY2AND3",
visibility = ["//:__subpackages__"],
+ deps = ["//absl/flags"],
)
py_test(
diff --git a/absl/testing/_bazelize_command.py b/absl/testing/_bazelize_command.py
index 93c135c..841eeb6 100644
--- a/absl/testing/_bazelize_command.py
+++ b/absl/testing/_bazelize_command.py
@@ -20,30 +20,43 @@ from __future__ import print_function
import os
+from absl import flags
-def get_executable_path(py_binary_path):
+FLAGS = flags.FLAGS
+
+
+def get_executable_path(py_binary_name):
"""Returns the executable path of a py_binary.
This returns the executable path of a py_binary that is in another Bazel
target's data dependencies.
- On Linux/macOS, it's the same as the py_binary_path.
- On Windows, the py_binary_path points to a zip file, and Bazel 0.5.3+
- generates a .cmd file that can be used to execute the py_binary.
+ On Linux/macOS, the path and __file__ has the same root directory.
+ On Windows, bazel builds an .exe file and we need to use the MANIFEST file
+ the location the actual binary.
Args:
- py_binary_path: string, the path of a py_binary that is in another Bazel
+ py_binary_name: string, the name of a py_binary that is in another Bazel
target's data dependencies.
+
+ Raises:
+ RuntimeError: Raised when it cannot locate the executable path.
"""
if os.name == 'nt':
- executable_path = py_binary_path + '.cmd'
- if executable_path.startswith('\\\\?\\'):
- # In Bazel 0.5.3 and Python 3, the paths starts with "\\?\".
- # However, Python subprocess doesn't support those paths well.
- # Strip them as we don't need the prefix.
- # See this page for more informaton about "\\?\":
- # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.
- executable_path = executable_path[4:]
- return executable_path
+ py_binary_name += '.exe'
+ manifest_file = os.path.join(FLAGS.test_srcdir, 'MANIFEST')
+ workspace_name = os.environ['TEST_WORKSPACE']
+ manifest_entry = '{}/{}'.format(workspace_name, py_binary_name)
+ with open(manifest_file, 'r') as manifest_fd:
+ for line in manifest_fd:
+ tokens = line.strip().split(' ')
+ if len(tokens) != 2:
+ continue
+ if manifest_entry == tokens[0]:
+ return tokens[1]
+ raise RuntimeError(
+ 'Cannot locate executable path for {}, MANIFEST file: {}.'.format(
+ py_binary_name, manifest_file))
else:
- return py_binary_path
+ root_directory = __file__[:-len('absl/testing/_bazelize_command.py')]
+ return os.path.join(root_directory, py_binary_name)
diff --git a/absl/testing/tests/absltest_filtering_test.py b/absl/testing/tests/absltest_filtering_test.py
index d008b4d..109d75b 100644
--- a/absl/testing/tests/absltest_filtering_test.py
+++ b/absl/testing/tests/absltest_filtering_test.py
@@ -33,8 +33,7 @@ class TestFilteringTest(absltest.TestCase):
"""
def setUp(self):
- self._test_path = os.path.join(
- os.path.dirname(__file__), 'absltest_filtering_test_helper')
+ self._test_name = 'absl/testing/tests/absltest_filtering_test_helper'
def _run_filtered(self, test_filter):
"""Runs the py_test binary in a subprocess.
@@ -54,7 +53,7 @@ class TestFilteringTest(absltest.TestCase):
env['TESTBRIDGE_TEST_ONLY'] = test_filter
proc = subprocess.Popen(
- args=[_bazelize_command.get_executable_path(self._test_path)],
+ args=[_bazelize_command.get_executable_path(self._test_name)],
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
diff --git a/absl/testing/tests/absltest_randomization_test.py b/absl/testing/tests/absltest_randomization_test.py
index ff00ca6..62ea2c2 100644
--- a/absl/testing/tests/absltest_randomization_test.py
+++ b/absl/testing/tests/absltest_randomization_test.py
@@ -37,8 +37,7 @@ class TestOrderRandomizationTest(parameterized.TestCase):
"""
def setUp(self):
- self._test_path = os.path.join(
- os.path.dirname(__file__), 'absltest_randomization_testcase')
+ self._test_name = 'absl/testing/tests/absltest_randomization_testcase'
def _run_test(self, extra_argv, extra_env):
"""Runs the py_test binary in a subprocess, with the given args or env.
@@ -58,7 +57,7 @@ class TestOrderRandomizationTest(parameterized.TestCase):
env.update(extra_env)
command = (
- [_bazelize_command.get_executable_path(self._test_path)] + extra_argv)
+ [_bazelize_command.get_executable_path(self._test_name)] + extra_argv)
proc = subprocess.Popen(
args=command,
env=env,
diff --git a/absl/testing/tests/absltest_sharding_test.py b/absl/testing/tests/absltest_sharding_test.py
index 1f0d03b..f32dc11 100755
--- a/absl/testing/tests/absltest_sharding_test.py
+++ b/absl/testing/tests/absltest_sharding_test.py
@@ -36,8 +36,7 @@ class TestShardingTest(absltest.TestCase):
"""
def setUp(self):
- self._test_path = os.path.join(
- os.path.dirname(__file__), 'absltest_sharding_test_helper')
+ self._test_name = 'absl/testing/tests/absltest_sharding_test_helper'
self._shard_file = None
def tearDown(self):
@@ -69,7 +68,7 @@ class TestShardingTest(absltest.TestCase):
os.unlink(shard_file)
proc = subprocess.Popen(
- args=[_bazelize_command.get_executable_path(self._test_path)],
+ args=[_bazelize_command.get_executable_path(self._test_name)],
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
diff --git a/absl/testing/tests/absltest_test.py b/absl/testing/tests/absltest_test.py
index b7473e4..b924dbe 100644
--- a/absl/testing/tests/absltest_test.py
+++ b/absl/testing/tests/absltest_test.py
@@ -41,7 +41,7 @@ class TestCaseTest(absltest.TestCase):
longMessage = True
def run_helper(self, test_id, args, env_overrides, expect_success):
- helper = os.path.join(os.path.dirname(__file__), 'absltest_test_helper')
+ helper = 'absl/testing/tests/absltest_test_helper'
env = os.environ.copy()
for key, value in six.iteritems(env_overrides):
if value is None:
diff --git a/absl/testing/tests/absltest_test_helper.py b/absl/testing/tests/absltest_test_helper.py
index 83f1e6a..306e963 100644
--- a/absl/testing/tests/absltest_test_helper.py
+++ b/absl/testing/tests/absltest_test_helper.py
@@ -47,29 +47,43 @@ class HelperTest(absltest.TestCase):
self.assertTrue(os.access(FLAGS.test_tmpdir, os.W_OK))
elif FLAGS.test_id == 2:
self.assertEqual(FLAGS.test_random_seed, 321)
- self.assertEqual(
+ self._assert_directories_equal(
FLAGS.test_srcdir,
os.environ['ABSLTEST_TEST_HELPER_EXPECTED_TEST_SRCDIR'])
- self.assertEqual(
+ self._assert_directories_equal(
FLAGS.test_tmpdir,
os.environ['ABSLTEST_TEST_HELPER_EXPECTED_TEST_TMPDIR'])
elif FLAGS.test_id == 3:
self.assertEqual(FLAGS.test_random_seed, 123)
- self.assertEqual(
+ self._assert_directories_equal(
FLAGS.test_srcdir,
os.environ['ABSLTEST_TEST_HELPER_EXPECTED_TEST_SRCDIR'])
- self.assertEqual(
+ self._assert_directories_equal(
FLAGS.test_tmpdir,
os.environ['ABSLTEST_TEST_HELPER_EXPECTED_TEST_TMPDIR'])
elif FLAGS.test_id == 4:
self.assertEqual(FLAGS.test_random_seed, 221)
- self.assertEqual(
+ self._assert_directories_equal(
FLAGS.test_srcdir,
os.environ['ABSLTEST_TEST_HELPER_EXPECTED_TEST_SRCDIR'])
- self.assertEqual(
+ self._assert_directories_equal(
FLAGS.test_tmpdir,
os.environ['ABSLTEST_TEST_HELPER_EXPECTED_TEST_TMPDIR'])
+ def _assert_directories_equal(self, expected, actual):
+ if os.name == 'nt':
+ # Bazel on Windows has a bug where backslashes passed to subprocess are
+ # unnecessarily unescaped. This is the workaround before a new Bazel
+ # release that includes the fix is available.
+ # See https://github.com/bazelbuild/bazel/issues/4001.
+ if expected == actual:
+ return
+ if expected == actual.replace('\\', '\\\\'):
+ return
+ raise AssertionError('{} != {}', expected, actual)
+ else:
+ self.assertEqual(expected, actual)
+
@unittest.expectedFailure
def test_expected_failure(self):
if FLAGS.test_id == 5:
diff --git a/absl/testing/tests/xml_reporter_test.py b/absl/testing/tests/xml_reporter_test.py
index 4e30add..723f170 100755
--- a/absl/testing/tests/xml_reporter_test.py
+++ b/absl/testing/tests/xml_reporter_test.py
@@ -693,8 +693,8 @@ class XmlReporterFixtureTest(absltest.TestCase):
os.close(xml_fhandle)
try:
- prog = os.path.join(os.path.dirname(__file__), 'xml_reporter_helper_test')
- args = [_bazelize_command.get_executable_path(prog),
+ binary_name = 'absl/testing/tests/xml_reporter_helper_test'
+ args = [_bazelize_command.get_executable_path(binary_name),
flag, '--xml_output_file=%s' % xml_fname]
ret = subprocess.call(args)
self.assertNotEqual(ret, 0)
@@ -710,8 +710,8 @@ class XmlReporterFixtureTest(absltest.TestCase):
os.close(xml_fhandle)
try:
- prog = os.path.join(os.path.dirname(__file__), 'xml_reporter_helper_test')
- args = [_bazelize_command.get_executable_path(prog),
+ binary_name = 'absl/testing/tests/xml_reporter_helper_test'
+ args = [_bazelize_command.get_executable_path(binary_name),
flag, '--xml_output_file=%s' % xml_fname]
ret = subprocess.call(args)
self.assertNotEqual(ret, 0)