@@ -130,14 +130,14 @@ type testDevcontainerExecer struct {
130
130
131
131
// CommandContext returns a test binary command that simulates devcontainer responses.
132
132
func (e * testDevcontainerExecer ) CommandContext (ctx context.Context , name string , args ... string ) * exec.Cmd {
133
- // Only handle "devcontainer" commands
133
+ // Only handle "devcontainer" commands.
134
134
if name != "devcontainer" {
135
- // For non-devcontainer commands, we use a standard execer
135
+ // For non-devcontainer commands, use a standard execer.
136
136
return agentexec .DefaultExecer .CommandContext (ctx , name , args ... )
137
137
}
138
138
139
139
// Create a command that runs the test binary with special flags
140
- // that tell it to simulate a devcontainer command
140
+ // that tell it to simulate a devcontainer command.
141
141
testArgs := []string {
142
142
"-test.run=TestDevcontainerHelperProcess" ,
143
143
"--" ,
@@ -147,15 +147,15 @@ func (e *testDevcontainerExecer) CommandContext(ctx context.Context, name string
147
147
148
148
//nolint:gosec // This is a test binary, so we don't need to worry about command injection.
149
149
cmd := exec .CommandContext (ctx , e .testExePath , testArgs ... )
150
- // Set this environment variable so the child process knows it's the helper
150
+ // Set this environment variable so the child process knows it's the helper.
151
151
cmd .Env = append (os .Environ (), "TEST_DEVCONTAINER_WANT_HELPER_PROCESS=1" )
152
152
153
153
return cmd
154
154
}
155
155
156
156
// PTYCommandContext returns a PTY command.
157
157
func (* testDevcontainerExecer ) PTYCommandContext (_ context.Context , name string , args ... string ) * pty.Cmd {
158
- // This method shouldn't be called for our devcontainer tests
158
+ // This method shouldn't be called for our devcontainer tests.
159
159
panic ("PTYCommandContext not expected in devcontainer tests" )
160
160
}
161
161
@@ -164,7 +164,7 @@ func (*testDevcontainerExecer) PTYCommandContext(_ context.Context, name string,
164
164
//
165
165
//nolint:revive,paralleltest // This is a test helper function.
166
166
func TestDevcontainerHelperProcess (t * testing.T ) {
167
- // If not called by the test as a helper process, run normally
167
+ // If not called by the test as a helper process, do nothing.
168
168
if os .Getenv ("TEST_DEVCONTAINER_WANT_HELPER_PROCESS" ) != "1" {
169
169
return
170
170
}
@@ -175,17 +175,15 @@ func TestDevcontainerHelperProcess(t *testing.T) {
175
175
os .Exit (2 )
176
176
}
177
177
178
- // Check if it's a devcontainer command
179
178
if helperArgs [0 ] != "devcontainer" {
180
179
fmt .Fprintf (os .Stderr , "Unknown command: %s\n " , helperArgs [0 ])
181
180
os .Exit (2 )
182
181
}
183
182
184
- // Verify arguments against expected arguments
183
+ // Verify arguments against expected arguments and skip
184
+ // "devcontainer", it's not included in the input args.
185
185
wantArgs := os .Getenv ("TEST_DEVCONTAINER_WANT_ARGS" )
186
- // Get the actual arguments (skip "devcontainer").
187
186
gotArgs := strings .Join (helperArgs [1 :], " " )
188
-
189
187
if gotArgs != wantArgs {
190
188
fmt .Fprintf (os .Stderr , "Arguments don't match.\n Want: %q\n Got: %q\n " ,
191
189
wantArgs , gotArgs )
@@ -270,17 +268,17 @@ func TestDockerDevcontainerCLI(t *testing.T) {
270
268
})
271
269
}
272
270
273
- // setupDevcontainerWorkspace creates a minimal devcontainer.json file
274
- // in the workspace directory and returns its path.
271
+ // setupDevcontainerWorkspace prepares a test environment with a minimal
272
+ // devcontainer.json configuration and returns the path to the config file .
275
273
func setupDevcontainerWorkspace (t * testing.T , workspaceFolder string ) string {
276
274
t .Helper ()
277
275
278
- // Create . devcontainer directory.
276
+ // Create the devcontainer directory structure .
279
277
devcontainerDir := filepath .Join (workspaceFolder , ".devcontainer" )
280
278
err := os .MkdirAll (devcontainerDir , 0o755 )
281
279
require .NoError (t , err , "create .devcontainer directory" )
282
280
283
- // Create a minimal devcontainer.json file with a test-specific label .
281
+ // Write a minimal configuration with test labels for identification .
284
282
configPath := filepath .Join (devcontainerDir , "devcontainer.json" )
285
283
content := `{
286
284
"image": "alpine:latest",
@@ -295,8 +293,8 @@ func setupDevcontainerWorkspace(t *testing.T, workspaceFolder string) string {
295
293
return configPath
296
294
}
297
295
298
- // findDevcontainerByID finds a container by ID and returns it along with a boolean
299
- // indicating whether it was found.
296
+ // findDevcontainerByID locates a container by its ID and verifies it has our
297
+ // test label. Returns the container and whether it was found.
300
298
func findDevcontainerByID (t * testing.T , pool * dockertest.Pool , id string ) (* docker.Container , bool ) {
301
299
t .Helper ()
302
300
@@ -310,7 +308,8 @@ func findDevcontainerByID(t *testing.T, pool *dockertest.Pool, id string) (*dock
310
308
return container , true
311
309
}
312
310
313
- // removeDevcontainerByID removes the test devcontainer if it exists.
311
+ // removeDevcontainerByID safely cleans up a test container by ID, verifying
312
+ // it has our test label before removal to prevent accidental deletion.
314
313
func removeDevcontainerByID (t * testing.T , pool * dockertest.Pool , id string ) {
315
314
t .Helper ()
316
315
0 commit comments