|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "strings" |
4 | 7 | "testing" |
5 | 8 | ) |
6 | 9 |
|
@@ -41,3 +44,48 @@ func TestRemoveLoginCache_NonExistent(t *testing.T) { |
41 | 44 | t.Fatalf("removeLoginCache should not error on non-existent file, got: %v", err) |
42 | 45 | } |
43 | 46 | } |
| 47 | + |
| 48 | +func TestPrintPostLogoutHintClarifiesFutureSessionsAndLoadedCredentials(t *testing.T) { |
| 49 | + output := captureStdout(t, printPostLogoutHint) |
| 50 | + |
| 51 | + expectedParts := []string{ |
| 52 | + "Local cache has been removed for future CLI sessions.", |
| 53 | + "Already-running tools that loaded temporary STS credentials before logout", |
| 54 | + "may continue to use them until those credentials expire.", |
| 55 | + } |
| 56 | + for _, part := range expectedParts { |
| 57 | + if !strings.Contains(output, part) { |
| 58 | + t.Fatalf("printPostLogoutHint output %q does not contain %q", output, part) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func captureStdout(t *testing.T, fn func()) string { |
| 64 | + t.Helper() |
| 65 | + |
| 66 | + originalStdout := os.Stdout |
| 67 | + reader, writer, err := os.Pipe() |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("creating stdout pipe: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + os.Stdout = writer |
| 73 | + defer func() { |
| 74 | + os.Stdout = originalStdout |
| 75 | + }() |
| 76 | + |
| 77 | + fn() |
| 78 | + |
| 79 | + if err := writer.Close(); err != nil { |
| 80 | + t.Fatalf("closing stdout writer: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + data, err := ioutil.ReadAll(reader) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("reading stdout: %v", err) |
| 86 | + } |
| 87 | + if err := reader.Close(); err != nil { |
| 88 | + t.Fatalf("closing stdout reader: %v", err) |
| 89 | + } |
| 90 | + return string(data) |
| 91 | +} |
0 commit comments