Skip to content

Commit

Permalink
Merge pull request #3493 from ActiveState/mitchell/dx-3011
Browse files Browse the repository at this point in the history
Added `ACTIVESTATE_CLI_IGNORE_ENV` for ignoring env vars during runtime setup.
  • Loading branch information
mitchell-as authored Sep 23, 2024
2 parents fa1ce5f + 75fd51c commit 5515ef4
Show file tree
Hide file tree
Showing 4 changed files with 49 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 @@ -490,3 +490,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
16 changes: 16 additions & 0 deletions pkg/runtime/internal/envdef/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"path/filepath"
"strings"

"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/sliceutils"
"github.com/thoas/go-funk"

"github.com/ActiveState/cli/internal/fileutils"
Expand Down Expand Up @@ -125,6 +127,20 @@ func NewEnvironmentDefinition(fp string) (*EnvironmentDefinition, error) {
if err != nil {
return nil, errs.Wrap(err, "could not unmarshal environment definition file: %s", fp)
}

if ignores := os.Getenv(constants.IgnoreEnvEnvVarName); ignores != "" {
ignore := make(map[string]bool)
for _, name := range strings.Split(ignores, ",") {
ignore[name] = true
}

// Remove any environment variables to ignore.
ed.Env = sliceutils.Filter(ed.Env, func(e EnvironmentVariable) bool {
_, exists := ignore[e.Name]
return !exists
})
}

return ed, nil
}

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 5515ef4

Please sign in to comment.