Skip to content

Commit

Permalink
fix: include/skip logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Sep 21, 2023
1 parent 46dc46e commit 4ac7c25
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
22 changes: 14 additions & 8 deletions cmd/porto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,20 @@ Add import path to a folder
}
skipDirsRegex = append(skipDirsRegex, userSkipDirsRegex...)

diffCount, err := porto.FindAndAddVanityImportForDir(workingDir, baseAbsDir, porto.Options{
WriteResultToFile: *flagWriteOutputToFile,
ListDiffFiles: *flagListDiff,
SkipFilesRegexes: skipFilesRegex,
SkipDirsRegexes: skipDirsRegex,
IncludeInternal: *flagIncludeInternal,
IncludeFilesRegexes: includeFilesRegex,
})
opts := porto.Options{
WriteResultToFile: *flagWriteOutputToFile,
ListDiffFiles: *flagListDiff,
IncludeInternal: *flagIncludeInternal,
}

if len(includeFilesRegex) > 0 {
opts.IncludeFilesRegexes = includeFilesRegex
} else {
opts.SkipFilesRegexes = skipFilesRegex
opts.SkipDirsRegexes = skipDirsRegex
}

diffCount, err := porto.FindAndAddVanityImportForDir(workingDir, baseAbsDir, opts)
if err != nil {
log.Fatal(err)
}
Expand Down
13 changes: 12 additions & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,18 @@ func findAndAddVanityImportForModuleDir(workingDir, absDir string, moduleName st
}

gc += c
} else if fileName := f.Name(); isGoFile(fileName) && !isGoTestFile(fileName) && matchesAny(opts.IncludeFilesRegexes, fileName) && !matchesAny(opts.SkipFilesRegexes, fileName) {
} else if fileName := f.Name(); isGoFile(fileName) && !isGoTestFile(fileName) {
shouldEvaluate := true
if len(opts.IncludeFilesRegexes) > 0 {
shouldEvaluate = matchesAny(opts.IncludeFilesRegexes, fileName)
} else if len(opts.SkipFilesRegexes) > 0 {
shouldEvaluate = !matchesAny(opts.SkipFilesRegexes, fileName)
}

if !shouldEvaluate {
continue
}

absFilepath := absDir + pathSeparator + fileName

hasChanged, newContent, err := addImportPath(absDir+pathSeparator+fileName, moduleName)
Expand Down
46 changes: 46 additions & 0 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,52 @@ func TestFindFilesWithVanityImport(t *testing.T) {
assert.Equal(t, 0, c)
})

t.Run("skip file", func(t *testing.T) {
c, err := findAndAddVanityImportForModuleDir(
cwd,
cwd+"/testdata/leftpad",
"github.com/jcchavezs/porto-integration-leftpad",
Options{
ListDiffFiles: true,
SkipFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`leftpad\.go`)},
},
)

require.NoError(t, err)
assert.Equal(t, 1, c)
})

t.Run("include file", func(t *testing.T) {
c, err := findAndAddVanityImportForModuleDir(
cwd,
cwd+"/testdata/leftpad",
"github.com/jcchavezs/porto-integration-leftpad",
Options{
ListDiffFiles: true,
IncludeFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`other\.go`)},
},
)

require.NoError(t, err)
assert.Equal(t, 1, c)
})

t.Run("skip and include file", func(t *testing.T) {
c, err := findAndAddVanityImportForModuleDir(
cwd,
cwd+"/testdata/leftpad",
"github.com/jcchavezs/porto-integration-leftpad",
Options{
ListDiffFiles: true,
IncludeFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`other\.go`)},
SkipFilesRegexes: []*regexp.Regexp{regexp.MustCompile(`leftpad\.go`)},
},
)

require.NoError(t, err)
assert.Equal(t, 1, c)
})

}

func TestMatchesAny(t *testing.T) {
Expand Down

0 comments on commit 4ac7c25

Please sign in to comment.