Skip to content

Commit

Permalink
Merge pull request #7 from sho-hata/feature/ignore-option
Browse files Browse the repository at this point in the history
Support ignore specified directories with cli option -i/-ignore
  • Loading branch information
sho-hata authored Aug 15, 2022
2 parents 2a9b2e5 + b62af20 commit e3a3e0d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func SampleTest(t *testing.T) {

- Support when called `t.Setenv()` in test
- Support when called `time.Now()` in test
- Ignore specified directories with cli option -i/-ignore.
- [x] Ignore specified directories with cli option -i/-ignore.
- Able to ignore main/sub test function by tparagen:ignore comment.

## Synopsis
Expand All @@ -130,8 +130,19 @@ $ tparagen
```

## Options
wip
```
$ tparagen -h
tparagen inserts `testing.T.Parallel()` in a test function in a specific source file or in an entire directory.
Usage of tparagen:
-i string
ignore directory names. ex: foo,bar,baz
(testdata directory is always ignored.)
-ignore string
ignore directory names. ex: foo,bar,baz
(testdata directory is always ignored.)
```
## Installation
```
go install -v github.com/sho-hata/tparagen/cmd/tparagen@latest
Expand Down
37 changes: 30 additions & 7 deletions tparagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,22 @@ func fill(args []string, outStream, errStream io.Writer) (*tparagen, error) {
flags.PrintDefaults()
}

var destDir string
var ignoreDirsString string
idesc := "ignore directory names. ex: foo,bar,baz\n(testdata directory is always ignored.)"
flags.StringVar(&ignoreDirsString, "ignore", "", idesc)
flags.StringVar(&ignoreDirsString, "i", "", idesc)

odesc := "destination directory."
flags.StringVar(&destDir, "destination", "", odesc)
var destDir string

if err := flags.Parse(args[1:]); err != nil {
return nil, err
}

ignoreDirs := []string{"testdata"}
if len(ignoreDirs) != 0 {
ignoreDirs = append(ignoreDirs, strings.Split(ignoreDirsString, ",")...)
}

targetDir := "./"

nargs := flags.Args()
Expand All @@ -64,16 +71,18 @@ func fill(args []string, outStream, errStream io.Writer) (*tparagen, error) {
}

return &tparagen{
in: targetDir,
dest: destDir,
outStream: outStream,
errStream: errStream,
in: targetDir,
dest: destDir,
outStream: outStream,
errStream: errStream,
ignoreDirs: ignoreDirs,
}, nil
}

type tparagen struct {
in, dest string
outStream, errStream io.Writer
ignoreDirs []string
errFlag bool
}

Expand All @@ -83,6 +92,10 @@ func (t *tparagen) run() error {
return err
}

if info.IsDir() && t.skipDir(path) {
return filepath.SkipDir
}

if info.IsDir() {
return nil
}
Expand Down Expand Up @@ -161,3 +174,13 @@ func (t *tparagen) writeOtherPath(in, dist, path string, got []byte) error {

return nil
}

func (t *tparagen) skipDir(p string) bool {
for _, dir := range t.ignoreDirs {
if filepath.Base(p) == dir {
return true
}
}

return false
}

0 comments on commit e3a3e0d

Please sign in to comment.