Skip to content

Commit

Permalink
watch: make space for debug flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed Nov 21, 2020
1 parent 2062ac3 commit a6916e7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func Run(name string, args []string) error {
}

func runWatcher(opts *options) error {
fn := func(pkg string) error {
fn := func(runOpts filewatcher.RunOptions) error {
opts := *opts
opts.packages = []string{pkg}
opts.packages = []string{runOpts.PkgPath}
err := run(&opts)
if !isExitCoder(err) {
return err
Expand Down
10 changes: 6 additions & 4 deletions internal/filewatcher/term_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

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

Expand All @@ -25,7 +25,7 @@ func newRedoHandler() *redoHandler {
log.Warnf("failed to put terminal (fd %d) into raw mode: %v", fd, err)
return nil
}
return &redoHandler{ch: make(chan string), reset: reset}
return &redoHandler{ch: make(chan RunOptions), reset: reset}
}

func enableNonBlockingRead(fd int) (func(), error) {
Expand Down Expand Up @@ -70,14 +70,16 @@ func (r *redoHandler) Run(ctx context.Context) {

switch char {
case 'r':
r.ch <- r.prevPath
r.ch <- RunOptions{PkgPath: r.prevPath}
case 'd':
r.ch <- RunOptions{PkgPath: r.prevPath, Debug: true}
case '\n':
fmt.Println()
}
}
}

func (r *redoHandler) Ch() <-chan string {
func (r *redoHandler) Ch() <-chan RunOptions {
if r == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/filewatcher/term_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func newRedoHandler() *redoHandler {

func (r *redoHandler) Run(_ context.Context) {}

func (r *redoHandler) Ch() <-chan string {
func (r *redoHandler) Ch() <-chan RunOptions {
return nil
}

Expand Down
26 changes: 16 additions & 10 deletions internal/filewatcher/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import (

const maxDepth = 7

func Watch(dirs []string, run func(pkg string) error) error {
type RunOptions struct {
PkgPath string
Debug bool
}

func Watch(dirs []string, run func(opts RunOptions) error) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down Expand Up @@ -46,10 +51,10 @@ func Watch(dirs []string, run func(pkg string) error) error {
case <-timer.C:
return fmt.Errorf("exceeded idle timeout while watching files")

case path := <-redo.Ch():
case opts := <-redo.Ch():
resetTimer(timer)
if err := h.runTests(path); err != nil {
return fmt.Errorf("failed to rerun tests for %v: %v", path, err)
if err := h.runTests(opts); err != nil {
return fmt.Errorf("failed to rerun tests for %v: %v", opts.PkgPath, err)
}

case event := <-watcher.Events:
Expand Down Expand Up @@ -189,7 +194,7 @@ func handleDirCreated(watcher *fsnotify.Watcher, event fsnotify.Event) (handled

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

const floodThreshold = 250 * time.Millisecond
Expand All @@ -207,13 +212,14 @@ 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(event.Name)
return h.runTests(RunOptions{PkgPath: event.Name})
}

func (h *handler) runTests(path string) error {
pkg := "./" + filepath.Dir(path)
fmt.Printf("\nRunning tests in %v\n", pkg)
if err := h.fn(pkg); err != nil {
func (h *handler) runTests(opts RunOptions) error {
fmt.Printf("\nRunning tests in %v\n", opts.PkgPath)

opts.PkgPath = "./" + filepath.Dir(opts.PkgPath)
if err := h.fn(opts); err != nil {
return err
}
h.last = time.Now()
Expand Down
2 changes: 1 addition & 1 deletion internal/filewatcher/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestHandler_HandleEvent(t *testing.T) {

fn := func(t *testing.T, tc testCase) {
var ran bool
run := func(pkg string) error {
run := func(opts RunOptions) error {
ran = true
return nil
}
Expand Down

0 comments on commit a6916e7

Please sign in to comment.