Skip to content

Commit

Permalink
test!: create ws e2e tests with subcommand updates (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
jahvon authored Sep 28, 2024
1 parent f7212ca commit 355db8b
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 54 deletions.
33 changes: 0 additions & 33 deletions cmd/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,46 +76,13 @@ func registerSetConfigCmd(ctx *context.Context, configCmd *cobra.Command) {
Aliases: []string{"s", "update"},
Short: "Update flow configuration values.",
}
registerSetWorkspaceCmd(ctx, setCmd)
registerSetNamespaceCmd(ctx, setCmd)
registerSetWorkspaceModeCmd(ctx, setCmd)
registerSetLogModeCmd(ctx, setCmd)
registerSetTUICmd(ctx, setCmd)
configCmd.AddCommand(setCmd)
}

func registerSetWorkspaceCmd(ctx *context.Context, setCmd *cobra.Command) {
workspaceCmd := &cobra.Command{
Use: "workspace NAME",
Aliases: []string{"ws"},
Short: "Change the current workspace.",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) { printContext(ctx, cmd) },
Run: func(cmd *cobra.Command, args []string) { setWorkspaceFunc(ctx, cmd, args) },
}
RegisterFlag(ctx, workspaceCmd, *flags.FixedWsModeFlag)
setCmd.AddCommand(workspaceCmd)
}

func setWorkspaceFunc(ctx *context.Context, cmd *cobra.Command, args []string) {
logger := ctx.Logger
workspace := args[0]
userConfig := ctx.Config
if _, found := userConfig.Workspaces[workspace]; !found {
logger.Fatalf("workspace %s not found", workspace)
}
userConfig.CurrentWorkspace = workspace
fixedMode := flags.ValueFor[bool](ctx, cmd, *flags.FixedWsModeFlag, false)
if fixedMode {
userConfig.WorkspaceMode = config.ConfigWorkspaceModeFixed
}

if err := filesystem.WriteConfig(userConfig); err != nil {
logger.FatalErr(err)
}
logger.PlainTextSuccess("Workspace set to " + workspace)
}

func registerSetNamespaceCmd(ctx *context.Context, setCmd *cobra.Command) {
namespaceCmd := &cobra.Command{
Use: "namespace NAME",
Expand Down
54 changes: 44 additions & 10 deletions cmd/internal/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/jahvon/flow/internal/io"
workspaceIO "github.com/jahvon/flow/internal/io/workspace"
"github.com/jahvon/flow/types/common"
"github.com/jahvon/flow/types/config"
"github.com/jahvon/flow/types/workspace"
)

Expand All @@ -29,26 +30,27 @@ func RegisterWorkspaceCmd(ctx *context.Context, rootCmd *cobra.Command) {
Aliases: []string{"ws"},
Short: "Manage flow workspaces.",
}
registerNewWorkspaceCmd(ctx, wsCmd)
registerCreateWorkspaceCmd(ctx, wsCmd)
registerSetWorkspaceCmd(ctx, wsCmd)
registerDeleteWsCmd(ctx, wsCmd)
registerListWorkspaceCmd(ctx, wsCmd)
registerViewWsCmd(ctx, wsCmd)
rootCmd.AddCommand(wsCmd)
}

func registerNewWorkspaceCmd(ctx *context.Context, wsCmd *cobra.Command) {
newCmd := &cobra.Command{
Use: "new NAME PATH",
Aliases: []string{"init", "create"},
func registerCreateWorkspaceCmd(ctx *context.Context, wsCmd *cobra.Command) {
createCmd := &cobra.Command{
Use: "create NAME PATH",
Aliases: []string{"init"},
Short: "Initialize a new workspace and register it in the user configurations.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) { newWorkspaceFunc(ctx, cmd, args) },
Run: func(cmd *cobra.Command, args []string) { createWorkspaceFunc(ctx, cmd, args) },
}
RegisterFlag(ctx, newCmd, *flags.SetAfterCreateFlag)
wsCmd.AddCommand(newCmd)
RegisterFlag(ctx, createCmd, *flags.SetAfterCreateFlag)
wsCmd.AddCommand(createCmd)
}

func newWorkspaceFunc(ctx *context.Context, cmd *cobra.Command, args []string) {
func createWorkspaceFunc(ctx *context.Context, cmd *cobra.Command, args []string) {
logger := ctx.Logger
name := args[0]
path := args[1]
Expand Down Expand Up @@ -107,6 +109,38 @@ func newWorkspaceFunc(ctx *context.Context, cmd *cobra.Command, args []string) {
logger.PlainTextSuccess(fmt.Sprintf("Workspace '%s' created in %s", name, path))
}

func registerSetWorkspaceCmd(ctx *context.Context, setCmd *cobra.Command) {
workspaceCmd := &cobra.Command{
Use: "set NAME",
Aliases: []string{"update"},
Short: "Change the current workspace.",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) { printContext(ctx, cmd) },
Run: func(cmd *cobra.Command, args []string) { setWorkspaceFunc(ctx, cmd, args) },
}
RegisterFlag(ctx, workspaceCmd, *flags.FixedWsModeFlag)
setCmd.AddCommand(workspaceCmd)
}

func setWorkspaceFunc(ctx *context.Context, cmd *cobra.Command, args []string) {
logger := ctx.Logger
workspace := args[0]
userConfig := ctx.Config
if _, found := userConfig.Workspaces[workspace]; !found {
logger.Fatalf("workspace %s not found", workspace)
}
userConfig.CurrentWorkspace = workspace
fixedMode := flags.ValueFor[bool](ctx, cmd, *flags.FixedWsModeFlag, false)
if fixedMode {
userConfig.WorkspaceMode = config.ConfigWorkspaceModeFixed
}

if err := filesystem.WriteConfig(userConfig); err != nil {
logger.FatalErr(err)
}
logger.PlainTextSuccess("Workspace set to " + workspace)
}

func registerDeleteWsCmd(ctx *context.Context, wsCmd *cobra.Command) {
deleteCmd := &cobra.Command{
Use: "delete NAME",
Expand Down Expand Up @@ -161,7 +195,7 @@ func deleteWsFunc(ctx *context.Context, _ *cobra.Command, args []string) {
logger.FatalErr(err)
}

logger.Warnf("Workspace '%s' removed", name)
logger.Warnf("Workspace '%s' deleted", name)

if err := cache.UpdateAll(logger); err != nil {
logger.FatalErr(errors.Wrap(err, "unable to update cache"))
Expand Down
1 change: 0 additions & 1 deletion docs/cli/flow_config_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ Update flow configuration values.
* [flow config set log-mode](flow_config_set_log-mode.md) - Set the default log mode.
* [flow config set namespace](flow_config_set_namespace.md) - Change the current namespace.
* [flow config set tui](flow_config_set_tui.md) - Enable or disable the interactive terminal UI experience.
* [flow config set workspace](flow_config_set_workspace.md) - Change the current workspace.
* [flow config set workspace-mode](flow_config_set_workspace-mode.md) - Switch between fixed and dynamic workspace modes.

3 changes: 2 additions & 1 deletion docs/cli/flow_workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ Manage flow workspaces.
### SEE ALSO

* [flow](flow.md) - flow is a command line interface designed to make managing and running development workflows easier.
* [flow workspace create](flow_workspace_create.md) - Initialize a new workspace and register it in the user configurations.
* [flow workspace delete](flow_workspace_delete.md) - Remove an existing workspace from the global configuration's workspaces list.
* [flow workspace list](flow_workspace_list.md) - View a list of registered workspaces.
* [flow workspace new](flow_workspace_new.md) - Initialize a new workspace and register it in the user configurations.
* [flow workspace set](flow_workspace_set.md) - Change the current workspace.
* [flow workspace view](flow_workspace_view.md) - View the documentation for a workspace. If the name is omitted, the current workspace is used.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
## flow workspace new
## flow workspace create

Initialize a new workspace and register it in the user configurations.

```
flow workspace new NAME PATH [flags]
flow workspace create NAME PATH [flags]
```

### Options

```
-h, --help help for new
-h, --help help for create
-s, --set Set the newly created workspace as the current workspace
```

Expand Down
27 changes: 27 additions & 0 deletions docs/cli/flow_workspace_set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## flow workspace set

Change the current workspace.

```
flow workspace set NAME [flags]
```

### Options

```
-f, --fixed Set the workspace mode to fixed
-h, --help help for set
```

### Options inherited from parent commands

```
-x, --non-interactive Disable displaying flow output via terminal UI rendering. This is only needed if the interactive output is enabled by default in flow's configuration.
--sync Sync flow cache and workspaces
--verbosity int Log verbosity level (-1 to 1)
```

### SEE ALSO

* [flow workspace](flow_workspace.md) - Manage flow workspaces.

11 changes: 6 additions & 5 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ building the project.
## Getting Started

Before developing on this project, you will need to make sure you have the latest `flow` version installed.
Refer to the [Installation](../README.md#installation) section for more information.
Refer to the [Installation](installation.md) section for more information.

After cloning the repository, you can start using the below commands after registering the repo workspace:

```sh
flow workspace new flow <repo-path>
flow workspace create flow <repo-path>
```

### Development Executables
Expand Down Expand Up @@ -63,10 +63,11 @@ _You should test all tuikit changes with a local flow build before submitting a

## Development Tools

The following tools are required for development:
Required tools for development:

- [mockgen](https://github.com/uber-go/mock) for generating test mocks
- [golangci-lint](https://golangci-lint.run/) for linting

Additionally, we recommend using the [ginkgo CLI](https://onsi.github.io/ginkgo/#ginkgo-cli-overview) for setting up and running tests.

Other tools used in the project:
- [goreleaser](https://goreleaser.com/) for releasing the project
- [ginkgo](https://onsi.github.io/ginkgo/) and [gomega](https://onsi.github.io/gomega/) for testing
2 changes: 1 addition & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A workspace can be created anywhere on your system but must be registered in ord
To create a new workspace, run the following command in the directory where you want the workspace to be created:

```shell
flow workspace new my-workspace .
flow workspace create my-workspace .
```

You can replace `my-workspace` with any name you want to give your workspace and `.` with the path to the root directory
Expand Down
98 changes: 98 additions & 0 deletions tests/workspace_cmds_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package tests_test

import (
stdCtx "context"
"fmt"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/jahvon/flow/internal/context"
"github.com/jahvon/flow/tests/utils"
)

var _ = Describe("workspace e2e", Ordered, func() {
var (
ctx *context.Context
run *utils.CommandRunner

wsName, wsPath, origWsName string
)

BeforeAll(func() {
ctx = utils.NewContext(stdCtx.Background(), GinkgoT())
run = utils.NewE2ECommandRunner()
tmp, err := os.MkdirTemp("", "flow-test-*")
Expect(err).NotTo(HaveOccurred())
origWsName = ctx.Config.CurrentWorkspace
wsName = "test-workspace"
wsPath = tmp
})

BeforeEach(func() {
utils.ResetTestContext(ctx, GinkgoT())
})

AfterEach(func() {
ctx.Finalize()
})

AfterAll(func() {
Expect(os.RemoveAll(wsPath)).To(Succeed())
})

When("creating a new workspace (flow workspace create)", func() {
It("creates successfully", func() {
stdOut := ctx.StdOut()
Expect(run.Run(ctx, "workspace", "create", wsName, wsPath)).To(Succeed())
out, err := readFileContent(stdOut)
Expect(err).NotTo(HaveOccurred())
Expect(out).To(ContainSubstring(fmt.Sprintf("Workspace '%s' created", wsName)))
})
})

When("setting a workspace (flow workspace set)", func() {
It("sets successfully", func() {
Expect(run.Run(ctx, "workspace", "set", wsName)).To(Succeed())
out, err := readFileContent(ctx.StdOut())
Expect(err).NotTo(HaveOccurred())
Expect(out).To(ContainSubstring(fmt.Sprintf("Workspace set to %s", wsName)))
})
})

When("getting a workspace (flow workspace view)", func() {
It("should returns the workspace", func() {
stdOut := ctx.StdOut()
Expect(run.Run(ctx, "workspace", "view", wsName)).To(Succeed())
out, err := readFileContent(stdOut)
Expect(err).NotTo(HaveOccurred())
Expect(out).To(ContainSubstring(wsName))
})
})

When("listing workspaces (flow workspace list)", func() {
It("should return the list of workspaces", func() {
stdOut := ctx.StdOut()
Expect(run.Run(ctx, "workspace", "list")).To(Succeed())
out, err := readFileContent(stdOut)
Expect(err).NotTo(HaveOccurred())
Expect(out).To(ContainSubstring(wsName))
})
})

When("deleting a workspace (flow workspace delete)", func() {
It("should remove the workspace from the user config", func() {
reader, writer, err := os.Pipe()
Expect(err).NotTo(HaveOccurred())
_, err = writer.Write([]byte("yes\n"))
Expect(err).ToNot(HaveOccurred())

ctx.SetIO(reader, ctx.StdOut())
Expect(run.Run(ctx, "workspace", "delete", origWsName)).To(Succeed())
out, err := readFileContent(ctx.StdOut())
Expect(err).NotTo(HaveOccurred())
Expect(out).To(ContainSubstring(fmt.Sprintf("Workspace '%s' deleted", origWsName)))
})
})
})

0 comments on commit 355db8b

Please sign in to comment.