Skip to content

test(cli): Fix TestTemplatePush/UseWorkingDir bad use of chdir #7160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ func (pf *templateUploadFlags) option() clibase.Option {
}
}

func (pf *templateUploadFlags) setWorkdir(wd string) {
if wd == "" {
return
}
if pf.directory == "" || pf.directory == "." {
pf.directory = wd
} else if !filepath.IsAbs(pf.directory) {
pf.directory = filepath.Join(wd, pf.directory)
}
}

func (pf *templateUploadFlags) stdin() bool {
return pf.directory == "-"
}
Expand Down Expand Up @@ -98,6 +109,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
var (
versionName string
provisioner string
workdir string
parameterFile string
variablesFile string
variables []string
Expand All @@ -114,6 +126,8 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
uploadFlags.setWorkdir(workdir)

organization, err := CurrentOrganization(inv, client)
if err != nil {
return err
Expand Down Expand Up @@ -174,11 +188,18 @@ func (r *RootCmd) templatePush() *clibase.Cmd {

cmd.Options = clibase.OptionSet{
{
Flag: "test.provisioner",
FlagShorthand: "p",
Description: "Customize the provisioner backend.",
Default: "terraform",
Value: clibase.StringOf(&provisioner),
Flag: "test.provisioner",
Description: "Customize the provisioner backend.",
Default: "terraform",
Value: clibase.StringOf(&provisioner),
// This is for testing!
Hidden: true,
},
{
Flag: "test.workdir",
Description: "Customize the working directory.",
Default: "",
Value: clibase.StringOf(&workdir),
// This is for testing!
Hidden: true,
},
Expand Down
10 changes: 3 additions & 7 deletions cli/templatepush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ func TestTemplatePush(t *testing.T) {
// This test modifies the working directory.
//nolint:paralleltest
t.Run("UseWorkingDir", func(t *testing.T) {
t.Parallel()

if runtime.GOOS == "windows" {
t.Skip(`On Windows this test flakes with: "The process cannot access the file because it is being used by another process"`)
}
Expand All @@ -179,15 +181,9 @@ func TestTemplatePush(t *testing.T) {
r.Name = filepath.Base(source)
})

oldDir, err := os.Getwd()
require.NoError(t, err)

os.Chdir(source)
defer os.Chdir(oldDir)

// Don't pass the name of the template, it should use the
// directory of the source.
inv, root := clitest.New(t, "templates", "push", "--test.provisioner", string(database.ProvisionerTypeEcho))
inv, root := clitest.New(t, "templates", "push", "--test.provisioner", string(database.ProvisionerTypeEcho), "--test.workdir", source)
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)

Expand Down