From 252833461b918f163b3e5d3258d0b0f975377329 Mon Sep 17 00:00:00 2001 From: Ahmed Moalla Date: Thu, 9 Jan 2025 18:46:00 +0100 Subject: [PATCH] add support to `;` for comments in unit files as per systemd documentation Signed-off-by: Ahmed Moalla --- pkg/systemd/parser/unitfile.go | 2 +- pkg/systemd/parser/unitfile_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pkg/systemd/parser/unitfile.go b/pkg/systemd/parser/unitfile.go index 0fefdb138d..5eb6d9bb56 100644 --- a/pkg/systemd/parser/unitfile.go +++ b/pkg/systemd/parser/unitfile.go @@ -204,7 +204,7 @@ func (f *UnitFile) Dup() *UnitFile { } func lineIsComment(line string) bool { - return len(line) == 0 || line[0] == '#' || line[0] == ':' + return len(line) == 0 || line[0] == '#' || line[0] == ':' || line[0] == ';' } func lineIsGroup(line string) bool { diff --git a/pkg/systemd/parser/unitfile_test.go b/pkg/systemd/parser/unitfile_test.go index 72b5db65e2..183bac6437 100644 --- a/pkg/systemd/parser/unitfile_test.go +++ b/pkg/systemd/parser/unitfile_test.go @@ -314,6 +314,35 @@ func TestUnitDropinPaths_Search(t *testing.T) { } } +func TestCommentsIgnored(t *testing.T) { + unitWithComments := `[Container] +# comment +Name=my-container +: second comment +; another comment +` + f := NewUnitFile() + if e := f.Parse(unitWithComments); e != nil { + panic(e) + } + + groups := f.ListGroups() + assert.Len(t, groups, 1) + assert.Equal(t, "Container", groups[0]) + + comments := make([]string, 0, 2) + for _, line := range f.groups[0].lines { + if line.isComment { + comments = append(comments, line.value) + } + } + + assert.Len(t, comments, 3) + assert.Equal(t, "# comment", comments[0]) + assert.Equal(t, ": second comment", comments[1]) + assert.Equal(t, "; another comment", comments[2]) +} + func FuzzParser(f *testing.F) { for _, sample := range samples { f.Add([]byte(sample))