Skip to content

Commit

Permalink
Check the default env file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
antham committed Jul 25, 2024
1 parent 3193f53 commit 18a0266
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
13 changes: 10 additions & 3 deletions internal/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,19 +479,26 @@ func (s WorkspaceManager) getWorkspace(name string) (Workspace, error) {
return Workspace{}, errors.New("the workspace does not exist")
}
app, err := s.GetConfig(name, "app")
if err != nil {
if err != nil || app == "" {
return Workspace{}, errors.New("the config file of the workspace is corrupted")
}
path, err := s.GetConfig(name, "path")
if err != nil {
if err != nil || path == "" {
return Workspace{}, errors.New("the config file of the workspace is corrupted")
}
if app != s.shell {
return Workspace{}, fmt.Errorf(`the "%s" app is not supported for this workspace, it works with "%s"`, app, s.shell)
}
_, err = os.ReadFile(s.resolveEnvFile(name, defaultEnv))
if os.IsNotExist(err) {
return Workspace{}, errors.New("the default env file of the workspace is corrupted")
}
if err != nil {
return Workspace{}, err

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

View check run for this annotation

Codecov / codecov/patch

internal/workspace/workspace.go#L497

Added line #L497 was not covered by tests
}
content, err := os.ReadFile(s.resolveFunctionFile(name))
if os.IsNotExist(err) {
return Workspace{}, errors.New("the workspace does not exist")
return Workspace{}, errors.New("the function file of the workspace is corrupted")
}
if err != nil {
return Workspace{}, err
Expand Down
88 changes: 88 additions & 0 deletions internal/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,3 +699,91 @@ func TestBuildAliases(t *testing.T) {
})
}
}

func TestGetWorkspace(t *testing.T) {
type scenario struct {
name string
setup func(*testing.T, string, string, WorkspaceManager)
test func(*testing.T, string, string, Workspace, error)
}
scenarios := []scenario{
{
"Get an unexisting workspace",
func(t *testing.T, projectPath string, configPath string, w WorkspaceManager) {
w.Remove("api")

Check failure on line 713 in internal/workspace/workspace_test.go

View workflow job for this annotation

GitHub Actions / call-workflow / lint

Error return value of `w.Remove` is not checked (errcheck)
},
func(t *testing.T, projectPath string, configPath string, workspace Workspace, err error) {
assert.Error(t, err)
},
},
{
"Get a workspace with a missing app config",
func(t *testing.T, projectPath string, configPath string, w WorkspaceManager) {
os.WriteFile(configPath+"/workspaces/api/config.toml", []byte("path = '/home'"), 0o666)

Check failure on line 722 in internal/workspace/workspace_test.go

View workflow job for this annotation

GitHub Actions / call-workflow / lint

Error return value of `os.WriteFile` is not checked (errcheck)
},
func(t *testing.T, projectPath string, configPath string, workspace Workspace, err error) {
assert.Error(t, err)
},
},
{
"Get a workspace with a missing path config",
func(t *testing.T, projectPath string, configPath string, w WorkspaceManager) {
os.WriteFile(configPath+"/workspaces/api/config.toml", []byte("app = 'fish'"), 0o666)

Check failure on line 731 in internal/workspace/workspace_test.go

View workflow job for this annotation

GitHub Actions / call-workflow / lint

Error return value of `os.WriteFile` is not checked (errcheck)
},
func(t *testing.T, projectPath string, configPath string, workspace Workspace, err error) {
assert.Error(t, err)
},
},
{
"Get a workspace with an invalid shell",
func(t *testing.T, projectPath string, configPath string, w WorkspaceManager) {
os.WriteFile(configPath+"/workspaces/api/config.toml", []byte("app = 'fish'\npath = '/home'"), 0o666)

Check failure on line 740 in internal/workspace/workspace_test.go

View workflow job for this annotation

GitHub Actions / call-workflow / lint

Error return value of `os.WriteFile` is not checked (errcheck)
},
func(t *testing.T, projectPath string, configPath string, workspace Workspace, err error) {
assert.Error(t, err)
},
},
{
"Get a workspace with a missing default env file",
func(t *testing.T, projectPath string, configPath string, w WorkspaceManager) {
assert.NoError(t, os.Remove(configPath+"/workspaces/api/envs/default.bash"))
},
func(t *testing.T, projectPath string, configPath string, workspace Workspace, err error) {
assert.Error(t, err)
},
},
{
"Get a workspace with a missing function file",
func(t *testing.T, projectPath string, configPath string, w WorkspaceManager) {
assert.NoError(t, os.Remove(configPath+"/workspaces/api/functions/functions.bash"))
},
func(t *testing.T, projectPath string, configPath string, workspace Workspace, err error) {
assert.Error(t, err)
},
},
{
"Get a workspace",
func(t *testing.T, projectPath string, configPath string, w WorkspaceManager) {
},
func(t *testing.T, projectPath string, configPath string, workspace Workspace, err error) {
assert.NoError(t, err)
assert.Equal(t, configPath+"/workspaces/api", workspace.dir)
assert.Equal(t, "api", workspace.Name)
},
},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
config := &config{}
project := &project{}
w, err := NewWorkspaceManager(WithEditor("emacs", "emacs"), WithShellPath("/bin/bash"), WithConfigPath(config.getPath(t)))
assert.NoError(t, err)
projectPath := project.getPath(t)
configPath := config.getPath(t)
w.Create("api", projectPath)

Check failure on line 783 in internal/workspace/workspace_test.go

View workflow job for this annotation

GitHub Actions / call-workflow / lint

Error return value of `w.Create` is not checked (errcheck)
s.setup(t, projectPath, configPath, w)
workspace, err := w.getWorkspace("api")
s.test(t, projectPath, configPath, workspace, err)
})
}
}

0 comments on commit 18a0266

Please sign in to comment.