Skip to content

Commit

Permalink
Disable WORKSPACE in python integration tests
Browse files Browse the repository at this point in the history
Working towards: #23023

Closes #23034.

PiperOrigin-RevId: 656036106
Change-Id: I2753b24dfc3f55e262176ca77d965eb1ad168ce9
  • Loading branch information
meteorcloudy authored and copybara-github committed Jul 25, 2024
1 parent 3f9fb7f commit d69c453
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 278 deletions.
189 changes: 88 additions & 101 deletions src/test/py/bazel/bazel_external_repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ def tearDown(self):
def testNewHttpArchive(self):
ip, port = self._http_server.server_address
rule_definition = [
'load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")',
(
'http_archive ='
' use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl",'
' "http_archive")'
),
'http_archive(',
' name = "six_archive",',
' urls = ["http://%s:%s/six-1.10.0.tar.gz"],' % (ip, port),
' sha256 = '
'"105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a",',
(
' sha256 = '
'"105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a",'
),
' strip_prefix = "six-1.10.0",',
' build_file = "@//third_party:six.BUILD",',
')',
Expand All @@ -82,8 +88,7 @@ def testNewHttpArchive(self):
' srcs = ["six.py"],',
')',
]
rule_definition.extend(self.GetDefaultRepoRules())
self.ScratchFile('WORKSPACE', rule_definition)
self.ScratchFile('MODULE.bazel', rule_definition)
self.ScratchFile('BUILD')
self.ScratchFile('third_party/BUILD')
self.ScratchFile('third_party/six.BUILD', build_file)
Expand Down Expand Up @@ -113,17 +118,22 @@ def testNewHttpArchive(self):
def testNewHttpZipArchiveWithSymlinks(self):
ip, port = self._http_server.server_address
rule_definition = [
'load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")',
(
'http_archive ='
' use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl",'
' "http_archive")'
),
'http_archive(',
' name = "archive_with_symlink",',
' urls = ["http://%s:%s/archive_with_symlink.zip"],' % (ip, port),
' build_file = "@//:archive_with_symlink.BUILD",',
' sha256 = ',
' "c9c32a48ff65f6319885246b1bfc704e60dd72fb0405dfafdffe403421a4c83a",'
')',
(
' "c9c32a48ff65f6319885246b1bfc704e60dd72fb0405dfafdffe403421a4c83a",'
')'
),
]
rule_definition.extend(self.GetDefaultRepoRules())
self.ScratchFile('WORKSPACE', rule_definition)
self.ScratchFile('MODULE.bazel', rule_definition)
# In the archive, A is a symlink pointing to B
self.ScratchFile('archive_with_symlink.BUILD', [
'filegroup(',
Expand All @@ -140,17 +150,22 @@ def testNewHttpZipArchiveWithSymlinks(self):
def testNewHttpTarArchiveWithSymlinks(self):
ip, port = self._http_server.server_address
rule_definition = [
'load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")',
(
'http_archive ='
' use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl",'
' "http_archive")'
),
'http_archive(',
' name = "archive_with_symlink",',
' urls = ["http://%s:%s/archive_with_symlink.tar.gz"],' % (ip, port),
' build_file = "@//:archive_with_symlink.BUILD",',
' sha256 = ',
' "5ea20285db1b18134e4efe608e41215687f03cd3e3fdd7529860b175fc12fe76",'
')',
(
' "5ea20285db1b18134e4efe608e41215687f03cd3e3fdd7529860b175fc12fe76",'
')'
),
]
rule_definition.extend(self.GetDefaultRepoRules())
self.ScratchFile('WORKSPACE', rule_definition)
self.ScratchFile('MODULE.bazel', rule_definition)
# In the archive, A is a symlink pointing to B
self.ScratchFile('archive_with_symlink.BUILD', [
'filegroup(',
Expand All @@ -172,7 +187,11 @@ def testNewHttpTarWithSparseFile(self):

ip, port = self._http_server.server_address
rule_definition = [
'load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")',
(
'http_archive ='
' use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl",'
' "http_archive")'
),
'http_archive(',
' name = "sparse_archive",',
' urls = ["http://%s:%s/sparse_archive.tar"],' % (ip, port),
Expand All @@ -183,8 +202,7 @@ def testNewHttpTarWithSparseFile(self):
')'
),
]
rule_definition.extend(self.GetDefaultRepoRules())
self.ScratchFile('WORKSPACE', rule_definition)
self.ScratchFile('MODULE.bazel', rule_definition)
self.ScratchFile(
'sparse_archive.BUILD',
[
Expand Down Expand Up @@ -240,9 +258,9 @@ def _CreatePyWritingStarlarkRule(self, print_string):
def testNewLocalRepositoryNoticesFileChangeInRepoRoot(self):
"""Regression test for https://github.com/bazelbuild/bazel/issues/7063."""
rule_definition = [
'load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")',
(
'load("@bazel_tools//tools/build_defs/repo:local.bzl",'
'new_local_repository ='
' use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl",'
' "new_local_repository")'
),
'new_local_repository(',
Expand All @@ -251,9 +269,7 @@ def testNewLocalRepositoryNoticesFileChangeInRepoRoot(self):
' build_file_content = "exports_files([\'foo.bzl\'])",',
')',
]
rule_definition.extend(self.GetDefaultRepoRules())
self.ScratchFile('WORKSPACE', rule_definition)
self.CreateWorkspaceWithDefaultRepos('repo/WORKSPACE')
self.ScratchFile('MODULE.bazel', rule_definition)
self._CreatePyWritingStarlarkRule('hello!')
self.ScratchFile('BUILD', [
'load("@r//:foo.bzl", "gen_py")',
Expand All @@ -271,8 +287,22 @@ def testNewLocalRepositoryNoticesFileChangeInRepoRoot(self):
self.assertNotIn('hello!', os.linesep.join(stdout))
self.assertIn('world', os.linesep.join(stdout))

def setUpOtherRepoMyRepo(self):
self.ScratchFile('other_repo/REPO.bazel')
self.ScratchFile(
'my_repo/MODULE.bazel',
[
(
'local_repository ='
' use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl",'
' "local_repository")'
),
'local_repository(name = "other_repo", path="../other_repo")',
],
)

def testDeletedPackagesOnExternalRepo(self):
self.ScratchFile('other_repo/WORKSPACE')
self.setUpOtherRepoMyRepo()
self.ScratchFile('other_repo/pkg/BUILD', [
'filegroup(',
' name = "file",',
Expand All @@ -284,36 +314,30 @@ def testDeletedPackagesOnExternalRepo(self):
])
self.ScratchFile('other_repo/pkg/ignore/file')
work_dir = self.ScratchDir('my_repo')
self.ScratchFile(
'my_repo/WORKSPACE',
[
(
'load("@bazel_tools//tools/build_defs/repo:local.bzl",'
' "local_repository")'
),
"local_repository(name = 'other_repo', path='../other_repo')",
],
)

exit_code, _, stderr = self.RunBazel(
args=['build', '@other_repo//pkg:file'],
cwd=work_dir,
allow_failure=True,
)
self.AssertExitCode(exit_code, 1, stderr)
self.assertIn("'@@other_repo//pkg/ignore' is a subpackage", ''.join(stderr))
self.assertIn(
"'@@_main~_repo_rules~other_repo//pkg/ignore' is a subpackage",
''.join(stderr),
)

self.RunBazel(
args=[
'build',
'@other_repo//pkg:file',
'--deleted_packages=@other_repo//pkg/ignore',
# TODO(bzlmod): support apparent repo name for --deleted_packages
'--deleted_packages=@@_main~_repo_rules~other_repo//pkg/ignore',
],
cwd=work_dir,
)

def testBazelignoreFileOnExternalRepo(self):
self.ScratchFile('other_repo/WORKSPACE')
self.setUpOtherRepoMyRepo()
self.ScratchFile('other_repo/pkg/BUILD', [
'filegroup(',
' name = "file",',
Expand All @@ -328,24 +352,17 @@ def testBazelignoreFileOnExternalRepo(self):
])
self.ScratchFile('other_repo/pkg/ignore/file.txt')
work_dir = self.ScratchDir('my_repo')
self.ScratchFile(
'my_repo/WORKSPACE',
[
(
'load("@bazel_tools//tools/build_defs/repo:local.bzl",'
' "local_repository")'
),
'local_repository(name = "other_repo", path="../other_repo")',
],
)

exit_code, _, stderr = self.RunBazel(
args=['build', '@other_repo//pkg:file'],
cwd=work_dir,
allow_failure=True,
)
self.AssertExitCode(exit_code, 1, stderr)
self.assertIn("'@@other_repo//pkg/ignore' is a subpackage", ''.join(stderr))
self.assertIn(
"'@@_main~_repo_rules~other_repo//pkg/ignore' is a subpackage",
''.join(stderr),
)

self.ScratchFile('other_repo/.bazelignore', [
'pkg/ignore',
Expand All @@ -363,10 +380,13 @@ def testBazelignoreFileOnExternalRepo(self):
args=['build', '//:all_files'], cwd=work_dir, allow_failure=True
)
self.AssertExitCode(exit_code, 1, stderr)
self.assertIn("no such package '@@other_repo//pkg/ignore'", ''.join(stderr))
self.assertIn(
"no such package '@@_main~_repo_rules~other_repo//pkg/ignore'",
''.join(stderr),
)

def testUniverseScopeWithBazelIgnoreInExternalRepo(self):
self.ScratchFile('other_repo/WORKSPACE')
self.setUpOtherRepoMyRepo()
self.ScratchFile('other_repo/pkg/BUILD', [
'filegroup(',
' name = "file",',
Expand All @@ -386,16 +406,6 @@ def testUniverseScopeWithBazelIgnoreInExternalRepo(self):
])

work_dir = self.ScratchDir('my_repo')
self.ScratchFile(
'my_repo/WORKSPACE',
[
(
'load("@bazel_tools//tools/build_defs/repo:local.bzl",'
' "local_repository")'
),
'local_repository(name = "other_repo", path="../other_repo")',
],
)

_, stdout, _ = self.RunBazel(
args=[
Expand All @@ -410,7 +420,7 @@ def testUniverseScopeWithBazelIgnoreInExternalRepo(self):

def testBazelignoreFileFromMainRepoDoesNotAffectExternalRepos(self):
# Regression test for https://github.com/bazelbuild/bazel/issues/10234
self.ScratchFile('other_repo/WORKSPACE')
self.setUpOtherRepoMyRepo()
self.ScratchFile('other_repo/foo/bar/BUILD', [
'filegroup(',
' name = "file",',
Expand All @@ -420,16 +430,6 @@ def testBazelignoreFileFromMainRepoDoesNotAffectExternalRepos(self):
self.ScratchFile('other_repo/foo/bar/file.txt')

work_dir = self.ScratchDir('my_repo')
self.ScratchFile(
'my_repo/WORKSPACE',
[
(
'load("@bazel_tools//tools/build_defs/repo:local.bzl",'
' "local_repository")'
),
'local_repository(name = "other_repo", path="../other_repo")',
],
)
# This should not exclude @other_repo//foo/bar
self.ScratchFile('my_repo/.bazelignore', ['foo/bar'])

Expand All @@ -439,7 +439,7 @@ def testBazelignoreFileFromMainRepoDoesNotAffectExternalRepos(self):
self.assertIn('@other_repo//foo/bar:file', ''.join(stdout))

def testBazelignoreFileFromExternalRepoDoesNotAffectMainRepo(self):
self.ScratchFile('other_repo/WORKSPACE')
self.setUpOtherRepoMyRepo()
# This should not exclude //foo/bar in main repo
self.ScratchFile('other_repo/.bazelignore', ['foo/bar'])
self.ScratchFile('other_repo/BUILD',)
Expand All @@ -452,22 +452,12 @@ def testBazelignoreFileFromExternalRepoDoesNotAffectMainRepo(self):
')',
])
self.ScratchFile('my_repo/foo/bar/file.txt')
self.ScratchFile(
'my_repo/WORKSPACE',
[
(
'load("@bazel_tools//tools/build_defs/repo:local.bzl",'
' "local_repository")'
),
'local_repository(name = "other_repo", path="../other_repo")',
],
)

_, stdout, _ = self.RunBazel(args=['query', '//foo/bar/...'], cwd=work_dir)
self.assertIn('//foo/bar:file', ''.join(stdout))

def testMainBazelignoreContainingRepoName(self):
self.ScratchFile('other_repo/WORKSPACE')
self.setUpOtherRepoMyRepo()
self.ScratchFile('other_repo/foo/bar/BUILD', [
'filegroup(',
' name = "file",',
Expand All @@ -477,24 +467,15 @@ def testMainBazelignoreContainingRepoName(self):
self.ScratchFile('other_repo/foo/bar/file.txt')

work_dir = self.ScratchDir('my_repo')
self.ScratchFile(
'my_repo/WORKSPACE',
[
(
'load("@bazel_tools//tools/build_defs/repo:local.bzl",'
' "local_repository")'
),
'local_repository(name = "other_repo", path="../other_repo")',
],
)

# This should not exclude @other_repo//foo/bar, because .bazelignore doesn't
# support having repository name in the path fragment.
self.ScratchFile('my_repo/.bazelignore', ['@other_repo//foo/bar'])

self.RunBazel(args=['build', '@other_repo//foo/bar:file'], cwd=work_dir)

def testExternalBazelignoreContainingRepoName(self):
self.ScratchFile('other_repo/WORKSPACE')
self.ScratchFile('other_repo/REPO.bazel')
# This should not exclude @third_repo//foo/bar, because .bazelignore doesn't
# support having repository name in the path fragment.
self.ScratchFile('other_repo/.bazelignore', ['@third_repo//foo/bar'])
Expand All @@ -505,7 +486,7 @@ def testExternalBazelignoreContainingRepoName(self):
')',
])

self.ScratchFile('third_repo/WORKSPACE')
self.ScratchFile('third_repo/REPO.bazel')
self.ScratchFile('third_repo/foo/bar/BUILD', [
'filegroup(',
' name = "file",',
Expand All @@ -517,10 +498,11 @@ def testExternalBazelignoreContainingRepoName(self):

work_dir = self.ScratchDir('my_repo')
self.ScratchFile(
'my_repo/WORKSPACE',
'my_repo/MODULE.bazel',
[
(
'load("@bazel_tools//tools/build_defs/repo:local.bzl",'
'local_repository ='
' use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl",'
' "local_repository")'
),
'local_repository(name = "other_repo", path="../other_repo")',
Expand All @@ -533,10 +515,15 @@ def testExternalBazelignoreContainingRepoName(self):
def testRepoEnv(self):
# Testing fix for issue: https://github.com/bazelbuild/bazel/issues/15430

self.ScratchFile('WORKSPACE', [
'load("//:main.bzl", "dump_env")', 'dump_env(', ' name = "debug"',
')'
])
self.ScratchFile(
'MODULE.bazel',
[
'dump_env = use_repo_rule("//:main.bzl", "dump_env")',
'dump_env(',
' name = "debug"',
')',
],
)
self.ScratchFile('BUILD')
self.ScratchFile('main.bzl', [
'def _dump_env(ctx):', ' val = ctx.os.environ.get("FOO", "nothing")',
Expand Down
Loading

0 comments on commit d69c453

Please sign in to comment.