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

add exclude path feature #70

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 29 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
license = flag.String("l", "apache", "license type: apache, bsd, mit, mpl")
licensef = flag.String("f", "", "license file")
year = flag.String("y", fmt.Sprint(time.Now().Year()), "copyright year(s)")
exclude = flag.String("e", "", "Files which exclude from check")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Files to exclude from check" seems more accurate

verbose = flag.Bool("v", false, "verbose mode: print the name of the files that are modified")
checkonly = flag.Bool("check", false, "check only mode: verify presence of license headers and exit with non-zero code if missing")
)
Expand Down Expand Up @@ -140,8 +141,10 @@ func main() {
}
}()

excludePath := NewExcludePaths(*exclude)

for _, d := range flag.Args() {
walk(ch, d)
walk(ch, d, excludePath)
}
close(ch)
<-done
Expand All @@ -152,7 +155,7 @@ type file struct {
mode os.FileMode
}

func walk(ch chan<- *file, start string) {
func walk(ch chan<- *file, start string, excludePath *ExcludePath) {
filepath.Walk(start, func(path string, fi os.FileInfo, err error) error {
if err != nil {
log.Printf("%s error: %v", path, err)
Expand All @@ -161,11 +164,34 @@ func walk(ch chan<- *file, start string) {
if fi.IsDir() {
return nil
}
if excludePath.Contains(path) {
log.Printf("%s skipped", path)
return nil
}
ch <- &file{path, fi.Mode()}
return nil
})
}

type ExcludePath struct {
paths []string
}

func NewExcludePaths(arg string) *ExcludePath {
return &ExcludePath{
paths: strings.Split(arg, ","),
}
}

func (e *ExcludePath) Contains(path string) bool {
for _, p := range e.paths {
if p == path {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work if someone specifies a relative path, e.g. /foo/bar, ./foo, ../foo/bar?
Perhaps you should canonicalize both strings to a full path to ensure you catch all those cases.

return true
}
}
return false
}

// addLicense add a license to the file if missing.
//
// It returns true if the file was updated.
Expand Down Expand Up @@ -272,6 +298,7 @@ func hashBang(b []byte) []byte {

// go generate: ^// Code generated .* DO NOT EDIT\.$
var goGenerated = regexp.MustCompile(`(?m)^.{1,2} Code generated .* DO NOT EDIT\.$`)

// cargo raze: ^DO NOT EDIT! Replaced on runs of cargo-raze$
var cargoRazeGenerated = regexp.MustCompile(`(?m)^DO NOT EDIT! Replaced on runs of cargo-raze$`)

Expand Down
30 changes: 30 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,33 @@ func TestMPL(t *testing.T) {
t.Fatalf("%v\n%s", err, out)
}
}

func TestExcludePath(t *testing.T) {
if os.Getenv("RUNME") != "" {
main()
return
}

tmp := tempDir(t)
t.Logf("tmp dir: %s", tmp)

origin1 := filepath.Join(tmp, "file.c")
origin2 := filepath.Join(tmp, "file.h")
expected1 := "testdata/initial/file.c"
expected2 := "testdata/expected/file.h"

run(t, "cp", "testdata/initial/file.c", origin1)
run(t, "cp", "testdata/initial/file.h", origin2)

cmd := exec.Command(os.Args[0],
"-test.run=TestExcludePath",
"-y", "2018", "-e", origin1, origin1, origin2,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions: 1) this is excluding a file. Isn't the more likely use case to exclude an entire dir tree? If so, could you add a test for that case? 2) Is the two appearances of origin1 a mistake or because you want to verify the logic handles duplicate paths?

Copy link
Author

@Shikugawa Shikugawa Jul 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. In this implementation, I expect that only to accept filename. for example in current implementation, we get /foo/bar, /foo/bar/baz.txt after filepath walk and configured /foo/bar as exclude path then /foo/bar/baz.txt won't be ignored.
  2. It also includes dup path test.

Copy link

@laurentsimon laurentsimon Jul 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be beneficial to support directories: the main use case (and ours!) is dev teams removing test folders from the path. Test folders have a number of files that change over time, so it's cumbersome to update the command every time a new file is added.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree - I think dirs is likely to be a far more valuable use case than enumerating individual files. If you do a prefix match then you get both, dirs and files, with a one line change.

)
cmd.Env = []string{"RUNME=1"}
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%v\n%s", err, out)
}

run(t, "diff", origin1, expected1)
run(t, "diff", origin2, expected2)
}