Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cf3cdc1

Browse filesBrowse files
authored
test(bzlmod): Make some tests bzlmod compatible with Bazel@HEAD (bazel-contrib#1482)
A few tests weren't compatible with bzlmod, so would fail when it was enabled. The various causes and fixes are: * Under bzlmod, `runfiles.CurrentRepository()` returns the empty string for the main repository. To fix, an environment variable is used to tell the test whether bzlmod is enabled or not. * Accessing data files through `TEST_SRCDIR` directly is error-prone under bzlmod because the directory name within runfiles changes from the workspace name to `_main`. To fix, use the runfiles libraries, which know how to map apparent repo names to the actual directory name. In the integration tests, the runfiles library isn't available, so just check for the `_main` directory instead. Work towards bazel-contrib#1469
1 parent 5b529ff commit cf3cdc1
Copy full SHA for cf3cdc1

File tree

Expand file treeCollapse file tree

5 files changed

+67
-88
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+67
-88
lines changed

‎examples/wheel/BUILD.bazel

Copy file name to clipboardExpand all lines: examples/wheel/BUILD.bazel
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,7 @@ py_test(
323323
":python_requires_in_a_package",
324324
":use_rule_with_dir_in_outs",
325325
],
326+
deps = [
327+
"//python/runfiles",
328+
],
326329
)

‎examples/wheel/wheel_test.py

Copy file name to clipboardExpand all lines: examples/wheel/wheel_test.py
+43-72Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,33 @@
1818
import unittest
1919
import zipfile
2020

21+
from python.runfiles import runfiles
22+
2123

2224
class WheelTest(unittest.TestCase):
2325
maxDiff = None
2426

27+
def setUp(self):
28+
super().setUp()
29+
self.runfiles = runfiles.Create()
30+
31+
def _get_path(self, filename):
32+
runfiles_path = os.path.join("rules_python/examples/wheel", filename)
33+
path = self.runfiles.Rlocation(runfiles_path)
34+
# The runfiles API can return None if the path doesn't exist or
35+
# can't be resolved.
36+
if not path:
37+
raise AssertionError(f"Runfiles failed to resolve {runfiles_path}")
38+
elif not os.path.exists(path):
39+
# A non-None value doesn't mean the file actually exists, though
40+
raise AssertionError(
41+
f"Path {path} does not exist (from runfiles path {runfiles_path}"
42+
)
43+
else:
44+
return path
45+
2546
def test_py_library_wheel(self):
26-
filename = os.path.join(
27-
os.environ["TEST_SRCDIR"],
28-
"rules_python",
29-
"examples",
30-
"wheel",
31-
"example_minimal_library-0.0.1-py3-none-any.whl",
32-
)
47+
filename = self._get_path("example_minimal_library-0.0.1-py3-none-any.whl")
3348
with zipfile.ZipFile(filename) as zf:
3449
self.assertEqual(
3550
zf.namelist(),
@@ -43,11 +58,7 @@ def test_py_library_wheel(self):
4358
)
4459

4560
def test_py_package_wheel(self):
46-
filename = os.path.join(
47-
os.environ["TEST_SRCDIR"],
48-
"rules_python",
49-
"examples",
50-
"wheel",
61+
filename = self._get_path(
5162
"example_minimal_package-0.0.1-py3-none-any.whl",
5263
)
5364
with zipfile.ZipFile(filename) as zf:
@@ -65,11 +76,7 @@ def test_py_package_wheel(self):
6576
)
6677

6778
def test_customized_wheel(self):
68-
filename = os.path.join(
69-
os.environ["TEST_SRCDIR"],
70-
"rules_python",
71-
"examples",
72-
"wheel",
79+
filename = self._get_path(
7380
"example_customized-0.0.1-py3-none-any.whl",
7481
)
7582
with zipfile.ZipFile(filename) as zf:
@@ -154,31 +161,27 @@ def test_customized_wheel(self):
154161
)
155162

156163
def test_legacy_filename_escaping(self):
157-
filename = os.path.join(
158-
os.environ['TEST_SRCDIR'],
159-
'rules_python',
160-
'examples',
161-
'wheel',
162-
'file_name_escaping-0.0.1_r7-py3-none-any.whl',
164+
filename = self._get_path(
165+
"file_name_escaping-0.0.1_r7-py3-none-any.whl",
163166
)
164167
with zipfile.ZipFile(filename) as zf:
165168
self.assertEquals(
166169
zf.namelist(),
167170
[
168-
'examples/wheel/lib/data.txt',
169-
'examples/wheel/lib/module_with_data.py',
170-
'examples/wheel/lib/simple_module.py',
171-
'examples/wheel/main.py',
171+
"examples/wheel/lib/data.txt",
172+
"examples/wheel/lib/module_with_data.py",
173+
"examples/wheel/lib/simple_module.py",
174+
"examples/wheel/main.py",
172175
# PEP calls for replacing only in the archive filename.
173176
# Alas setuptools also escapes in the dist-info directory
174177
# name, so let's be compatible.
175-
'file_name_escaping-0.0.1_r7.dist-info/WHEEL',
176-
'file_name_escaping-0.0.1_r7.dist-info/METADATA',
177-
'file_name_escaping-0.0.1_r7.dist-info/RECORD',
178+
"file_name_escaping-0.0.1_r7.dist-info/WHEEL",
179+
"file_name_escaping-0.0.1_r7.dist-info/METADATA",
180+
"file_name_escaping-0.0.1_r7.dist-info/RECORD",
178181
],
179182
)
180183
metadata_contents = zf.read(
181-
'file_name_escaping-0.0.1_r7.dist-info/METADATA'
184+
"file_name_escaping-0.0.1_r7.dist-info/METADATA"
182185
)
183186
self.assertEquals(
184187
metadata_contents,
@@ -192,11 +195,7 @@ def test_legacy_filename_escaping(self):
192195
)
193196

194197
def test_filename_escaping(self):
195-
filename = os.path.join(
196-
os.environ["TEST_SRCDIR"],
197-
"rules_python",
198-
"examples",
199-
"wheel",
198+
filename = self._get_path(
200199
"file_name_escaping-0.0.1rc1+ubuntu.r7-py3-none-any.whl",
201200
)
202201
with zipfile.ZipFile(filename) as zf:
@@ -230,11 +229,7 @@ def test_filename_escaping(self):
230229
)
231230

232231
def test_custom_package_root_wheel(self):
233-
filename = os.path.join(
234-
os.environ["TEST_SRCDIR"],
235-
"rules_python",
236-
"examples",
237-
"wheel",
232+
filename = self._get_path(
238233
"examples_custom_package_root-0.0.1-py3-none-any.whl",
239234
)
240235

@@ -262,11 +257,7 @@ def test_custom_package_root_wheel(self):
262257
self.assertFalse(line.startswith("/"))
263258

264259
def test_custom_package_root_multi_prefix_wheel(self):
265-
filename = os.path.join(
266-
os.environ["TEST_SRCDIR"],
267-
"rules_python",
268-
"examples",
269-
"wheel",
260+
filename = self._get_path(
270261
"example_custom_package_root_multi_prefix-0.0.1-py3-none-any.whl",
271262
)
272263

@@ -293,11 +284,7 @@ def test_custom_package_root_multi_prefix_wheel(self):
293284
self.assertFalse(line.startswith("/"))
294285

295286
def test_custom_package_root_multi_prefix_reverse_order_wheel(self):
296-
filename = os.path.join(
297-
os.environ["TEST_SRCDIR"],
298-
"rules_python",
299-
"examples",
300-
"wheel",
287+
filename = self._get_path(
301288
"example_custom_package_root_multi_prefix_reverse_order-0.0.1-py3-none-any.whl",
302289
)
303290

@@ -324,11 +311,7 @@ def test_custom_package_root_multi_prefix_reverse_order_wheel(self):
324311
self.assertFalse(line.startswith("/"))
325312

326313
def test_python_requires_wheel(self):
327-
filename = os.path.join(
328-
os.environ["TEST_SRCDIR"],
329-
"rules_python",
330-
"examples",
331-
"wheel",
314+
filename = self._get_path(
332315
"example_python_requires_in_a_package-0.0.1-py3-none-any.whl",
333316
)
334317
with zipfile.ZipFile(filename) as zf:
@@ -359,11 +342,7 @@ def test_python_abi3_binary_wheel(self):
359342
"Windows": "win",
360343
}
361344
os_string = os_strings[platform.system()]
362-
filename = os.path.join(
363-
os.environ["TEST_SRCDIR"],
364-
"rules_python",
365-
"examples",
366-
"wheel",
345+
filename = self._get_path(
367346
f"example_python_abi3_binary_wheel-0.0.1-cp38-abi3-{os_string}_{arch}.whl",
368347
)
369348
with zipfile.ZipFile(filename) as zf:
@@ -396,11 +375,7 @@ def test_python_abi3_binary_wheel(self):
396375
)
397376

398377
def test_rule_creates_directory_and_is_included_in_wheel(self):
399-
filename = os.path.join(
400-
os.environ["TEST_SRCDIR"],
401-
"rules_python",
402-
"examples",
403-
"wheel",
378+
filename = self._get_path(
404379
"use_rule_with_dir_in_outs-0.0.1-py3-none-any.whl",
405380
)
406381

@@ -417,12 +392,8 @@ def test_rule_creates_directory_and_is_included_in_wheel(self):
417392
)
418393

419394
def test_rule_expands_workspace_status_keys_in_wheel_metadata(self):
420-
filename = os.path.join(
421-
os.environ["TEST_SRCDIR"],
422-
"rules_python",
423-
"examples",
424-
"wheel",
425-
"example_minimal_library_BUILD_USER_-0.1._BUILD_TIMESTAMP_-py3-none-any.whl",
395+
filename = self._get_path(
396+
"example_minimal_library_BUILD_USER_-0.1._BUILD_TIMESTAMP_-py3-none-any.whl"
426397
)
427398

428399
with zipfile.ZipFile(filename) as zf:

‎python/tests/toolchains/run_acceptance_test.py.tmpl

Copy file name to clipboardExpand all lines: python/tests/toolchains/run_acceptance_test.py.tmpl
+9-4Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ import os
1616
import subprocess
1717
import unittest
1818

19-
2019
class TestPythonVersion(unittest.TestCase):
2120
@classmethod
2221
def setUpClass(cls):
2322
os.chdir("%test_location%")
24-
rules_python_path = os.path.join(os.environ["TEST_SRCDIR"], "rules_python")
23+
test_srcdir = os.environ["TEST_SRCDIR"]
24+
# When bzlmod is enabled, the name of the directory in runfiles changes
25+
# to _main instead of rules_python
26+
if os.path.exists(os.path.join(test_srcdir, "_main")):
27+
rules_python_path = os.path.join(test_srcdir, "_main")
28+
else:
29+
rules_python_path = os.path.join(test_srcdir, "rules_python")
2530

2631
test_tmpdir = os.environ["TEST_TMPDIR"]
2732
if %is_windows%:
@@ -57,13 +62,13 @@ class TestPythonVersion(unittest.TestCase):
5762

5863
def test_match_toolchain(self):
5964
output = subprocess.check_output(
60-
f"bazel run @python//:python3 -- --version",
65+
f"bazel run --announce_rc @python//:python3 -- --version",
6166
shell = True, # Shell needed to look up via PATH
6267
text=True,
6368
).strip()
6469
self.assertEqual(output, "Python %python_version%")
6570

66-
subprocess.run("bazel test //...", shell=True, check=True)
71+
subprocess.run("bazel test --announce_rc //...", shell=True, check=True)
6772

6873

6974
if __name__ == "__main__":

‎tests/runfiles/BUILD.bazel

Copy file name to clipboard
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
load("@rules_python//python:defs.bzl", "py_test")
1+
load("@rules_python//python:py_test.bzl", "py_test")
2+
load("@rules_python//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility
23

34
py_test(
45
name = "runfiles_test",
56
srcs = ["runfiles_test.py"],
7+
env = {
8+
"BZLMOD_ENABLED": "1" if BZLMOD_ENABLED else "0",
9+
},
610
deps = ["//python/runfiles"],
711
)

‎tests/runfiles/runfiles_test.py

Copy file name to clipboardExpand all lines: tests/runfiles/runfiles_test.py
+7-11Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -514,18 +514,14 @@ def testDirectoryBasedRlocationWithRepoMappingFromOtherRepo(self):
514514
)
515515

516516
def testCurrentRepository(self):
517-
# This test assumes that it is running without --enable_bzlmod as the
518-
# correct result with Bzlmod would be the empty string - the canonical
519-
# name # of the main repository. Without Bzlmod, the main repository is
520-
# treated just like any other repository and has the name of its
521-
# runfiles directory returned, which coincides with the name specified
522-
# in the WORKSPACE file.
523-
#
524-
# Specify a fake runfiles directory to verify that its value isn't used
525-
# by the function.
517+
# Under bzlmod, the current repository name is the empty string instead
518+
# of the name in the workspace file.
519+
if bool(int(os.environ["BZLMOD_ENABLED"])):
520+
expected = ""
521+
else:
522+
expected = "rules_python"
526523
self.assertEqual(
527-
runfiles.Create({"RUNFILES_DIR": "whatever"}).CurrentRepository(),
528-
"rules_python",
524+
runfiles.Create({"RUNFILES_DIR": "whatever"}).CurrentRepository(), expected
529525
)
530526

531527
@staticmethod

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.