Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Covers skip dir #18

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading