Skip to content

Commit 3f9d80c

Browse files
fmeumcopybara-github
authored andcommitted
Use an HTTP registry in Bzlmod tests
This is in preparation for tracking the hashes of remote (non-`file:` URL) files in the lockfile. If the tests use local registries, they wouldn't be representative of the default situation. Work towards bazelbuild#20369 Closes bazelbuild#21906. PiperOrigin-RevId: 622290283 Change-Id: Ibe825d2bede84c1b0672dbb699aaf3ee5168a813
1 parent 55f5fd7 commit 3f9d80c

11 files changed

+146
-77
lines changed

src/test/py/bazel/bzlmod/bazel_fetch_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def setUp(self):
2929
self.main_registry = BazelRegistry(
3030
os.path.join(self.registries_work_dir, 'main')
3131
)
32+
self.main_registry.start()
3233
self.ScratchFile(
3334
'.bazelrc',
3435
[
@@ -48,6 +49,10 @@ def setUp(self):
4849
self.ScratchFile('MODULE.bazel')
4950
self.generatBuiltinModules()
5051

52+
def tearDown(self):
53+
self.main_registry.stop()
54+
test_base.TestBase.tearDown(self)
55+
5156
def generatBuiltinModules(self):
5257
self.ScratchFile('platforms_mock/BUILD')
5358
self.ScratchFile(

src/test/py/bazel/bzlmod/bazel_lockfile_test.py

Lines changed: 68 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def setUp(self):
3232
self.main_registry = BazelRegistry(
3333
os.path.join(self.registries_work_dir, 'main')
3434
)
35+
self.main_registry.start()
3536
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
3637
'aaa', '1.1'
3738
).createCcModule('bbb', '1.0', {'aaa': '1.0'}).createCcModule(
@@ -59,6 +60,10 @@ def setUp(self):
5960
# deterministic?
6061
os.remove(self.Path('MODULE.bazel.lock'))
6162

63+
def tearDown(self):
64+
self.main_registry.stop()
65+
test_base.TestBase.tearDown(self)
66+
6267
def testChangeModuleInRegistryWithoutLockfile(self):
6368
# Add module 'sss' to the registry with dep on 'aaa'
6469
self.main_registry.createCcModule('sss', '1.3', {'aaa': '1.1'})
@@ -1276,66 +1281,70 @@ def testExtensionEvaluationOnlyRerunOnRelevantUsagesChanges(self):
12761281

12771282
def testLockfileWithNoUserSpecificPath(self):
12781283
self.my_registry = BazelRegistry(os.path.join(self._test_cwd, 'registry'))
1279-
self.my_registry.setModuleBasePath('projects')
1280-
patch_file = self.ScratchFile(
1281-
'ss.patch',
1282-
[
1283-
'--- a/aaa.cc',
1284-
'+++ b/aaa.cc',
1285-
'@@ -1,6 +1,6 @@',
1286-
' #include <stdio.h>',
1287-
' #include "aaa.h"',
1288-
' void hello_aaa(const std::string& caller) {',
1289-
'- std::string lib_name = "[email protected]";',
1290-
'+ std::string lib_name = "[email protected] (remotely patched)";',
1291-
' printf("%s => %s\\n", caller.c_str(), lib_name.c_str());',
1292-
' }',
1293-
],
1294-
)
1295-
# Module with a local patch & extension
1296-
self.my_registry.createCcModule(
1297-
'ss',
1298-
'1.3-1',
1299-
{'ext': '1.0'},
1300-
patches=[patch_file],
1301-
patch_strip=1,
1302-
extra_module_file_contents=[
1303-
'my_ext = use_extension("@ext//:ext.bzl", "ext")',
1304-
'use_repo(my_ext, "justRepo")',
1305-
],
1306-
)
1307-
ext_src = [
1308-
'def _repo_impl(ctx): ctx.file("BUILD")',
1309-
'repo = repository_rule(_repo_impl)',
1310-
'def _ext_impl(ctx): repo(name=justRepo)',
1311-
'ext=module_extension(_ext_impl)',
1312-
]
1313-
self.my_registry.createLocalPathModule('ext', '1.0', 'ext')
1314-
scratchFile(self.my_registry.projects.joinpath('ext', 'BUILD'))
1315-
scratchFile(self.my_registry.projects.joinpath('ext', 'ext.bzl'), ext_src)
1316-
1317-
self.ScratchFile(
1318-
'MODULE.bazel',
1319-
[
1320-
'bazel_dep(name = "ss", version = "1.3-1")',
1321-
],
1322-
)
1323-
self.ScratchFile('BUILD.bazel', ['filegroup(name = "lala")'])
1324-
self.RunBazel(
1325-
['build', '--registry=file:///%workspace%/registry', '//:lala']
1326-
)
1284+
self.my_registry.start()
1285+
try:
1286+
self.my_registry.setModuleBasePath('projects')
1287+
patch_file = self.ScratchFile(
1288+
'ss.patch',
1289+
[
1290+
'--- a/aaa.cc',
1291+
'+++ b/aaa.cc',
1292+
'@@ -1,6 +1,6 @@',
1293+
' #include <stdio.h>',
1294+
' #include "aaa.h"',
1295+
' void hello_aaa(const std::string& caller) {',
1296+
'- std::string lib_name = "[email protected]";',
1297+
'+ std::string lib_name = "[email protected] (remotely patched)";',
1298+
' printf("%s => %s\\n", caller.c_str(), lib_name.c_str());',
1299+
' }',
1300+
],
1301+
)
1302+
# Module with a local patch & extension
1303+
self.my_registry.createCcModule(
1304+
'ss',
1305+
'1.3-1',
1306+
{'ext': '1.0'},
1307+
patches=[patch_file],
1308+
patch_strip=1,
1309+
extra_module_file_contents=[
1310+
'my_ext = use_extension("@ext//:ext.bzl", "ext")',
1311+
'use_repo(my_ext, "justRepo")',
1312+
],
1313+
)
1314+
ext_src = [
1315+
'def _repo_impl(ctx): ctx.file("BUILD")',
1316+
'repo = repository_rule(_repo_impl)',
1317+
'def _ext_impl(ctx): repo(name=justRepo)',
1318+
'ext=module_extension(_ext_impl)',
1319+
]
1320+
self.my_registry.createLocalPathModule('ext', '1.0', 'ext')
1321+
scratchFile(self.my_registry.projects.joinpath('ext', 'BUILD'))
1322+
scratchFile(self.my_registry.projects.joinpath('ext', 'ext.bzl'), ext_src)
1323+
1324+
self.ScratchFile(
1325+
'MODULE.bazel',
1326+
[
1327+
'bazel_dep(name = "ss", version = "1.3-1")',
1328+
],
1329+
)
1330+
self.ScratchFile('BUILD.bazel', ['filegroup(name = "lala")'])
1331+
self.RunBazel(
1332+
['build', '--registry=file:///%workspace%/registry', '//:lala']
1333+
)
13271334

1328-
with open('MODULE.bazel.lock', 'r') as json_file:
1329-
lockfile = json.load(json_file)
1330-
ss_dep = lockfile['moduleDepGraph']['[email protected]']
1331-
remote_patches = ss_dep['repoSpec']['attributes']['remote_patches']
1332-
ext_usage_location = ss_dep['extensionUsages'][0]['location']['file']
1333-
1334-
self.assertNotIn(self.my_registry.getURL(), ext_usage_location)
1335-
self.assertIn('%workspace%', ext_usage_location)
1336-
for key in remote_patches.keys():
1337-
self.assertNotIn(self.my_registry.getURL(), key)
1338-
self.assertIn('%workspace%', key)
1335+
with open('MODULE.bazel.lock', 'r') as json_file:
1336+
lockfile = json.load(json_file)
1337+
ss_dep = lockfile['moduleDepGraph']['[email protected]']
1338+
remote_patches = ss_dep['repoSpec']['attributes']['remote_patches']
1339+
ext_usage_location = ss_dep['extensionUsages'][0]['location']['file']
1340+
1341+
self.assertNotIn(self.my_registry.getURL(), ext_usage_location)
1342+
self.assertIn('%workspace%', ext_usage_location)
1343+
for key in remote_patches.keys():
1344+
self.assertNotIn(self.my_registry.getURL(), key)
1345+
self.assertIn('%workspace%', key)
1346+
finally:
1347+
self.my_registry.stop()
13391348

13401349
def testExtensionEvaluationRerunsIfDepGraphOrderChanges(self):
13411350
self.ScratchFile(

src/test/py/bazel/bzlmod/bazel_module_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def setUp(self):
3232
self.registries_work_dir = tempfile.mkdtemp(dir=self._test_cwd)
3333
self.main_registry = BazelRegistry(
3434
os.path.join(self.registries_work_dir, 'main'))
35+
self.main_registry.start()
3536
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
3637
'aaa', '1.1'
3738
).createCcModule(
@@ -63,6 +64,10 @@ def setUp(self):
6364
],
6465
)
6566

67+
def tearDown(self):
68+
self.main_registry.stop()
69+
test_base.TestBase.tearDown(self)
70+
6671
def writeMainProjectFiles(self):
6772
self.ScratchFile('aaa.patch', [
6873
'--- a/aaa.cc',

src/test/py/bazel/bzlmod/bazel_overrides_test.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def setUp(self):
2929
self.main_registry = BazelRegistry(
3030
os.path.join(self.registries_work_dir, 'main')
3131
)
32+
self.main_registry.start()
3233
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
3334
'aaa', '1.1'
3435
).createCcModule('bbb', '1.0', {'aaa': '1.0'}).createCcModule(
@@ -54,6 +55,10 @@ def setUp(self):
5455
],
5556
)
5657

58+
def tearDown(self):
59+
self.main_registry.stop()
60+
test_base.TestBase.tearDown(self)
61+
5762
def writeMainProjectFiles(self):
5863
self.ScratchFile(
5964
'aaa.patch',
@@ -122,22 +127,26 @@ def testRegistryOverride(self):
122127
os.path.join(self.registries_work_dir, 'another'),
123128
' from another registry',
124129
)
125-
another_registry.createCcModule('aaa', '1.0')
126-
self.ScratchFile(
127-
'MODULE.bazel',
128-
[
129-
'bazel_dep(name = "aaa", version = "1.0")',
130-
'bazel_dep(name = "bbb", version = "1.0")',
131-
'single_version_override(',
132-
' module_name = "aaa",',
133-
' registry = "%s",' % another_registry.getURL(),
134-
')',
135-
],
136-
)
137-
_, stdout, _ = self.RunBazel(['run', '//:main'])
138-
self.assertIn('main function => [email protected] from another registry', stdout)
139-
self.assertIn('main function => [email protected]', stdout)
140-
self.assertIn('[email protected] => [email protected] from another registry', stdout)
130+
another_registry.start()
131+
try:
132+
another_registry.createCcModule('aaa', '1.0')
133+
self.ScratchFile(
134+
'MODULE.bazel',
135+
[
136+
'bazel_dep(name = "aaa", version = "1.0")',
137+
'bazel_dep(name = "bbb", version = "1.0")',
138+
'single_version_override(',
139+
' module_name = "aaa",',
140+
' registry = "%s",' % another_registry.getURL(),
141+
')',
142+
],
143+
)
144+
_, stdout, _ = self.RunBazel(['run', '//:main'])
145+
self.assertIn('main function => [email protected] from another registry', stdout)
146+
self.assertIn('main function => [email protected]', stdout)
147+
self.assertIn('[email protected] => [email protected] from another registry', stdout)
148+
finally:
149+
another_registry.stop()
141150

142151
def testArchiveOverride(self):
143152
self.writeMainProjectFiles()

src/test/py/bazel/bzlmod/bazel_repo_mapping_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def setUp(self):
3030
self.main_registry = BazelRegistry(
3131
os.path.join(self.registries_work_dir, 'main')
3232
)
33+
self.main_registry.start()
3334
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
3435
'aaa', '1.1'
3536
).createCcModule('bbb', '1.0', {'aaa': '1.0'}).createCcModule(
@@ -59,6 +60,10 @@ def setUp(self):
5960
],
6061
)
6162

63+
def tearDown(self):
64+
self.main_registry.stop()
65+
test_base.TestBase.tearDown(self)
66+
6267
def testRunfilesRepoMappingManifest(self):
6368
self.main_registry.setModuleBasePath('projects')
6469
projects_dir = self.main_registry.projects

src/test/py/bazel/bzlmod/bazel_vendor_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def setUp(self):
2929
self.main_registry = BazelRegistry(
3030
os.path.join(self.registries_work_dir, 'main')
3131
)
32+
self.main_registry.start()
3233
self.ScratchFile(
3334
'.bazelrc',
3435
[
@@ -49,6 +50,10 @@ def setUp(self):
4950
self.ScratchFile('MODULE.bazel')
5051
self.generateBuiltinModules()
5152

53+
def tearDown(self):
54+
self.main_registry.stop()
55+
test_base.TestBase.tearDown(self)
56+
5257
def generateBuiltinModules(self):
5358
self.ScratchFile('platforms_mock/BUILD')
5459
self.ScratchFile(

src/test/py/bazel/bzlmod/bazel_yanked_versions_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def setUp(self):
2929
self.main_registry = BazelRegistry(
3030
os.path.join(self.registries_work_dir, 'main')
3131
)
32+
self.main_registry.start()
3233
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
3334
'aaa', '1.1'
3435
).createCcModule('bbb', '1.0', {'aaa': '1.0'}).createCcModule(
@@ -50,6 +51,10 @@ def setUp(self):
5051
)
5152
self.writeBazelrcFile()
5253

54+
def tearDown(self):
55+
self.main_registry.stop()
56+
test_base.TestBase.tearDown(self)
57+
5358
def writeBazelrcFile(self, allow_yanked_versions=True):
5459
self.ScratchFile(
5560
'.bazelrc',

src/test/py/bazel/bzlmod/bzlmod_query_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def setUp(self):
3030
self.registries_work_dir = tempfile.mkdtemp(dir=self._test_cwd)
3131
self.main_registry = BazelRegistry(
3232
os.path.join(self.registries_work_dir, 'main'))
33+
self.main_registry.start()
3334
self.main_registry.createCcModule('aaa', '1.0', {'ccc': '1.2'}) \
3435
.createCcModule('aaa', '1.1') \
3536
.createCcModule('bbb', '1.0', {'aaa': '1.0'}, {'aaa': 'com_foo_bar_aaa'}) \
@@ -50,6 +51,10 @@ def setUp(self):
5051
],
5152
)
5253

54+
def tearDown(self):
55+
self.main_registry.stop()
56+
test_base.TestBase.tearDown(self)
57+
5358
def testQueryModuleRepoTargetsBelow(self):
5459
self.ScratchFile('MODULE.bazel', [
5560
'bazel_dep(name = "aaa", version = "1.0", repo_name = "my_repo")',

src/test/py/bazel/bzlmod/external_repo_completion_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def setUp(self):
3737
self.main_registry = BazelRegistry(
3838
os.path.join(self.registries_work_dir, 'main')
3939
)
40+
self.main_registry.start()
4041
self.main_registry.setModuleBasePath('projects')
4142
self.projects_dir = self.main_registry.projects
4243
self.maxDiff = None # there are some long diffs in this test
@@ -157,6 +158,10 @@ def setUp(self):
157158
)
158159
scratchFile(self.projects_dir.joinpath('ext2', 'ext.bzl'), ext_src)
159160

161+
def tearDown(self):
162+
self.main_registry.stop()
163+
test_base.TestBase.tearDown(self)
164+
160165
def complete(self, bazel_args):
161166
"""Get the bash completions for the given "bazel" command line."""
162167

src/test/py/bazel/bzlmod/mod_command_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def setUp(self):
3232
self.main_registry = BazelRegistry(
3333
os.path.join(self.registries_work_dir, 'main')
3434
)
35+
self.main_registry.start()
3536
self.main_registry.setModuleBasePath('projects')
3637
self.projects_dir = self.main_registry.projects
3738
self.maxDiff = None # there are some long diffs in this test
@@ -133,6 +134,10 @@ def setUp(self):
133134
scratchFile(self.projects_dir.joinpath('ext2', 'BUILD'))
134135
scratchFile(self.projects_dir.joinpath('ext2', 'ext.bzl'), ext_src)
135136

137+
def tearDown(self):
138+
self.main_registry.stop()
139+
test_base.TestBase.tearDown(self)
140+
136141
def testFailWithoutBzlmod(self):
137142
_, _, stderr = self.RunBazel(
138143
['mod', 'graph', '--noenable_bzlmod'], allow_failure=True

0 commit comments

Comments
 (0)