Skip to content

Commit

Permalink
The current project should fall back on the activated one in a shell …
Browse files Browse the repository at this point in the history
…if applicable.
  • Loading branch information
mitchell-as committed Aug 23, 2023
1 parent dcf8cc5 commit 1de235a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 1 addition & 3 deletions internal/virtualenvironment/virtualenvironment.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ func (v *VirtualEnvironment) GetEnv(inherit bool, useExecutors bool, projectDir,
}

if projectDir != "" {
configFile := filepath.Join(projectDir, constants.ConfigFileName)

envMap[constants.ActivatedStateEnvVarName] = projectDir
envMap[constants.ProjectEnvVarName] = configFile
envMap[constants.ActivatedStateIDEnvVarName] = v.activationID
envMap[constants.ActivatedStateNamespaceEnvVarName] = namespace

// Get project from explicitly defined configuration file
configFile := filepath.Join(projectDir, constants.ConfigFileName)
pj, err := project.Parse(configFile)
if err != nil {
return envMap, locale.WrapError(err, "err_parse_project", "", configFile)
Expand Down
13 changes: 13 additions & 0 deletions pkg/projectfile/projectfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ func GetProjectFilePath() (string, error) {
lookup := []func() (string, error){
getProjectFilePathFromEnv,
getProjectFilePathFromWd,
getProjectFilePathFromShell,
getProjectFilePathFromDefault,
}
for _, getProjectFilePath := range lookup {
Expand Down Expand Up @@ -792,6 +793,18 @@ func getProjectFilePathFromWd() (string, error) {
return path, nil
}

func getProjectFilePathFromShell() (string, error) {
projectFilePath := os.Getenv(constants.ActivatedStateEnvVarName)
if projectFilePath != "" {
configFile := filepath.Join(projectFilePath, constants.ConfigFileName)
if fileutils.FileExists(configFile) {
return configFile, nil
}
return "", &ErrorNoProject{locale.NewInputError("err_shell_no_projectfile", "The project for the shell you are in no longer exists.")}
}
return "", nil
}

func getProjectFilePathFromDefault() (_ string, rerr error) {
cfg, err := config.New()
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/projectfile/projectfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ func TestGetProjectFilePath(t *testing.T) {
assert.Equal(t, expectedPath, configPath, "Project path is properly detected using the ProjectEnvVarName")

os.Unsetenv(constants.ProjectEnvVarName)

os.Setenv(constants.ActivatedStateEnvVarName, "/some/path")
defer os.Unsetenv(constants.ActivatedStateEnvVarName)
configPath, err = GetProjectFilePath()
require.Nil(t, err)
assert.Equal(t, expectedPath, configPath, "Project path prioritizes working directory project over activated project")
os.Unsetenv(constants.ActivatedStateEnvVarName)

cfg, err := config.New()
require.NoError(t, err)
defer func() { require.NoError(t, cfg.Close()) }()
Expand All @@ -246,6 +254,11 @@ func TestGetProjectFilePath(t *testing.T) {
os.Chdir(tmpDir)
_, err = GetProjectFilePath()
assert.Error(t, err, "GetProjectFilePath should fail")
os.Setenv(constants.ActivatedStateEnvVarName, filepath.Dir(expectedPath))
configPath, err = GetProjectFilePath()
require.Nil(t, err)
assert.Equal(t, expectedPath, configPath, "Project path falls back on activated project path")
os.Unsetenv(constants.ActivatedStateEnvVarName)
cfg.Set(constants.GlobalDefaultPrefname, expectedPath)
configPath, err = GetProjectFilePath()
assert.NoError(t, err, "GetProjectFilePath should succeed")
Expand Down

0 comments on commit 1de235a

Please sign in to comment.