From c7fb47e97a9a0d2d956103093a7ace790226bc37 Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Sun, 28 Apr 2024 14:22:00 +0100 Subject: [PATCH] build: remove directive text not applied on build. --- pkg/prebuild/directive/core.go | 7 +++++++ pkg/prebuild/directive/filter.go | 4 ++-- pkg/prebuild/directive/filter_test.go | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pkg/prebuild/directive/core.go b/pkg/prebuild/directive/core.go index 0d638c0c..74498484 100644 --- a/pkg/prebuild/directive/core.go +++ b/pkg/prebuild/directive/core.go @@ -61,6 +61,13 @@ func NewOption(file *paths.Path, match []string) *Option { } } +// Clean the selected directive from profile. +// Useful to remove directive text applied on some condition only +func (o *Option) Clean(profile string) string { + reg := regexp.MustCompile(`\s*` + Keyword + o.Name + ` .*$`) + return reg.ReplaceAllString(profile, "") +} + func RegisterDirective(d Directive) { Directives[d.Name()] = d } diff --git a/pkg/prebuild/directive/filter.go b/pkg/prebuild/directive/filter.go index b4cc54af..305b3b0b 100644 --- a/pkg/prebuild/directive/filter.go +++ b/pkg/prebuild/directive/filter.go @@ -43,10 +43,10 @@ func filterRuleForUs(opt *Option) bool { func filter(only bool, opt *Option, profile string) string { if only && filterRuleForUs(opt) { - return profile + return opt.Clean(profile) } if !only && !filterRuleForUs(opt) { - return profile + return opt.Clean(profile) } inline := true diff --git a/pkg/prebuild/directive/filter_test.go b/pkg/prebuild/directive/filter_test.go index 4dbeca91..6ef62c6a 100644 --- a/pkg/prebuild/directive/filter_test.go +++ b/pkg/prebuild/directive/filter_test.go @@ -31,7 +31,7 @@ func TestFilterOnly_Apply(t *testing.T) { Raw: " @{bin}/arch-audit rPx, #aa:only apt", }, profile: " @{bin}/arch-audit rPx, #aa:only apt", - want: " @{bin}/arch-audit rPx, #aa:only apt", + want: " @{bin}/arch-audit rPx,", }, { name: "paragraph", @@ -121,7 +121,7 @@ func TestFilterExclude_Apply(t *testing.T) { Raw: " @{bin}/dpkg rPx -> child-dpkg, #aa:exclude debian", }, profile: " @{bin}/dpkg rPx -> child-dpkg, #aa:exclude debian", - want: " @{bin}/dpkg rPx -> child-dpkg, #aa:exclude debian", + want: " @{bin}/dpkg rPx -> child-dpkg,", }, } for _, tt := range tests {