Skip to content

Commit

Permalink
Add 'l' action for watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed May 28, 2021
1 parent 99b2618 commit cdd0de1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ When the `--watch` flag is set, `gotestsum` will watch directories using
[file system notifications](https://pkg.go.dev/github.com/fsnotify/fsnotify).
When a Go file in one of those directories is modified, `gotestsum` will run the
tests for the package which contains the changed file. By default all
directories under the current directory will be watched. Use the `--packages` flag
to specify a different list.
directories with at least one file with a `.go` extension, under the current
directory will be watched. Use the `--packages` flag to specify a different list.

While in watch mode, pressing some keys will perform an action:

Expand All @@ -321,6 +321,9 @@ While in watch mode, pressing some keys will perform an action:
breakpoints can be added with [`runtime.Breakpoint`](https://golang.org/pkg/runtime/#Breakpoint)
or by using the delve command prompt.
* `a` will run tests for all packages, by using `./...` as the package selector.
* `l` will scan the directory list again, and if there are any new directories
which contain a file with a `.go` extension, they will be added to the watch
list.

Note that [delve] must be installed in order to use debug (`d`).

Expand Down
3 changes: 3 additions & 0 deletions internal/filewatcher/term_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func (r *redoHandler) Run(ctx context.Context) {
case 'a':
chResume = make(chan struct{})
r.ch <- RunOptions{resume: chResume, PkgPath: "./..."}
case 'l':
chResume = make(chan struct{})
r.ch <- RunOptions{resume: chResume, reloadPaths: true}
case '\n':
fmt.Println()
continue
Expand Down
36 changes: 27 additions & 9 deletions internal/filewatcher/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,26 @@ import (
const maxDepth = 7

type RunOptions struct {
PkgPath string
Debug bool
resume chan struct{}
PkgPath string
Debug bool
resume chan struct{}
reloadPaths bool
}

// Watch dirs for filesystem events, and run tests when .go files are saved.
// nolint: gocyclo
func Watch(dirs []string, run func(opts RunOptions) error) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

toWatch := findAllDirs(dirs, maxDepth)
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("failed to create file watcher: %w", err)
}
defer watcher.Close() // nolint: errcheck // always returns nil error

fmt.Printf("Watching %v directories. Use Ctrl-c to to stop a run or exit.\n", len(toWatch))
for _, dir := range toWatch {
if err = watcher.Add(dir); err != nil {
return fmt.Errorf("failed to watch directory %v: %w", dir, err)
}
if err := loadPaths(watcher, dirs); err != nil {
return err
}

timer := time.NewTimer(maxIdleTime)
Expand All @@ -55,6 +54,14 @@ func Watch(dirs []string, run func(opts RunOptions) error) error {
case opts := <-redo.Ch():
resetTimer(timer)

if opts.reloadPaths {
if err := loadPaths(watcher, dirs); err != nil {
return err
}
close(opts.resume)
continue
}

redo.ResetTerm()
if err := h.runTests(opts); err != nil {
return fmt.Errorf("failed to rerun tests for %v: %v", opts.PkgPath, err)
Expand Down Expand Up @@ -89,6 +96,17 @@ func resetTimer(timer *time.Timer) {
timer.Reset(maxIdleTime)
}

func loadPaths(watcher *fsnotify.Watcher, dirs []string) error {
toWatch := findAllDirs(dirs, maxDepth)
fmt.Printf("Watching %v directories. Use Ctrl-c to to stop a run or exit.\n", len(toWatch))
for _, dir := range toWatch {
if err := watcher.Add(dir); err != nil {
return fmt.Errorf("failed to watch directory %v: %w", dir, err)
}
}
return nil
}

func findAllDirs(dirs []string, maxDepth int) []string {
if len(dirs) == 0 {
dirs = []string{"./..."}
Expand Down

0 comments on commit cdd0de1

Please sign in to comment.