Skip to content

Commit 51841e9

Browse files
authored
test(cli): Fix TestTemplatePush/UseWorkingDir bad use of chdir (#7160)
1 parent 53f521a commit 51841e9

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

cli/templatepush.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ func (pf *templateUploadFlags) option() clibase.Option {
3232
}
3333
}
3434

35+
func (pf *templateUploadFlags) setWorkdir(wd string) {
36+
if wd == "" {
37+
return
38+
}
39+
if pf.directory == "" || pf.directory == "." {
40+
pf.directory = wd
41+
} else if !filepath.IsAbs(pf.directory) {
42+
pf.directory = filepath.Join(wd, pf.directory)
43+
}
44+
}
45+
3546
func (pf *templateUploadFlags) stdin() bool {
3647
return pf.directory == "-"
3748
}
@@ -98,6 +109,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
98109
var (
99110
versionName string
100111
provisioner string
112+
workdir string
101113
parameterFile string
102114
variablesFile string
103115
variables []string
@@ -114,6 +126,8 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
114126
r.InitClient(client),
115127
),
116128
Handler: func(inv *clibase.Invocation) error {
129+
uploadFlags.setWorkdir(workdir)
130+
117131
organization, err := CurrentOrganization(inv, client)
118132
if err != nil {
119133
return err
@@ -174,11 +188,18 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
174188

175189
cmd.Options = clibase.OptionSet{
176190
{
177-
Flag: "test.provisioner",
178-
FlagShorthand: "p",
179-
Description: "Customize the provisioner backend.",
180-
Default: "terraform",
181-
Value: clibase.StringOf(&provisioner),
191+
Flag: "test.provisioner",
192+
Description: "Customize the provisioner backend.",
193+
Default: "terraform",
194+
Value: clibase.StringOf(&provisioner),
195+
// This is for testing!
196+
Hidden: true,
197+
},
198+
{
199+
Flag: "test.workdir",
200+
Description: "Customize the working directory.",
201+
Default: "",
202+
Value: clibase.StringOf(&workdir),
182203
// This is for testing!
183204
Hidden: true,
184205
},

cli/templatepush_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ func TestTemplatePush(t *testing.T) {
159159
// This test modifies the working directory.
160160
//nolint:paralleltest
161161
t.Run("UseWorkingDir", func(t *testing.T) {
162+
t.Parallel()
163+
162164
if runtime.GOOS == "windows" {
163165
t.Skip(`On Windows this test flakes with: "The process cannot access the file because it is being used by another process"`)
164166
}
@@ -179,15 +181,9 @@ func TestTemplatePush(t *testing.T) {
179181
r.Name = filepath.Base(source)
180182
})
181183

182-
oldDir, err := os.Getwd()
183-
require.NoError(t, err)
184-
185-
os.Chdir(source)
186-
defer os.Chdir(oldDir)
187-
188184
// Don't pass the name of the template, it should use the
189185
// directory of the source.
190-
inv, root := clitest.New(t, "templates", "push", "--test.provisioner", string(database.ProvisionerTypeEcho))
186+
inv, root := clitest.New(t, "templates", "push", "--test.provisioner", string(database.ProvisionerTypeEcho), "--test.workdir", source)
191187
clitest.SetupConfig(t, client, root)
192188
pty := ptytest.New(t).Attach(inv)
193189

0 commit comments

Comments
 (0)