TestDevcontainerCLI_WithOutput tests that WithUpOutput and WithExecOutput capture CLI logs to provided writers.
(t *testing.T)
| 358 | // TestDevcontainerCLI_WithOutput tests that WithUpOutput and WithExecOutput capture CLI |
| 359 | // logs to provided writers. |
| 360 | func 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{} |
nothing calls this directly
no test coverage detected