Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check the default env file exists #32

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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) {
assert.NoError(t, w.Remove("api"))
},
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) {
assert.NoError(t, os.WriteFile(configPath+"/workspaces/api/config.toml", []byte("path = '/home'"), 0o666))
},
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) {
assert.NoError(t, os.WriteFile(configPath+"/workspaces/api/config.toml", []byte("app = 'fish'"), 0o666))
},
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) {
assert.NoError(t, os.WriteFile(configPath+"/workspaces/api/config.toml", []byte("app = 'fish'\npath = '/home'"), 0o666))
},
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)
assert.NoError(t, w.Create("api", projectPath))
s.setup(t, projectPath, configPath, w)
workspace, err := w.getWorkspace("api")
s.test(t, projectPath, configPath, workspace, err)
})
}
}
Loading