forked from magefile/mage
/
deps_internal_test.go
56 lines (47 loc) · 1.09 KB
/
deps_internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package mg
import (
"bytes"
"fmt"
"log"
"os"
"strings"
"testing"
)
func TestDepsLogging(t *testing.T) {
os.Setenv("MAGEFILE_VERBOSE", "1")
defer os.Unsetenv("MAGEFILE_VERBOSE")
buf := &bytes.Buffer{}
defaultLogger := logger
logger = log.New(buf, "", 0)
defer func() { logger = defaultLogger }()
foo()
if strings.Count(buf.String(), "Running dependency: github.com/magefile/mage/mg.baz") != 1 {
t.Fatalf("expected one baz to be logged, but got\n%s", buf)
}
}
func foo() {
Deps(bar, baz)
}
func bar() {
Deps(baz)
}
func baz() {}
func TestDepWasNotInvoked(t *testing.T) {
fn1 := func() error {
return nil
}
defer func() {
err := recover()
if err == nil {
t.Fatal("expected panic, but didn't get one")
}
gotErr := fmt.Sprint(err)
wantErr := "non-function used as a target dependency: <nil>. The mg.Deps, mg.SerialDeps and mg.CtxDeps functions accept function names, such as mg.Deps(TargetA, TargetB)"
if !strings.Contains(gotErr, wantErr) {
t.Fatalf(`expected to get "%s" but got "%s"`, wantErr, gotErr)
}
}()
func(fns ...interface{}) {
checkFns(fns)
}(fn1())
}