use stderr instead and add tests

This commit is contained in:
Amanda Lin 2022-04-06 09:57:36 -05:00
parent 6490f658de
commit 5b10b6e986
2 changed files with 61 additions and 3 deletions

View file

@ -155,8 +155,9 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
cs := a.io.ColorScheme()
fmt.Fprintln(a.io.Out, codespace.Name)
if codespace.IdleTimeoutNotice != "" {
fmt.Fprintln(a.io.Out, cs.Cyan("Notice:"), codespace.IdleTimeoutNotice)
if a.io.IsStderrTTY() && codespace.IdleTimeoutNotice != "" {
fmt.Fprintln(a.io.ErrOut, cs.Yellow("Notice:"), codespace.IdleTimeoutNotice)
}
return nil

View file

@ -22,6 +22,7 @@ func TestApp_Create(t *testing.T) {
wantErr error
wantStdout string
wantStderr string
isTTY bool
}{
{
name: "create codespace with default branch and 30m idle timeout",
@ -116,7 +117,59 @@ func TestApp_Create(t *testing.T) {
showStatus: false,
idleTimeout: 30 * time.Minute,
},
wantStdout: "monalisa-dotfiles-abcd1234\nNotice: Idle timeout for this codespace is set to 10 minutes in compliance with your organization's policy\n",
wantStdout: "monalisa-dotfiles-abcd1234\n",
wantStderr: "Notice: Idle timeout for this codespace is set to 10 minutes in compliance with your organization's policy\n",
isTTY: true,
},
{
name: "create codespace with default branch does not show idle timeout notice if not conntected to terminal",
fields: fields{
apiClient: &apiClientMock{
GetCodespaceRegionLocationFunc: func(ctx context.Context) (string, error) {
return "EUROPE", nil
},
GetRepositoryFunc: func(ctx context.Context, nwo string) (*api.Repository, error) {
return &api.Repository{
ID: 1234,
FullName: nwo,
DefaultBranch: "main",
}, nil
},
GetCodespacesMachinesFunc: func(ctx context.Context, repoID int, branch, location string) ([]*api.Machine, error) {
return []*api.Machine{
{
Name: "GIGA",
DisplayName: "Gigabits of a machine",
},
}, nil
},
CreateCodespaceFunc: func(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error) {
if params.Branch != "main" {
return nil, fmt.Errorf("got branch %q, want %q", params.Branch, "main")
}
if params.IdleTimeoutMinutes != 30 {
return nil, fmt.Errorf("idle timeout minutes was %v", params.IdleTimeoutMinutes)
}
return &api.Codespace{
Name: "monalisa-dotfiles-abcd1234",
IdleTimeoutNotice: "Idle timeout for this codespace is set to 10 minutes in compliance with your organization's policy",
}, nil
},
GetCodespaceRepoSuggestionsFunc: func(ctx context.Context, partialSearch string, params api.RepoSearchParameters) ([]string, error) {
return nil, nil // We can't ask for suggestions without a terminal.
},
},
},
opts: createOptions{
repo: "monalisa/dotfiles",
branch: "",
machine: "GIGA",
showStatus: false,
idleTimeout: 30 * time.Minute,
},
wantStdout: "monalisa-dotfiles-abcd1234\n",
wantStderr: "",
isTTY: false,
},
{
name: "create codespace that requires accepting additional permissions",
@ -173,6 +226,10 @@ Alternatively, you can run "create" with the "--default-permissions" option to c
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
io, _, stdout, stderr := iostreams.Test()
io.SetStdoutTTY(tt.isTTY)
io.SetStdinTTY(tt.isTTY)
io.SetStderrTTY(tt.isTTY)
a := &App{
io: io,
apiClient: tt.fields.apiClient,