Skip to content

Commit cf5e536

Browse files
aledbfclaude
andcommitted
test(config): hold the edits mutate makes in types, loader and varsub
Without Docker no unit test refused edits to BuildContext, the union types' MarshalJSON, the worktree and git-root walks, the empty ${} tag, containerEnv's Windows case folding, templateOption's one-name rule and the host platform. Test each against the TS reference. Drop the strings.Builder capacity hint in substituteTags, which changes no result. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 51db7a8 commit cf5e536

4 files changed

Lines changed: 180 additions & 1 deletion

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
// A workspace that is not in a repository mounts at /workspaces/<basename> with
11+
// no extra mount, even with the worktree common-dir mount enabled: TS
12+
// getWorkspaceConfiguration finds no `.git` and adds nothing.
13+
func TestComputeWorkspaceConfigOutsideARepository(t *testing.T) {
14+
ws := filepath.Join(t.TempDir(), "plain")
15+
if err := os.MkdirAll(ws, 0o755); err != nil {
16+
t.Fatal(err)
17+
}
18+
wc := computeWorkspaceConfig(&Workspace{RootFolderPath: ws}, &DevContainer{}, true, true)
19+
if len(wc.AdditionalMounts) != 0 || wc.WorkspaceFolder != "/workspaces/plain" {
20+
t.Errorf("additionalMounts = %q, workspaceFolder = %q; want none and /workspaces/plain", wc.AdditionalMounts, wc.WorkspaceFolder)
21+
}
22+
}
23+
24+
// The ceiling is compared as a clean path, so one written with a trailing
25+
// separator still stops the walk before a `.git` above it.
26+
func TestDetectGitRootUntilCleansTheCeiling(t *testing.T) {
27+
root := t.TempDir()
28+
if err := os.MkdirAll(filepath.Join(root, ".git"), 0o755); err != nil {
29+
t.Fatal(err)
30+
}
31+
ceiling := filepath.Join(root, "sub")
32+
nested := filepath.Join(ceiling, "a")
33+
if err := os.MkdirAll(nested, 0o755); err != nil {
34+
t.Fatal(err)
35+
}
36+
if got := detectGitRootUntil(nested, ceiling+string(filepath.Separator)); got != "" {
37+
t.Errorf("walk with ceiling %q found %q, want no repository", ceiling+string(filepath.Separator), got)
38+
}
39+
}
40+
41+
// Off Windows a config's ${localEnv:NAME} is case-sensitive (TS
42+
// replaceWithContext → lookupValue lowercases the name only when the platform
43+
// is win32), so a name in another case than the host variable resolves to "".
44+
func TestLoadResolvesLocalEnvCaseSensitivelyOffWindows(t *testing.T) {
45+
if runtime.GOOS == "windows" {
46+
t.Skip("the host is Windows")
47+
}
48+
t.Setenv("GOOS_OVERRIDE", "")
49+
t.Setenv("DC_HELD_CASE", "host")
50+
ws := t.TempDir()
51+
cfg := filepath.Join(ws, ".devcontainer", "devcontainer.json")
52+
writeFile(t, cfg, `{"image":"x","containerEnv":{"EXACT":"${localEnv:DC_HELD_CASE}","LOWER":"${localEnv:dc_held_case}"}}`)
53+
res, err := LoadDevContainerConfigWithMounts(ws, cfg, "", MountOptions{})
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
if env := res.Config.ContainerEnv; env["EXACT"] != "host" || env["LOWER"] != "" {
58+
t.Errorf("containerEnv = %v, want EXACT=host and LOWER empty", env)
59+
}
60+
}

‎internal/config/types_held_test.go‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package config
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
)
7+
8+
// The build context is the legacy top-level "context", else build.context, else
9+
// none (TS getDockerContextPath, spec-node/utils.ts: `'dockerFile' in config ?
10+
// config.context : config.build.context`).
11+
func TestBuildContextPrefersTheLegacyProperty(t *testing.T) {
12+
for in, want := range map[string]string{
13+
`{"dockerFile":"D","context":"legacy","build":{"context":"build"}}`: "legacy",
14+
`{"build":{"context":".."}}`: "..",
15+
`{"build":{"dockerfile":"Dockerfile"}}`: "",
16+
`{}`: "",
17+
} {
18+
var c DevContainer
19+
if err := json.Unmarshal([]byte(in), &c); err != nil {
20+
t.Fatal(err)
21+
}
22+
if got := c.BuildContext(); got != want {
23+
t.Errorf("BuildContext(%s) = %q, want %q", in, got, want)
24+
}
25+
}
26+
}
27+
28+
// A lifecycle command marshals back as it was written - the string, array or
29+
// object TS keeps as-is in the metadata label - and an absent one as null.
30+
func TestLifecycleCommandMarshalsAsWritten(t *testing.T) {
31+
for _, in := range []string{`"echo hi"`, `["echo","hi"]`, `{"a":"echo a"}`, `null`} {
32+
var c LifecycleCommand
33+
if err := json.Unmarshal([]byte(in), &c); err != nil {
34+
t.Fatal(err)
35+
}
36+
got, err := json.Marshal(c)
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
if string(got) != in {
41+
t.Errorf("marshal(%s) = %s, want it unchanged", in, got)
42+
}
43+
}
44+
if got, err := json.Marshal(LifecycleCommand{}); err != nil || string(got) != "null" {
45+
t.Errorf("marshal of no command = %s, %v; want null", got, err)
46+
}
47+
}
48+
49+
// A single dockerComposeFile marshals as the string, more than one as the array
50+
// (the `string | string[]` shape the spec gives the property).
51+
func TestStringOrStringsMarshalsOneAsAString(t *testing.T) {
52+
for in, want := range map[string]string{
53+
`"a.yml"`: `"a.yml"`,
54+
`["a.yml"]`: `"a.yml"`,
55+
`["a.yml","b.yml"]`: `["a.yml","b.yml"]`,
56+
} {
57+
var s StringOrStrings
58+
if err := json.Unmarshal([]byte(in), &s); err != nil {
59+
t.Fatal(err)
60+
}
61+
got, err := json.Marshal(s)
62+
if err != nil {
63+
t.Fatal(err)
64+
}
65+
if string(got) != want {
66+
t.Errorf("marshal(%s) = %s, want %s", in, got, want)
67+
}
68+
}
69+
}

‎internal/config/varsub.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ func substituteTags(s string, replace func(tag string) (string, bool, error)) (s
127127
return s, nil
128128
}
129129
var b strings.Builder
130-
b.Grow(len(s) + len(s)/4)
131130
for {
132131
i := strings.Index(s, "${")
133132
if i < 0 {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package config
2+
3+
import "testing"
4+
5+
// An empty ${} is left as written and the tags after it are still resolved: TS
6+
// matches /\$\{(.*?)\}/g, so "${}" is a match whose empty variable no case
7+
// handles (replaceWithContext returns the match), and the scan goes on.
8+
func TestResolveKeepsAnEmptyTagAndResolvesTheRest(t *testing.T) {
9+
ctx := SubstitutionContext{HostSubContext: HostSubContext{LocalWorkspaceFolder: "/home/me/proj"}}
10+
got, err := NewVariableResolver().Resolve(ctx, PhaseHost, "${}-${localWorkspaceFolderBasename}")
11+
if err != nil {
12+
t.Fatal(err)
13+
}
14+
if got != "${}-proj" {
15+
t.Errorf("Resolve = %q, want ${}-proj", got)
16+
}
17+
}
18+
19+
// ${containerEnv:NAME} matches the name case-insensitively only for a Windows
20+
// platform (TS containerSubstitute: normalizeEnv lowercases the keys and
21+
// lookupValue the name when isWindows); elsewhere a name in another case is
22+
// missing and resolves to "".
23+
func TestResolveContainerEnvIgnoresCaseOnlyOnWindows(t *testing.T) {
24+
for platform, want := range map[string]string{"win32": "/bin", "linux": ""} {
25+
ctx := SubstitutionContext{
26+
HostSubContext: HostSubContext{Platform: platform},
27+
ContainerEnv: map[string]string{"Path": "/bin"},
28+
}
29+
got, err := NewVariableResolver().Resolve(ctx, PhaseContainer, "${containerEnv:PATH}")
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
if got != want {
34+
t.Errorf("%s: Resolve = %q, want %q", platform, got, want)
35+
}
36+
}
37+
}
38+
39+
// Only ${templateOption:NAME} with a single name is a template option: TS
40+
// replaceTemplatedValues matches /\${templateOption:\s*(\w+?)\s*}/g, which
41+
// neither a second argument nor another variable fits, so both stay as written.
42+
func TestResolveTemplateOptionTakesOneName(t *testing.T) {
43+
ctx := SubstitutionContext{TemplateOptions: map[string]string{"a": "x"}}
44+
got, err := NewVariableResolver().Resolve(ctx, PhaseTemplate, "${templateOption:a}|${templateOption:a:b}|${other:a}")
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
if want := "x|${templateOption:a:b}|${other:a}"; got != want {
49+
t.Errorf("Resolve = %q, want %q", got, want)
50+
}
51+
}

0 commit comments

Comments
 (0)