-
Notifications
You must be signed in to change notification settings - Fork 0
/
makesense_test.go
183 lines (178 loc) · 4.92 KB
/
makesense_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"bufio"
"bytes"
"encoding/json"
"os/exec"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestIndentAndTrim(t *testing.T) {
type IndentAndTrimTest struct {
Input string
ExpectedIndentation int
ExpectedTrimmedString string
}
tests := []IndentAndTrimTest{
{
Input: " a",
ExpectedIndentation: 3,
ExpectedTrimmedString: "a",
},
{
Input: "a b c",
ExpectedIndentation: 0,
ExpectedTrimmedString: "a b c",
},
}
for _, test := range tests {
trimmed, indentLevel := findIndentAndTrim(test.Input)
if indentLevel != test.ExpectedIndentation {
t.Errorf("Indent level should be %d was %d", test.ExpectedIndentation, indentLevel)
}
if trimmed != test.ExpectedTrimmedString {
t.Errorf("Trimmed should be `%s` was `%s`", test.ExpectedTrimmedString, trimmed)
}
}
}
func TestTargetNameFromLine(t *testing.T) {
type TargetNameFromLineTest struct {
Input string
ExpectedTarget string
}
tests := []TargetNameFromLineTest{
{
Input: "Considering target file `b'.",
ExpectedTarget: "b",
},
{
Input: "Considering target file `my great target'.",
ExpectedTarget: "my great target",
},
{
Input: "Considering target file 'my great target'.",
ExpectedTarget: "my great target",
},
}
for _, test := range tests {
target := targetNameFromLine(test.Input)
if target != test.ExpectedTarget {
t.Errorf("Target should be `%s` was `%s`", test.ExpectedTarget, target)
}
}
}
func TestJsonOutput(t *testing.T) {
type JsonOutputTest struct {
Input string
ExpectedTarget map[string]interface{}
}
tests := []JsonOutputTest{
{
Input: "testdata/c",
ExpectedTarget: map[string]interface{}{
"Targets": map[string]interface{}{
"<ROOT>": map[string]interface{}{
"MustRemake": false,
"Name": "<ROOT>",
"Children": []interface{}{"Makefile"},
},
"Makefile": map[string]interface{}{
"MustRemake": false,
"Name": "Makefile",
"Children": []interface{}{"hellomake"},
},
"hellofunc.c": map[string]interface{}{
"MustRemake": false,
"Name": "hellofunc.c",
},
"hellofunc.o": map[string]interface{}{
"Children": []interface{}{"hellofunc.c", "hellomake.h"},
"Cmds": []interface{}{"gcc -c -o hellofunc.o hellofunc.c -I."},
"MustRemake": true,
"Name": "hellofunc.o",
},
"hellomake": map[string]interface{}{
"Children": []interface{}{"hellomake.o", "hellofunc.o"},
"Cmds": []interface{}{"gcc -o hellomake hellomake.o hellofunc.o -I."},
"MustRemake": true,
"Name": "hellomake",
},
"hellomake.c": map[string]interface{}{
"MustRemake": false,
"Name": "hellomake.c",
},
"hellomake.h": map[string]interface{}{
"MustRemake": false,
"Name": "hellomake.h",
},
"hellomake.o": map[string]interface{}{
"Children": []interface{}{"hellomake.c", "hellomake.h"},
"Cmds": []interface{}{"gcc -c -o hellomake.o hellomake.c -I."},
"MustRemake": true,
"Name": "hellomake.o",
},
},
},
},
{
Input: "testdata/basic",
ExpectedTarget: map[string]interface{}{
"Targets": map[string]interface{}{
"<ROOT>": map[string]interface{}{
"MustRemake": false,
"Name": "<ROOT>",
"Children": []interface{}{"Makefile"},
},
"Makefile": map[string]interface{}{
"Children": []interface{}{"a"},
"MustRemake": false,
"Name": "Makefile",
},
"a": map[string]interface{}{
"Cmds": []interface{}{"echo a"},
"MustRemake": true,
"Name": "a",
"Children": []interface{}{"b", "c", "test.txt"},
},
"b": map[string]interface{}{
"Cmds": []interface{}{"echo b"},
"MustRemake": true,
"Name": "b",
"Children": []interface{}{"c"},
},
"c": map[string]interface{}{
"Cmds": []interface{}{"echo c", "echo multi", "echo line"},
"MustRemake": true,
"Name": "c",
},
"test.txt": map[string]interface{}{
"Cmds": []interface{}{"# Testing comments", "echo \"hello\" >test.txt "},
"MustRemake": true,
"Name": "test.txt",
},
},
},
},
}
for _, test := range tests {
output, err := exec.Command("make", "-C", test.Input, "-Bnd").CombinedOutput()
if err != nil {
t.Error(string(output))
t.Fatal(err)
}
g := &MakesenseGraph{
Targets: map[string]*target{},
}
scanner := bufio.NewScanner(bytes.NewReader(output))
root := g.GetTarget("<ROOT>")
g.GraphScan(root, scanner, 0)
var b strings.Builder
g.dump(JSON, &b)
var m map[string]interface{}
json.NewDecoder(strings.NewReader(b.String())).Decode(&m)
if diff := cmp.Diff(test.ExpectedTarget, m); diff != "" {
t.Errorf("Diff: %s", diff)
}
}
}