Skip to content

Commit 4827573

Browse files
aledbfclaude
andcommitted
test(cli): hold positional limits, terminal implications and cleanup
Without Docker no unit test refused raising the one-path limit of stop, down, open, generate-docs and features test, the boundary of the terminal-columns/rows implications, or prepareDockerfileBuild's cleanup. Test them; exempt the written Dockerfile's mode, which only the build this process starts reads, and drop cacheFolder's empty-username check, since TS builds devcontainercli-<username> whatever the name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1b920ee commit 4827573

5 files changed

Lines changed: 88 additions & 1 deletion

File tree

‎internal/cli/build.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ func prepareDockerfileBuild(cfg *config.DevContainer) (*dockerfileBuildPrep, err
318318
prep.StageName = stageName
319319
if modified != "" {
320320
tmpDockerfile := dockerfilePath + ".devcontainer.build"
321+
// mutate-exempt: only the docker build this process starts reads the file, as its owner, and Cleanup removes it
321322
if err := os.WriteFile(tmpDockerfile, []byte(modified), 0644); err != nil {
322323
return nil, fmt.Errorf("write build dockerfile: %w", err)
323324
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cli
2+
3+
import (
4+
"errors"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
10+
"github.com/devcontainers/cli/internal/config"
11+
)
12+
13+
// The Dockerfile written to name the final stage sits next to the user's with
14+
// a copy of its .dockerignore, and Cleanup removes both and leaves the user's
15+
// files alone.
16+
func TestPrepareDockerfileBuildCleanupRemovesTheWrittenFiles(t *testing.T) {
17+
dir := t.TempDir()
18+
dockerfile := filepath.Join(dir, "Dockerfile")
19+
if err := os.WriteFile(dockerfile, []byte("FROM alpine\n"), 0o644); err != nil {
20+
t.Fatal(err)
21+
}
22+
if err := os.WriteFile(dockerfile+".dockerignore", []byte("secret\n"), 0o644); err != nil {
23+
t.Fatal(err)
24+
}
25+
prep, err := prepareDockerfileBuild(&config.DevContainer{
26+
ConfigFilePath: filepath.Join(dir, "devcontainer.json"),
27+
Build: &config.Build{Dockerfile: "Dockerfile"},
28+
})
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
written, err := os.ReadFile(prep.Path)
33+
if err != nil || !strings.Contains(string(written), "AS dev_container_auto_added_stage_label") {
34+
t.Fatalf("written Dockerfile = %q, %v; want the final stage named", written, err)
35+
}
36+
if ignore, err := os.ReadFile(prep.Path + ".dockerignore"); err != nil || string(ignore) != "secret\n" {
37+
t.Fatalf("written .dockerignore = %q, %v; want the user's copied", ignore, err)
38+
}
39+
prep.Cleanup()
40+
for _, p := range []string{prep.Path, prep.Path + ".dockerignore"} {
41+
if _, err := os.Stat(p); !errors.Is(err, os.ErrNotExist) {
42+
t.Errorf("%s after Cleanup: %v, want it removed", p, err)
43+
}
44+
}
45+
for _, p := range []string{dockerfile, dockerfile + ".dockerignore"} {
46+
if _, err := os.Stat(p); err != nil {
47+
t.Errorf("user's %s after Cleanup: %v", p, err)
48+
}
49+
}
50+
}

‎internal/cli/disallowed.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
func cacheFolder() string {
2828
base := "devcontainercli"
2929
if runtime.GOOS == "linux" {
30-
if u, err := user.Current(); err == nil && u.Username != "" {
30+
if u, err := user.Current(); err == nil {
3131
base = "devcontainercli-" + u.Username
3232
}
3333
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cli
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
// The commands that take an optional positional path refuse a second one, as
11+
// the TS CLI's yargs parser (.strict()) refuses arguments its commands do not
12+
// declare; nothing is run.
13+
func TestPathCommandsRefuseASecondPositional(t *testing.T) {
14+
for name, newCmd := range map[string]func() *cobra.Command{
15+
"stop": newStopCmd,
16+
"down": newDownCmd,
17+
"open": newOpenCmd,
18+
"features generate-docs": realFeaturesGenerateDocsCmd,
19+
"templates generate-docs": realTemplatesGenerateDocsCmd,
20+
"features test": realFeaturesTestCmd,
21+
} {
22+
cmd := newCmd()
23+
cmd.SilenceUsage, cmd.SilenceErrors = true, true
24+
var out bytes.Buffer
25+
cmd.SetOut(&out)
26+
cmd.SetErr(&bytes.Buffer{})
27+
cmd.SetArgs([]string{t.TempDir(), t.TempDir()})
28+
err := cmd.ExecuteContext(t.Context())
29+
if want := "accepts at most 1 arg(s), received 2"; err == nil || err.Error() != want || out.Len() != 0 {
30+
t.Errorf("%s with two paths: err = %v, stdout = %q; want %q and no output", name, err, out.String(), want)
31+
}
32+
}
33+
}

‎internal/cli/validate_repo_held_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func TestValidateTerminalImplications(t *testing.T) {
6262
{80, 24, ""},
6363
{80, 0, "Implications failed:\n terminal-columns -> terminal-rows"},
6464
{0, 24, "Implications failed:\n terminal-rows -> terminal-columns"},
65+
{1, 0, "Implications failed:\n terminal-columns -> terminal-rows"},
66+
{0, 1, "Implications failed:\n terminal-rows -> terminal-columns"},
67+
{1, 1, ""},
6568
}
6669
for _, c := range cases {
6770
err := validateTerminalImplications(c.cols, c.rows)

0 commit comments

Comments
 (0)