feat: add JSON output format to many CLI commands (#6082)

This commit is contained in:
Dean Sheather
2023-02-08 17:09:38 +00:00
committed by GitHub
parent 5655ec6862
commit d60ec3e4bf
36 changed files with 851 additions and 285 deletions
+41 -3
View File
@@ -1,6 +1,9 @@
package cli_test
import (
"bytes"
"context"
"encoding/json"
"sort"
"testing"
@@ -8,7 +11,9 @@ import (
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)
func TestTemplateList(t *testing.T) {
@@ -32,12 +37,15 @@ func TestTemplateList(t *testing.T) {
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancelFunc()
errC := make(chan error)
go func() {
errC <- cmd.Execute()
errC <- cmd.ExecuteContext(ctx)
}()
// expect that templates are listed alphebetically
// expect that templates are listed alphabetically
var templatesList = []string{firstTemplate.Name, secondTemplate.Name}
sort.Strings(templatesList)
@@ -47,6 +55,33 @@ func TestTemplateList(t *testing.T) {
pty.ExpectMatch(name)
}
})
t.Run("ListTemplatesJSON", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
firstVersion := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, firstVersion.ID)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, firstVersion.ID)
secondVersion := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, secondVersion.ID)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, secondVersion.ID)
cmd, root := clitest.New(t, "templates", "list", "--output=json")
clitest.SetupConfig(t, client, root)
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancelFunc()
out := bytes.NewBuffer(nil)
cmd.SetOut(out)
err := cmd.ExecuteContext(ctx)
require.NoError(t, err)
var templates []codersdk.Template
require.NoError(t, json.Unmarshal(out.Bytes(), &templates))
require.Len(t, templates, 2)
})
t.Run("NoTemplates", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{})
@@ -59,9 +94,12 @@ func TestTemplateList(t *testing.T) {
cmd.SetIn(pty.Input())
cmd.SetErr(pty.Output())
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancelFunc()
errC := make(chan error)
go func() {
errC <- cmd.Execute()
errC <- cmd.ExecuteContext(ctx)
}()
require.NoError(t, <-errC)