Merge pull request #2335 from git-lfs/githistory-log-list-task

git/githistory/log: implement ListTask type
This commit is contained in:
Taylor Blau 2017-06-19 17:40:25 -04:00 committed by GitHub
commit 42661cdeed
3 changed files with 101 additions and 0 deletions

@ -0,0 +1,41 @@
package log
import "fmt"
// ListTask is a Task implementation that logs all updates in a list where each
// entry is line-delimited.
//
// For example:
// entry #1
// entry #2
// msg: ..., done
type ListTask struct {
msg string
ch chan string
}
// NewListTask instantiates a new *ListTask instance with the given message.
func NewListTask(msg string) *ListTask {
return &ListTask{
msg: msg,
ch: make(chan string, 1),
}
}
// Entry logs a line-delimited task entry.
func (l *ListTask) Entry(update string) {
l.ch <- fmt.Sprintf("%s\n", update)
}
func (l *ListTask) Complete() {
l.ch <- fmt.Sprintf("%s: ...", l.msg)
close(l.ch)
}
// Throttled implements the Task.Throttled function and ensures that all log
// updates are printed to the sink.
func (l *ListTask) Throttled() bool { return false }
// Updates implements the Task.Updates function and returns a channel of updates
// to log to the sink.
func (l *ListTask) Updates() <-chan string { return l.ch }

@ -0,0 +1,52 @@
package log
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestListTaskCallsDoneWhenComplete(t *testing.T) {
task := NewListTask("example")
task.Complete()
select {
case update, ok := <-task.Updates():
assert.Equal(t, "example: ...", update)
assert.True(t, ok,
"git/githistory/log: expected Updates() to remain open")
default:
t.Fatal("git/githistory/log: expected update from *ListTask")
}
select {
case update, ok := <-task.Updates():
assert.False(t, ok,
"git/githistory.log: unexpected *ListTask.Update(): %s", update)
default:
t.Fatal("git/githistory/log: expected *ListTask.Updates() to be closed")
}
}
func TestListTaskWritesEntries(t *testing.T) {
task := NewListTask("example")
task.Entry("1")
select {
case update, ok := <-task.Updates():
assert.True(t, ok,
"git/githistory/log: expected ListTask.Updates() to remain open")
assert.Equal(t, "1\n", update)
default:
t.Fatal("git/githistory/log: expected task.Updates() to have an update")
}
}
func TestListTaskIsNotThrottled(t *testing.T) {
task := NewListTask("example")
throttled := task.Throttled()
assert.False(t, throttled,
"git/githistory/log: expected *ListTask to be Throttle()-d")
}

@ -94,6 +94,14 @@ func (l *Logger) Percentage(msg string, total uint64) *PercentageTask {
return t
}
// List creates and enqueues a new *ListTask.
func (l *Logger) List(msg string) *ListTask {
t := NewListTask(msg)
l.enqueue(t)
return t
}
// enqueue enqueues the given Tasks "ts".
func (l *Logger) enqueue(ts ...Task) {
if l == nil {