From 8748bb0b1a758b2fa940014762d18a191a9e603c Mon Sep 17 00:00:00 2001 From: Tyler McGoffin Date: Fri, 21 Feb 2025 09:33:19 -0800 Subject: [PATCH] Add testing to GlobWindowsPaths (#2) --- pkg/cmd/gist/create/create.go | 11 ++- pkg/cmd/release/shared/upload.go | 9 +- pkg/cmdutil/args.go | 10 +-- pkg/cmdutil/args_test.go | 150 ++++++++++++++++++++++++++++++- 4 files changed, 167 insertions(+), 13 deletions(-) diff --git a/pkg/cmd/gist/create/create.go b/pkg/cmd/gist/create/create.go index ad697e2ce56..b186524c57d 100644 --- a/pkg/cmd/gist/create/create.go +++ b/pkg/cmd/gist/create/create.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "regexp" + "runtime" "sort" "strings" @@ -102,9 +103,13 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co } func createRun(opts *CreateOptions) error { - filenames, err := cmdutil.GlobWindowsPaths(opts.Filenames) - if err != nil { - return err + filenames := opts.Filenames + if runtime.GOOS == "windows" { + globbedNames, err := cmdutil.GlobWindowsPaths(opts.Filenames) + if err != nil { + return err + } + filenames = globbedNames } if len(filenames) == 0 { diff --git a/pkg/cmd/release/shared/upload.go b/pkg/cmd/release/shared/upload.go index 159f86e2377..1f3570b59f6 100644 --- a/pkg/cmd/release/shared/upload.go +++ b/pkg/cmd/release/shared/upload.go @@ -10,6 +10,7 @@ import ( "net/url" "os" "path" + "runtime" "strings" "time" @@ -37,9 +38,11 @@ type AssetForUpload struct { } func AssetsFromArgs(args []string) (assets []*AssetForUpload, err error) { - args, err = cmdutil.GlobWindowsPaths(args) - if err != nil { - return nil, err + if runtime.GOOS == "windows" { + args, err = cmdutil.GlobWindowsPaths(args) + if err != nil { + return nil, err + } } for _, arg := range args { var label string diff --git a/pkg/cmdutil/args.go b/pkg/cmdutil/args.go index 9dfbce2b1ad..196621b823c 100644 --- a/pkg/cmdutil/args.go +++ b/pkg/cmdutil/args.go @@ -3,7 +3,6 @@ package cmdutil import ( "fmt" "path/filepath" - "runtime" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -61,14 +60,12 @@ func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error { } func GlobWindowsPaths(patterns []string) ([]string, error) { - if runtime.GOOS != "windows" { - return patterns, nil - } - var expansions []string + expansions := []string{} + for _, pattern := range patterns { matches, err := filepath.Glob(pattern) if err != nil { - return nil, fmt.Errorf("%s: %s", pattern, err) + return nil, fmt.Errorf("%s: %v", pattern, err) } if len(matches) > 0 { expansions = append(expansions, matches...) @@ -76,5 +73,6 @@ func GlobWindowsPaths(patterns []string) ([]string, error) { expansions = append(expansions, pattern) } } + return expansions, nil } diff --git a/pkg/cmdutil/args_test.go b/pkg/cmdutil/args_test.go index db0b9651089..60a37c29532 100644 --- a/pkg/cmdutil/args_test.go +++ b/pkg/cmdutil/args_test.go @@ -1,6 +1,13 @@ package cmdutil -import "testing" +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) func TestMinimumArgs(t *testing.T) { tests := []struct { @@ -48,3 +55,144 @@ func TestMinimumNs_with_error(t *testing.T) { } } } + +func TestGlobWindowsPaths(t *testing.T) { + tests := []struct { + name string + os string + patterns []string + wantOut []string + wantErr error + }{ + { + name: "When no patterns are passed, return an empty slice", + patterns: []string{}, + wantOut: []string{}, + wantErr: nil, + }, + { + name: "When no files match, it returns an empty expansions array, it returns the unmatched patterns", + patterns: []string{"foo", "bar"}, + wantOut: []string{"foo", "bar"}, + wantErr: nil, + }, + { + name: "When a single pattern, '*.txt' is passed with one match, it returns that match", + patterns: []string{ + "*.txt", + }, + wantOut: []string{ + "rootFile.txt", + }, + wantErr: nil, + }, + { + name: "When a single pattern, '*/*.txt' is passed with multiple matches, it returns those matches", + patterns: []string{ + "*/*.txt", + }, + wantOut: []string{ + "subDir1/subDir1_file.txt", + "subDir2/subDir2_file.txt", + }, + wantErr: nil, + }, + { + name: "When multiple patterns, '*/*.txt' and '*/*.go', are passed with multiple matches, it returns those matches", + patterns: []string{ + "*/*.txt", + "*/*.go", + }, + wantOut: []string{ + "subDir1/subDir1_file.txt", + "subDir2/subDir2_file.txt", + "subDir2/subDir2_file.go", + }, + wantErr: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cleanupFn := createTestDir(t) + defer cleanupFn() + + got, err := GlobWindowsPaths(tt.patterns) + if tt.wantErr != nil { + assert.EqualError(t, err, tt.wantErr.Error()) + } else { + require.NoError(t, err) + } + assert.Equal(t, tt.wantOut, got) + }) + } +} + +// Creates a temporary directory with the structure below. Returns +// a cleanup function that will remove the directory and all of its +// contents. The cleanup function should be wrapped in a defer statement. +// +// | root +// |-- rootFile.txt +// |-- subDir1 +// | |-- subDir1_file.txt +// | +// |-- subDir2 +// |-- subDir2_file.go +// |-- subDir2_file.txt +func createTestDir(t *testing.T) (cleanupFn func()) { + t.Helper() + // Make Directories + rootDir := t.TempDir() + + // Move workspace to temporary directory + cwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + err = os.Chdir(rootDir) + if err != nil { + t.Fatal(err) + } + + // Make subdirectories + err = os.Mkdir(filepath.Join(rootDir, "subDir1"), 0755) + if err != nil { + t.Fatal(err) + } + + err = os.Mkdir(filepath.Join(rootDir, "subDir2"), 0755) + if err != nil { + t.Fatal(err) + } + + // Make Files + err = os.WriteFile(filepath.Join(rootDir, "rootFile.txt"), []byte(""), 0644) + if err != nil { + t.Fatal(err) + } + + err = os.WriteFile(filepath.Join(rootDir, "subDir1", "subDir1_file.txt"), []byte(""), 0o644) + if err != nil { + t.Fatal(err) + } + + err = os.WriteFile(filepath.Join(rootDir, "subDir2", "subDir2_file.go"), []byte(""), 0o644) + if err != nil { + t.Fatal(err) + } + + err = os.WriteFile(filepath.Join(rootDir, "subDir2", "subDir2_file.txt"), []byte(""), 0o644) + if err != nil { + t.Fatal(err) + } + + cleanupFn = func() { + os.RemoveAll(rootDir) + err = os.Chdir(cwd) + if err != nil { + t.Fatal(err) + } + } + return cleanupFn +}