Skip to content

Commit

Permalink
Merge pull request #18 from jcchavezs/covers_skip_dir
Browse files Browse the repository at this point in the history
Covers skip dir
  • Loading branch information
jcchavezs authored Oct 1, 2023
2 parents b037e56 + 1aec06c commit 6709cc4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cmd/porto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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...)
}
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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$`)},
},
)

Expand Down

0 comments on commit 6709cc4

Please sign in to comment.