Skip to content

Commit

Permalink
Merge pull request #2936 from ActiveState/mitchell/dx-2353
Browse files Browse the repository at this point in the history
When determining the current project, prefer the activated project before anything else.
  • Loading branch information
mitchell-as authored Dec 7, 2023
2 parents b51523c + 71785d6 commit 26b7b08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/projectfile/projectfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,21 @@ func GetProjectFilePath() (string, error) {
}

func getProjectFilePathFromEnv() (string, error) {
projectFilePath := os.Getenv(constants.ProjectEnvVarName)
var projectFilePath string

if activatedProjectDirPath := os.Getenv(constants.ActivatedStateEnvVarName); activatedProjectDirPath != "" {
projectFilePath = filepath.Join(activatedProjectDirPath, constants.ConfigFileName)
} else {
projectFilePath = os.Getenv(constants.ProjectEnvVarName)
}

if projectFilePath != "" {
if fileutils.FileExists(projectFilePath) {
return projectFilePath, nil
}
return "", &ErrorNoProjectFromEnv{locale.NewInputError("err_project_env_file_not_exist", "", projectFilePath)}
}

return "", nil
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/projectfile/projectfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func TestGetProjectFilePath(t *testing.T) {
require.Nil(t, err)
expectedPath := filepath.Join(root, "pkg", "projectfile", "testdata", constants.ConfigFileName)
assert.Equal(t, expectedPath, configPath, "Project path is properly detected")
os.Chdir(cwd) // restore

defer os.Unsetenv(constants.ProjectEnvVarName)

Expand Down Expand Up @@ -251,6 +252,14 @@ func TestGetProjectFilePath(t *testing.T) {
configPath, err = GetProjectFilePath()
assert.NoError(t, err, "GetProjectFilePath should succeed")
assert.Equal(t, expectedPath, configPath, "Project path is properly detected using default path from config")

// The activestate.yaml for an activated project should be used no matter what.
defer os.Unsetenv(constants.ActivatedStateEnvVarName)
os.Setenv(constants.ActivatedStateEnvVarName, filepath.Dir(expectedPath))
configPath, err = GetProjectFilePath()
require.Nil(t, err)
assert.Equal(t, expectedPath, configPath, "Project path is properly detected using the ActivatedStateEnvVarName")
os.Unsetenv(constants.ActivatedStateEnvVarName)
}

// TestGet the config
Expand Down

0 comments on commit 26b7b08

Please sign in to comment.