Skip to content

Commit 7ba6ee9

Browse files
committed
Fix integration test not properly sandboxed
1 parent 2ad9338 commit 7ba6ee9

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

internal/testhelpers/e2e/session.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ type Session struct {
6666
ignoreLogErrors bool
6767
cfg *config.Instance
6868
cache keyCache
69-
cfg *config.Instance
7069
}
7170

7271
type keyCache map[string]string
@@ -769,13 +768,27 @@ func (s *Session) SetupRCFile() {
769768
if runtime.GOOS == "windows" {
770769
return
771770
}
772-
s.T.Setenv("HOME", s.Dirs.HomeDir)
773-
defer s.T.Setenv("HOME", os.Getenv("HOME"))
774-
775-
cfg, err := config.New()
776-
require.NoError(s.T, err)
771+
s.WithEnv(func() {
772+
s.SetupRCFileCustom(subshell.New(s.cfg))
773+
})
774+
}
777775

778-
s.SetupRCFileCustom(subshell.New(cfg))
776+
func (s *Session) WithEnv(do func()) {
777+
env := osutils.EnvSliceToMap(s.Env)
778+
for k, v := range env {
779+
_, exists := os.LookupEnv(k)
780+
if exists {
781+
defer s.T.Setenv(k, os.Getenv(k))
782+
} else {
783+
defer func() {
784+
if err := os.Unsetenv(k); err != nil {
785+
s.T.Logf("Failed to unset env var %s: %v", k, err)
786+
}
787+
}()
788+
}
789+
s.T.Setenv(k, v)
790+
}
791+
do()
779792
}
780793

781794
func (s *Session) SetupRCFileCustom(subshell subshell.SubShell) {
@@ -791,7 +804,11 @@ func (s *Session) SetupRCFileCustom(subshell subshell.SubShell) {
791804
} else {
792805
err = fileutils.Touch(filepath.Join(s.Dirs.HomeDir, filepath.Base(rcFile)))
793806
}
794-
require.NoError(s.T, err)
807+
require.NoError(s.T, err, errs.JoinMessage(err))
808+
}
809+
810+
func (s *Session) Config() *config.Instance {
811+
return s.cfg
795812
}
796813

797814
func (s *Session) SetConfig(key string, value interface{}) {

test/integration/shell_int_test.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,25 @@ func (suite *ShellIntegrationTestSuite) TestUseShellUpdates() {
193193
ts := e2e.New(suite.T(), false)
194194
defer ts.Close()
195195

196-
suite.SetupRCFile(ts)
196+
if runtime.GOOS != "windows" {
197+
ts.SetupRCFileCustom(&bash.SubShell{})
198+
ts.SetupRCFileCustom(&zsh.SubShell{})
199+
}
197200

198201
cp := ts.Spawn("checkout", "ActiveState-CLI/Empty")
199202
cp.Expect("Checked out project")
200203
cp.ExpectExitCode(0)
201204

202205
// Create a zsh RC file
203206
var zshRcFile string
204-
var err error
205-
if runtime.GOOS != "windows" {
206-
zsh := &zsh.SubShell{}
207-
zshRcFile, err = zsh.RcFile()
208-
suite.NoError(err)
209-
}
207+
ts.WithEnv(func() {
208+
var err error
209+
if runtime.GOOS != "windows" {
210+
zsh := &zsh.SubShell{}
211+
zshRcFile, err = zsh.RcFile()
212+
suite.NoError(err)
213+
}
214+
})
210215

211216
cp = ts.SpawnWithOpts(
212217
e2e.OptArgs("use", "ActiveState-CLI/Empty"),
@@ -216,13 +221,12 @@ func (suite *ShellIntegrationTestSuite) TestUseShellUpdates() {
216221
cp.ExpectExitCode(0)
217222

218223
// Ensure both bash and zsh RC files are updated
219-
cfg, err := config.New()
220-
suite.NoError(err)
221-
rcfile, err := subshell.New(cfg).RcFile()
224+
rcfile, err := subshell.New(ts.Config()).RcFile()
225+
rcfile = filepath.Join(ts.Dirs.HomeDir, filepath.Base(rcfile))
222226
if runtime.GOOS != "windows" && fileutils.FileExists(rcfile) {
223227
suite.NoError(err)
224-
suite.Contains(string(fileutils.ReadFileUnsafe(rcfile)), ts.Dirs.DefaultBin, "PATH does not have your project in it")
225-
suite.Contains(string(fileutils.ReadFileUnsafe(zshRcFile)), ts.Dirs.DefaultBin, "PATH does not have your project in it")
228+
suite.Contains(string(fileutils.ReadFileUnsafe(rcfile)), ts.Dirs.DefaultBin, rcfile+": PATH does not have your project in it")
229+
suite.Contains(string(fileutils.ReadFileUnsafe(zshRcFile)), ts.Dirs.DefaultBin, zshRcFile+": PATH does not have your project in it")
226230
}
227231
}
228232

@@ -237,15 +241,6 @@ func (suite *ShellIntegrationTestSuite) TestJSON() {
237241
AssertValidJSON(suite.T(), cp)
238242
}
239243

240-
func (suite *ShellIntegrationTestSuite) SetupRCFile(ts *e2e.Session) {
241-
if runtime.GOOS == "windows" {
242-
return
243-
}
244-
245-
ts.SetupRCFile()
246-
ts.SetupRCFileCustom(&zsh.SubShell{})
247-
}
248-
249244
func (suite *ShellIntegrationTestSuite) TestRuby() {
250245
suite.OnlyRunForTags(tagsuite.Shell)
251246
ts := e2e.New(suite.T(), false)

0 commit comments

Comments
 (0)