1
0
mirror of https://github.com/go-task/task.git synced 2024-12-14 10:52:43 +02:00
task/internal/summary/summary_test.go

174 lines
3.6 KiB
Go
Raw Normal View History

2019-02-24 18:05:37 +02:00
package summary_test
import (
"bytes"
"strings"
2019-03-04 13:04:31 +02:00
"testing"
2019-02-24 18:05:37 +02:00
"github.com/go-task/task/v2/internal/logger"
"github.com/go-task/task/v2/internal/summary"
"github.com/go-task/task/v2/internal/taskfile"
2019-03-04 13:04:31 +02:00
2019-02-24 18:05:37 +02:00
"github.com/stretchr/testify/assert"
)
2019-02-24 18:10:59 +02:00
func TestPrintsDependenciesIfPresent(t *testing.T) {
buffer, l := createDummyLogger()
2019-02-24 18:05:37 +02:00
task := &taskfile.Task{
Deps: []*taskfile.Dep{
{Task: "dep1"},
{Task: "dep2"},
{Task: "dep3"},
},
}
summary.Print(&l, task)
assert.Contains(t, buffer.String(), "\ndependencies:\n - dep1\n - dep2\n - dep3\n")
2019-02-24 18:05:37 +02:00
}
func createDummyLogger() (*bytes.Buffer, logger.Logger) {
2019-02-24 18:05:37 +02:00
buffer := &bytes.Buffer{}
l := logger.Logger{
Stderr: buffer,
Stdout: buffer,
2019-02-24 18:05:37 +02:00
Verbose: false,
}
return buffer, l
}
func TestDoesNotPrintDependenciesIfMissing(t *testing.T) {
buffer, l := createDummyLogger()
2019-02-24 18:05:37 +02:00
task := &taskfile.Task{
Deps: []*taskfile.Dep{},
}
summary.Print(&l, task)
assert.NotContains(t, buffer.String(), "dependencies:")
}
2019-02-24 18:10:59 +02:00
func TestPrintTaskName(t *testing.T) {
buffer, l := createDummyLogger()
2019-02-24 18:10:59 +02:00
task := &taskfile.Task{
Task: "my-task-name",
}
summary.Print(&l, task)
assert.Contains(t, buffer.String(), "task: my-task-name\n")
}
func TestPrintTaskCommandsIfPresent(t *testing.T) {
buffer, l := createDummyLogger()
2019-02-24 18:10:59 +02:00
task := &taskfile.Task{
Cmds: []*taskfile.Cmd{
{Cmd: "command-1"},
{Cmd: "command-2"},
2019-02-24 18:23:31 +02:00
{Task: "task-1"},
2019-02-24 18:10:59 +02:00
},
}
summary.Print(&l, task)
assert.Contains(t, buffer.String(), "\ncommands:\n")
assert.Contains(t, buffer.String(), "\n - command-1\n")
assert.Contains(t, buffer.String(), "\n - command-2\n")
2019-02-24 18:23:31 +02:00
assert.Contains(t, buffer.String(), "\n - Task: task-1\n")
2019-02-24 18:10:59 +02:00
}
2019-02-24 18:12:22 +02:00
func TestDoesNotPrintCommandIfMissing(t *testing.T) {
buffer, l := createDummyLogger()
2019-02-24 18:12:22 +02:00
task := &taskfile.Task{
Cmds: []*taskfile.Cmd{},
}
summary.Print(&l, task)
assert.NotContains(t, buffer.String(), "commands")
}
2019-02-24 18:20:29 +02:00
2019-02-24 18:29:03 +02:00
func TestLayout(t *testing.T) {
buffer, l := createDummyLogger()
2019-02-24 18:20:29 +02:00
task := &taskfile.Task{
Task: "sample-task",
Summary: "line1\nline2\nline3\n",
Deps: []*taskfile.Dep{
{Task: "dependency"},
},
Cmds: []*taskfile.Cmd{
{Cmd: "command"},
},
}
summary.Print(&l, task)
2019-02-24 18:25:03 +02:00
assert.Equal(t, expectedOutput(), buffer.String())
}
func expectedOutput() string {
2019-02-24 18:28:06 +02:00
expected := `task: sample-task
line1
line2
line3
dependencies:
- dependency
commands:
- command
`
2019-02-24 18:25:03 +02:00
return expected
2019-02-24 18:20:29 +02:00
}
2019-02-24 19:20:59 +02:00
func TestPrintDescriptionAsFallback(t *testing.T) {
buffer, l := createDummyLogger()
2019-02-24 19:20:59 +02:00
taskWithoutSummary := &taskfile.Task{
Desc: "description",
}
taskWithSummary := &taskfile.Task{
Desc: "description",
Summary: "summary",
}
taskWithoutSummaryOrDescription := &taskfile.Task{}
2019-02-24 19:20:59 +02:00
summary.Print(&l, taskWithoutSummary)
2019-02-24 19:20:59 +02:00
assert.Contains(t, buffer.String(), "description")
buffer.Reset()
summary.Print(&l, taskWithSummary)
2019-02-24 19:20:59 +02:00
assert.NotContains(t, buffer.String(), "description")
buffer.Reset()
summary.Print(&l, taskWithoutSummaryOrDescription)
assert.Contains(t, buffer.String(), "\n(task does not have description or summary)\n")
2019-02-24 19:20:59 +02:00
}
func TestPrintAllWithSpaces(t *testing.T) {
buffer, l := createDummyLogger()
t1 := &taskfile.Task{Task: "t1"}
t2 := &taskfile.Task{Task: "t2"}
t3 := &taskfile.Task{Task: "t3"}
tasks := make(taskfile.Tasks, 3)
tasks["t1"] = t1
tasks["t2"] = t2
tasks["t3"] = t3
2019-03-04 13:46:53 +02:00
summary.PrintAll(&l,
&taskfile.Taskfile{Tasks: tasks},
[]taskfile.Call{{Task: "t1"}, {Task: "t2"}, {Task: "t3"}})
assert.True(t, strings.HasPrefix(buffer.String(), "task: t1"))
assert.Contains(t, buffer.String(), "\n(task does not have description or summary)\n\n\ntask: t2")
assert.Contains(t, buffer.String(), "\n(task does not have description or summary)\n\n\ntask: t3")
}