Skip to content

Commit

Permalink
watch: move lastPath storage to handler
Browse files Browse the repository at this point in the history
This commit fixes a bug where attempting to redo after a debug run would run tests in the wrong
package.

The problem was that debug would create a test binary in the working directory. The creation of
that file would trigger an event. The event wouldn't be handled, but it still caused
prevPath to be updated.

By moving the storage of the lastPath to handler it removes the need for the Save method on
redoHandler, and keeps the tracking of the previous run details (last time.Time) together.
  • Loading branch information
dnephin committed Nov 21, 2020
1 parent 3ab60fc commit f3e8321
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
15 changes: 4 additions & 11 deletions internal/filewatcher/term_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import (
)

type redoHandler struct {
prevPath string
ch chan RunOptions
reset func()
ch chan RunOptions
reset func()
}

func newRedoHandler() *redoHandler {
Expand Down Expand Up @@ -77,10 +76,10 @@ func (r *redoHandler) Run(ctx context.Context) {
switch char {
case 'r':
chResume = make(chan struct{})
r.ch <- RunOptions{PkgPath: r.prevPath, resume: chResume}
r.ch <- RunOptions{resume: chResume}
case 'd':
chResume = make(chan struct{})
r.ch <- RunOptions{PkgPath: r.prevPath, Debug: true, resume: chResume}
r.ch <- RunOptions{Debug: true, resume: chResume}
case '\n':
fmt.Println()
continue
Expand Down Expand Up @@ -108,9 +107,3 @@ func (r *redoHandler) ResetTerm() {
r.reset()
}
}

func (r *redoHandler) Save(path string) {
if r != nil {
r.prevPath = path
}
}
2 changes: 0 additions & 2 deletions internal/filewatcher/term_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@ func (r *redoHandler) Ch() <-chan RunOptions {
func (r *redoHandler) SetupTerm() {}

func (r *redoHandler) ResetTerm() {}

func (r *redoHandler) Save(_ string) {}
11 changes: 6 additions & 5 deletions internal/filewatcher/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func Watch(dirs []string, run func(opts RunOptions) error) error {
resetTimer(timer)

redo.ResetTerm()
opts.PkgPath = h.lastPath
if err := h.runTests(opts); err != nil {
return fmt.Errorf("failed to rerun tests for %v: %v", opts.PkgPath, err)
}
Expand All @@ -73,7 +74,6 @@ func Watch(dirs []string, run func(opts RunOptions) error) error {
if err := h.handleEvent(event); err != nil {
return fmt.Errorf("failed to run tests for %v: %v", event.Name, err)
}
redo.Save(event.Name)

case err := <-watcher.Errors:
return fmt.Errorf("failed while watching files: %v", err)
Expand Down Expand Up @@ -198,8 +198,9 @@ func handleDirCreated(watcher *fsnotify.Watcher, event fsnotify.Event) (handled
}

type handler struct {
last time.Time
fn func(opts RunOptions) error
last time.Time
lastPath string
fn func(opts RunOptions) error
}

const floodThreshold = 250 * time.Millisecond
Expand All @@ -217,16 +218,16 @@ func (h *handler) handleEvent(event fsnotify.Event) error {
log.Debugf("skipping event received less than %v after the previous", floodThreshold)
return nil
}
return h.runTests(RunOptions{PkgPath: event.Name})
return h.runTests(RunOptions{PkgPath: "./" + filepath.Dir(event.Name)})
}

func (h *handler) runTests(opts RunOptions) error {
opts.PkgPath = "./" + filepath.Dir(opts.PkgPath)
fmt.Printf("\nRunning tests in %v\n", opts.PkgPath)

if err := h.fn(opts); err != nil {
return err
}
h.last = time.Now()
h.lastPath = opts.PkgPath
return nil
}

0 comments on commit f3e8321

Please sign in to comment.