Skip to content

Commit 06031ef

Browse files
committed
cyclic: refactor to return error instead
1 parent 9bea80b commit 06031ef

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

cyclic.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
package task
22

3-
// HasCyclicDep checks if a task tree has any cyclic dependency
4-
func (e *Executor) HasCyclicDep() bool {
3+
// CheckCyclicDep checks if a task tree has any cyclic dependency
4+
func (e *Executor) CheckCyclicDep() error {
55
visits := make(map[string]struct{}, len(e.Tasks))
66

7-
var checkCyclicDep func(string, *Task) bool
8-
checkCyclicDep = func(name string, t *Task) bool {
7+
var checkCyclicDep func(string, *Task) error
8+
checkCyclicDep = func(name string, t *Task) error {
99
if _, ok := visits[name]; ok {
10-
return false
10+
return ErrCyclicDepDetected
1111
}
1212
visits[name] = struct{}{}
1313
defer delete(visits, name)
1414

1515
for _, d := range t.Deps {
16-
if !checkCyclicDep(d.Task, e.Tasks[d.Task]) {
17-
return false
16+
if err := checkCyclicDep(d.Task, e.Tasks[d.Task]); err != nil {
17+
return err
1818
}
1919
}
20-
return true
20+
return nil
2121
}
2222

2323
for k, v := range e.Tasks {
24-
if !checkCyclicDep(k, v) {
25-
return true
24+
if err := checkCyclicDep(k, v); err != nil {
25+
return err
2626
}
2727
}
28-
return false
28+
return nil
2929
}

cyclic_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"testing"
55

66
"github.com/go-task/task"
7+
8+
"github.com/stretchr/testify/assert"
79
)
810

911
func TestCyclicDepCheck(t *testing.T) {
@@ -18,9 +20,7 @@ func TestCyclicDepCheck(t *testing.T) {
1820
},
1921
}
2022

21-
if !isCyclic.HasCyclicDep() {
22-
t.Error("Task should be cyclic")
23-
}
23+
assert.Equal(t, task.ErrCyclicDepDetected, isCyclic.CheckCyclicDep(), "task should be cyclic")
2424

2525
isNotCyclic := &task.Executor{
2626
Tasks: task.Tasks{
@@ -34,7 +34,5 @@ func TestCyclicDepCheck(t *testing.T) {
3434
},
3535
}
3636

37-
if isNotCyclic.HasCyclicDep() {
38-
t.Error("Task should not be cyclic")
39-
}
37+
assert.NoError(t, isNotCyclic.CheckCyclicDep())
4038
}

errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
var (
9-
// ErrCyclicDependencyDetected is returned when a cyclic dependency was found in the Taskfile
10-
ErrCyclicDependencyDetected = errors.New("task: cyclic dependency detected")
9+
// ErrCyclicDepDetected is returned when a cyclic dependency was found in the Taskfile
10+
ErrCyclicDepDetected = errors.New("task: cyclic dependency detected")
1111
// ErrTaskfileAlreadyExists is returned on creating a Taskfile if one already exists
1212
ErrTaskfileAlreadyExists = errors.New("task: A Taskfile already exists")
1313
)

task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ type Task struct {
6161

6262
// Run runs Task
6363
func (e *Executor) Run(args ...string) error {
64-
if e.HasCyclicDep() {
65-
return ErrCyclicDependencyDetected
64+
if err := e.CheckCyclicDep(); err != nil {
65+
return err
6666
}
6767

6868
if e.Stdin == nil {

0 commit comments

Comments
 (0)