Skip to content

Commit 89b87ab

Browse files
aledbfclaude
andcommitted
test: hold the edits mutation testing found unguarded
Cover what no test refused: read-configuration fails through one helper whose exit code the existing failure tests hold, and the missing --container-id test checks it is the inspect failure that ends the command; prepareDockerfileBuild's target and the cleanup of the named Dockerfile; a parallel lifecycle command's header only on success; the 52-digit padding of the dev container id; a digest with a second colon; an empty request to the TS oracle. The final-stage label condition loses a clause that could never differ, and up's container metadata reader names the runtime lanes that run it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2e3e5cc commit 89b87ab

8 files changed

Lines changed: 94 additions & 12 deletions

File tree

‎internal/cli/build_held_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,33 @@ func TestRunBuildBuildKitNeverIgnoresBuildx(t *testing.T) {
225225
})
226226
}
227227
}
228+
229+
// The Dockerfile build targets the config's target when it names one, else
230+
// the final stage, named when it has no name; a Dockerfile without FROM is
231+
// TS's error.
232+
func TestPrepareDockerfileBuildStage(t *testing.T) {
233+
ws := writeConfigFixture(t, map[string]string{
234+
".devcontainer/Dockerfile": "FROM alpine:3 AS base\nFROM base\n",
235+
".devcontainer/Empty": "RUN true\n",
236+
})
237+
cfgFile := filepath.Join(ws, ".devcontainer", "devcontainer.json")
238+
prep, err := prepareDockerfileBuild(&config.DevContainer{ConfigFilePath: cfgFile, Build: &config.Build{Dockerfile: "Dockerfile", Target: "base"}})
239+
if err != nil || prep.StageName != "base" {
240+
t.Fatalf("with a target: %+v, %v; want stage base", prep, err)
241+
}
242+
prep.Cleanup()
243+
prep, err = prepareDockerfileBuild(&config.DevContainer{ConfigFilePath: cfgFile, Build: &config.Build{Dockerfile: "Dockerfile"}})
244+
if err != nil || prep.StageName != "dev_container_auto_added_stage_label" {
245+
t.Fatalf("without a target: %+v, %v", prep, err)
246+
}
247+
if _, err := os.Stat(prep.Path); err != nil {
248+
t.Fatalf("named Dockerfile: %v", err)
249+
}
250+
prep.Cleanup()
251+
if _, err := os.Stat(prep.Path); !os.IsNotExist(err) {
252+
t.Errorf("named Dockerfile %s left after Cleanup: %v", prep.Path, err)
253+
}
254+
if _, err := prepareDockerfileBuild(&config.DevContainer{ConfigFilePath: cfgFile, Build: &config.Build{Dockerfile: "Empty"}}); err == nil || !strings.Contains(err.Error(), "no FROM instructions") {
255+
t.Errorf("no FROM: err = %v", err)
256+
}
257+
}

‎internal/cli/read_configuration.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ func runReadConfiguration(ctx context.Context, out Output, opts *readConfigOpts)
144144
containerID := readConfigurationContainerID(ctx, engine, opts, workspaceFolder, result)
145145
containerCfg, inspectErr := inspectContainerConfig(ctx, engine, containerID)
146146
if inspectErr != nil {
147-
lgr.Write(inspectErr.Error(), log.LevelError)
148-
return &coreerrors.ExitCodeError{Code: 1, Err: inspectErr}
147+
return readConfigurationFailure(lgr, inspectErr)
149148
}
150149
cfgMap, subErr := substituteContainerEnv(containerCfg, cfgMap)
151150
if subErr != nil {
@@ -178,8 +177,7 @@ func runReadConfiguration(ctx context.Context, out Output, opts *readConfigOpts)
178177
if featErr != nil {
179178
// TS readConfiguration writes the error to the log and exits 1 with no
180179
// result; a Feature that cannot be resolved is not an empty one.
181-
lgr.Write(featErr.Error(), log.LevelError)
182-
return &coreerrors.ExitCodeError{Code: 1, Err: featErr}
180+
return readConfigurationFailure(lgr, featErr)
183181
}
184182
if featResult != nil {
185183
defer os.RemoveAll(featResult.TmpDir)
@@ -204,8 +202,7 @@ func runReadConfiguration(ctx context.Context, out Output, opts *readConfigOpts)
204202
// featuresConfiguration above, as in TS.
205203
buildInfo, biErr := readConfigurationImageBuildInfo(ctx, lgr, engine, opts.dockerPath, result)
206204
if biErr != nil {
207-
lgr.Write(biErr.Error(), log.LevelError)
208-
return &coreerrors.ExitCodeError{Code: 1, Err: biErr}
205+
return readConfigurationFailure(lgr, biErr)
209206
}
210207
base = append(base, buildInfo.Metadata...)
211208
}
@@ -217,8 +214,7 @@ func runReadConfiguration(ctx context.Context, out Output, opts *readConfigOpts)
217214
}
218215
entries, entriesErr := readConfigurationMetadata(append(base, featureEntries...), featureEntries, containerCfg, result, opts.idLabels, workspaceFolder, lgr)
219216
if entriesErr != nil {
220-
lgr.Write(entriesErr.Error(), log.LevelError)
221-
return &coreerrors.ExitCodeError{Code: 1, Err: entriesErr}
217+
return readConfigurationFailure(lgr, entriesErr)
222218
}
223219
merged, mergeErr := mergedConfigurationOutput(entries, containerCfg, cfgMap)
224220
if mergeErr != nil {
@@ -240,6 +236,14 @@ func runReadConfiguration(ctx context.Context, out Output, opts *readConfigOpts)
240236
return nil
241237
}
242238

239+
// readConfigurationFailure is how read-configuration fails, as TS
240+
// readConfiguration does: the error is logged and the command exits 1 with no
241+
// result.
242+
func readConfigurationFailure(lgr log.Logger, err error) error {
243+
lgr.Write(err.Error(), log.LevelError)
244+
return &coreerrors.ExitCodeError{Code: 1, Err: err}
245+
}
246+
243247
// readConfigurationPaths resolves --workspace-folder, --config and
244248
// --override-config against cwd. Without --container-id, --id-label or
245249
// --workspace-folder, the workspace folder is cwd (0.88).

‎internal/cli/read_configuration_held_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,10 @@ func TestReadConfigurationCommandWithoutConfigExitsOne(t *testing.T) {
437437
func TestReadConfigurationCommandContainerIDWithoutWorkspace(t *testing.T) {
438438
// --container-id alone loads no configuration; a container that cannot be
439439
// inspected fails the command with no result, as TS's inspectContainer throws.
440-
out, err := runReadConfigurationCmd(t, "--container-id", "devcontainer-cli-test-no-such-container")
440+
_, err := runReadConfigurationCmd(t, "--container-id", "devcontainer-cli-test-no-such-container")
441441
wantExitCode(t, err, 1)
442-
if out != nil {
443-
t.Errorf("result = %v, want none", out)
442+
if !strings.Contains(err.Error(), "inspect container devcontainer-cli-test-no-such-container") {
443+
t.Errorf("err = %v, want the inspect failure (no configuration is loaded)", err)
444444
}
445445
}
446446

‎internal/cli/up.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,10 @@ func (r *upRunner) featureBuildOptions(cfg *config.DevContainer, loadResult *con
13741374
// getImageMetadataFromContainer with up's id labels and substitutions. A
13751375
// container without a config (the engine could not inspect it) has the
13761376
// configuration's entry alone.
1377+
//
1378+
// mutate-lane: TestParityMatrix - up.feature-hook-failure-error and
1379+
// up.dockerfile-with-parallel-commands-success run the hooks it reads from a
1380+
// real container; also TestDifferentialUp.
13771381
func (r *upRunner) metadataOfContainer(ctr container.InspectResponse, cfg *config.DevContainer) ([]imagemeta.Entry, error) {
13781382
var labels map[string]string
13791383
if ctr.Config != nil {

‎internal/config/varsub_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"errors"
5+
"fmt"
56
"strings"
67
"testing"
78
)
@@ -118,3 +119,20 @@ func TestSubstituteTags(t *testing.T) {
118119
t.Errorf("error prop = %v", err)
119120
}
120121
}
122+
123+
// The id is the hash in base 32 padded to 52 digits, also when the hash's
124+
// leading digits are zeros.
125+
func TestComputeDevContainerIDIsPaddedTo52Digits(t *testing.T) {
126+
padded := false
127+
for i := 0; i < 200; i++ {
128+
labels := map[string]string{"devcontainer.local_folder": fmt.Sprintf("/ws/%d", i)}
129+
id := ComputeDevContainerID(labels)
130+
if len(id) != 52 {
131+
t.Fatalf("id for %v = %q, %d digits", labels, id, len(id))
132+
}
133+
padded = padded || id[0] == '0'
134+
}
135+
if !padded {
136+
t.Error("no id needed padding: widen the search")
137+
}
138+
}

‎internal/devtools/tsoracle_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,19 @@ func TestRefreshTSOracleGoldensRejects(t *testing.T) {
152152
})
153153
}
154154
}
155+
156+
// With no cases the oracle is still asked with a cases array, which the
157+
// tools/tsoracle protocol requires, not with null.
158+
func TestRefreshTSOracleGoldensAsksWithAnEmptyCasesArray(t *testing.T) {
159+
var asked string
160+
_, err := RefreshTSOracleGoldens(map[string][]byte{"g.json": []byte(`{"cases":[]}`)}, func(request []byte) ([]byte, error) {
161+
asked = string(request)
162+
return []byte(`{"cases":[]}`), nil
163+
})
164+
if err != nil {
165+
t.Fatal(err)
166+
}
167+
if asked != `{"cases":[]}` {
168+
t.Errorf("request = %s, want an empty cases array", asked)
169+
}
170+
}

‎internal/docker/dockerfile.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func EnsureFinalStageName(content, defaultName string) (stageName string, modifi
206206
if m == nil {
207207
return "", "", errors.New("Error parsing Dockerfile: failed to parse final FROM line")
208208
}
209-
if m[2] != -1 && m[3] > m[2] {
209+
if m[2] != -1 { // the label group, when it matched, is never empty
210210
return content[lineStart+m[2] : lineStart+m[3]], "", nil
211211
}
212212
pos := lineStart + m[1]

‎internal/oci/ref_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package oci
22

33
import (
44
"encoding/json"
5+
"strings"
56
"testing"
67

78
"github.com/devcontainers/cli/internal/tsoracle/oracletest"
@@ -61,3 +62,12 @@ func TestTSOracleRef(t *testing.T) {
6162
},
6263
})
6364
}
65+
66+
// TS getRef splits the digest on every colon and wants exactly two parts, so
67+
// a second colon is a malformed digest, even after a valid hex.
68+
func TestParseRefDigestWithASecondColon(t *testing.T) {
69+
_, err := ParseRef("ghcr.io/a/b@sha256:" + strings.Repeat("a", 64) + ":def")
70+
if err == nil || !strings.Contains(err.Error(), "invalid digest format") {
71+
t.Errorf("err = %v, want the digest format error", err)
72+
}
73+
}

0 commit comments

Comments
 (0)