diff --git a/internal/app/wwctl/overlay/info/main.go b/internal/app/wwctl/overlay/info/main.go index f0e2af6b..e92bc4f7 100644 --- a/internal/app/wwctl/overlay/info/main.go +++ b/internal/app/wwctl/overlay/info/main.go @@ -32,12 +32,19 @@ func CobraRunE(cmd *cobra.Command, args []string) error { commentMap := ov.ParseCommentVars(filePath) commentKeys := maps.Keys(commentMap) sort.Strings(commentKeys) + hasWwdoc := false for _, docLn := range commentKeys { if strings.Contains(docLn, "wwdoc") { wwlog.Info(commentMap[docLn]) + hasWwdoc = true } } + // Add newline after wwdoc lines if they exist + if hasWwdoc { + fmt.Fprintln(cmd.OutOrStdout()) + } + // Sort variables by name for consistent output varNames := maps.Keys(varFields) sort.Strings(varNames) diff --git a/internal/app/wwctl/overlay/info/main_test.go b/internal/app/wwctl/overlay/info/main_test.go index e10b78b0..2b906741 100644 --- a/internal/app/wwctl/overlay/info/main_test.go +++ b/internal/app/wwctl/overlay/info/main_test.go @@ -11,61 +11,76 @@ import ( ) func Test_Overlay_Variables(t *testing.T) { - env := testenv.New(t) - defer env.RemoveAll() - wwlog.SetLogLevel(wwlog.DEBUG) - warewulfd.SetNoDaemon() - - templateContent := ` + tests := []struct { + name string + writeFiles map[string]string + args []string + expectError bool + expectedOutput string + }{ + { + name: "overlay variables", + writeFiles: map[string]string{ + "var/lib/warewulf/overlays/test-overlay/test.ww": ` {{/* .Kernel.Tags.foo: "some help text" */}} {{/* wwdoc1: First Line */}} -{{/* wwdoc2: Second Line */}} -{{ .Kernel.Tags.foo }} {{ .Node.Tags.bar }} -{{ .Cluster.Tags.baz }} -{{ .Kernel.Vars }} -` - env.WriteFile("var/lib/warewulf/overlays/test-overlay/test.ww", templateContent) +{{/* wwdoc2: Second Line */}} +`, + }, + args: []string{"test-overlay", "test.ww"}, + expectError: false, + expectedOutput: `First Line +Second Line - t.Run("overlay variables", func(t *testing.T) { - baseCmd := GetCommand() - buf := new(bytes.Buffer) - baseCmd.SetOut(buf) - baseCmd.SetErr(buf) - wwlog.SetLogWriter(buf) +VARIABLE OPTION TYPE HELP +-------- ------ ---- ---- +.Node.Tags.bar string +`, + }, + { + name: "overlay variables no file", + writeFiles: map[string]string{ + "var/lib/warewulf/overlays/test-overlay/test.ww": ``, + }, + args: []string{"test-overlay", "no-file.ww"}, + expectError: true, + }, + { + name: "overlay variables no overlay", + args: []string{"no-overlay", "test.ww"}, + expectError: true, + }, + } - baseCmd.SetArgs([]string{"test-overlay", "test.ww"}) - err := baseCmd.Execute() - assert.NoError(t, err) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + env := testenv.New(t) + defer env.RemoveAll() + warewulfd.SetNoDaemon() - output := buf.String() - assert.Contains(t, output, "VARIABLE") - assert.Contains(t, output, ".Kernel.Tags.foo") - assert.Contains(t, output, "some help text") - assert.Regexp(t, `(?s)First Line.*Second Line`, output, "First Line should come before Second Line") - }) + for path, content := range tt.writeFiles { + env.WriteFile(path, content) + } + baseCmd := GetCommand() + buf := new(bytes.Buffer) + baseCmd.SetOut(buf) + baseCmd.SetErr(buf) + wwlog.SetLogWriter(buf) - t.Run("overlay variables no file", func(t *testing.T) { - baseCmd := GetCommand() - buf := new(bytes.Buffer) - baseCmd.SetOut(buf) - baseCmd.SetErr(buf) - wwlog.SetLogWriter(buf) + baseCmd.SetArgs(tt.args) + err := baseCmd.Execute() - baseCmd.SetArgs([]string{"test-overlay", "no-file.ww"}) - err := baseCmd.Execute() - assert.Error(t, err) - }) + if tt.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } - t.Run("overlay variables no overlay", func(t *testing.T) { - baseCmd := GetCommand() - buf := new(bytes.Buffer) - baseCmd.SetOut(buf) - baseCmd.SetErr(buf) - wwlog.SetLogWriter(buf) - - baseCmd.SetArgs([]string{"no-overlay", "test.ww"}) - err := baseCmd.Execute() - assert.Error(t, err) - }) + if tt.expectedOutput != "" { + output := buf.String() + assert.Equal(t, tt.expectedOutput, output) + } + }) + } }