Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions distro/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ py_test(
":small_module",
"//:standard_package",
],
env_inherit = ["PATH"],
imports = [".."],
local = True,
python_version = "PY3",
Expand Down
53 changes: 36 additions & 17 deletions distro/packaging_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import re
import subprocess
import tarfile
import unittest

from python.runfiles import runfiles
Expand Down Expand Up @@ -57,26 +58,20 @@ def testBuild(self):
tempdir = os.path.join(os.environ['TEST_TMPDIR'], 'build')
if not os.path.exists(tempdir):
os.makedirs(tempdir)
with open(os.path.join(tempdir, 'WORKSPACE'), 'w') as workspace:
filename, setup_lines, bazel_flags = self._select_bazel_supported_setup()
with open(os.path.join(tempdir, filename), 'w') as setup:
file_name = release_tools.package_basename(self.source_repo, self.version)
# The code looks wrong, but here is why it is correct.
# - Rlocation requires '/' as path separators, not os.path.sep.
# - When we read the file, the path must use os.path.sep
local_path = self.data_files.Rlocation(
'rules_pkg/distro/' + file_name).replace('/', os.path.sep)
sha256 = release_tools.get_package_sha256(local_path)
workspace_content = '\n'.join((
'workspace(name = "test_rules_pkg_packaging")',
release_tools.workspace_content(
'file://%s' % local_path, self.source_repo, sha256,
rename_repo=self.dest_repo,
deps_method='rules_pkg_dependencies'
)
))
workspace.write(workspace_content)
setup_content = '\n'.join(setup_lines(local_path, sha256))
setup.write(setup_content)
if _VERBOSE:
print('=== WORKSPACE ===')
print(workspace_content)
print(f'=== {filename} ===')
print(setup_content)

# We do a little dance of renaming *.tpl to *, mostly so that we do not
# have a BUILD file in testdata, which would create a package boundary.
Expand All @@ -91,15 +86,39 @@ def CopyTestFile(source_name, dest_name):
CopyTestFile('BUILD.tpl', 'BUILD')

os.chdir(tempdir)
build_result = subprocess.check_output(['bazel', 'build', '--enable_workspace', ':dummy_tar'])
build_result = subprocess.check_output(['bazel', 'build'] + bazel_flags + [':dummy_tar'])
if _VERBOSE:
print('=== Build Result ===')
print(build_result)

# TODO(aiuto): Find tar in a disciplined way
content = subprocess.check_output(
['tar', 'tzf', 'bazel-bin/dummy_tar.tar.gz'])
self.assertEqual(b'etc/\netc/BUILD\n', content)
with tarfile.open('bazel-bin/dummy_tar.tar.gz', 'r') as tar:
self.assertEqual(['etc', 'etc/BUILD'], tar.getnames())

def _select_bazel_supported_setup(self):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only have to support building the distro from bazel 8+,
so a module is always available. The workspace test is essentaally dead code to maintain.

output = subprocess.check_output(['bazel', 'version'], text=True)
major_version = re.search(r'Build label:\s+(\d+)', output)
if major_version and int(major_version.group(1)) >= 9:
return 'MODULE.bazel', self._module_bazel_lines, []
return 'WORKSPACE', self._workspace_lines, ['--enable_workspace']

def _module_bazel_lines(self, local_path, sha256):
return (
'module(name = "test_rules_pkg_packaging")',
f'bazel_dep(name = "{self.source_repo}", version = "{self.version}", repo_name = "{self.dest_repo}")',
f'archive_override(module_name = "{self.source_repo}", sha256 = "{sha256}", url = "file://{local_path}")',
)

def _workspace_lines(self, local_path, sha256):
return (
'workspace(name = "test_rules_pkg_packaging")',
release_tools.workspace_content(
url=f'file://{local_path}',
repo=self.source_repo,
sha256=sha256,
rename_repo=self.dest_repo,
deps_method='rules_pkg_dependencies',
),
)


if __name__ == '__main__':
Expand Down