Skip to content

Commit

Permalink
skip over testdata dirs like we do with vendor
Browse files Browse the repository at this point in the history
And, just like vendor, one can opt into formatting them by explicitly
passing them as arguments.

Updates #250.
Fixes #260.
  • Loading branch information
mvdan committed Apr 9, 2023
1 parent 48add90 commit e2f1a6e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -19,7 +19,7 @@ The project includes copies of `go/printer` and `go/doc/comment` as of Go 1.20
to ensure consistent formatting independent of what Go version is being used.
The [added formatting rules](#Added-rules) are implemented in the `format` package.

Note that vendor directories are skipped unless given as explicit arguments.
`vendor` and `testdata` directories are skipped unless given as explicit arguments.
Similarly, the added rules do not apply to generated Go files unless they are
given as explicit arguments.

Expand Down
10 changes: 5 additions & 5 deletions gofmt.go
Expand Up @@ -84,9 +84,6 @@ var fdSem = make(chan bool, 200)
var (
fileSet = token.NewFileSet() // per process FileSet
parserMode parser.Mode

// walkingVendorDir is true if we are explicitly walking a vendor directory.
walkingVendorDir bool
)

func usage() {
Expand Down Expand Up @@ -512,11 +509,14 @@ func gofmtMain(s *sequencer) {
})
default:
// Directories are walked, ignoring non-Go files.
walkingVendorDir = filepath.Base(arg) == "vendor"
err := filepath.WalkDir(arg, func(path string, f fs.DirEntry, err error) error {
if !walkingVendorDir && filepath.Base(path) == "vendor" {
// vendor and testdata directories are skipped,
// unless they are explicitly passed as an argument.
base := filepath.Base(path)
if path != arg && (base == "vendor" || base == "testdata") {
return filepath.SkipDir
}

if err != nil || !isGoFile(f) {
return err
}
Expand Down
55 changes: 55 additions & 0 deletions testdata/script/ignore-dirs.txtar
@@ -0,0 +1,55 @@
exec gofumpt orig.go.golden
cp stdout formatted.go.golden
mkdir -p vendor/foo testdata/foo
cp orig.go.golden vendor/foo/foo.go
cp orig.go.golden testdata/foo/foo.go

# format explicit dirs
exec gofumpt -l vendor testdata
stdout -count=1 'vendor[/\\]foo[/\\]foo.go'
stdout -count=1 'testdata[/\\]foo[/\\]foo.go'
! stderr .

# format explicit files
exec gofumpt -l vendor/foo/foo.go testdata/foo/foo.go
stdout -count=1 'vendor[/\\]foo[/\\]foo.go'
stdout -count=1 'testdata[/\\]foo[/\\]foo.go'
! stderr .

# ignore implicit dirs via fs walking
exec gofumpt -l .
! stdout .
! stderr .

# format explicit pkg while ignoring rest
mkdir vendor/ignore testdata/ignore
cp orig.go.golden vendor/ignore/ignore.go
cp orig.go.golden testdata/ignore/ignore.go
exec gofumpt -l vendor/foo testdata/foo .
stdout -count=1 'vendor[/\\]foo[/\\]foo.go'
stdout -count=1 'testdata[/\\]foo[/\\]foo.go'
! stderr .

# format explicit dirs without clean paths
exec gofumpt -l $WORK//vendor ./testdata/./
stdout -count=1 'vendor[/\\]foo[/\\]foo.go'
stdout -count=1 'testdata[/\\]foo[/\\]foo.go'
! stderr .

-- orig.go.golden --
package p

func f() {
if true {
// lone comment
}
{

}

{

// lone comment

}
}
44 changes: 0 additions & 44 deletions testdata/script/ignore-implicit-vendor.txtar

This file was deleted.

0 comments on commit e2f1a6e

Please sign in to comment.