Skip to content

Commit

Permalink
Only export to an activated shell activestate.yaml constants marked f…
Browse files Browse the repository at this point in the history
…or export.
  • Loading branch information
mitchell-as committed Dec 19, 2023
1 parent 32a62e2 commit 4a7ab1f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions internal/virtualenvironment/virtualenvironment.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (v *VirtualEnvironment) GetEnv(inherit bool, useExecutors bool, projectDir,
return envMap, locale.WrapError(err, "err_parse_project", "", configFile)
}
for _, constant := range pj.Constants() {
if !constant.Export() {
continue
}
v, err := constant.Value()
envMap[constant.Name()] = strings.Replace(v, "\n", `\n`, -1)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ func (c *Constant) Value() (string, error) {
return ExpandFromProject(c.constant.Value, c.project)
}

// Export returns whether or not the constant should be exported to an activated shell environment.
func (c *Constant) Export() bool { return c.constant.ConstantFields.Export }

// SecretScope defines the scope of a secret
type SecretScope string

Expand Down
1 change: 1 addition & 0 deletions pkg/projectfile/projectfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type Build map[string]string
// ConstantFields are the common fields for the Constant type. This is required
// for type composition related to its yaml.Unmarshaler implementation.
type ConstantFields struct {
Export bool `yaml:"export"`
Conditional Conditional `yaml:"if"`
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/projectfile/projectfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ func TestConstantStruct(t *testing.T) {
constant := Constant{}
dat := strings.TrimSpace(`
name: valueForName
value: valueForConstant`)
value: valueForConstant
export: true`)

err := yaml.Unmarshal([]byte(dat), &constant)
assert.Nil(t, err, "Should not throw an error")

assert.Equal(t, "valueForName", constant.Name, "Name should be set")
assert.Equal(t, "valueForConstant", constant.Value, "Constant should be set")
assert.Equal(t, true, constant.Export, "Constant export should be set")
}

func TestSecretStruct(t *testing.T) {
Expand Down Expand Up @@ -150,6 +152,7 @@ func TestParse(t *testing.T) {

assert.NotEmpty(t, project.Constants[0].Name, "Constant name should be set")
assert.NotEmpty(t, project.Constants[0].Value, "Constant value should be set")
assert.False(t, project.Constants[0].Export, "Constant export value should not be set")

assert.NotEmpty(t, project.Secrets.User[0].Name, "Variable name should be set")
assert.NotEmpty(t, project.Secrets.Project[0].Name, "Variable name should be set")
Expand Down
41 changes: 41 additions & 0 deletions test/integration/shell_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,47 @@ func (suite *ShellIntegrationTestSuite) TestProjectOrder() {
cp.ExpectNotExitCode(0)
}

func (suite *ShellIntegrationTestSuite) TestExportedConstants() {
suite.OnlyRunForTags(tagsuite.Shell)
ts := e2e.New(suite.T(), false)
defer ts.Close()

ts.PrepareProject("ActiveState-CLI/Perl-5.32", "a4762408-def6-41e4-b709-4cb548765005")
suite.Require().NoError(fileutils.AmendFile(filepath.Join(ts.Dirs.Work, constants.ConfigFileName), []byte(`
constants:
- name: foo
value: bar
- name: baz
value: quux
export: true`), fileutils.AmendByAppend))

cp := ts.SpawnWithOpts(
e2e.OptArgs("shell"),
e2e.OptAppendEnv(constants.DisableRuntime+"=false"),
)
cp.Expect("Activated", e2e.RuntimeSourcingTimeoutOpt)

cp.ExpectInput()
if runtime.GOOS == "windows" {
cp.SendLine("echo %foo%")
} else {
cp.SendLine("echo $foo")
}
cp.ExpectInput()
suite.Assert().NotContains(cp.Snapshot(), "bar", "variable 'foo' was exported, but should not have been")

if runtime.GOOS == "windows" {
cp.SendLine("echo %baz%")
} else {
cp.SendLine("echo $baz")
}
cp.Expect("quux")

cp.SendLine("exit")
cp.Expect("Deactivated")
cp.ExpectExit() // exit code varies depending on shell; just assert the shell exited
}

func TestShellIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(ShellIntegrationTestSuite))
}

0 comments on commit 4a7ab1f

Please sign in to comment.