mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
Closes https://github.com/coder/coder/issues/16312 We intend to modify the behavior of the CLI handler based on the specified output format. However, the output format is currently only accessible within the `OutputFormatter` structure. Therefore, I propose extending `OutputFormatter` by introducing a public `FormatID` method, which will allow us to retrieve the format identifier and use it to customize the behavior of the CLI handler accordingly.
104 lines
2.9 KiB
Go
104 lines
2.9 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/cli/clitest"
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbfake"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/pty/ptytest"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestList(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("Single", func(t *testing.T) {
|
|
t.Parallel()
|
|
client, db := coderdtest.NewWithDatabase(t, nil)
|
|
owner := coderdtest.CreateFirstUser(t, client)
|
|
member, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
|
// setup template
|
|
r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
|
|
OrganizationID: owner.OrganizationID,
|
|
OwnerID: memberUser.ID,
|
|
}).WithAgent().Do()
|
|
|
|
inv, root := clitest.New(t, "ls")
|
|
clitest.SetupConfig(t, member, root)
|
|
pty := ptytest.New(t).Attach(inv)
|
|
|
|
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
defer cancelFunc()
|
|
done := make(chan any)
|
|
go func() {
|
|
errC := inv.WithContext(ctx).Run()
|
|
assert.NoError(t, errC)
|
|
close(done)
|
|
}()
|
|
pty.ExpectMatch(r.Workspace.Name)
|
|
pty.ExpectMatch("Started")
|
|
cancelFunc()
|
|
<-done
|
|
})
|
|
|
|
t.Run("JSON", func(t *testing.T) {
|
|
t.Parallel()
|
|
client, db := coderdtest.NewWithDatabase(t, nil)
|
|
owner := coderdtest.CreateFirstUser(t, client)
|
|
member, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
|
_ = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
|
|
OrganizationID: owner.OrganizationID,
|
|
OwnerID: memberUser.ID,
|
|
}).WithAgent().Do()
|
|
|
|
inv, root := clitest.New(t, "list", "--output=json")
|
|
clitest.SetupConfig(t, member, root)
|
|
|
|
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
defer cancelFunc()
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
inv.Stdout = out
|
|
err := inv.WithContext(ctx).Run()
|
|
require.NoError(t, err)
|
|
|
|
var workspaces []codersdk.Workspace
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &workspaces))
|
|
require.Len(t, workspaces, 1)
|
|
})
|
|
|
|
t.Run("NoWorkspacesJSON", func(t *testing.T) {
|
|
t.Parallel()
|
|
client := coderdtest.New(t, nil)
|
|
owner := coderdtest.CreateFirstUser(t, client)
|
|
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
|
|
|
inv, root := clitest.New(t, "list", "--output=json")
|
|
clitest.SetupConfig(t, member, root)
|
|
|
|
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
defer cancelFunc()
|
|
|
|
stdout := bytes.NewBuffer(nil)
|
|
stderr := bytes.NewBuffer(nil)
|
|
inv.Stdout = stdout
|
|
inv.Stderr = stderr
|
|
err := inv.WithContext(ctx).Run()
|
|
require.NoError(t, err)
|
|
|
|
var workspaces []codersdk.Workspace
|
|
require.NoError(t, json.Unmarshal(stdout.Bytes(), &workspaces))
|
|
require.Len(t, workspaces, 0)
|
|
|
|
require.Len(t, stderr.Bytes(), 0)
|
|
})
|
|
}
|