|
1 | 1 | package taskfile |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "errors" |
| 6 | + "os" |
| 7 | + "slices" |
5 | 8 | "sync" |
6 | 9 | "testing" |
7 | 10 |
|
@@ -51,3 +54,53 @@ func TestDefaultShellRunnerCachesOpLookup(t *testing.T) { |
51 | 54 | } |
52 | 55 | assert.Equal(t, 1, calls, "op lookup must only happen once per runner") |
53 | 56 | } |
| 57 | + |
| 58 | +func TestOpRunUsesNoMaskingWhenStdioIsATerminal(t *testing.T) { |
| 59 | + // `op run` masks secrets in stdout/stderr by piping them through itself, |
| 60 | + // which strips the TTY and breaks any interactive TUI the command may |
| 61 | + // launch (docker agent eval, fzf, less, vim, …). When both streams are |
| 62 | + // terminals we pass --no-masking so op leaves them untouched. |
| 63 | + tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0) |
| 64 | + if err != nil { |
| 65 | + t.Skipf("no /dev/tty available: %v", err) |
| 66 | + } |
| 67 | + t.Cleanup(func() { _ = tty.Close() }) |
| 68 | + |
| 69 | + args := opRunArgs(ShellCommand{ |
| 70 | + Command: "echo hi", |
| 71 | + Stdout: tty, |
| 72 | + Stderr: tty, |
| 73 | + }) |
| 74 | + assert.Contains(t, args, "--no-masking") |
| 75 | + assert.Equal(t, []string{"--", "/bin/sh", "-c", "echo hi"}, args[len(args)-4:]) |
| 76 | +} |
| 77 | + |
| 78 | +func TestOpRunKeepsMaskingWhenStdioIsNotATerminal(t *testing.T) { |
| 79 | + // In CI / scripts (output redirected to a buffer or a file) we keep the |
| 80 | + // default masking so accidental secret leaks into logs are still |
| 81 | + // concealed. |
| 82 | + args := opRunArgs(ShellCommand{ |
| 83 | + Command: "echo hi", |
| 84 | + Stdout: &bytes.Buffer{}, |
| 85 | + Stderr: &bytes.Buffer{}, |
| 86 | + }) |
| 87 | + assert.False(t, slices.Contains(args, "--no-masking")) |
| 88 | + assert.Equal(t, []string{"run", "--", "/bin/sh", "-c", "echo hi"}, args) |
| 89 | +} |
| 90 | + |
| 91 | +func TestOpRunKeepsMaskingWhenOnlyStderrIsATerminal(t *testing.T) { |
| 92 | + // Half-redirected: piping just stdout (e.g. `gogo evals | tee log`) is a |
| 93 | + // non-interactive use, so masking stays on. |
| 94 | + tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0) |
| 95 | + if err != nil { |
| 96 | + t.Skipf("no /dev/tty available: %v", err) |
| 97 | + } |
| 98 | + t.Cleanup(func() { _ = tty.Close() }) |
| 99 | + |
| 100 | + args := opRunArgs(ShellCommand{ |
| 101 | + Command: "echo hi", |
| 102 | + Stdout: &bytes.Buffer{}, |
| 103 | + Stderr: tty, |
| 104 | + }) |
| 105 | + assert.False(t, slices.Contains(args, "--no-masking")) |
| 106 | +} |
0 commit comments