summaryrefslogtreecommitdiffstats
path: root/pkg/tasks
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2023-07-09 21:09:52 +1000
committerJesse Duffield <jessedduffield@gmail.com>2023-07-10 17:12:21 +1000
commit6b9390409eb533fe87648be55e9db7d36b1d9ee3 (patch)
tree43e35b86a17dbee8aac245cc93c2645727b61148 /pkg/tasks
parent8964cedf27cbdb81f59e2400cfc89684d7458605 (diff)
Use an interface for tasks instead of a concrete struct
By using an interface for tasks we can use a fake implementation in tests with extra methods
Diffstat (limited to 'pkg/tasks')
-rw-r--r--pkg/tasks/async_handler.go6
-rw-r--r--pkg/tasks/async_handler_test.go4
-rw-r--r--pkg/tasks/tasks.go11
-rw-r--r--pkg/tasks/tasks_test.go37
4 files changed, 30 insertions, 28 deletions
diff --git a/pkg/tasks/async_handler.go b/pkg/tasks/async_handler.go
index 03ae100ca..6f3f41b29 100644
--- a/pkg/tasks/async_handler.go
+++ b/pkg/tasks/async_handler.go
@@ -18,10 +18,10 @@ type AsyncHandler struct {
lastId int
mutex deadlock.Mutex
onReject func()
- onWorker func(func(*gocui.Task))
+ onWorker func(func(gocui.Task))
}
-func NewAsyncHandler(onWorker func(func(*gocui.Task))) *AsyncHandler {
+func NewAsyncHandler(onWorker func(func(gocui.Task))) *AsyncHandler {
return &AsyncHandler{
mutex: deadlock.Mutex{},
onWorker: onWorker,
@@ -34,7 +34,7 @@ func (self *AsyncHandler) Do(f func() func()) {
id := self.currentId
self.mutex.Unlock()
- self.onWorker(func(*gocui.Task) {
+ self.onWorker(func(gocui.Task) {
after := f()
self.handle(after, id)
})
diff --git a/pkg/tasks/async_handler_test.go b/pkg/tasks/async_handler_test.go
index d4a6f6c3b..ebead7aa7 100644
--- a/pkg/tasks/async_handler_test.go
+++ b/pkg/tasks/async_handler_test.go
@@ -13,8 +13,8 @@ func TestAsyncHandler(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(2)
- onWorker := func(f func(*gocui.Task)) {
- go f(&gocui.Task{})
+ onWorker := func(f func(gocui.Task)) {
+ go f(gocui.NewFakeTask())
}
handler := NewAsyncHandler(onWorker)
handler.onReject = func() {
diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go
index 498d8a1da..e2aed471e 100644
--- a/pkg/tasks/tasks.go
+++ b/pkg/tasks/tasks.go
@@ -50,7 +50,10 @@ type ViewBufferManager struct {
onEndOfInput func()
// see docs/dev/Busy.md
- newTask func() *gocui.Task
+ // A gocui task is not the same thing as the tasks defined in this file.
+ // A gocui task simply represents the fact that lazygit is busy doing something,
+ // whereas the tasks in this file are about rendering content to a view.
+ newGocuiTask func() gocui.Task
// if the user flicks through a heap of items, with each one
// spawning a process to render something to the main view,
@@ -80,7 +83,7 @@ func NewViewBufferManager(
refreshView func(),
onEndOfInput func(),
onNewKey func(),
- newTask func() *gocui.Task,
+ newGocuiTask func() gocui.Task,
) *ViewBufferManager {
return &ViewBufferManager{
Log: log,
@@ -90,7 +93,7 @@ func NewViewBufferManager(
onEndOfInput: onEndOfInput,
readLines: make(chan LinesToRead, 1024),
onNewKey: onNewKey,
- newTask: newTask,
+ newGocuiTask: newGocuiTask,
}
}
@@ -296,7 +299,7 @@ type TaskOpts struct {
}
func (self *ViewBufferManager) NewTask(f func(TaskOpts) error, key string) error {
- task := self.newTask()
+ task := self.newGocuiTask()
var completeTaskOnce sync.Once
diff --git a/pkg/tasks/tasks_test.go b/pkg/tasks/tasks_test.go
index 49eedad31..6f2edbbf4 100644
--- a/pkg/tasks/tasks_test.go
+++ b/pkg/tasks/tasks_test.go
@@ -20,11 +20,6 @@ func getCounter() (func(), func() int) {
return func() { counter++ }, func() int { return counter }
}
-func getIncDecCounter(initialValue int) (func(), func(), func() int) {
- counter := initialValue
- return func() { counter++ }, func() { counter-- }, func() int { return counter }
-}
-
func TestNewCmdTaskInstantStop(t *testing.T) {
writer := bytes.NewBuffer(nil)
beforeStart, getBeforeStartCallCount := getCounter()
@@ -32,8 +27,8 @@ func TestNewCmdTaskInstantStop(t *testing.T) {
onEndOfInput, getOnEndOfInputCallCount := getCounter()
onNewKey, getOnNewKeyCallCount := getCounter()
onDone, getOnDoneCallCount := getCounter()
- task := &gocui.Task{}
- newTask := func() *gocui.Task {
+ task := gocui.NewFakeTask()
+ newTask := func() gocui.Task {
return task
}
@@ -79,8 +74,8 @@ func TestNewCmdTaskInstantStop(t *testing.T) {
}
}
- if getBusyCount() != 0 {
- t.Errorf("expected busy count to be 0, got %d", getBusyCount())
+ if task.Status() != gocui.TaskStatusDone {
+ t.Errorf("expected task status to be 'done', got '%s'", task.FormatStatus())
}
expectedContent := ""
@@ -97,7 +92,10 @@ func TestNewCmdTask(t *testing.T) {
onEndOfInput, getOnEndOfInputCallCount := getCounter()
onNewKey, getOnNewKeyCallCount := getCounter()
onDone, getOnDoneCallCount := getCounter()
- incBusyCount, decBusyCount, getBusyCount := getIncDecCounter(1)
+ task := gocui.NewFakeTask()
+ newTask := func() gocui.Task {
+ return task
+ }
manager := NewViewBufferManager(
utils.NewDummyLog(),
@@ -106,8 +104,7 @@ func TestNewCmdTask(t *testing.T) {
refreshView,
onEndOfInput,
onNewKey,
- incBusyCount,
- decBusyCount,
+ newTask,
)
stop := make(chan struct{})
@@ -127,7 +124,7 @@ func TestNewCmdTask(t *testing.T) {
close(stop)
wg.Done()
}()
- _ = fn(TaskOpts{Stop: stop, InitialContentLoaded: decBusyCount})
+ _ = fn(TaskOpts{Stop: stop, InitialContentLoaded: func() { task.Done() }})
wg.Wait()
@@ -148,8 +145,8 @@ func TestNewCmdTask(t *testing.T) {
}
}
- if getBusyCount() != 0 {
- t.Errorf("expected busy count to be 0, got %d", getBusyCount())
+ if task.Status() != gocui.TaskStatusDone {
+ t.Errorf("expected task status to be 'done', got '%s'", task.FormatStatus())
}
expectedContent := "prefix\ntest\n"
@@ -230,7 +227,10 @@ func TestNewCmdTaskRefresh(t *testing.T) {
lineCountsOnRefresh = append(lineCountsOnRefresh, strings.Count(writer.String(), "\n"))
}
- decBusyCount := func() {}
+ task := gocui.NewFakeTask()
+ newTask := func() gocui.Task {
+ return task
+ }
manager := NewViewBufferManager(
utils.NewDummyLog(),
@@ -239,8 +239,7 @@ func TestNewCmdTaskRefresh(t *testing.T) {
refreshView,
func() {},
func() {},
- func() {},
- decBusyCount,
+ newTask,
)
stop := make(chan struct{})
@@ -260,7 +259,7 @@ func TestNewCmdTaskRefresh(t *testing.T) {
close(stop)
wg.Done()
}()
- _ = fn(TaskOpts{Stop: stop, InitialContentLoaded: decBusyCount})
+ _ = fn(TaskOpts{Stop: stop, InitialContentLoaded: func() { task.Done() }})
wg.Wait()