package cli import ( "path/filepath" "strings" "testing" ) func TestParseBuildTakesDockersFlags(t *testing.T) { opts, err := parseBuild([]string{"-f", "docker/Dockerfile.build", "-t", "ghcr.io/example/build:2026.10", "--no-cache", "ctx"}) if err != nil { t.Fatal(err) } if opts.Dockerfile != "docker/Dockerfile.build" { t.Errorf("dockerfile %q", opts.Dockerfile) } if opts.Tag != "ghcr.io/example/build:2026.10" { t.Errorf("tag %q", opts.Tag) } if opts.Context != "ctx" || !opts.NoCache { t.Errorf("context %q, no-cache %v", opts.Context, opts.NoCache) } } func TestParseBuildDefaultsTheDockerfileToTheContext(t *testing.T) { opts, err := parseBuild([]string{"--tag", "built", "/srv/project"}) if err != nil { t.Fatal(err) } if want := filepath.Join("/srv/project", "Dockerfile"); opts.Dockerfile != want { t.Errorf("dockerfile %q, want %q", opts.Dockerfile, want) } } func TestParseBuildRefusesLoudly(t *testing.T) { tests := []struct { name string args []string wantErr string }{ {"no context", []string{"-t", "x"}, "needs a CONTEXT"}, {"no tag", []string{"ctx"}, "-t REPOSITORY"}, {"two tags", []string{"-t", "a", "-t", "b", "ctx"}, "one -t per build"}, {"an invalid reference", []string{"-t", "Not A Ref", "ctx"}, "invalid"}, {"a second argument", []string{"-t", "x", "ctx", "extra"}, `unexpected argument "extra"`}, {"a Dockerfile on stdin", []string{"-t", "x", "-"}, "needs no context"}, {"--build-arg", []string{"-t", "x", "--build-arg", "V=1", "ctx"}, "stage 2 of RFC 0006"}, {"--target", []string{"-t", "x", "--target", "builder", "ctx"}, "stage 3 of RFC 0006"}, {"--platform", []string{"-t", "x", "--platform", "linux/arm64", "ctx"}, "host's platform"}, {"--secret", []string{"-t", "x", "--secret", "id=a", "ctx"}, "CapEff is 0"}, {"--cache-from", []string{"-t", "x", "--cache-from", "a", "ctx"}, "no layer cache"}, {"--squash", []string{"-t", "x", "--squash", "ctx"}, "one flat tree already"}, {"--push", []string{"-t", "x", "--push", "ctx"}, "no push verb"}, {"an unknown flag", []string{"-t", "x", "--frobnicate", "ctx"}, "flag provided but not defined"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { _, err := parseBuild(tt.args) if err == nil || !strings.Contains(err.Error(), tt.wantErr) { t.Errorf("got %v, want it to contain %q", err, tt.wantErr) } }) } }