Add testing to GlobWindowsPaths (#2)
This commit is contained in:
parent
f0ac3bcf3f
commit
8748bb0b1a
4 changed files with 167 additions and 13 deletions
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -102,9 +103,13 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRun(opts *CreateOptions) error {
|
func createRun(opts *CreateOptions) error {
|
||||||
filenames, err := cmdutil.GlobWindowsPaths(opts.Filenames)
|
filenames := opts.Filenames
|
||||||
if err != nil {
|
if runtime.GOOS == "windows" {
|
||||||
return err
|
globbedNames, err := cmdutil.GlobWindowsPaths(opts.Filenames)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
filenames = globbedNames
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(filenames) == 0 {
|
if len(filenames) == 0 {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -37,9 +38,11 @@ type AssetForUpload struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func AssetsFromArgs(args []string) (assets []*AssetForUpload, err error) {
|
func AssetsFromArgs(args []string) (assets []*AssetForUpload, err error) {
|
||||||
args, err = cmdutil.GlobWindowsPaths(args)
|
if runtime.GOOS == "windows" {
|
||||||
if err != nil {
|
args, err = cmdutil.GlobWindowsPaths(args)
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
var label string
|
var label string
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package cmdutil
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
|
@ -61,14 +60,12 @@ func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GlobWindowsPaths(patterns []string) ([]string, error) {
|
func GlobWindowsPaths(patterns []string) ([]string, error) {
|
||||||
if runtime.GOOS != "windows" {
|
expansions := []string{}
|
||||||
return patterns, nil
|
|
||||||
}
|
|
||||||
var expansions []string
|
|
||||||
for _, pattern := range patterns {
|
for _, pattern := range patterns {
|
||||||
matches, err := filepath.Glob(pattern)
|
matches, err := filepath.Glob(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %s", pattern, err)
|
return nil, fmt.Errorf("%s: %v", pattern, err)
|
||||||
}
|
}
|
||||||
if len(matches) > 0 {
|
if len(matches) > 0 {
|
||||||
expansions = append(expansions, matches...)
|
expansions = append(expansions, matches...)
|
||||||
|
|
@ -76,5 +73,6 @@ func GlobWindowsPaths(patterns []string) ([]string, error) {
|
||||||
expansions = append(expansions, pattern)
|
expansions = append(expansions, pattern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return expansions, nil
|
return expansions, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,13 @@
|
||||||
package cmdutil
|
package cmdutil
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
func TestMinimumArgs(t *testing.T) {
|
func TestMinimumArgs(t *testing.T) {
|
||||||
tests := []struct {
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue