Skip to content

Commit

Permalink
Merge #11804
Browse files Browse the repository at this point in the history
11804: cli/new: Ignore VCS directories r=abhinav a=abhinav

# Description

The following set of operations currently fail with the CLI.

    git init
    pulumi new

This is because `pulumi new` refuses to run inside
non-empty directories.

This changes `pulumi new` to ignore .git, .hg, and .bzr
files/directories when considering whether a directory is empty.

Resolves #11789

## Checklist

- [x] I have added tests that prove my fix is effective or that my feature works
- [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change


Co-authored-by: Abhinav Gupta <abhinav@pulumi.com>
  • Loading branch information
bors[bot] and abhinav committed Jan 9, 2023
2 parents 2957b6b + eac01fd commit 2b9aae0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: cli/new
description: Allow running inside new VCS repositories.
19 changes: 18 additions & 1 deletion pkg/cmd/pulumi/new.go
Expand Up @@ -506,14 +506,31 @@ func newNewCmd() *cobra.Command {
return cmd
}

// File or directory names that are considered invisible
// when considering whether a directory is empty.
var invisibleDirEntries = map[string]struct{}{
".git": {},
".hg": {},
".bzr": {},
}

// errorIfNotEmptyDirectory returns an error if path is not empty.
func errorIfNotEmptyDirectory(path string) error {
infos, err := os.ReadDir(path)
if err != nil {
return err
}

if len(infos) > 0 {
var nonEmpty bool
for _, info := range infos {
if _, ignore := invisibleDirEntries[info.Name()]; ignore {
continue
}
nonEmpty = true
break
}

if nonEmpty {
return fmt.Errorf("%s is not empty; "+
"rerun in an empty directory, pass the path to an empty directory to --dir, or use --force", path)
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/cmd/pulumi/new_test.go
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pulumi/pulumi/pkg/v3/backend"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

//nolint:paralleltest // changes directory for process
Expand Down Expand Up @@ -710,3 +711,68 @@ func TestSetFail(t *testing.T) {
})
}
}

func TestErrorIfNotEmptyDirectory(t *testing.T) {
t.Parallel()

tests := []struct {
desc string
files []string
dirs []string
ok bool
}{
{
desc: "empty",
ok: true,
},
{
desc: "non-empty",
files: []string{"foo"},
dirs: []string{"bar"},
ok: false,
},
{
desc: "empty git repository",
dirs: []string{".git"},
ok: true,
},
{
desc: "non-empty git repository",
dirs: []string{".git"},
files: []string{".gitignore"},
ok: false,
},
{
desc: "every VCS",
dirs: []string{".git", ".hg", ".bzr"},
ok: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()

path := t.TempDir()

// Fill test directory with files and directories
// requested by the test case.
for _, name := range tt.dirs {
err := os.MkdirAll(filepath.Join(path, name), 01700)
require.NoError(t, err)
}
for _, name := range tt.files {
err := os.WriteFile(filepath.Join(path, name), nil /* body */, 0600)
require.NoError(t, err)
}

err := errorIfNotEmptyDirectory(path)
if tt.ok {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}

0 comments on commit 2b9aae0

Please sign in to comment.