Skip to content

Commit

Permalink
test: fix up
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Sep 19, 2024
1 parent 728082c commit 78f0da2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/cloudx/client/command_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestCommandHelper(t *testing.T) {

t.Run("func=CreateProject", func(t *testing.T) {
t.Parallel()
ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfigFile)
ctx, _ := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfigFile)

h, err := client.NewCommandHelper(ctx)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/cloudx/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func NewCreateProjectCmd() *cobra.Command {
}

wsID := h.WorkspaceID()
if wsID == nil || len(environment) == 0 {
return errors.New("a workspace and environment is required to create a project")
if wsID == nil {
return errors.New("a workspace is required to create a project")
}

if len(name) == 0 && flagx.MustGetBool(cmd, cmdx.FlagQuiet) {
Expand Down
24 changes: 15 additions & 9 deletions cmd/cloudx/project/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package project_test

import (
"context"
"encoding/json"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -38,25 +40,29 @@ func TestCreateProject(t *testing.T) {
t.Run("requires workspace and environment", func(t *testing.T) {
t.Parallel()

ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
testhelpers.SetDefaultProject(ctx, t, defaultProject.Id)
ctx, newConfig := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
conf := testhelpers.ReadConfig(t, newConfig)
conf.SelectedWorkspace = uuid.Nil
rawConfig, err := json.Marshal(conf)
require.NoError(t, err)
require.NoError(t, os.WriteFile(newConfig, rawConfig, 0600))

for _, extraArgs := range [][]string{
{},
{"--workspace", uuid.Must(uuid.NewV4()).String()},
{"--environment", "dev"},
} {
_, stderr, err := testhelpers.Cmd(ctx).Exec(nil, append([]string{"create", "project", "--name", testhelpers.TestName(), "--format", "json"}, extraArgs...)...)
require.Error(t, err)
assert.ErrorContains(t, err, "a workspace and environment is required to create a project", stderr)
assert.Contains(t, stderr, "a workspace and environment is required to create a project")
t.Run("args="+strings.Join(extraArgs, " "), func(t *testing.T) {
_, stderr, err := testhelpers.Cmd(ctx).Exec(nil, append([]string{"create", "project", "--name", testhelpers.TestName(), "--format", "json"}, extraArgs...)...)
require.Error(t, err)
assert.Contains(t, stderr, "a workspace is required to create a project")
})
}
})

t.Run("is able to create a project", func(t *testing.T) {
t.Parallel()

ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
ctx, _ := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
testhelpers.SetDefaultProject(ctx, t, defaultProject.Id)

wsID := testhelpers.CreateWorkspace(ctx, t)
Expand All @@ -72,7 +78,7 @@ func TestCreateProject(t *testing.T) {
t.Run("is able to create a project and use the project as default", func(t *testing.T) {
t.Parallel()

ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
ctx, _ := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)

workspace := testhelpers.CreateWorkspace(ctx, t)
name := testhelpers.TestName()
Expand Down
6 changes: 3 additions & 3 deletions cmd/cloudx/project/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type execFunc = func(stdin io.Reader, args ...string) (string, string, error)

func runWithProjectAsDefault(ctx context.Context, t *testing.T, projectID string, test func(t *testing.T, exec execFunc)) {
t.Run("project passed as default", func(t *testing.T) {
ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
ctx, _ := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
testhelpers.SetDefaultProject(ctx, t, projectID)

test(t, testhelpers.Cmd(ctx).Exec)
Expand All @@ -30,7 +30,7 @@ func runWithProjectAsDefault(ctx context.Context, t *testing.T, projectID string

func runWithProjectAsArgument(ctx context.Context, t *testing.T, projectID string, test func(t *testing.T, exec execFunc)) {
t.Run("project passed as argument", func(t *testing.T) {
ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
ctx, _ := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
selectedProject := testhelpers.GetDefaultProjectID(ctx, t)
require.NotEqual(t, selectedProject, projectID, "to ensure correct isolation, please use another project than the 'default' selected")

Expand All @@ -46,7 +46,7 @@ func runWithProjectAsArgument(ctx context.Context, t *testing.T, projectID strin

func runWithProjectAsFlag(ctx context.Context, t *testing.T, projectID string, test func(t *testing.T, exec execFunc)) {
t.Run("project passed as flag", func(t *testing.T) {
ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
ctx, _ := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
selectedProject := testhelpers.GetDefaultProjectID(ctx, t)
require.NotEqual(t, selectedProject, projectID, "to ensure correct isolation, please use another project than the 'default' selected")

Expand Down
2 changes: 1 addition & 1 deletion cmd/cloudx/project/use_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestUseProject(t *testing.T) {
t.Run("is able to use project", func(t *testing.T) {
t.Parallel()

ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
ctx, _ := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
testhelpers.SetDefaultProject(ctx, t, defaultProject.Id)

stdout, _, err := testhelpers.Cmd(ctx).Exec(nil, "use", "project", extraProject.Id, "--quiet")
Expand Down
4 changes: 2 additions & 2 deletions cmd/cloudx/testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func WithCleanConfigFile(ctx context.Context, t testing.TB) context.Context {
return client.ContextWithOptions(ctx, client.WithConfigLocation(NewConfigFile(t)))
}

func WithDuplicatedConfigFile(ctx context.Context, t testing.TB, originalFile string) context.Context {
func WithDuplicatedConfigFile(ctx context.Context, t testing.TB, originalFile string) (context.Context, string) {
dst, err := os.Create(NewConfigFile(t))
require.NoError(t, err)
defer dst.Close()
Expand All @@ -90,7 +90,7 @@ func WithDuplicatedConfigFile(ctx context.Context, t testing.TB, originalFile st
_, err = io.Copy(dst, src)
require.NoError(t, err)

return client.ContextWithOptions(ctx, client.WithConfigLocation(dst.Name()))
return client.ContextWithOptions(ctx, client.WithConfigLocation(dst.Name())), dst.Name()
}

func Cmd(ctx context.Context) *cmdx.CommandExecuter {
Expand Down

0 comments on commit 78f0da2

Please sign in to comment.