Skip to content

Commit b400b97

Browse files
author
Antonio Salinas
authored
fix: Add Undefines to Build Config (#443)
* Add undefines build config * Added comment undefines useage
1 parent d9a0b5c commit b400b97

6 files changed

Lines changed: 104 additions & 1 deletion

File tree

internal/app/azldev/core/sources/sourceprep.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ func (p *sourcePreparerImpl) writeMacrosFile(component components.Component, out
139139
// All macros are collected into a single map and sorted alphabetically by name for
140140
// deterministic output. If the same macro is defined multiple times (e.g., via both
141141
// a with flag and an explicit define), the later definition wins. The order of processing
142-
// is: with flags, then without flags, then explicit defines.
142+
// is: with flags, then without flags, then explicit defines, then undefines.
143+
//
144+
// Undefines remove entries from the generated macros map. This only affects macros that
145+
// originate from the merged TOML configuration (with, without, and defines fields); it
146+
// does not undefine arbitrary system-level RPM macros. The primary use case is allowing
147+
// a component-level TOML file to override a distro-level or project-level default by
148+
// removing a macro that would otherwise be emitted into the macros file.
143149
//
144150
// Note: RPM macro values can contain spaces without special escaping; everything
145151
// after the macro name (and separating whitespace) is treated as the macro body.
@@ -167,6 +173,12 @@ func GenerateMacrosFileContents(buildConfig projectconfig.ComponentBuildConfig)
167173
macros[name] = value
168174
}
169175

176+
// Remove any macros listed in undefines. This allows component-level config to
177+
// override distro-level default macro definitions by removing them entirely.
178+
for _, undef := range buildConfig.Undefines {
179+
delete(macros, undef)
180+
}
181+
170182
// Sort macro names for deterministic output.
171183
macroNames := lo.Keys(macros)
172184
slices.Sort(macroNames)

internal/app/azldev/core/sources/sourceprep_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,71 @@ func TestGenerateMacrosFileContents_ValuesWithSpaces(t *testing.T) {
248248
assert.Contains(t, contents, "%description A package with multiple words")
249249
}
250250

251+
func TestGenerateMacrosFileContents_UndefinesRemovesDefine(t *testing.T) {
252+
// Undefines should remove a macro that was added via defines.
253+
contents := sources.GenerateMacrosFileContents(projectconfig.ComponentBuildConfig{
254+
Defines: map[string]string{
255+
"dist": ".azl3",
256+
"vendor": "Microsoft",
257+
},
258+
Undefines: []string{"dist"},
259+
})
260+
261+
assert.NotContains(t, contents, "%dist")
262+
assert.Contains(t, contents, "%vendor Microsoft")
263+
}
264+
265+
func TestGenerateMacrosFileContents_UndefinesRemovesWithFlag(t *testing.T) {
266+
// Undefines should be able to remove a macro generated from a with flag.
267+
contents := sources.GenerateMacrosFileContents(projectconfig.ComponentBuildConfig{
268+
With: []string{"tests", "docs"},
269+
Undefines: []string{"_with_tests"},
270+
})
271+
272+
assert.NotContains(t, contents, "_with_tests")
273+
assert.Contains(t, contents, "%_with_docs 1")
274+
}
275+
276+
func TestGenerateMacrosFileContents_UndefinesRemovesWithoutFlag(t *testing.T) {
277+
// Undefines should be able to remove a macro generated from a without flag.
278+
contents := sources.GenerateMacrosFileContents(projectconfig.ComponentBuildConfig{
279+
Without: []string{"debug", "static"},
280+
Undefines: []string{"_without_debug"},
281+
})
282+
283+
assert.NotContains(t, contents, "_without_debug")
284+
assert.Contains(t, contents, "%_without_static 1")
285+
}
286+
287+
func TestGenerateMacrosFileContents_UndefinesNonexistentMacroIsNoop(t *testing.T) {
288+
// Undefining a macro that doesn't exist should not cause an error.
289+
contents := sources.GenerateMacrosFileContents(projectconfig.ComponentBuildConfig{
290+
Defines: map[string]string{
291+
"vendor": "Microsoft",
292+
},
293+
Undefines: []string{"nonexistent"},
294+
})
295+
296+
assert.Contains(t, contents, "%vendor Microsoft")
297+
}
298+
299+
func TestGenerateMacrosFileContents_UndefinesAllMacros(t *testing.T) {
300+
// Undefining all macros should result in only the header.
301+
contents := sources.GenerateMacrosFileContents(projectconfig.ComponentBuildConfig{
302+
With: []string{"tests"},
303+
Without: []string{"debug"},
304+
Defines: map[string]string{
305+
"dist": ".azl3",
306+
},
307+
Undefines: []string{"_with_tests", "_without_debug", "dist"},
308+
})
309+
310+
assert.Contains(t, contents, sources.MacrosFileHeader)
311+
assert.NotContains(t, contents, "%_with_tests")
312+
assert.NotContains(t, contents, "%_without_debug")
313+
assert.NotContains(t, contents, "%dist")
314+
}
315+
251316
func TestGenerateMacrosFileContents_FullConfig(t *testing.T) {
252317
contents := sources.GenerateMacrosFileContents(projectconfig.ComponentBuildConfig{
253318
With: []string{"tests", "docs"},

internal/projectconfig/build.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type ComponentBuildConfig struct {
3838
Without []string `toml:"without,omitempty" json:"without,omitempty" jsonschema:"title=Without options,description='without' options to pass to the builder."`
3939
// Macro definitions.
4040
Defines map[string]string `toml:"defines,omitempty" json:"defines,omitempty" jsonschema:"title=Macro definitions,description=Macro definitions to pass to the builder."`
41+
// Undefine macros that would otherwise be defined by the component configuration.
42+
Undefines []string `toml:"undefines,omitempty" json:"undefines,omitempty" jsonschema:"title=Undefined macros,description=Macro names to undefine when passing to the builder."`
4143
// Check section configuration.
4244
Check CheckConfig `toml:"check,omitempty" json:"check,omitempty" jsonschema:"title=Check configuration,description=Configuration for the %check section"`
4345
}

scenario/__snapshots__/TestSnapshotsContainer_config_generate-schema_stdout_1.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@
4545
"title": "Macro definitions",
4646
"description": "Macro definitions to pass to the builder."
4747
},
48+
"undefines": {
49+
"items": {
50+
"type": "string"
51+
},
52+
"type": "array",
53+
"title": "Undefined macros",
54+
"description": "Macro names to undefine when passing to the builder."
55+
},
4856
"check": {
4957
"$ref": "#/$defs/CheckConfig",
5058
"title": "Check configuration",

scenario/__snapshots__/TestSnapshots_config_generate-schema_stdout_1.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@
4545
"title": "Macro definitions",
4646
"description": "Macro definitions to pass to the builder."
4747
},
48+
"undefines": {
49+
"items": {
50+
"type": "string"
51+
},
52+
"type": "array",
53+
"title": "Undefined macros",
54+
"description": "Macro names to undefine when passing to the builder."
55+
},
4856
"check": {
4957
"$ref": "#/$defs/CheckConfig",
5058
"title": "Check configuration",

schemas/azldev.schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@
4545
"title": "Macro definitions",
4646
"description": "Macro definitions to pass to the builder."
4747
},
48+
"undefines": {
49+
"items": {
50+
"type": "string"
51+
},
52+
"type": "array",
53+
"title": "Undefined macros",
54+
"description": "Macro names to undefine when passing to the builder."
55+
},
4856
"check": {
4957
"$ref": "#/$defs/CheckConfig",
5058
"title": "Check configuration",

0 commit comments

Comments
 (0)