summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/mkshwalker_test.go
blob: a638dd1e347440450fea8d3aeab2fa64fbd18512 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package pkglint

import "gopkg.in/check.v1"

func (s *Suite) Test_MkShWalker_Walk(c *check.C) {

	pathFor := map[string]bool{}

	outputPathFor := func(kinds ...string) {
		for key := range pathFor {
			pathFor[key] = false
		}
		for _, kind := range kinds {
			pathFor[kind] = true
		}
	}

	test := func(program string, output ...string) {
		list, err := parseShellProgram(dummyLine, program)

		if !c.Check(err, check.IsNil) || !c.Check(list, check.NotNil) {
			return
		}

		walker := NewMkShWalker()
		var commands []string

		add := func(kind string, format string, args ...interface{}) {
			if format != "" && !contains(format, "%") {
				panic(format)
			}
			detail := sprintf(format, args...)
			commands = append(commands, sprintf("%16s %s", kind, detail))
			if pathFor[kind] {
				commands = append(commands, sprintf("%16s %s", "Path", walker.Path()))
			}
		}

		callback := &walker.Callback
		callback.List = func(list *MkShList) { add("List", "with %d andOrs", len(list.AndOrs)) }
		callback.AndOr = func(andor *MkShAndOr) { add("AndOr", "with %d pipelines", len(andor.Pipes)) }
		callback.Pipeline = func(pipeline *MkShPipeline) { add("Pipeline", "with %d commands", len(pipeline.Cmds)) }
		callback.Command = func(command *MkShCommand) { add("Command", "") }
		callback.SimpleCommand = func(command *MkShSimpleCommand) {
			add("SimpleCommand", "%s", NewStrCommand(command).String())
		}
		callback.CompoundCommand = func(command *MkShCompoundCommand) { add("CompoundCommand", "") }
		callback.Case = func(caseClause *MkShCase) { add("Case", "with %d items", len(caseClause.Cases)) }
		callback.CaseItem = func(caseItem *MkShCaseItem) { add("CaseItem", "with %d patterns", len(caseItem.Patterns)) }
		callback.FunctionDefinition = func(funcdef *MkShFunctionDefinition) { add("FunctionDef", "for %s", funcdef.Name) }
		callback.If = func(ifClause *MkShIf) { add("If", "with %d then-branches", len(ifClause.Conds)) }
		callback.Loop = func(loop *MkShLoop) { add("Loop", "") }
		callback.Words = func(words []*ShToken) { add("Words", "with %d words", len(words)) }
		callback.Word = func(word *ShToken) { add("Word", "%s", word.MkText) }
		callback.Redirects = func(redirects []*MkShRedirection) { add("Redirects", "with %d redirects", len(redirects)) }
		callback.Redirect = func(redirect *MkShRedirection) { add("Redirect", "%s", redirect.Op) }
		callback.For = func(forClause *MkShFor) { add("For", "variable %s", forClause.Varname) }
		callback.Varname = func(varname string) { add("Varname", "%s", varname) }

		walker.Walk(list)

		// TODO: Provide a reduced AST that omits all "AndOr with 1 pipelines", etc.
		//  It should look like this:
		//
		//  List with 5 andOrs (or generic Commands?)
		//    If with 1 then-branch(es)
		//      SimpleCommand condition
		//      SimpleCommand action
		//    Case with 1 item(s)
		//      ...

		c.Check(commands, deepEquals, output)

		// After parsing, there is not a single level of indentation,
		// therefore even Parent(0) returns nil.
		//
		// This ensures that the w.push/w.pop calls are balanced.
		c.Check(walker.Parent(0), equals, nil)
	}

	outputPathFor("SimpleCommand")
	test(""+
		"if condition; then action; else case selector in pattern) case-item-action ;; esac; fi; "+
		"set -e; "+
		"cd ${WRKSRC}/locale; "+
		"for lang in *.po; do "+
		"  [ \"$${lang}\" = \"wxstd.po\" ] && continue; "+
		"  ${TOOLS_PATH.msgfmt} -c -o \"$${lang%.po}.mo\" \"$${lang}\"; "+
		"done; "+
		"while :; do fun() { :; } 1>&2; done",

		"            List with 5 andOrs",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		" CompoundCommand ",
		"              If with 1 then-branches",
		"            List with 1 andOrs",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand condition",
		"            Path List.AndOr[0].Pipeline[0].Command[0].CompoundCommand.If.List[0].AndOr[0].Pipeline[0].Command[0].SimpleCommand",
		"            Word condition",
		"            List with 1 andOrs",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand action",
		"            Path List.AndOr[0].Pipeline[0].Command[0].CompoundCommand.If.List[1].AndOr[0].Pipeline[0].Command[0].SimpleCommand",
		"            Word action",
		"            List with 1 andOrs",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		" CompoundCommand ",
		"            Case with 1 items",
		"            Word selector",
		"        CaseItem with 1 patterns",
		"           Words with 1 words",
		"            Word pattern",
		"            List with 1 andOrs",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand case-item-action",
		"            Path List.AndOr[0].Pipeline[0].Command[0].CompoundCommand.If."+
			"List[2].AndOr[0].Pipeline[0].Command[0].CompoundCommand.Case.CaseItem[0]."+
			"List.AndOr[0].Pipeline[0].Command[0].SimpleCommand",
		"            Word case-item-action",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand set -e",
		"            Path List.AndOr[1].Pipeline[0].Command[0].SimpleCommand",
		"            Word set",
		"           Words with 1 words",
		"            Word -e",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand cd ${WRKSRC}/locale",
		"            Path List.AndOr[2].Pipeline[0].Command[0].SimpleCommand",
		"            Word cd",
		"           Words with 1 words",
		"            Word ${WRKSRC}/locale",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		" CompoundCommand ",
		"             For variable lang",
		"         Varname lang",
		"           Words with 1 words",
		"            Word *.po",
		"            List with 2 andOrs",
		"           AndOr with 2 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand [ \"$${lang}\" = \"wxstd.po\" ]",
		"            Path List.AndOr[3].Pipeline[0].Command[0].CompoundCommand.For.List.AndOr[0].Pipeline[0].Command[0].SimpleCommand",
		"            Word [",
		"           Words with 4 words",
		"            Word \"$${lang}\"",
		"            Word =",
		"            Word \"wxstd.po\"",
		"            Word ]",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand continue",
		"            Path List.AndOr[3].Pipeline[0].Command[0].CompoundCommand.For.List.AndOr[0].Pipeline[1].Command[0].SimpleCommand",
		"            Word continue",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand ${TOOLS_PATH.msgfmt} -c -o \"$${lang%.po}.mo\" \"$${lang}\"",
		"            Path List.AndOr[3].Pipeline[0].Command[0].CompoundCommand.For.List.AndOr[1].Pipeline[0].Command[0].SimpleCommand",
		"            Word ${TOOLS_PATH.msgfmt}",
		"           Words with 4 words",
		"            Word -c",
		"            Word -o",
		"            Word \"$${lang%.po}.mo\"",
		"            Word \"$${lang}\"",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		" CompoundCommand ",
		"            Loop ",
		"            List with 1 andOrs",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand :",
		"            Path List.AndOr[4].Pipeline[0].Command[0].CompoundCommand.Loop.List[0].AndOr[0].Pipeline[0].Command[0].SimpleCommand",
		"            Word :",
		"            List with 1 andOrs",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"     FunctionDef for fun",
		" CompoundCommand ",
		"            List with 1 andOrs",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand :",
		"            Path List.AndOr[4].Pipeline[0].Command[0].CompoundCommand.Loop."+
			"List[1].AndOr[0].Pipeline[0].Command[0].FunctionDefinition.CompoundCommand."+
			"List.AndOr[0].Pipeline[0].Command[0].SimpleCommand",
		"            Word :",
		"       Redirects with 1 redirects",
		"        Redirect >&",
		"            Word 2")

	outputPathFor("Redirects", "Redirect", "Word")
	test(""+
		"echo 'hello world' 1>/dev/null 2>&1 0</dev/random",

		"            List with 1 andOrs",
		"           AndOr with 1 pipelines",
		"        Pipeline with 1 commands",
		"         Command ",
		"   SimpleCommand echo 'hello world'",
		"            Word echo",
		"            Path List.AndOr[0].Pipeline[0].Command[0].SimpleCommand.ShToken",
		"           Words with 1 words",
		"            Word 'hello world'",
		"            Path List.AndOr[0].Pipeline[0].Command[0].SimpleCommand.[]ShToken[1].ShToken[0]",
		"       Redirects with 3 redirects",
		"            Path List.AndOr[0].Pipeline[0].Command[0].SimpleCommand.[]MkShRedirection",
		"        Redirect >",
		"            Path List.AndOr[0].Pipeline[0].Command[0].SimpleCommand.[]MkShRedirection.Redirection[0]",
		"            Word /dev/null",
		"            Path List.AndOr[0].Pipeline[0].Command[0].SimpleCommand.[]MkShRedirection.Redirection[0].ShToken[0]",
		"        Redirect >&",
		"            Path List.AndOr[0].Pipeline[0].Command[0].SimpleCommand.[]MkShRedirection.Redirection[1]",
		"            Word 1",
		"            Path List.AndOr[0].Pipeline[0].Command[0].SimpleCommand.[]MkShRedirection.Redirection[1].ShToken[1]",
		"        Redirect <",
		"            Path List.AndOr[0].Pipeline[0].Command[0].SimpleCommand.[]MkShRedirection.Redirection[2]",
		"            Word /dev/random",
		"            Path List.AndOr[0].Pipeline[0].Command[0].SimpleCommand.[]MkShRedirection.Redirection[2].ShToken[2]")
}