From 1205c1c8ada030fc6292307305a7c72e08c638a9 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Tue, 5 Jul 2022 22:03:58 +0000 Subject: [PATCH 01/25] Add support to expand glob patterns --- cmd/root.go | 36 +++++++++- cmd/root_test.go | 93 ++++++++++++++++++++++++-- docs/usage.md | 22 +++++- go.mod | 1 + go.sum | 2 + pkg/parser/parser.go | 3 - testdata/subdir1/good.yml | 2 + testdata/subdir1/subdir2/good.yml | 2 + testdata/subdir1/subdir2/whitelist.yml | 2 + testdata/subdir1/whitelist.yml | 2 + 10 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 testdata/subdir1/good.yml create mode 100644 testdata/subdir1/subdir2/good.yml create mode 100644 testdata/subdir1/subdir2/whitelist.yml create mode 100644 testdata/subdir1/whitelist.yml diff --git a/cmd/root.go b/cmd/root.go index e4822d03..061ea5d2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,6 +26,7 @@ import ( "errors" "fmt" "os" + "path/filepath" "runtime" "strings" "time" @@ -36,6 +37,7 @@ import ( "github.com/get-woke/woke/pkg/parser" "github.com/get-woke/woke/pkg/printer" + "github.com/bmatcuk/doublestar/v4" "github.com/mitchellh/go-homedir" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -121,7 +123,11 @@ func rootRunE(cmd *cobra.Command, args []string) error { return err } - findings := p.ParsePaths(print, parseArgs(args)...) + files, err := parseArgs(args) + if err != nil { + return err + } + findings := p.ParsePaths(print, files...) if exitOneOnFailure && findings > 0 { // We intentionally return an error if exitOneOnFailure is true, but don't want to show usage @@ -162,7 +168,10 @@ func GetRootCmd() cobra.Command { return *rootCmd } -func parseArgs(args []string) []string { +// parseArgs parses the command-line positional arguments that contain file glob patterns. +// If no argument is provided, return the default path (current directory). +// Perform glob pattern expansion. +func parseArgs(args []string) ([]string, error) { if len(args) == 0 { args = parser.DefaultPath } @@ -170,8 +179,29 @@ func parseArgs(args []string) []string { if stdin { args = []string{os.Stdin.Name()} } + // Perform glob expansion. + var files []string + for _, arg := range args { + var f []string + var err error + if strings.Contains(arg, "**") { + // Double star glob expansion. + base, pattern := doublestar.SplitPattern(arg) + fsys := os.DirFS(base) + f, err = doublestar.Glob(fsys, pattern) + for i := range f { + f[i] = filepath.Join(base, f[i]) + } + } else { + f, err = filepath.Glob(arg) + } + if err != nil { + return nil, err + } + files = append(files, f...) + } - return args + return files, nil } func setDebugLogLevel() { diff --git a/cmd/root_test.go b/cmd/root_test.go index 87071e7e..a2b9d46e 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -4,7 +4,9 @@ import ( "bytes" "io" "os" + "path/filepath" "regexp" + "strings" "testing" "github.com/get-woke/woke/pkg/output" @@ -70,12 +72,93 @@ func TestParseArgs(t *testing.T) { t.Cleanup(func() { stdin = false }) - assert.Equal(t, parser.DefaultPath, parseArgs([]string{})) - assert.Equal(t, []string{"../.."}, parseArgs([]string{"../.."})) + tests := []struct { + stdin bool + args []string + expectedArgs []string + expectedError error + }{ + { + stdin: false, + args: []string{}, + expectedArgs: parser.DefaultPath, + expectedError: nil, + }, + { + stdin: false, + args: []string{"../.."}, + expectedArgs: []string{"../.."}, + expectedError: nil, + }, + + // Test glob expansion + { + stdin: false, + args: []string{"../testdata/*.yml"}, + expectedArgs: []string{"../testdata/good.yml", "../testdata/whitelist.yml"}, + expectedError: nil, + }, + { + stdin: false, + args: []string{"../testdata/g??d.yml"}, + expectedArgs: []string{"../testdata/good.yml"}, + expectedError: nil, + }, + { + stdin: false, + args: []string{"../testdata/[a-z]ood.yml"}, + expectedArgs: []string{"../testdata/good.yml"}, + expectedError: nil, + }, + { + stdin: false, + args: []string{"../testdata/*/*.yml"}, + expectedArgs: []string{"../testdata/subdir1/good.yml", "../testdata/subdir1/whitelist.yml"}, + expectedError: nil, + }, + { + stdin: false, + args: []string{"../testdata/**/*.yml"}, + expectedArgs: []string{ + "../testdata/good.yml", + "../testdata/whitelist.yml", + "../testdata/subdir1/good.yml", + "../testdata/subdir1/whitelist.yml", + "../testdata/subdir1/subdir2/good.yml", + "../testdata/subdir1/subdir2/whitelist.yml", + }, + expectedError: nil, + }, - stdin = true - assert.Equal(t, []string{os.Stdin.Name()}, parseArgs([]string{})) - assert.Equal(t, []string{os.Stdin.Name()}, parseArgs([]string{"../.."})) + // Bad glob pattern + { + stdin: false, + args: []string{"r[.go"}, + expectedArgs: nil, + expectedError: filepath.ErrBadPattern, + }, + + { + stdin: true, + args: []string{}, + expectedArgs: []string{os.Stdin.Name()}, + expectedError: nil, + }, + { + stdin: true, + args: []string{"../.."}, + expectedArgs: []string{os.Stdin.Name()}, + expectedError: nil, + }, + } + for _, tt := range tests { + t.Run(strings.Join(tt.args, " "), func(t *testing.T) { + stdin = tt.stdin + files, err := parseArgs(tt.args) + assert.ErrorIs(t, err, tt.expectedError) + assert.Equal(t, tt.expectedArgs, files) + }) + } } func TestRunE(t *testing.T) { diff --git a/docs/usage.md b/docs/usage.md index 642ba963..05b75a67 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -28,10 +28,30 @@ No findings found. ### File globs By default, `woke` will run against all text files in your current directory. -To change this, supply a space-separated list of globs as the first argument. +To change this, supply a space-separated list of file glob patterns. +`woke` supports the following glob pattern: + +``` +pattern: + { term } +term: + '*' matches any sequence of non-Separator characters + '?' matches any single non-Separator character + '[' [ '^' ] { character-range } ']' + character class (must be non-empty) + c matches character c (c != '*', '?', '\\', '[') + '\\' c matches character c + +character-range: + c matches character c (c != '\\', '-', ']') + '\\' c matches character c + lo '-' hi matches character c for lo <= c <= hi +``` This can be something like `**/*.go`, or a space-separated list of filenames. +If `woke` is invoked from a shell, the invoking shell performs file glob pattern expansion according to the shell glob rules. + ```bash $ woke test.txt test.txt:2:2-11: `Blacklist` may be insensitive, use `denylist`, `blocklist` instead (warning) diff --git a/go.mod b/go.mod index a6bb25d9..7c5d61a4 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( require ( github.com/acomagu/bufpipe v1.0.3 // indirect + github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect diff --git a/go.sum b/go.sum index 8ef6fe46..4b35be2d 100644 --- a/go.sum +++ b/go.sum @@ -60,6 +60,8 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= +github.com/bmatcuk/doublestar/v4 v4.0.2 h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA= +github.com/bmatcuk/doublestar/v4 v4.0.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/caitlinelfring/go-env-default v1.0.0 h1:DY8qY3OKb9PrGe+sjop7dw7tOKh6kV8cdZONlESSbZc= github.com/caitlinelfring/go-env-default v1.0.0/go.mod h1:vY8iS64s+wIBKayqiNGJsWMwc19NrxaNNTyWzuPvE44= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index a542b7f3..c5d79062 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -54,9 +54,6 @@ func (p *Parser) ParsePaths(print printer.Printer, paths ...string) int { return r.Len() } - if len(paths) == 0 { - paths = DefaultPath - } var wg sync.WaitGroup done := make(chan bool) diff --git a/testdata/subdir1/good.yml b/testdata/subdir1/good.yml new file mode 100644 index 00000000..806ceece --- /dev/null +++ b/testdata/subdir1/good.yml @@ -0,0 +1,2 @@ +--- +this file has no findings. diff --git a/testdata/subdir1/subdir2/good.yml b/testdata/subdir1/subdir2/good.yml new file mode 100644 index 00000000..806ceece --- /dev/null +++ b/testdata/subdir1/subdir2/good.yml @@ -0,0 +1,2 @@ +--- +this file has no findings. diff --git a/testdata/subdir1/subdir2/whitelist.yml b/testdata/subdir1/subdir2/whitelist.yml new file mode 100644 index 00000000..9c5d68cf --- /dev/null +++ b/testdata/subdir1/subdir2/whitelist.yml @@ -0,0 +1,2 @@ +--- +this file also has a whitelist finding diff --git a/testdata/subdir1/whitelist.yml b/testdata/subdir1/whitelist.yml new file mode 100644 index 00000000..9c5d68cf --- /dev/null +++ b/testdata/subdir1/whitelist.yml @@ -0,0 +1,2 @@ +--- +this file also has a whitelist finding From 2dbd54354916ababb935c22299952ca4eb51279b Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" <serosset@cisco.com> Date: Tue, 26 Jul 2022 09:02:06 -0700 Subject: [PATCH 02/25] upgrade to doublestart v4.2.0 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 7c5d61a4..b5667777 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( require ( github.com/acomagu/bufpipe v1.0.3 // indirect - github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect + github.com/bmatcuk/doublestar/v4 v4.2.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect diff --git a/go.sum b/go.sum index 4b35be2d..f2ca97b2 100644 --- a/go.sum +++ b/go.sum @@ -62,6 +62,8 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= github.com/bmatcuk/doublestar/v4 v4.0.2 h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA= github.com/bmatcuk/doublestar/v4 v4.0.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= +github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA= +github.com/bmatcuk/doublestar/v4 v4.2.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/caitlinelfring/go-env-default v1.0.0 h1:DY8qY3OKb9PrGe+sjop7dw7tOKh6kV8cdZONlESSbZc= github.com/caitlinelfring/go-env-default v1.0.0/go.mod h1:vY8iS64s+wIBKayqiNGJsWMwc19NrxaNNTyWzuPvE44= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= From 94efac60fb3175ae33b8dd2bfd4c67d284ac3c00 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" <serosset@cisco.com> Date: Tue, 26 Jul 2022 09:06:03 -0700 Subject: [PATCH 03/25] upgrade to doublestart v4.2.0 --- cmd/root.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 061ea5d2..741d00ff 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -186,12 +186,7 @@ func parseArgs(args []string) ([]string, error) { var err error if strings.Contains(arg, "**") { // Double star glob expansion. - base, pattern := doublestar.SplitPattern(arg) - fsys := os.DirFS(base) - f, err = doublestar.Glob(fsys, pattern) - for i := range f { - f[i] = filepath.Join(base, f[i]) - } + f, err = doublestar.FilepathGlob(arg) } else { f, err = filepath.Glob(arg) } From 07b7da0ddbdcf92b56e4696141930fad80e218ce Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" <serosset@cisco.com> Date: Tue, 26 Jul 2022 18:50:05 -0700 Subject: [PATCH 04/25] run go mod tidy --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 4d2caf97..cb99da01 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/get-woke/woke go 1.17 require ( + github.com/bmatcuk/doublestar/v4 v4.2.0 github.com/caitlinelfring/go-env-default v1.1.0 github.com/fatih/color v1.13.0 github.com/get-woke/fastwalk v1.0.0 @@ -19,7 +20,6 @@ require ( require ( github.com/acomagu/bufpipe v1.0.3 // indirect - github.com/bmatcuk/doublestar/v4 v4.2.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.5.4 // indirect From 60471921db6ec296ebd64caf6f6b8801eab608d3 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" <serosset@cisco.com> Date: Tue, 26 Jul 2022 18:51:09 -0700 Subject: [PATCH 05/25] run go mod tidy --- go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.sum b/go.sum index d1eafb87..7238566a 100644 --- a/go.sum +++ b/go.sum @@ -81,8 +81,6 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= -github.com/bmatcuk/doublestar/v4 v4.0.2 h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA= -github.com/bmatcuk/doublestar/v4 v4.0.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA= github.com/bmatcuk/doublestar/v4 v4.2.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/caitlinelfring/go-env-default v1.1.0 h1:bhDfXmUolvcIGfQCX8qevQX8wxC54NGz0aimoUnhvDM= From f17d697e8125337096a3f90e3d4a37d09e82ea79 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" <serosset@cisco.com> Date: Tue, 26 Jul 2022 18:52:25 -0700 Subject: [PATCH 06/25] run go mod tidy --- go.sum | 1 - 1 file changed, 1 deletion(-) diff --git a/go.sum b/go.sum index 7238566a..5dea26df 100644 --- a/go.sum +++ b/go.sum @@ -80,7 +80,6 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= -github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA= github.com/bmatcuk/doublestar/v4 v4.2.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/caitlinelfring/go-env-default v1.1.0 h1:bhDfXmUolvcIGfQCX8qevQX8wxC54NGz0aimoUnhvDM= From bb055f0d58b5da7960cf99f3e10fbf427f8978bb Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" <serosset@cisco.com> Date: Wed, 27 Jul 2022 16:22:10 -0700 Subject: [PATCH 07/25] simplify call to FilepathGlob --- cmd/root.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 741d00ff..4930ece9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,7 +26,6 @@ import ( "errors" "fmt" "os" - "path/filepath" "runtime" "strings" "time" @@ -182,14 +181,7 @@ func parseArgs(args []string) ([]string, error) { // Perform glob expansion. var files []string for _, arg := range args { - var f []string - var err error - if strings.Contains(arg, "**") { - // Double star glob expansion. - f, err = doublestar.FilepathGlob(arg) - } else { - f, err = filepath.Glob(arg) - } + f, err := doublestar.FilepathGlob(arg) if err != nil { return nil, err } From 0b05fe1c46432498e2dff93d2148c703d341a7a5 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Sun, 6 Nov 2022 16:40:38 +0000 Subject: [PATCH 08/25] sync from main --- go.sum | 4 ---- 1 file changed, 4 deletions(-) diff --git a/go.sum b/go.sum index fad724e4..fdf3b773 100644 --- a/go.sum +++ b/go.sum @@ -45,10 +45,6 @@ github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= -github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= -github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= -github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA= github.com/bmatcuk/doublestar/v4 v4.2.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/caitlinelfring/go-env-default v1.1.0 h1:bhDfXmUolvcIGfQCX8qevQX8wxC54NGz0aimoUnhvDM= From 5580a09591bb401351df520addedb0cf145be2d6 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Sun, 6 Nov 2022 16:42:11 +0000 Subject: [PATCH 09/25] upgade doublestar --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 5cb2a861..6b5532fc 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/get-woke/woke go 1.18 require ( - github.com/bmatcuk/doublestar/v4 v4.2.0 + github.com/bmatcuk/doublestar/v4 v4.3.1 github.com/caitlinelfring/go-env-default v1.1.0 github.com/fatih/color v1.13.0 github.com/get-woke/fastwalk v1.0.0 diff --git a/go.sum b/go.sum index fdf3b773..b4e853df 100644 --- a/go.sum +++ b/go.sum @@ -47,6 +47,8 @@ github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYU github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA= github.com/bmatcuk/doublestar/v4 v4.2.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= +github.com/bmatcuk/doublestar/v4 v4.3.1 h1:JmbSJX7X1c6ImkExYeRTcviuGnp3QFUK468GPrMlW9w= +github.com/bmatcuk/doublestar/v4 v4.3.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/caitlinelfring/go-env-default v1.1.0 h1:bhDfXmUolvcIGfQCX8qevQX8wxC54NGz0aimoUnhvDM= github.com/caitlinelfring/go-env-default v1.1.0/go.mod h1:tESXPr8zFPP/cRy3cwxrHBmjJIf2A1x/o4C9CET2rEk= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= From d3228daf6ff69dbcc1e0dbc83c6a380e2fe8dead Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Sun, 6 Nov 2022 20:48:50 +0000 Subject: [PATCH 10/25] upgade doublestar --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 6b5532fc..dba1e0fb 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/get-woke/woke go 1.18 require ( - github.com/bmatcuk/doublestar/v4 v4.3.1 + github.com/bmatcuk/doublestar/v4 v4.3.2 github.com/caitlinelfring/go-env-default v1.1.0 github.com/fatih/color v1.13.0 github.com/get-woke/fastwalk v1.0.0 diff --git a/go.sum b/go.sum index b4e853df..d92d8b28 100644 --- a/go.sum +++ b/go.sum @@ -49,6 +49,8 @@ github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p github.com/bmatcuk/doublestar/v4 v4.2.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/bmatcuk/doublestar/v4 v4.3.1 h1:JmbSJX7X1c6ImkExYeRTcviuGnp3QFUK468GPrMlW9w= github.com/bmatcuk/doublestar/v4 v4.3.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= +github.com/bmatcuk/doublestar/v4 v4.3.2 h1:jhwioE79ok+L6P4xulmrNBfCD7yEkrR/BcGSCNDeVag= +github.com/bmatcuk/doublestar/v4 v4.3.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/caitlinelfring/go-env-default v1.1.0 h1:bhDfXmUolvcIGfQCX8qevQX8wxC54NGz0aimoUnhvDM= github.com/caitlinelfring/go-env-default v1.1.0/go.mod h1:tESXPr8zFPP/cRy3cwxrHBmjJIf2A1x/o4C9CET2rEk= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= From e6fd20e2d0a9815fc7378aa286ce49b0084c32c0 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 00:21:29 +0000 Subject: [PATCH 11/25] fix UT issue --- cmd/root_test.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index a2b9d46e..5690d6fb 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -2,16 +2,16 @@ package cmd import ( "bytes" + "fmt" "io" "os" - "path/filepath" "regexp" "strings" "testing" + "github.com/bmatcuk/doublestar/v4" "github.com/get-woke/woke/pkg/output" "github.com/get-woke/woke/pkg/parser" - "github.com/mitchellh/go-homedir" "github.com/rs/zerolog" "github.com/spf13/cobra" @@ -22,9 +22,12 @@ import ( // run profiling with // go test -v -cpuprofile cpu.prof -memprofile mem.prof -bench=. ./cmd // memory: -// go tool pprof mem.prof +// +// go tool pprof mem.prof +// // cpu: -// go tool pprof cpu.prof +// +// go tool pprof cpu.prof func BenchmarkRootRunE(b *testing.B) { zerolog.SetGlobalLevel(zerolog.NoLevel) output.Stdout = io.Discard @@ -135,7 +138,7 @@ func TestParseArgs(t *testing.T) { stdin: false, args: []string{"r[.go"}, expectedArgs: nil, - expectedError: filepath.ErrBadPattern, + expectedError: doublestar.ErrBadPattern, }, { @@ -155,7 +158,8 @@ func TestParseArgs(t *testing.T) { t.Run(strings.Join(tt.args, " "), func(t *testing.T) { stdin = tt.stdin files, err := parseArgs(tt.args) - assert.ErrorIs(t, err, tt.expectedError) + assert.ErrorIs(t, err, tt.expectedError, + fmt.Sprintf("arguments: %v. Expected '%v', Got '%v'", tt.args, err, tt.expectedError)) assert.Equal(t, tt.expectedArgs, files) }) } From af98f002533cf641a1476de6af4bfbb2e3175d91 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 00:30:58 +0000 Subject: [PATCH 12/25] fix UT issue for windows --- cmd/root_test.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 5690d6fb..7ea970c4 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "path/filepath" "regexp" "strings" "testing" @@ -96,39 +97,45 @@ func TestParseArgs(t *testing.T) { // Test glob expansion { - stdin: false, - args: []string{"../testdata/*.yml"}, - expectedArgs: []string{"../testdata/good.yml", "../testdata/whitelist.yml"}, + stdin: false, + args: []string{"../testdata/*.yml"}, + expectedArgs: []string{ + filepath.Join("..", "testdata/good.yml"), + filepath.Join("..", "testdata/whitelist.yml"), + }, expectedError: nil, }, { stdin: false, args: []string{"../testdata/g??d.yml"}, - expectedArgs: []string{"../testdata/good.yml"}, + expectedArgs: []string{filepath.Join("..", "testdata/good.yml")}, expectedError: nil, }, { stdin: false, args: []string{"../testdata/[a-z]ood.yml"}, - expectedArgs: []string{"../testdata/good.yml"}, + expectedArgs: []string{filepath.Join("..", "testdata", "good.yml")}, expectedError: nil, }, { - stdin: false, - args: []string{"../testdata/*/*.yml"}, - expectedArgs: []string{"../testdata/subdir1/good.yml", "../testdata/subdir1/whitelist.yml"}, + stdin: false, + args: []string{"../testdata/*/*.yml"}, + expectedArgs: []string{ + filepath.Join("..", "testdata", "subdir1", "good.yml"), + filepath.Join("..", "testdata", "subdir1", "whitelist.yml"), + }, expectedError: nil, }, { stdin: false, args: []string{"../testdata/**/*.yml"}, expectedArgs: []string{ - "../testdata/good.yml", - "../testdata/whitelist.yml", - "../testdata/subdir1/good.yml", - "../testdata/subdir1/whitelist.yml", - "../testdata/subdir1/subdir2/good.yml", - "../testdata/subdir1/subdir2/whitelist.yml", + filepath.Join("..", "testdata", "good.yml"), + filepath.Join("..", "testdata", "whitelist.yml"), + filepath.Join("..", "testdata", "subdir1", "good.yml"), + filepath.Join("..", "testdata", "subdir1", "whitelist.yml"), + filepath.Join("..", "testdata", "subdir1", "subdir2", "good.yml"), + filepath.Join("..", "testdata", "subdir1", "subdir2", "whitelist.yml"), }, expectedError: nil, }, From 4adaedb4b297a2687eb921f027c3b0d0d837fc68 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 00:34:12 +0000 Subject: [PATCH 13/25] fix usage formatting issues --- docs/usage.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 094b5fb6..c8644a72 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -31,21 +31,21 @@ By default, `woke` will run against all text files in your current directory. To change this, supply a space-separated list of file glob patterns. `woke` supports the following glob pattern: -``` +```console pattern: - { term } + { term } term: - '*' matches any sequence of non-Separator characters - '?' matches any single non-Separator character - '[' [ '^' ] { character-range } ']' - character class (must be non-empty) - c matches character c (c != '*', '?', '\\', '[') - '\\' c matches character c + '*' matches any sequence of non-Separator characters + '?' matches any single non-Separator character + '[' [ '^' ] { character-range } ']' + character class (must be non-empty) + c matches character c (c != '*', '?', '\\', '[') + '\\' c matches character c character-range: - c matches character c (c != '\\', '-', ']') - '\\' c matches character c - lo '-' hi matches character c for lo <= c <= hi + c matches character c (c != '\\', '-', ']') + '\\' c matches character c + lo '-' hi matches character c for lo <= c <= hi ``` This can be something like `**/*.go`, or a space-separated list of filenames. From 3202cefda1e7b6257e2844f5b3d1bc2d801e6c3e Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 00:41:37 +0000 Subject: [PATCH 14/25] fix UT issue for windows --- cmd/root_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 7ea970c4..8dd37983 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -91,7 +91,7 @@ func TestParseArgs(t *testing.T) { { stdin: false, args: []string{"../.."}, - expectedArgs: []string{"../.."}, + expectedArgs: []string{filepath.Join("..", "..")}, expectedError: nil, }, From 8c7a20322b7bd2602b701be557c76d1901fc1abe Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 13:40:03 +0000 Subject: [PATCH 15/25] fix UT issue for windows and stdin --- cmd/root.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 4930ece9..bfa44ce5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -174,18 +174,18 @@ func parseArgs(args []string) ([]string, error) { if len(args) == 0 { args = parser.DefaultPath } - - if stdin { - args = []string{os.Stdin.Name()} - } - // Perform glob expansion. var files []string - for _, arg := range args { - f, err := doublestar.FilepathGlob(arg) - if err != nil { - return nil, err + if stdin { + files = []string{os.Stdin.Name()} + } else { + // Perform glob expansion. + for _, arg := range args { + f, err := doublestar.FilepathGlob(arg) + if err != nil { + return nil, err + } + files = append(files, f...) } - files = append(files, f...) } return files, nil From 2c0cb4e25626dfed20294b1023bbbc32a664cd70 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 15:28:23 +0000 Subject: [PATCH 16/25] this is a test for woke output format --- .github/workflows/woke.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/woke.yml b/.github/workflows/woke.yml index 7426fdbe..ec3e0904 100644 --- a/.github/workflows/woke.yml +++ b/.github/workflows/woke.yml @@ -33,3 +33,4 @@ jobs: with: # Cause the check to fail on any broke rules fail-on-error: true + args: "-o text" From 741f6e766d1f8e8f7353e1789587d1239bdc27b8 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 15:34:05 +0000 Subject: [PATCH 17/25] this is a test for woke output format --- .github/workflows/woke.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/woke.yml b/.github/workflows/woke.yml index ec3e0904..37a1e32e 100644 --- a/.github/workflows/woke.yml +++ b/.github/workflows/woke.yml @@ -33,4 +33,4 @@ jobs: with: # Cause the check to fail on any broke rules fail-on-error: true - args: "-o text" + woke-args: "-o text" From 05fb6cadd46502dd493c39e262053282f0f53aa6 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 15:49:51 +0000 Subject: [PATCH 18/25] rename test files to avoid woke issue in ci --- cmd/root_test.go | 10 +++++----- testdata/{subdir1/subdir2/whitelist.yml => bad.yml} | 0 testdata/subdir1/{whitelist.yml => bad.yml} | 0 testdata/{whitelist.yml => subdir1/subdir2/bad.yml} | 0 4 files changed, 5 insertions(+), 5 deletions(-) rename testdata/{subdir1/subdir2/whitelist.yml => bad.yml} (100%) rename testdata/subdir1/{whitelist.yml => bad.yml} (100%) rename testdata/{whitelist.yml => subdir1/subdir2/bad.yml} (100%) diff --git a/cmd/root_test.go b/cmd/root_test.go index 8dd37983..773d53cc 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -100,8 +100,8 @@ func TestParseArgs(t *testing.T) { stdin: false, args: []string{"../testdata/*.yml"}, expectedArgs: []string{ + filepath.Join("..", "testdata/bad.yml"), filepath.Join("..", "testdata/good.yml"), - filepath.Join("..", "testdata/whitelist.yml"), }, expectedError: nil, }, @@ -121,8 +121,8 @@ func TestParseArgs(t *testing.T) { stdin: false, args: []string{"../testdata/*/*.yml"}, expectedArgs: []string{ + filepath.Join("..", "testdata", "subdir1", "bad.yml"), filepath.Join("..", "testdata", "subdir1", "good.yml"), - filepath.Join("..", "testdata", "subdir1", "whitelist.yml"), }, expectedError: nil, }, @@ -130,12 +130,12 @@ func TestParseArgs(t *testing.T) { stdin: false, args: []string{"../testdata/**/*.yml"}, expectedArgs: []string{ + filepath.Join("..", "testdata", "bad.yml"), filepath.Join("..", "testdata", "good.yml"), - filepath.Join("..", "testdata", "whitelist.yml"), + filepath.Join("..", "testdata", "subdir1", "bad.yml"), filepath.Join("..", "testdata", "subdir1", "good.yml"), - filepath.Join("..", "testdata", "subdir1", "whitelist.yml"), + filepath.Join("..", "testdata", "subdir1", "subdir2", "bad.yml"), filepath.Join("..", "testdata", "subdir1", "subdir2", "good.yml"), - filepath.Join("..", "testdata", "subdir1", "subdir2", "whitelist.yml"), }, expectedError: nil, }, diff --git a/testdata/subdir1/subdir2/whitelist.yml b/testdata/bad.yml similarity index 100% rename from testdata/subdir1/subdir2/whitelist.yml rename to testdata/bad.yml diff --git a/testdata/subdir1/whitelist.yml b/testdata/subdir1/bad.yml similarity index 100% rename from testdata/subdir1/whitelist.yml rename to testdata/subdir1/bad.yml diff --git a/testdata/whitelist.yml b/testdata/subdir1/subdir2/bad.yml similarity index 100% rename from testdata/whitelist.yml rename to testdata/subdir1/subdir2/bad.yml From 8968dd78dbd2ef1583c3dc508820e2070a5b80c1 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 16:14:50 +0000 Subject: [PATCH 19/25] group imports --- cmd/root_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 773d53cc..66854e0a 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -10,9 +10,10 @@ import ( "strings" "testing" - "github.com/bmatcuk/doublestar/v4" "github.com/get-woke/woke/pkg/output" "github.com/get-woke/woke/pkg/parser" + + "github.com/bmatcuk/doublestar/v4" "github.com/mitchellh/go-homedir" "github.com/rs/zerolog" "github.com/spf13/cobra" From 605f45e74b92ebc5db855ed28ccbef09cff68655 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 16:16:36 +0000 Subject: [PATCH 20/25] run go mod tidy --- go.sum | 4 ---- 1 file changed, 4 deletions(-) diff --git a/go.sum b/go.sum index d92d8b28..5be89b63 100644 --- a/go.sum +++ b/go.sum @@ -45,10 +45,6 @@ github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= -github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA= -github.com/bmatcuk/doublestar/v4 v4.2.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= -github.com/bmatcuk/doublestar/v4 v4.3.1 h1:JmbSJX7X1c6ImkExYeRTcviuGnp3QFUK468GPrMlW9w= -github.com/bmatcuk/doublestar/v4 v4.3.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/bmatcuk/doublestar/v4 v4.3.2 h1:jhwioE79ok+L6P4xulmrNBfCD7yEkrR/BcGSCNDeVag= github.com/bmatcuk/doublestar/v4 v4.3.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/caitlinelfring/go-env-default v1.1.0 h1:bhDfXmUolvcIGfQCX8qevQX8wxC54NGz0aimoUnhvDM= From db9bfd329f9a3d971e0816605a750bacb382e855 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Mon, 7 Nov 2022 16:19:05 +0000 Subject: [PATCH 21/25] remove woke-args which was used to troubleshoot CI issue --- .github/workflows/woke.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/woke.yml b/.github/workflows/woke.yml index 37a1e32e..7426fdbe 100644 --- a/.github/workflows/woke.yml +++ b/.github/workflows/woke.yml @@ -33,4 +33,3 @@ jobs: with: # Cause the check to fail on any broke rules fail-on-error: true - woke-args: "-o text" From 5d44eb3e2f19e6b6b428da4cc99e9579a2174438 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Wed, 9 Nov 2022 19:12:19 +0000 Subject: [PATCH 22/25] improve documentation, add unit tests --- cmd/root_test.go | 32 ++++++++++++++++++++++++++++++-- docs/usage.md | 22 +++++++++++----------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 66854e0a..c5a0b3f9 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -108,16 +108,34 @@ func TestParseArgs(t *testing.T) { }, { stdin: false, - args: []string{"../testdata/g??d.yml"}, + args: []string{"../testdata/g??d.yml"}, // matches any single non-separator character expectedArgs: []string{filepath.Join("..", "testdata/good.yml")}, expectedError: nil, }, { stdin: false, - args: []string{"../testdata/[a-z]ood.yml"}, + args: []string{"../testdata/[a-z]ood.yml"}, // character range expectedArgs: []string{filepath.Join("..", "testdata", "good.yml")}, expectedError: nil, }, + { + stdin: false, + args: []string{"../testdata/[^abc]ood.yml"}, // character class with negation. + expectedArgs: []string{filepath.Join("..", "testdata", "good.yml")}, + expectedError: nil, + }, + { + stdin: false, + args: []string{"../testdata/[!abc]ood.yml"}, // character class with negation. + expectedArgs: []string{filepath.Join("..", "testdata", "good.yml")}, + expectedError: nil, + }, + { + stdin: false, + args: []string{"../testdata/[^g]ood.yml"}, // character class with negation. + expectedArgs: nil, + expectedError: nil, + }, { stdin: false, args: []string{"../testdata/*/*.yml"}, @@ -140,6 +158,16 @@ func TestParseArgs(t *testing.T) { }, expectedError: nil, }, + { + stdin: false, + args: []string{"../testdata/**/?ood.yml"}, + expectedArgs: []string{ + filepath.Join("..", "testdata", "good.yml"), + filepath.Join("..", "testdata", "subdir1", "good.yml"), + filepath.Join("..", "testdata", "subdir1", "subdir2", "good.yml"), + }, + expectedError: nil, + }, // Bad glob pattern { diff --git a/docs/usage.md b/docs/usage.md index c8644a72..caa6f410 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -35,17 +35,17 @@ To change this, supply a space-separated list of file glob patterns. pattern: { term } term: - '*' matches any sequence of non-Separator characters - '?' matches any single non-Separator character - '[' [ '^' ] { character-range } ']' - character class (must be non-empty) - c matches character c (c != '*', '?', '\\', '[') - '\\' c matches character c - -character-range: - c matches character c (c != '\\', '-', ']') - '\\' c matches character c - lo '-' hi matches character c for lo <= c <= hi + * matches any sequence of non-separator characters + ? matches any single non-separator character + /**/ matches zero or more directories + [class] matches any single non-path-separator character against a class of characters + {alt1,...} matches a sequence of characters if one of the comma-separated alternatives matches + +characters classes: + [abc] matches any single character within the set + [a-z] matches any single character in the range + [^class] matches any single character which does not match the class + [!class] same as ^: negates the class ``` This can be something like `**/*.go`, or a space-separated list of filenames. From 2c6bd9624b455c94d7f55284adf72b7ece43f0ce Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Wed, 9 Nov 2022 19:43:51 +0000 Subject: [PATCH 23/25] add unit tests and code coverage --- cmd/root_test.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index c5a0b3f9..13036392 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -158,6 +158,19 @@ func TestParseArgs(t *testing.T) { }, expectedError: nil, }, + { + stdin: false, + args: []string{"../testdata/**/{good,bad}.yml"}, // Alternate pattern + expectedArgs: []string{ + filepath.Join("..", "testdata", "bad.yml"), + filepath.Join("..", "testdata", "good.yml"), + filepath.Join("..", "testdata", "subdir1", "bad.yml"), + filepath.Join("..", "testdata", "subdir1", "good.yml"), + filepath.Join("..", "testdata", "subdir1", "subdir2", "bad.yml"), + filepath.Join("..", "testdata", "subdir1", "subdir2", "good.yml"), + }, + expectedError: nil, + }, { stdin: false, args: []string{"../testdata/**/?ood.yml"}, @@ -172,7 +185,13 @@ func TestParseArgs(t *testing.T) { // Bad glob pattern { stdin: false, - args: []string{"r[.go"}, + args: []string{"r[.go"}, // Invalid character class + expectedArgs: nil, + expectedError: doublestar.ErrBadPattern, + }, + { + stdin: false, + args: []string{"{.go"}, // Bad alternate pattern expectedArgs: nil, expectedError: doublestar.ErrBadPattern, }, @@ -232,7 +251,7 @@ func TestRunE(t *testing.T) { assert.Equal(t, expected, got) }) - t.Run("findings w error", func(t *testing.T) { + t.Run("findings with inclusive language issues", func(t *testing.T) { exitOneOnFailure = true // don't ignore testdata folder noIgnore = true @@ -245,6 +264,19 @@ func TestRunE(t *testing.T) { assert.Regexp(t, regexp.MustCompile(`^files with findings: \d`), err.Error()) }) + t.Run("findings with invalid glob pattern", func(t *testing.T) { + exitOneOnFailure = true + // don't ignore testdata folder + noIgnore = true + + t.Cleanup(func() { + exitOneOnFailure = false + }) + err := rootRunE(new(cobra.Command), []string{"../testdata/**/[.yml"}) + assert.Error(t, err) + assert.Regexp(t, regexp.MustCompile(`syntax error in pattern`), err.Error()) + }) + t.Run("no rules enabled", func(t *testing.T) { disableDefaultRules = true t.Cleanup(func() { From 0533ec6818c9ed936504900488b2848184bbe453 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Wed, 9 Nov 2022 19:54:36 +0000 Subject: [PATCH 24/25] replace tab with space characters --- docs/usage.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index caa6f410..654cd722 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -44,8 +44,8 @@ term: characters classes: [abc] matches any single character within the set [a-z] matches any single character in the range - [^class] matches any single character which does not match the class - [!class] same as ^: negates the class + [^class] matches any single character which does not match the class + [!class] same as ^: negates the class ``` This can be something like `**/*.go`, or a space-separated list of filenames. From 92f7ec07649dc302f2c9a73379d9e95297da7a27 Mon Sep 17 00:00:00 2001 From: serosset <serosset@cisco.com> Date: Thu, 10 Nov 2022 14:42:58 +0000 Subject: [PATCH 25/25] remove tabs --- docs/usage.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 654cd722..b7dfaf11 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -42,8 +42,8 @@ term: {alt1,...} matches a sequence of characters if one of the comma-separated alternatives matches characters classes: - [abc] matches any single character within the set - [a-z] matches any single character in the range + [abc] matches any single character within the set + [a-z] matches any single character in the range [^class] matches any single character which does not match the class [!class] same as ^: negates the class ```