From 081ee6f7b848bf7717ba927c51e1753dbf6508a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Ch=C3=A1vez?= Date: Sun, 1 Oct 2023 22:12:53 +0200 Subject: [PATCH 1/2] tests: covers skip dir. --- import.go | 6 +++++- import_test.go | 23 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/import.go b/import.go index 385dc8c..00449e9 100644 --- a/import.go +++ b/import.go @@ -106,7 +106,11 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st gc := 0 for _, f := range files { - if isDir, dirName := f.IsDir(), f.Name(); isDir && !matchesAny(opts.SkipDirsRegexes, dirName) { + if isDir, dirName := f.IsDir(), f.Name(); isDir { + if matchesAny(opts.SkipDirsRegexes, dirName) { + continue + } + var ( c int err error diff --git a/import_test.go b/import_test.go index d41f2aa..64e7cb9 100644 --- a/import_test.go +++ b/import_test.go @@ -87,14 +87,33 @@ func TestFindFilesWithVanityImport(t *testing.T) { assert.Equal(t, 1, c) }) - t.Run("include file", func(t *testing.T) { + t.Run("skip dir", func(t *testing.T) { + c, err := findAndAddVanityImportForModuleDir( + cwd, + cwd+"/testdata", + "github.com/jcchavezs/porto/integration", + Options{ + ListDiffFiles: true, + SkipDirsRegexes: []*regexp.Regexp{ + regexp.MustCompile(`^codegen$`), + regexp.MustCompile(`^leftpad$`), + regexp.MustCompile(`^rightpad$`), + }, + }, + ) + + require.NoError(t, err) + assert.Equal(t, 1, c) + }) + + t.Run("restrict to files", func(t *testing.T) { c, err := findAndAddVanityImportForModuleDir( cwd, cwd+"/testdata/leftpad", "github.com/jcchavezs/porto-integration-leftpad", Options{ ListDiffFiles: true, - RestrictToFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`other\.go`)}, + RestrictToFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`^other\.go$`)}, }, ) From 1aec06cc338511991d286f483411240267f053ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Ch=C3=A1vez?= Date: Sun, 1 Oct 2023 22:38:51 +0200 Subject: [PATCH 2/2] fix: restrict-to-files only avoids skip-files, not skip-dirs. --- cmd/porto/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/porto/main.go b/cmd/porto/main.go index 25a961e..706f6cd 100644 --- a/cmd/porto/main.go +++ b/cmd/porto/main.go @@ -18,7 +18,7 @@ func main() { flagSkipDirs := flag.String("skip-dirs", "", "Regexps of directories to skip") flagSkipDefaultDirs := flag.Bool("skip-dirs-use-default", true, "Use default skip directory list") flagIncludeInternal := flag.Bool("include-internal", false, "Include internal folders") - flagRestrictToFiles := flag.String("restrict-to-files", "", "Regexps of files to restrict the inspection on. It takes precedence over -skip-files and -skip-dirs") + flagRestrictToFiles := flag.String("restrict-to-files", "", "Regexps of files to restrict the inspection on. It takes precedence over -skip-files") flag.Parse() baseDir := flag.Arg(0) @@ -54,7 +54,7 @@ Add import path to a folder log.Fatalf("failed to build files regexes to include: %v", err) } - var skipDirsRegex []*regexp.Regexp + var skipDirsRegex = []*regexp.Regexp{} if *flagSkipDefaultDirs { skipDirsRegex = append(skipDirsRegex, porto.StdExcludeDirRegexps...) } @@ -68,13 +68,13 @@ Add import path to a folder WriteResultToFile: *flagWriteOutputToFile, ListDiffFiles: *flagListDiff, IncludeInternal: *flagIncludeInternal, + SkipDirsRegexes: skipDirsRegex, } if len(restrictToFilesRegex) > 0 { opts.RestrictToFilesRegexes = restrictToFilesRegex } else { opts.SkipFilesRegexes = skipFilesRegex - opts.SkipDirsRegexes = skipDirsRegex } diffCount, err := porto.FindAndAddVanityImportForDir(workingDir, baseAbsDir, opts)