MCPcopy Index your code
hub / github.com/coder/coder / TestDevcontainerCLI_WithOutput

Function TestDevcontainerCLI_WithOutput

agent/agentcontainers/devcontainercli_test.go:360–441  ·  view source on GitHub ↗

TestDevcontainerCLI_WithOutput tests that WithUpOutput and WithExecOutput capture CLI logs to provided writers.

(t *testing.T)

Source from the content-addressed store, hash-verified

358// TestDevcontainerCLI_WithOutput tests that WithUpOutput and WithExecOutput capture CLI
359// logs to provided writers.
360func TestDevcontainerCLI_WithOutput(t *testing.T) {
361 t.Parallel()
362
363 // Prepare test executable and logger.
364 testExePath, err := os.Executable()
365 require.NoError(t, err, "get test executable path")
366
367 t.Run("Up", func(t *testing.T) {
368 t.Parallel()
369
370 if runtime.GOOS == "windows" {
371 t.Skip("Windows uses CRLF line endings, golden file is LF")
372 }
373
374 // Buffers to capture stdout and stderr.
375 outBuf := &bytes.Buffer{}
376 errBuf := &bytes.Buffer{}
377
378 // Simulate CLI execution with a standard up.log file.
379 wantArgs := "up --log-format json --workspace-folder /test/workspace"
380 testExecer := &testDevcontainerExecer{
381 testExePath: testExePath,
382 wantArgs: wantArgs,
383 wantError: false,
384 logFile: filepath.Join("testdata", "devcontainercli", "parse", "up.log"),
385 }
386 logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
387 dccli := agentcontainers.NewDevcontainerCLI(logger, testExecer)
388
389 // Call Up with WithUpOutput to capture CLI logs.
390 ctx := testutil.Context(t, testutil.WaitMedium)
391 containerID, err := dccli.Up(ctx, "/test/workspace", "", agentcontainers.WithUpOutput(outBuf, errBuf))
392 require.NoError(t, err, "Up should succeed")
393 require.NotEmpty(t, containerID, "expected non-empty container ID")
394
395 // Read expected log content.
396 expLog, err := os.ReadFile(filepath.Join("testdata", "devcontainercli", "parse", "up.golden"))
397 require.NoError(t, err, "reading expected log file")
398
399 // Verify stdout buffer contains the CLI logs and stderr is empty.
400 assert.Equal(t, string(expLog), outBuf.String(), "stdout buffer should match CLI logs")
401 assert.Empty(t, errBuf.String(), "stderr buffer should be empty on success")
402 })
403
404 t.Run("Exec", func(t *testing.T) {
405 t.Parallel()
406
407 logFile := filepath.Join(t.TempDir(), "exec.log")
408 f, err := os.Create(logFile)
409 require.NoError(t, err, "create exec log file")
410 _, err = f.WriteString("exec command log\n")
411 require.NoError(t, err, "write to exec log file")
412 err = f.Close()
413 require.NoError(t, err, "close exec log file")
414
415 // Buffers to capture stdout and stderr.
416 outBuf := &bytes.Buffer{}
417 errBuf := &bytes.Buffer{}

Callers

nothing calls this directly

Calls 15

UpMethod · 0.95
ExecMethod · 0.95
NewDevcontainerCLIFunction · 0.92
ContextFunction · 0.92
WithUpOutputFunction · 0.92
WithExecContainerIDFunction · 0.92
WithExecOutputFunction · 0.92
SkipMethod · 0.80
NotEmptyMethod · 0.80
WriteStringMethod · 0.80
RunMethod · 0.65
ReadFileMethod · 0.65

Tested by

no test coverage detected