Skip to content

Commit

Permalink
Fix RPM package release and version files expansion (bazelbuild#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
k0walik authored Feb 8, 2024
1 parent 3a2508f commit 37cccd4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
8 changes: 4 additions & 4 deletions pkg/rpm_pfg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _pkg_rpm_impl(ctx):
if ctx.attr.version:
fail("Both version and version_file attributes were specified")

preamble_pieces.append("Version: ${VERSION_FROM_FILE}")
preamble_pieces.append("Version: ${{VERSION_FROM_FILE}}")
args.append("--version=@" + ctx.file.version_file.path)
files.append(ctx.file.version_file)
elif ctx.attr.version:
Expand All @@ -287,7 +287,7 @@ def _pkg_rpm_impl(ctx):
if ctx.attr.release:
fail("Both release and release_file attributes were specified")

preamble_pieces.append("Release: ${RELEASE_FROM_FILE}")
preamble_pieces.append("Release: ${{RELEASE_FROM_FILE}}")
args.append("--release=@" + ctx.file.release_file.path)
files.append(ctx.file.release_file)
elif ctx.attr.release:
Expand Down Expand Up @@ -684,8 +684,8 @@ def _pkg_rpm_impl(ctx):
)

changes = []
if ctx.attr.changelog:
changes = [ctx.attr.changelog]
if ctx.file.changelog:
changes = [ctx.file.changelog]

output_groups = {
"out": [default_file],
Expand Down
58 changes: 51 additions & 7 deletions tests/rpm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,25 @@ _POSTTRANS_SCRIPTLET = "echo posttrans"
]
]

############################################################################
# versionfile for testing
############################################################################

_VERSION = "1.1.1"
_RELEASE = "2222"

genrule(
name = "version_file",
outs = ["version"],
cmd = "echo '{}' > $@".format(_VERSION),
)

genrule(
name = "release_file",
outs = ["release"],
cmd = "echo '{}' > $@".format(_RELEASE),
)

############################################################################
# Test RPMs
############################################################################
Expand All @@ -167,12 +186,12 @@ pkg_rpm(
preun_scriptlet = _PREUN_SCRIPTLET,
posttrans_scriptlet = _POSTTRANS_SCRIPTLET,
provides = ["test"],
release = "2222",
release = _RELEASE,
requires = ["test-lib > 1.0"],
requires_contextual = {"preun": ["bash"]},
spec_template = "template-test.spec.tpl",
summary = "pkg_rpm test rpm summary",
version = "1.1.1",
version = _VERSION,
)

# Just like the above one, except the compression is changed.
Expand All @@ -192,12 +211,12 @@ pkg_rpm(
preun_scriptlet = _PREUN_SCRIPTLET,
posttrans_scriptlet = _POSTTRANS_SCRIPTLET,
provides = ["test"],
release = "2222",
release = _RELEASE,
requires = ["test-lib > 1.0"],
requires_contextual = {"preun": ["bash"]},
spec_template = "template-test.spec.tpl",
summary = "pkg_rpm test rpm summary",
version = "1.1.1",
version = _VERSION,
)

# Like the first one, except `srcs` is now passed in without using a
Expand All @@ -220,12 +239,12 @@ pkg_rpm(
preun_scriptlet = _PREUN_SCRIPTLET,
posttrans_scriptlet = _POSTTRANS_SCRIPTLET,
provides = ["test"],
release = "2222",
release = _RELEASE,
requires = ["test-lib > 1.0"],
requires_contextual = {"preun": ["bash"]},
spec_template = "template-test.spec.tpl",
summary = "pkg_rpm test rpm summary",
version = "1.1.1",
version = _VERSION,
)

# Like the first one, except we use files for scriptlets
Expand All @@ -249,7 +268,31 @@ pkg_rpm(
requires_contextual = {"preun": ["bash"]},
spec_template = "template-test.spec.tpl",
summary = "pkg_rpm test rpm summary",
version = "1.1.1",
version = _VERSION,
)

# Like the first one, except we use release and version files
pkg_rpm(
name = "test_rpm_release_version_files",
srcs = [
":test_pfg",
],
architecture = "noarch",
conflicts = ["not-a-test"],
description = """pkg_rpm test rpm description""",
license = "Apache 2.0",
post_scriptlet_file = ":post",
postun_scriptlet_file = ":postun",
pre_scriptlet_file = ":pre",
preun_scriptlet_file = ":preun",
posttrans_scriptlet_file = ":posttrans",
provides = ["test"],
release_file = ":release_file",
requires = ["test-lib > 1.0"],
requires_contextual = {"preun": ["bash"]},
spec_template = "template-test.spec.tpl",
summary = "pkg_rpm test rpm summary",
version_file = ":version_file",
)

############################################################################
Expand Down Expand Up @@ -363,6 +406,7 @@ sh_library(
":test_rpm_manifest",
":test_rpm_metadata",
":test_rpm_scriptlets_files",
":test_rpm_release_version_files",
],
)

Expand Down
28 changes: 17 additions & 11 deletions tests/rpm/pkg_rpm_basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def setUp(self):
"rules_pkg/tests/rpm/test_rpm_bzip2-1.1.1-2222.noarch.rpm")
self.test_rpm_scriptlets_files_path = self.runfiles.Rlocation(
"rules_pkg/tests/rpm/test_rpm_scriptlets_files-1.1.1-2222.noarch.rpm")
self.test_rpm_release_version_files = self.runfiles.Rlocation(
"rules_pkg/tests/rpm/test_rpm_release_version_files--.noarch.rpm")
self.maxDiff = None

def test_scriptlet_content(self):
Expand All @@ -71,23 +73,27 @@ def test_scriptlet_content(self):
self.assertEqual(output, expected)

def test_basic_headers(self):
fields = {
"NAME": b"test_rpm",
common_fields = {
"VERSION": b"1.1.1",
"RELEASE": b"2222",
"ARCH": b"noarch",
"GROUP": b"Unspecified",
"SUMMARY": b"pkg_rpm test rpm summary",
}
for fieldname, expected in fields.items():
output = subprocess.check_output([
"rpm", "-qp", "--queryformat", "%{" + fieldname + "}",
self.test_rpm_path
])

self.assertEqual(
output, expected,
"RPM Tag {} does not match expected value".format(fieldname))
for rpm, fields in [
(self.test_rpm_path, {"NAME": b"test_rpm"}),
(self.test_rpm_release_version_files, {"NAME": b"test_rpm_release_version_files"}),
]:
fields.update(common_fields)
for fieldname, expected in fields.items():
output = subprocess.check_output([
"rpm", "-qp", "--queryformat", "%{" + fieldname + "}",
rpm,
])

self.assertEqual(
output, expected,
"RPM Tag {} does not match expected value".format(fieldname))

def test_contents(self):
manifest_file = self.runfiles.Rlocation(
Expand Down

0 comments on commit 37cccd4

Please sign in to comment.