Skip to content

Commit

Permalink
Fix Python tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Feb 6, 2024
1 parent d854b59 commit f7379b1
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 132 deletions.
14 changes: 7 additions & 7 deletions src/test/py/bazel/bzlmod/bazel_fetch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def testFetchAll(self):
self.RunBazel(['fetch', '--all'])
_, stdout, _ = self.RunBazel(['info', 'output_base'])
repos_fetched = os.listdir(stdout[0] + '/external')
self.assertIn('aaa~1.0', repos_fetched)
self.assertIn('bbb~1.0', repos_fetched)
self.assertIn('aaa~', repos_fetched)
self.assertIn('bbb~', repos_fetched)
self.assertIn('_main~ext~hello', repos_fetched)

def testFetchConfig(self):
Expand Down Expand Up @@ -141,7 +141,7 @@ def testFetchConfig(self):
self.RunBazel(['fetch', '--configure'])
_, stdout, _ = self.RunBazel(['info', 'output_base'])
repos_fetched = os.listdir(stdout[0] + '/external')
self.assertNotIn('aaa~1.0', repos_fetched)
self.assertNotIn('aaa~', repos_fetched)
self.assertNotIn('_main~ext~notConfig', repos_fetched)
self.assertIn('_main~ext~IamConfig', repos_fetched)

Expand Down Expand Up @@ -179,12 +179,12 @@ def testFetchRepo(self):
)
self.ScratchFile('BUILD')
# Test canonical/apparent repo names & multiple repos
self.RunBazel(['fetch', '--repo=@@bbb~1.0', '--repo=@my_repo'])
self.RunBazel(['fetch', '--repo=@@bbb~', '--repo=@my_repo'])
_, stdout, _ = self.RunBazel(['info', 'output_base'])
repos_fetched = os.listdir(stdout[0] + '/external')
self.assertIn('bbb~1.0', repos_fetched)
self.assertIn('ccc~1.0', repos_fetched)
self.assertNotIn('aaa~1.0', repos_fetched)
self.assertIn('bbb~', repos_fetched)
self.assertIn('ccc~', repos_fetched)
self.assertNotIn('aaa~', repos_fetched)

def testFetchInvalidRepo(self):
# Invalid repo name (not canonical or apparent)
Expand Down
97 changes: 49 additions & 48 deletions src/test/py/bazel/bzlmod/bazel_lockfile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,13 +1519,11 @@ def testInnateModuleExtension(self):
def testExtensionRepoMappingChange(self):
# Regression test for #20721
self.main_registry.createCcModule('foo', '1.0')
self.main_registry.createCcModule('foo', '2.0')
self.main_registry.createCcModule('bar', '1.0')
self.main_registry.createCcModule('bar', '2.0')
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name="foo",version="1.0")',
'bazel_dep(name="foo",version="1.0",repo_name="repo_name")',
'bazel_dep(name="bar",version="1.0")',
'ext = use_extension(":ext.bzl", "ext")',
'use_repo(ext, "repo")',
Expand All @@ -1548,13 +1546,13 @@ def testExtensionRepoMappingChange(self):
'repo = repository_rule(_repo_impl,attrs={"value":attr.label()})',
'def _ext_impl(mctx):',
' print("ran the extension!")',
' repo(name = "repo", value = Label("@foo//:lib_foo"))',
' repo(name = "repo", value = Label("@repo_name//:lib_foo"))',
'ext = module_extension(_ext_impl)',
],
)

_, _, stderr = self.RunBazel(['build', ':lol'])
self.assertIn('STR=@@foo~1.0//:lib_foo', '\n'.join(stderr))
self.assertIn('STR=@@foo~//:lib_foo', '\n'.join(stderr))

# Shutdown bazel to make sure we rely on the lockfile and not skyframe
self.RunBazel(['shutdown'])
Expand All @@ -1563,50 +1561,51 @@ def testExtensionRepoMappingChange(self):

# Shutdown bazel to make sure we rely on the lockfile and not skyframe
self.RunBazel(['shutdown'])
# Now, for something spicy: upgrade foo to 2.0 and change nothing else.
# The extension should rerun despite the lockfile being present, and no
# usages or .bzl files having changed.
# Now, for something spicy: let repo_name point to bar and change nothing
# else. The extension should rerun despite the lockfile being present, and
# no usages or .bzl files having changed.
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name="foo",version="2.0")',
'bazel_dep(name="bar",version="1.0")',
'bazel_dep(name="foo",version="1.0")',
'bazel_dep(name="bar",version="1.0",repo_name="repo_name")',
'ext = use_extension(":ext.bzl", "ext")',
'use_repo(ext, "repo")',
],
)
_, _, stderr = self.RunBazel(['build', ':lol'])
self.assertIn('STR=@@foo~2.0//:lib_foo', '\n'.join(stderr))
stderr = '\n'.join(stderr)
self.assertIn('ran the extension!', stderr)
self.assertIn('STR=@@bar~//:lib_foo', stderr)

# Shutdown bazel to make sure we rely on the lockfile and not skyframe
self.RunBazel(['shutdown'])
# More spicy! upgrade bar to 2.0 and change nothing else.
# The extension should NOT rerun, since it never used the @bar repo mapping.
# More spicy! change the repo_name of foo, but nothing else.
# The extension should NOT rerun, since it never used the @other_name repo
# mapping.
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name="foo",version="2.0")',
'bazel_dep(name="bar",version="2.0")',
'bazel_dep(name="foo",version="1.0",repo_name="other_name")',
'bazel_dep(name="bar",version="1.0",repo_name="repo_name")',
'ext = use_extension(":ext.bzl", "ext")',
'use_repo(ext, "repo")',
],
)
_, _, stderr = self.RunBazel(['build', ':lol'])
stderr = '\n'.join(stderr)
self.assertNotIn('ran the extension!', stderr)
self.assertIn('STR=@@foo~2.0//:lib_foo', stderr)
self.assertIn('STR=@@bar~//:lib_foo', stderr)

def testExtensionRepoMappingChange_BzlInit(self):
# Regression test for #20721; same test as above, except that the call to
# Label() in ext.bzl is now done at bzl load time.
self.main_registry.createCcModule('foo', '1.0')
self.main_registry.createCcModule('foo', '2.0')
self.main_registry.createCcModule('bar', '1.0')
self.main_registry.createCcModule('bar', '2.0')
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name="foo",version="1.0")',
'bazel_dep(name="foo",version="1.0",repo_name="repo_name")',
'bazel_dep(name="bar",version="1.0")',
'ext = use_extension(":ext.bzl", "ext")',
'use_repo(ext, "repo")',
Expand All @@ -1623,7 +1622,7 @@ def testExtensionRepoMappingChange_BzlInit(self):
self.ScratchFile(
'ext.bzl',
[
'constant = Label("@foo//:lib_foo")',
'constant = Label("@repo_name//:lib_foo")',
'def _repo_impl(rctx):',
' rctx.file("BUILD")',
' rctx.file("defs.bzl", "STR = " + repr(str(rctx.attr.value)))',
Expand All @@ -1645,65 +1644,67 @@ def testExtensionRepoMappingChange_BzlInit(self):

# Shutdown bazel to make sure we rely on the lockfile and not skyframe
self.RunBazel(['shutdown'])
# Now, for something spicy: upgrade foo to 2.0 and change nothing else.
# The extension should rerun despite the lockfile being present, and no
# usages or .bzl files having changed.
# Now, for something spicy: let repo_name point to bar and change nothing
# else. The extension should rerun despite the lockfile being present, and
# no usages or .bzl files having changed.
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name="foo",version="2.0")',
'bazel_dep(name="bar",version="1.0")',
'bazel_dep(name="foo",version="1.0")',
'bazel_dep(name="bar",version="1.0",repo_name="repo_name")',
'ext = use_extension(":ext.bzl", "ext")',
'use_repo(ext, "repo")',
],
)
_, _, stderr = self.RunBazel(['build', ':lol'])
self.assertIn('STR=@@foo~2.0//:lib_foo', '\n'.join(stderr))
stderr = '\n'.join(stderr)
self.assertIn('ran the extension!', stderr)
self.assertIn('STR=@@bar~//:lib_foo', stderr)

# Shutdown bazel to make sure we rely on the lockfile and not skyframe
self.RunBazel(['shutdown'])
# More spicy! upgrade bar to 2.0 and change nothing else.
# The extension should NOT rerun, since it never used the @bar repo mapping.
# More spicy! change the repo_name of foo, but nothing else.
# The extension should NOT rerun, since it never used the @other_name repo
# mapping.
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name="foo",version="2.0")',
'bazel_dep(name="bar",version="2.0")',
'bazel_dep(name="foo",version="1.0",repo_name="other_name")',
'bazel_dep(name="bar",version="1.0",repo_name="repo_name")',
'ext = use_extension(":ext.bzl", "ext")',
'use_repo(ext, "repo")',
],
)
_, _, stderr = self.RunBazel(['build', ':lol'])
stderr = '\n'.join(stderr)
self.assertNotIn('ran the extension!', stderr)
self.assertIn('STR=@@foo~2.0//:lib_foo', stderr)
self.assertIn('STR=@@bar~//:lib_foo', stderr)

def testExtensionRepoMappingChange_loadsAndRepoRelativeLabels(self):
# Regression test for #20721; same test as above, except that the call to
# Label() in ext.bzl is now moved to @foo//:defs.bzl and doesn't itself
# use repo mapping (ie. is a repo-relative label). bar is removed as it's
# just a distraction.
# Label() in ext.bzl is now moved to @{foo,bar}//:defs.bzl and doesn't
# itself use repo mapping (ie. is a repo-relative label).
self.main_registry.setModuleBasePath('projects')
projects_dir = self.main_registry.projects

self.main_registry.createLocalPathModule('foo', '1.0', 'foo1')
scratchFile(projects_dir.joinpath('foo1', 'BUILD'))
self.main_registry.createLocalPathModule('foo', '1.0', 'foo')
scratchFile(projects_dir.joinpath('foo', 'BUILD'))
scratchFile(
projects_dir.joinpath('foo1', 'defs.bzl'),
projects_dir.joinpath('foo', 'defs.bzl'),
['constant=Label("//:BUILD")'],
)
self.main_registry.createLocalPathModule('foo', '2.0', 'foo2')
scratchFile(projects_dir.joinpath('foo2', 'BUILD'))
# Exactly the same as foo1!
self.main_registry.createLocalPathModule('bar', '1.0', 'bar')
scratchFile(projects_dir.joinpath('bar', 'BUILD'))
# Exactly the same as foo!
scratchFile(
projects_dir.joinpath('foo2', 'defs.bzl'),
projects_dir.joinpath('bar', 'defs.bzl'),
['constant=Label("//:BUILD")'],
)

self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name="foo",version="1.0")',
'bazel_dep(name="foo",version="1.0",repo_name="repo_name")',
'ext = use_extension(":ext.bzl", "ext")',
'use_repo(ext, "repo")',
],
Expand All @@ -1719,7 +1720,7 @@ def testExtensionRepoMappingChange_loadsAndRepoRelativeLabels(self):
self.ScratchFile(
'ext.bzl',
[
'load("@foo//:defs.bzl", "constant")',
'load("@repo_name//:defs.bzl", "constant")',
'def _repo_impl(rctx):',
' rctx.file("BUILD")',
' rctx.file("defs.bzl", "STR = " + repr(str(rctx.attr.value)))',
Expand All @@ -1732,7 +1733,7 @@ def testExtensionRepoMappingChange_loadsAndRepoRelativeLabels(self):
)

_, _, stderr = self.RunBazel(['build', ':lol'])
self.assertIn('STR=@@foo~1.0//:BUILD', '\n'.join(stderr))
self.assertIn('STR=@@foo~//:BUILD', '\n'.join(stderr))

# Shutdown bazel to make sure we rely on the lockfile and not skyframe
self.RunBazel(['shutdown'])
Expand All @@ -1741,19 +1742,19 @@ def testExtensionRepoMappingChange_loadsAndRepoRelativeLabels(self):

# Shutdown bazel to make sure we rely on the lockfile and not skyframe
self.RunBazel(['shutdown'])
# Now, for something spicy: upgrade foo to 2.0 and change nothing else.
# Now, for something spicy: change repo_name to point to bar.
# The extension should rerun despite the lockfile being present, and no
# usages or .bzl files having changed.
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name="foo",version="2.0")',
'bazel_dep(name="bar",version="1.0",repo_name="repo_name")',
'ext = use_extension(":ext.bzl", "ext")',
'use_repo(ext, "repo")',
],
)
_, _, stderr = self.RunBazel(['build', ':lol'])
self.assertIn('STR=@@foo~2.0//:BUILD', '\n'.join(stderr))
self.assertIn('STR=@@bar~//:BUILD', '\n'.join(stderr))

def testExtensionRepoMappingChange_sourceRepoNoLongerExistent(self):
# Regression test for #20721; a very convoluted setup to make sure that
Expand Down Expand Up @@ -1892,7 +1893,7 @@ def testExtensionRepoMappingChange_mainRepoEvalCycleWithWorkspace(self):
self.ScratchFile('WORKSPACE.bzlmod', ['load("@repo//:defs.bzl","STR")'])

_, _, stderr = self.RunBazel(['build', '--enable_workspace', ':lol'])
self.assertIn('STR=@@foo~1.0//:lib_foo', '\n'.join(stderr))
self.assertIn('STR=@@foo~//:lib_foo', '\n'.join(stderr))

# Shutdown bazel to make sure we rely on the lockfile and not skyframe
self.RunBazel(['shutdown'])
Expand Down
22 changes: 11 additions & 11 deletions src/test/py/bazel/bzlmod/bazel_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ def testNativePackageRelativeLabel(self):

_, _, stderr = self.RunBazel(['build', '@bar//quux:book'])
stderr = '\n'.join(stderr)
self.assertIn('1st: @@bar~override//quux:bleb', stderr)
self.assertIn('2nd: @@bar~override//bleb:bleb', stderr)
self.assertIn('1st: @@bar~//quux:bleb', stderr)
self.assertIn('2nd: @@bar~//bleb:bleb', stderr)
self.assertIn('3rd: @@//bleb:bleb', stderr)
self.assertIn('4th: @@bar~override//bleb:bleb', stderr)
self.assertIn('4th: @@bar~//bleb:bleb', stderr)
self.assertIn('5th: @@bleb//bleb:bleb', stderr)
self.assertIn('6th: @@//bleb:bleb', stderr)

Expand Down Expand Up @@ -459,10 +459,10 @@ def testWorkspaceEvaluatedBzlCanSeeRootModuleMappings(self):
stderr = '\n'.join(stderr)
# @bar is mapped to @@baz, which Bzlmod doesn't recognize, so we leave it be
self.assertIn('1st: @@baz//:z', stderr)
# @my_aaa is mapped to @@aaa, which Bzlmod remaps to @@aaa~1.0
self.assertIn('2nd: @@aaa~1.0//:z', stderr)
# @bbb isn't mapped in WORKSPACE, but Bzlmod maps it to @@bbb~1.0
self.assertIn('3rd: @@bbb~1.0//:z', stderr)
# @my_aaa is mapped to @@aaa, which Bzlmod remaps to @@aaa~
self.assertIn('2nd: @@aaa~//:z', stderr)
# @bbb isn't mapped in WORKSPACE, but Bzlmod maps it to @@bbb~
self.assertIn('3rd: @@bbb~//:z', stderr)
# @blarg isn't mapped by WORKSPACE or Bzlmod
self.assertIn('4th: @@blarg//:z', stderr)

Expand Down Expand Up @@ -641,11 +641,11 @@ def testNativeModuleNameAndVersion(self):
)
stderr = '\n'.join(stderr)
self.assertIn('@@ reporting in: [email protected]', stderr)
self.assertIn('@@foo~1.0 reporting in: [email protected]', stderr)
self.assertIn('@@foo~ reporting in: [email protected]', stderr)
self.assertIn(
'@@foo~1.0~report_ext~report_repo reporting in: [email protected]', stderr
'@@foo~~report_ext~report_repo reporting in: [email protected]', stderr
)
self.assertIn('@@bar~override reporting in: [email protected]', stderr)
self.assertIn('@@bar~ reporting in: [email protected]', stderr)
self.assertIn('@@quux reporting in: None@None', stderr)

def testWorkspaceToolchainRegistrationWithPlatformsConstraint(self):
Expand Down Expand Up @@ -791,7 +791,7 @@ def testLocationNonRegistry(self):
)
self.ScratchFile('hello/MODULE.bazel', ['wat'])
_, _, stderr = self.RunBazel(['build', '@what'], allow_failure=True)
self.assertIn('ERROR: @@hello~override//:MODULE.bazel', '\n'.join(stderr))
self.assertIn('ERROR: @@hello~//:MODULE.bazel', '\n'.join(stderr))

def testLoadRulesJavaSymbolThroughBazelTools(self):
"""Tests that loads from @bazel_tools that delegate to other modules resolve."""
Expand Down
6 changes: 3 additions & 3 deletions src/test/py/bazel/bzlmod/bazel_overrides_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def testCmdAbsoluteModuleOverride(self):
)
# module file override should be ignored, and bb directory should be used
self.assertIn(
'Target @@ss~override//:choose_me up-to-date (nothing to build)', stderr
'Target @@ss~//:choose_me up-to-date (nothing to build)', stderr
)

def testCmdRelativeModuleOverride(self):
Expand Down Expand Up @@ -303,7 +303,7 @@ def testCmdRelativeModuleOverride(self):
cwd=self.Path('aa/cc'),
)
self.assertIn(
'Target @@ss~override//:choose_me up-to-date (nothing to build)', stderr
'Target @@ss~//:choose_me up-to-date (nothing to build)', stderr
)

def testCmdWorkspaceRelativeModuleOverride(self):
Expand Down Expand Up @@ -338,7 +338,7 @@ def testCmdWorkspaceRelativeModuleOverride(self):
cwd=self.Path('aa'),
)
self.assertIn(
'Target @@ss~override//:choose_me up-to-date (nothing to build)', stderr
'Target @@ss~//:choose_me up-to-date (nothing to build)', stderr
)


Expand Down
Loading

0 comments on commit f7379b1

Please sign in to comment.