Skip to content

Commit

Permalink
fix: Fixes #53, do not extract manifests with empty content
Browse files Browse the repository at this point in the history
Signed-off-by: Dustin Scott <[email protected]>
  • Loading branch information
scottd018 committed Jul 22, 2024
1 parent 6e90791 commit b71831e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/workload/v1/manifests/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ func (manifest *Manifest) ExtractManifests() []string {

var content string

for _, line := range lines {
if strings.TrimRight(line, " ") == "---" {
for i := range lines {
if strings.TrimRight(lines[i], " ") == "---" {
if content != "" {
manifests = append(manifests, content)
content = ""
}
} else {
content = content + "\n" + line
content = content + "\n" + lines[i]
}
}

if content != "" {
if content != "" && !contentIsEmpty(content) {
manifests = append(manifests, content)
}

Expand Down Expand Up @@ -200,3 +200,8 @@ func getFileNames(relativeFileName string) []string {

return fileNames
}

// contentIsEmpty checks if the content is empty.
func contentIsEmpty(content string) bool {
return len(strings.TrimSpace(content)) == 0

Check failure on line 206 in internal/workload/v1/manifests/manifest.go

View workflow job for this annotation

GitHub Actions / Lint

emptyStringTest: replace `len(strings.TrimSpace(content)) == 0` with `strings.TrimSpace(content) == ""` (gocritic)
}
47 changes: 47 additions & 0 deletions internal/workload/v1/manifests/manifest_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,50 @@ func Test_getFileNames(t *testing.T) {
})
}
}

func TestContentHasOnlyNewLines(t *testing.T) {
t.Parallel()

tests := []struct {
name string
content string
expected bool
}{
{
name: "empty string",
content: "",
expected: true,
},
{
name: "string with only whitespace",
content: " \t\n",
expected: true,
},
{
name: "string with only newline",
content: "\n",
expected: true,
},
{
name: "string with newline and non-whitespace characters",
content: "\nHello, World!",
expected: false,
},
{
name: "string with non-whitespace characters",
content: "Hello, World!",
expected: false,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := contentIsEmpty(tt.content)
if got != tt.expected {
t.Errorf("ContentHasOnlyNewLines() = %v, want %v", got, tt.expected)
}
})
}
}
3 changes: 3 additions & 0 deletions test/cases/edge-standalone/.workloadConfig/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,6 @@ spec:
requests.memory: 4Gi
limits.cpu: "4"
limits.memory: 4Gi
# test the trailing yaml separator
# see https://github.com/nukleros/operator-builder/issues/53
---

0 comments on commit b71831e

Please sign in to comment.