Skip to content

Commit

Permalink
internal/filewatcher: rename some types and methods
Browse files Browse the repository at this point in the history
The new names should hopefully make this code easier to read and understand.
  • Loading branch information
dnephin committed May 28, 2021
1 parent cdd0de1 commit cba7ff6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 40 deletions.
8 changes: 4 additions & 4 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ type watchRuns struct {
prevExec *testjson.Execution
}

func (w *watchRuns) run(runOpts filewatcher.RunOptions) error {
if runOpts.Debug {
func (w *watchRuns) run(event filewatcher.Event) error {
if event.Debug {
path, cleanup, err := delveInitFile(w.prevExec)
if err != nil {
return fmt.Errorf("failed to write delve init file: %w", err)
}
defer cleanup()
o := delveOpts{
pkgPath: runOpts.PkgPath,
pkgPath: event.PkgPath,
args: w.opts.args,
initFilePath: path,
}
Expand All @@ -41,7 +41,7 @@ func (w *watchRuns) run(runOpts filewatcher.RunOptions) error {
}

opts := w.opts
opts.packages = []string{runOpts.PkgPath}
opts.packages = []string{event.PkgPath}
var err error
if w.prevExec, err = runSingle(&opts); !isExitCoder(err) {
return err
Expand Down
33 changes: 20 additions & 13 deletions internal/filewatcher/term_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ import (
"gotest.tools/gotestsum/log"
)

type redoHandler struct {
ch chan RunOptions
type terminal struct {
ch chan Event
reset func()
}

func newRedoHandler() *redoHandler {
h := &redoHandler{ch: make(chan RunOptions)}
h.SetupTerm()
func newTerminal() *terminal {
h := &terminal{ch: make(chan Event)}
h.Start()
return h
}

func (r *redoHandler) SetupTerm() {
// Start the terminal is non-blocking read mode. The terminal can be reset to
// normal mode by calling Reset.
func (r *terminal) Start() {
if r == nil {
return
}
Expand Down Expand Up @@ -59,7 +61,9 @@ func enableNonBlockingRead(fd int) (func(), error) {
return reset, nil
}

func (r *redoHandler) Run(ctx context.Context) {
// Monitor the terminal for key presses. If the key press is associated with an
// action, an event will be sent to channel returned by Events.
func (r *terminal) Monitor(ctx context.Context) {
if r == nil {
return
}
Expand All @@ -76,16 +80,16 @@ func (r *redoHandler) Run(ctx context.Context) {
switch char {
case 'r':
chResume = make(chan struct{})
r.ch <- RunOptions{resume: chResume}
r.ch <- Event{resume: chResume}
case 'd':
chResume = make(chan struct{})
r.ch <- RunOptions{Debug: true, resume: chResume}
r.ch <- Event{Debug: true, resume: chResume}
case 'a':
chResume = make(chan struct{})
r.ch <- RunOptions{resume: chResume, PkgPath: "./..."}
r.ch <- Event{resume: chResume, PkgPath: "./..."}
case 'l':
chResume = make(chan struct{})
r.ch <- RunOptions{resume: chResume, reloadPaths: true}
r.ch <- Event{resume: chResume, reloadPaths: true}
case '\n':
fmt.Println()
continue
Expand All @@ -101,14 +105,17 @@ func (r *redoHandler) Run(ctx context.Context) {
}
}

func (r *redoHandler) Ch() <-chan RunOptions {
// Events returns a channel which will receive events when keys are pressed.
// When an event is received, the caller must close the resume channel to
// resume monitoring for events.
func (r *terminal) Events() <-chan Event {
if r == nil {
return nil
}
return r.ch
}

func (r *redoHandler) ResetTerm() {
func (r *terminal) Reset() {
if r != nil && r.reset != nil {
r.reset()
}
Expand Down
12 changes: 6 additions & 6 deletions internal/filewatcher/term_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package filewatcher

import "context"

type redoHandler struct{}
type terminal struct{}

func newRedoHandler() *redoHandler {
func newTerminal() *terminal {
return nil
}

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

func (r *redoHandler) Ch() <-chan RunOptions {
func (r *terminal) Events() <-chan Event {
return nil
}

func (r *redoHandler) SetupTerm() {}
func (r *terminal) Start() {}

func (r *redoHandler) ResetTerm() {}
func (r *terminal) Reset() {}
32 changes: 16 additions & 16 deletions internal/filewatcher/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

const maxDepth = 7

type RunOptions struct {
type Event struct {
PkgPath string
Debug bool
resume chan struct{}
Expand All @@ -24,7 +24,7 @@ type RunOptions struct {

// 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 {
func Watch(dirs []string, run func(Event) error) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand All @@ -41,33 +41,33 @@ func Watch(dirs []string, run func(opts RunOptions) error) error {
timer := time.NewTimer(maxIdleTime)
defer timer.Stop()

redo := newRedoHandler()
defer redo.ResetTerm()
go redo.Run(ctx)
term := newTerminal()
defer term.Reset()
go term.Monitor(ctx)

h := &handler{last: time.Now(), fn: run}
for {
select {
case <-timer.C:
return fmt.Errorf("exceeded idle timeout while watching files")

case opts := <-redo.Ch():
case event := <-term.Events():
resetTimer(timer)

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

redo.ResetTerm()
if err := h.runTests(opts); err != nil {
return fmt.Errorf("failed to rerun tests for %v: %v", opts.PkgPath, err)
term.Reset()
if err := h.runTests(event); err != nil {
return fmt.Errorf("failed to rerun tests for %v: %v", event.PkgPath, err)
}
redo.SetupTerm()
close(opts.resume)
term.Start()
close(event.resume)

case event := <-watcher.Events:
resetTimer(timer)
Expand Down Expand Up @@ -218,7 +218,7 @@ func handleDirCreated(watcher *fsnotify.Watcher, event fsnotify.Event) (handled
type handler struct {
last time.Time
lastPath string
fn func(opts RunOptions) error
fn func(opts Event) error
}

const floodThreshold = 250 * time.Millisecond
Expand All @@ -236,10 +236,10 @@ 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: "./" + filepath.Dir(event.Name)})
return h.runTests(Event{PkgPath: "./" + filepath.Dir(event.Name)})
}

func (h *handler) runTests(opts RunOptions) error {
func (h *handler) runTests(opts Event) error {
if opts.PkgPath == "" {
opts.PkgPath = h.lastPath
}
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(opts RunOptions) error {
run := func(opts Event) error {
ran = true
return nil
}
Expand Down

0 comments on commit cba7ff6

Please sign in to comment.