Skip to content

Commit 0aedacb

Browse files
kgodara912Kshitiz Godara
andauthored
versionsprocessor: sanitize dots and plus signs in RPM macro names (#16914)
Co-authored-by: Kshitiz Godara <kgodara@microsoft.com>
1 parent 64adae0 commit 0aedacb

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

toolkit/tools/versionsprocessor/versionsprocessor.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
)
3030

3131
var packageVersionRegexp = regexp.MustCompile(`^[^:]+: (?:(.+):)?(.+)-(.+)$`)
32+
var invalidMacroCharRegexp = regexp.MustCompile(`[^a-zA-Z0-9_]`)
3233

3334
var (
3435
app = kingpin.New("versionsprocessor", "A tool to generate a macro file of all specs version and release")
@@ -189,9 +190,10 @@ func processPackageVersionString(packageVersionString string, specFileName strin
189190
release := releaseVerSplit[2]
190191
releaseClean := strings.Replace(release, distTag, "", 1)
191192

192-
// strip out the .spec suffix and replace '-' with '_' as RPM macros cannot have '-'
193+
// strip out the .spec suffix and replace characters invalid in RPM macro names with '_'
194+
// RPM macro names may only contain alphanumeric characters and underscores.
193195
packageFileNameMacroFormat := strings.Replace(specFileName, ".spec", "", 1)
194-
packageFileNameMacroFormat = strings.ReplaceAll(packageFileNameMacroFormat, "-", "_")
196+
packageFileNameMacroFormat = invalidMacroCharRegexp.ReplaceAllString(packageFileNameMacroFormat, "_")
195197

196198
epochReleaseString := prefix + "_" + packageFileNameMacroFormat + "_epoch"
197199
versionMacroString := prefix + "_" + packageFileNameMacroFormat + "_version"

toolkit/tools/versionsprocessor/versionsprocessor_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,38 @@ func TestProcessPackageVersionString_TableDriven(t *testing.T) {
127127
expectedVersion: "%azl_openssl_version 3.3.0",
128128
expectedRelease: "%azl_openssl_release 1.1",
129129
},
130+
{
131+
name: "dot in spec name replaced with underscore",
132+
input: "rubygem-cool.io: 1.8.0-1.azl3",
133+
specFile: "rubygem-cool.io.spec",
134+
prefix: "azl",
135+
expectedVersion: "%azl_rubygem_cool_io_version 1.8.0",
136+
expectedRelease: "%azl_rubygem_cool_io_release 1",
137+
},
138+
{
139+
name: "plus in spec name replaced with underscore",
140+
input: "libxml++: 5.0.3-1.azl3",
141+
specFile: "libxml++.spec",
142+
prefix: "azl",
143+
expectedVersion: "%azl_libxml___version 5.0.3",
144+
expectedRelease: "%azl_libxml___release 1",
145+
},
146+
{
147+
name: "versioned spec name with dot",
148+
input: "rust-1.75: 1.75.0-27.azl3",
149+
specFile: "rust-1.75.spec",
150+
prefix: "azl",
151+
expectedVersion: "%azl_rust_1_75_version 1.75.0",
152+
expectedRelease: "%azl_rust_1_75_release 27",
153+
},
154+
{
155+
name: "golang versioned spec with dot",
156+
input: "golang-1.25: 1.25.9-1.azl3",
157+
specFile: "golang-1.25.spec",
158+
prefix: "azl",
159+
expectedVersion: "%azl_golang_1_25_version 1.25.9",
160+
expectedRelease: "%azl_golang_1_25_release 1",
161+
},
130162
}
131163

132164
for _, tt := range tests {

0 commit comments

Comments
 (0)