Add testing to GlobWindowsPaths (#2)

This commit is contained in:
Tyler McGoffin 2025-02-21 09:33:19 -08:00 committed by GitHub
parent f0ac3bcf3f
commit 8748bb0b1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 167 additions and 13 deletions

View file

@ -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 {

View file

@ -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

View file

@ -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
}

View file

@ -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
}