Skip to content

Commit

Permalink
Add the fix command
Browse files Browse the repository at this point in the history
The fix command recreate the missing folders and files to make the wo
configuration works
  • Loading branch information
antham committed Sep 1, 2024
1 parent 46f098a commit 40c80ea
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 4 deletions.
21 changes: 21 additions & 0 deletions internal/cmd/fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"github.com/spf13/cobra"
)

func newFixCmd(workspaceManager workspaceManager) *cobra.Command {
return &cobra.Command{
Use: "fix",
Short: "Fix the possible failure in the config folder",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
err := workspaceManager.Fix()
if err != nil {
return err
}
cmd.Print(regularStyle.Render("Config folder fixed"))
return nil
},
}
}
57 changes: 57 additions & 0 deletions internal/cmd/fix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

import (
"bytes"
"errors"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewFixCmd(t *testing.T) {
type scenario struct {
name string
setup func(*testing.T) workspaceManager
test func(*testing.T, *bytes.Buffer, *bytes.Buffer, error)
}
scenarios := []scenario{
{
"An error occurred when fixing the config",
func(t *testing.T) workspaceManager {
w := newMockWorkspaceManager(t)
w.Mock.On("Fix").Return(errors.New("an error occurred"))
return w
},
func(t *testing.T, outBuf *bytes.Buffer, errBuf *bytes.Buffer, err error) {
assert.Error(t, err)
},
},
{
"Creating a workspace env successfully",
func(t *testing.T) workspaceManager {
w := newMockWorkspaceManager(t)
w.Mock.On("Fix").Return(nil)
return w
},
func(t *testing.T, outBuf *bytes.Buffer, errBuf *bytes.Buffer, err error) {
assert.NoError(t, err)
assert.Equal(t, "Config folder fixed", outBuf.String())
},
},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
os.Setenv("EDITOR", "emacs")
os.Setenv("SHELL", "/bin/sh")
errBuf := &bytes.Buffer{}
outBuf := &bytes.Buffer{}
w := s.setup(t)
cmd := newFixCmd(w)
cmd.SetArgs([]string{})
cmd.SetErr(errBuf)
cmd.SetOut(outBuf)
s.test(t, outBuf, errBuf, cmd.Execute())
})
}
}
1 change: 1 addition & 0 deletions internal/cmd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type workspaceManager interface {
CreateEnv(string, string) error
Edit(string) error
EditEnv(string, string) error
Fix() error
List() ([]workspace.Workspace, error)
RunFunction(string, string, []string) error
Remove(string) error
Expand Down
71 changes: 71 additions & 0 deletions internal/cmd/internal/completion/mock_Decorator_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/cmd/mock_completionManager_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion internal/cmd/mock_workspaceManager_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func newRootCmd() *cobra.Command {
rootCmd.AddCommand(envCmd)
rootCmd.AddCommand(globalCmd)
rootCmd.AddCommand(newSetupCmd(w))
rootCmd.AddCommand(newFixCmd(w))

Check warning on line 97 in internal/cmd/root.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/root.go#L97

Added line #L97 was not covered by tests
rootCmd.AddCommand(newCreateCmd(w, dirCompMgr))
rootCmd.AddCommand(newEditCmd(w, wksCompMgr))
rootCmd.AddCommand(newListCmd(w))
Expand Down
2 changes: 1 addition & 1 deletion internal/workspace/mock_Commander_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions internal/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,35 @@ func (s WorkspaceManager) Migrate() error {
return nil
}

func (s WorkspaceManager) Fix() error {
entries, err := os.ReadDir(s.getWorkspacesDir())
if os.IsNotExist(err) {
return err

Check warning on line 331 in internal/workspace/workspace.go

View check run for this annotation

Codecov / codecov/patch

internal/workspace/workspace.go#L331

Added line #L331 was not covered by tests
}
err = os.MkdirAll(s.getWorkspacesDir(), 0o777)
if err != nil {
return err

Check warning on line 335 in internal/workspace/workspace.go

View check run for this annotation

Codecov / codecov/patch

internal/workspace/workspace.go#L335

Added line #L335 was not covered by tests
}
for _, e := range entries {
if !e.IsDir() {
continue

Check warning on line 339 in internal/workspace/workspace.go

View check run for this annotation

Codecov / codecov/patch

internal/workspace/workspace.go#L339

Added line #L339 was not covered by tests
}
err = s.createWorkspaceFolder(e.Name())
if err != nil {
return err

Check warning on line 343 in internal/workspace/workspace.go

View check run for this annotation

Codecov / codecov/patch

internal/workspace/workspace.go#L343

Added line #L343 was not covered by tests
}
err = s.createFile(s.resolveFunctionFile(e.Name()))
if err != nil {
return err

Check warning on line 347 in internal/workspace/workspace.go

View check run for this annotation

Codecov / codecov/patch

internal/workspace/workspace.go#L347

Added line #L347 was not covered by tests
}
err = s.createFile(s.resolveEnvFile(e.Name(), defaultEnv))
if err != nil {
return err

Check warning on line 351 in internal/workspace/workspace.go

View check run for this annotation

Codecov / codecov/patch

internal/workspace/workspace.go#L351

Added line #L351 was not covered by tests
}
}
return nil
}

func (s WorkspaceManager) appendLoadStatement(name string, env string, functionAndArgs []string) []string {
data := []string{}
data = append(data, s.CreateEnvVariableStatement(fmt.Sprintf("%s_NAME", envVariablePrefix), name))
Expand Down
57 changes: 57 additions & 0 deletions internal/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,63 @@ func TestRemove(t *testing.T) {
}
}

func TestFix(t *testing.T) {
config := &config{}
project := &project{}
type scenario struct {
name string
setup func(*testing.T, WorkspaceManager)
test func(*testing.T, error)
}
scenarios := []scenario{
{
"Fix a failing workspace",
func(*testing.T, WorkspaceManager) {
path := config.getPath(t)
err := os.RemoveAll(path + "/workspaces/test/envs")
assert.NoError(t, err)
err = os.Remove(path + "/workspaces/test/functions/functions.bash")
assert.NoError(t, err)
err = os.RemoveAll(path + "/workspaces/front/envs")
assert.NoError(t, err)
err = os.Remove(path + "/workspaces/front/functions/functions.bash")
assert.NoError(t, err)
},
func(t *testing.T, e error) {
assert.NoError(t, e)
path := config.getPath(t)
_, err := os.Stat(path + "/workspaces/test/functions/functions.bash")
assert.NoError(t, err)
_, err = os.Stat(path + "/workspaces/test/envs/default.bash")
assert.NoError(t, err)
_, err = os.Stat(path + "/workspaces/front/functions/functions.bash")
assert.NoError(t, err)
_, err = os.Stat(path + "/workspaces/front/envs/default.bash")
assert.NoError(t, err)
},
},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
os.RemoveAll(config.getPath(t))
w, err := NewWorkspaceManager(WithEditor("emacs", "emacs"), WithShellPath("/bin/bash"), WithConfigPath(config.getPath(t)))
assert.NoError(t, err)
err = w.Create("test", project.getPath(t))
assert.NoError(t, err)
err = w.CreateEnv("test", "prod")
assert.NoError(t, err)
err = w.CreateEnv("test", "dev")
assert.NoError(t, err)
err = w.Create("front", project.getPath(t))
assert.NoError(t, err)
err = w.CreateEnv("front", "dev")
assert.NoError(t, err)
s.setup(t, w)
s.test(t, w.Fix())
})
}
}

func TestSetConfig(t *testing.T) {
config := &config{}
project := &project{}
Expand Down

0 comments on commit 40c80ea

Please sign in to comment.