Skip to content

Commit

Permalink
fix: added validation error when --image or --build are used with mul…
Browse files Browse the repository at this point in the history
…tiple score files
  • Loading branch information
astromechza committed Sep 5, 2024
1 parent 293dfaf commit dc6c6da
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/command/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ arguments.
}
slog.Debug("Input Workload names", "names", workloadNames)

// Forbid --image and --build when multiple score files are provided
if v, _ := cmd.Flags().GetString(generateCmdImageFlag); v != "" && len(workloadNames) > 1 {
return fmt.Errorf("--%s cannot be used when multiple score files are provided", generateCmdImageFlag)
}
if v, _ := cmd.Flags().GetStringArray(generateCmdBuildFlag); len(v) > 0 && len(workloadNames) > 1 {
return fmt.Errorf("--%s cannot be used when multiple score files are provided", generateCmdBuildFlag)
}

// Now read and apply any overrides files to the score files
if v, _ := cmd.Flags().GetString(generateCmdOverridesFileFlag); v != "" {
if len(workloadNames) == 0 {
Expand Down
56 changes: 56 additions & 0 deletions internal/command/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1359,3 +1359,59 @@ resources:
})
}
}

func TestGenerateMultipleSpecsWithImage(t *testing.T) {
td := changeToTempDir(t)
stdout, _, err := executeAndResetCommand(context.Background(), rootCmd, []string{"init"})
assert.NoError(t, err)
assert.Equal(t, "", stdout)
assert.NoError(t, os.WriteFile(filepath.Join(td, "scoreA.yaml"), []byte(`
apiVersion: score.dev/v1b1
metadata:
name: example-a
containers:
hello:
image: foo
`), 0644))
assert.NoError(t, os.WriteFile(filepath.Join(td, "scoreB.yaml"), []byte(`
apiVersion: score.dev/v1b1
metadata:
name: example-b
containers:
hello:
image: foo
`), 0644))
stdout, _, err = executeAndResetCommand(context.Background(), rootCmd, []string{
"generate", "--image", "nginx:latest", "scoreA.yaml", "scoreB.yaml",
})
assert.EqualError(t, err, "--image cannot be used when multiple score files are provided")
assert.Equal(t, "", stdout)
}

func TestGenerateMultipleSpecsWithBuild(t *testing.T) {
td := changeToTempDir(t)
stdout, _, err := executeAndResetCommand(context.Background(), rootCmd, []string{"init"})
assert.NoError(t, err)
assert.Equal(t, "", stdout)
assert.NoError(t, os.WriteFile(filepath.Join(td, "scoreA.yaml"), []byte(`
apiVersion: score.dev/v1b1
metadata:
name: example-a
containers:
hello:
image: foo
`), 0644))
assert.NoError(t, os.WriteFile(filepath.Join(td, "scoreB.yaml"), []byte(`
apiVersion: score.dev/v1b1
metadata:
name: example-b
containers:
hello:
image: foo
`), 0644))
stdout, _, err = executeAndResetCommand(context.Background(), rootCmd, []string{
"generate", "--build", "foo=.", "scoreA.yaml", "scoreB.yaml",
})
assert.EqualError(t, err, "--build cannot be used when multiple score files are provided")
assert.Equal(t, "", stdout)
}

0 comments on commit dc6c6da

Please sign in to comment.