Skip to content

Commit

Permalink
Added ACTIVESTATE_CLI_IGNORE_ENV for ignoring env vars during runti…
Browse files Browse the repository at this point in the history
…me setup.

For example, PYTHONPATH.
  • Loading branch information
mitchell-as committed Sep 17, 2024
1 parent 1373a50 commit d893ec6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,6 @@ const PlatformPrivateNamespace = "private"

// OverrideShellEnvVarName is the environment variable to set when overriding the shell for shell detection.
const OverrideShellEnvVarName = "ACTIVESTATE_CLI_SHELL_OVERRIDE"

// IgnoreEnvEnvVarName is the environment variable to set for skipping specific environment variables during runtime setup.
const IgnoreEnvEnvVarName = "ACTIVESTATE_CLI_IGNORE_ENV"
1 change: 1 addition & 0 deletions internal/testhelpers/tagsuite/tagsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
DeleteProjects = "delete-uuid-projects"
Deploy = "deploy"
Edit = "edit"
Environment = "environment"
Errors = "error"
Events = "events"
Exec = "exec"
Expand Down
10 changes: 10 additions & 0 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"maps"
"os"
"path/filepath"
"strings"

"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/logging"
Expand Down Expand Up @@ -157,6 +159,14 @@ func (r *Runtime) getEnv(inherit bool) (map[string]string, map[string]string, er
return empty, empty, errs.Wrap(err, "Failed to get environment variables")
}

if ignores := os.Getenv(constants.IgnoreEnvEnvVarName); ignores != "" {
for _, name := range strings.Split(ignores, ",") {
if _, exists := vars[name]; exists {
delete(vars, name)
}
}
}

executorsPath := ExecutorsPath(r.path)

execVars := maps.Clone(vars)
Expand Down
29 changes: 29 additions & 0 deletions test/integration/runtime_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/testhelpers/e2e"
"github.com/ActiveState/cli/internal/testhelpers/osutil"
Expand Down Expand Up @@ -179,6 +180,34 @@ func (suite *RuntimeIntegrationTestSuite) TestBuildInProgress() {
cp.ExpectExitCode(0)
}

func (suite *RuntimeIntegrationTestSuite) TestIgnoreEnvironmentVars() {
suite.OnlyRunForTags(tagsuite.Environment)
ts := e2e.New(suite.T(), false)
defer ts.Close()

cp := ts.Spawn("checkout", "ActiveState-CLI/small-python", ".")
cp.Expect("Checked out project", e2e.RuntimeSourcingTimeoutOpt)
cp.ExpectExitCode(0)

pythonPath := "my/path"

cp = ts.SpawnWithOpts(
e2e.OptArgs("exec", "python3", "--", "-c", `print(__import__("os").environ["PYTHONPATH"])`),
e2e.OptAppendEnv("PYTHONPATH="+pythonPath),
)
cp.ExpectExitCode(0)
suite.Assert().NotContains(cp.Snapshot(), pythonPath)

cp = ts.SpawnWithOpts(
e2e.OptArgs("exec", "python3", "--", "-c", `print(__import__("os").environ["PYTHONPATH"])`),
e2e.OptAppendEnv(
"PYTHONPATH="+pythonPath,
constants.IgnoreEnvEnvVarName+"=PYTHONPATH",
))
cp.Expect(pythonPath)
cp.ExpectExitCode(0)
}

func TestRuntimeIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(RuntimeIntegrationTestSuite))
}

0 comments on commit d893ec6

Please sign in to comment.