From e7a7559b10dc8ffffc4c2e7a087876efc4c90691 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Wed, 17 Jul 2024 09:29:40 +0200 Subject: [PATCH] Check the default env file exists --- internal/workspace/workspace.go | 13 +++- internal/workspace/workspace_test.go | 88 ++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/internal/workspace/workspace.go b/internal/workspace/workspace.go index 2c1e3a4..07f6b56 100644 --- a/internal/workspace/workspace.go +++ b/internal/workspace/workspace.go @@ -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 + } 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 diff --git a/internal/workspace/workspace_test.go b/internal/workspace/workspace_test.go index 022f977..1884779 100644 --- a/internal/workspace/workspace_test.go +++ b/internal/workspace/workspace_test.go @@ -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) + }) + } +}