Skip to content

Commit

Permalink
Set HOME env var for nested shell notification tests.
Browse files Browse the repository at this point in the history
This allows for testing bare 'bash' and 'zsh' commands.

Also revert fish to zsh test change.
  • Loading branch information
mitchell-as committed Aug 18, 2023
1 parent 39efdf8 commit b065046
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
18 changes: 9 additions & 9 deletions internal/subshell/sscommon/rcfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ func TestWriteRcFile(t *testing.T) {
env map[string]string
}

zsh := fmt.Sprintf(
`export PATH="foo:$PATH"
if [[ ! -z "$%s" && -f "$%s/%s" ]]; then
fish := fmt.Sprintf(
`set -xg PATH "foo:$PATH"
if test ! -z "$%s"; test -f "$%s/%s"
echo "State Tool is operating on project $%s, located at $%s"
fi`,
end`,
constants.ActivatedStateEnvVarName,
constants.ActivatedStateEnvVarName,
constants.ConfigFileName,
constants.ActivatedStateNamespaceEnvVarName,
constants.ActivatedStateEnvVarName)
if runtime.GOOS == "windows" {
zsh = strings.ReplaceAll(zsh, "\n", "\r\n")
fish = strings.ReplaceAll(fish, "\n", "\r\n")
}

tests := []struct {
Expand All @@ -68,26 +68,26 @@ fi`,
{
"Write RC to empty file",
args{
"zshrc_append.sh",
"fishrc_append.fish",
fakeFileWithContents("", "", ""),
map[string]string{
"PATH": "foo",
},
},
nil,
fakeContents("", zsh, ""),
fakeContents("", fish, ""),
},
{
"Write RC update",
args{
"zshrc_append.sh",
"fishrc_append.fish",
fakeFileWithContents("before", "SOMETHING ELSE", "after"),
map[string]string{
"PATH": "foo",
},
},
nil,
fakeContents(strings.Join([]string{"before", "after"}, fileutils.LineEnd), zsh, ""),
fakeContents(strings.Join([]string{"before", "after"}, fileutils.LineEnd), fish, ""),
},
}
for _, tt := range tests {
Expand Down
20 changes: 7 additions & 13 deletions test/integration/shell_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"testing"

"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/subshell"
"github.com/ActiveState/cli/internal/subshell/bash"
"github.com/ActiveState/cli/internal/subshell/sscommon"
"github.com/ActiveState/cli/internal/subshell/zsh"
"github.com/ActiveState/cli/internal/testhelpers/e2e"
Expand Down Expand Up @@ -289,12 +289,15 @@ func (suite *ShellIntegrationTestSuite) TestNestedShellNotification() {
cfg, err := config.New()
suite.Require().NoError(err)

os.Setenv(constants.HomeEnvVarName, ts.Dirs.HomeDir)
defer func() { os.Unsetenv(constants.HomeEnvVarName) }()
ss := subshell.New(cfg)
err = subshell.ConfigureAvailableShells(ss, cfg, nil, sscommon.InstallID, true) // mimic installer
suite.Require().NoError(err)

rcFile, err := ss.RcFile()
suite.Require().NoError(err)
suite.Require().Equal(filepath.Dir(rcFile), ts.Dirs.HomeDir, "rc file not in test suite homedir")
suite.Require().FileExists(rcFile)
suite.Require().Contains(string(fileutils.ReadFileUnsafe(rcFile)), "State Tool is operating on project")

Expand All @@ -304,21 +307,12 @@ func (suite *ShellIntegrationTestSuite) TestNestedShellNotification() {

cp = ts.SpawnWithOpts(
e2e.WithArgs("shell", "small-python"),
e2e.AppendEnv("ACTIVESTATE_CLI_DISABLE_RUNTIME=false"),
)
e2e.AppendEnv("ACTIVESTATE_CLI_DISABLE_RUNTIME=false"))
cp.Expect("Activated")
suite.Assert().NotContains(cp.TrimmedSnapshot(), "State Tool is operating on project")
cp.SendLine(fmt.Sprintf(`export HOME="%s"`, ts.Dirs.HomeDir)) // some shells do not forward this

binary := ss.Binary() // platform-specific shell (zsh on macOS, bash on Linux, etc.)
// Cannot run bare binary because it will not load the rcFile in ts.Dirs.HomeDir.
// Instead, tell the shell to load rcFile. This is not needed in a non-test environment.
switch ss.Shell() {
case bash.Name:
binary = fmt.Sprintf("%s --rcfile %s", binary, rcFile)
case zsh.Name:
binary = fmt.Sprintf("ZDOTDIR=%s %s", filepath.Dir(rcFile), binary)
}
cp.SendLine(binary)
cp.SendLine(ss.Binary()) // platform-specific shell (zsh on macOS, bash on Linux, etc.)
cp.ExpectLongString("State Tool is operating on project ActiveState-CLI/small-python")
cp.SendLine("exit") // subshell within a subshell
cp.SendLine("exit")
Expand Down

0 comments on commit b065046

Please sign in to comment.