]> git.sur5r.net Git - i3/i3/blob - testcases/t/201-config-parser.t
tests: correctly close stdout with IPC::Run
[i3/i3] / testcases / t / 201-config-parser.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # Please read the following documents before working on tests:
5 # • http://build.i3wm.org/docs/testsuite.html
6 #   (or docs/testsuite)
7 #
8 # • http://build.i3wm.org/docs/lib-i3test.html
9 #   (alternatively: perldoc ./testcases/lib/i3test.pm)
10 #
11 # • http://build.i3wm.org/docs/ipc.html
12 #   (or docs/ipc)
13 #
14 # • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
15 #   (unless you are already familiar with Perl)
16 #
17 # Tests the standalone parser binary to see if it calls the right code when
18 # confronted with various commands, if it prints proper error messages for
19 # wrong commands and if it terminates in every case.
20 #
21 use i3test i3_autostart => 0;
22 use IPC::Run qw(run);
23
24 sub parser_calls {
25     my ($command) = @_;
26
27     my $stdout;
28     run [ '../test.config_parser', $command ],
29         '>/dev/null',
30         '2>', \$stdout;
31     # TODO: use a timeout, so that we can error out if it doesn’t terminate
32
33     # Filter out all debugging output.
34     my @lines = split("\n", $stdout);
35     @lines = grep { not /^# / } @lines;
36
37     # The criteria management calls are irrelevant and not what we want to test
38     # in the first place.
39     @lines = grep { !(/cfg_criteria_init/ || /cfg_criteria_pop_state/) } @lines;
40     return join("\n", @lines) . "\n";
41 }
42
43 my $config = <<'EOT';
44 mode "meh" {
45     bindsym Mod1 + Shift +   x resize grow
46     bindcode Mod1+44 resize shrink
47     bindsym --release Mod1+x exec foo
48 }
49 EOT
50
51 my $expected = <<'EOT';
52 cfg_enter_mode(meh)
53 cfg_mode_binding(bindsym, Mod1,Shift, x, (null), resize grow)
54 cfg_mode_binding(bindcode, Mod1, 44, (null), resize shrink)
55 cfg_mode_binding(bindsym, Mod1, x, --release, exec foo)
56 EOT
57
58 is(parser_calls($config),
59    $expected,
60    'single number (move workspace 3) ok');
61
62 ################################################################################
63 # exec and exec_always
64 ################################################################################
65
66 $config = <<'EOT';
67 exec geeqie
68 exec --no-startup-id /tmp/foo.sh
69 exec_always firefox
70 exec_always --no-startup-id /tmp/bar.sh
71 EOT
72
73 $expected = <<'EOT';
74 cfg_exec(exec, (null), geeqie)
75 cfg_exec(exec, --no-startup-id, /tmp/foo.sh)
76 cfg_exec(exec_always, (null), firefox)
77 cfg_exec(exec_always, --no-startup-id, /tmp/bar.sh)
78 EOT
79
80 is(parser_calls($config),
81    $expected,
82    'exec okay');
83
84 ################################################################################
85 # for_window
86 ################################################################################
87
88 $config = <<'EOT';
89 for_window [class="^Chrome"] floating enable
90 EOT
91
92 $expected = <<'EOT';
93 cfg_criteria_add(class, ^Chrome)
94 cfg_for_window(floating enable)
95 EOT
96
97 is(parser_calls($config),
98    $expected,
99    'for_window okay');
100
101 ################################################################################
102 # assign
103 ################################################################################
104
105 $config = <<'EOT';
106 assign [class="^Chrome"] 4
107 assign [class="^Chrome"] named workspace
108 assign [class="^Chrome"] "quoted named workspace"
109 assign [class="^Chrome"] → "quoted named workspace"
110 EOT
111
112 $expected = <<'EOT';
113 cfg_criteria_add(class, ^Chrome)
114 cfg_assign(4)
115 cfg_criteria_add(class, ^Chrome)
116 cfg_assign(named workspace)
117 cfg_criteria_add(class, ^Chrome)
118 cfg_assign(quoted named workspace)
119 cfg_criteria_add(class, ^Chrome)
120 cfg_assign(quoted named workspace)
121 EOT
122
123 is(parser_calls($config),
124    $expected,
125    'for_window okay');
126
127 ################################################################################
128 # floating_minimum_size / floating_maximum_size
129 ################################################################################
130
131 $config = <<'EOT';
132 floating_minimum_size 80x55
133 floating_minimum_size 80    x  55  
134 floating_maximum_size 73 x 10
135 EOT
136
137 $expected = <<'EOT';
138 cfg_floating_minimum_size(80, 55)
139 cfg_floating_minimum_size(80, 55)
140 cfg_floating_maximum_size(73, 10)
141 EOT
142
143 is(parser_calls($config),
144    $expected,
145    'floating_minimum_size ok');
146
147 ################################################################################
148 # floating_modifier
149 ################################################################################
150
151 $config = <<'EOT';
152 floating_modifier Mod1
153 floating_modifier mOd1
154 EOT
155
156 $expected = <<'EOT';
157 cfg_floating_modifier(Mod1)
158 cfg_floating_modifier(Mod1)
159 EOT
160
161 is(parser_calls($config),
162    $expected,
163    'floating_modifier ok');
164
165 ################################################################################
166 # default_orientation
167 ################################################################################
168
169 $config = <<'EOT';
170 default_orientation horizontal
171 default_orientation vertical
172 default_orientation auto
173 EOT
174
175 $expected = <<'EOT';
176 cfg_default_orientation(horizontal)
177 cfg_default_orientation(vertical)
178 cfg_default_orientation(auto)
179 EOT
180
181 is(parser_calls($config),
182    $expected,
183    'default_orientation ok');
184
185 ################################################################################
186 # workspace_layout
187 ################################################################################
188
189 $config = <<'EOT';
190 workspace_layout default
191 workspace_layout stacked
192 workspace_layout stacking
193 workspace_layout tabbed
194 EOT
195
196 $expected = <<'EOT';
197 cfg_workspace_layout(default)
198 cfg_workspace_layout(stacked)
199 cfg_workspace_layout(stacking)
200 cfg_workspace_layout(tabbed)
201 EOT
202
203 is(parser_calls($config),
204    $expected,
205    'workspace_layout ok');
206
207 ################################################################################
208 # workspace assignments, with trailing whitespace (ticket #921)
209 ################################################################################
210
211 $config = <<'EOT';
212 workspace "3" output DP-1 
213 workspace "3" output            VGA-1   
214 EOT
215
216 $expected = <<'EOT';
217 cfg_workspace(3, DP-1)
218 cfg_workspace(3, VGA-1)
219 EOT
220
221 is(parser_calls($config),
222    $expected,
223    'workspace assignment ok');
224
225 ################################################################################
226 # new_window
227 ################################################################################
228
229 $config = <<'EOT';
230 new_window 1pixel
231 new_window normal
232 new_window none
233 new_float 1pixel
234 new_float normal
235 new_float none
236 EOT
237
238 $expected = <<'EOT';
239 cfg_new_window(new_window, 1pixel, -1)
240 cfg_new_window(new_window, normal, 2)
241 cfg_new_window(new_window, none, -1)
242 cfg_new_window(new_float, 1pixel, -1)
243 cfg_new_window(new_float, normal, 2)
244 cfg_new_window(new_float, none, -1)
245 EOT
246
247 is(parser_calls($config),
248    $expected,
249    'new_window ok');
250
251 ################################################################################
252 # hide_edge_borders
253 ################################################################################
254
255 $config = <<'EOT';
256 hide_edge_borders none
257 hide_edge_borders vertical
258 hide_edge_borders horizontal
259 hide_edge_borders both
260 EOT
261
262 $expected = <<'EOT';
263 cfg_hide_edge_borders(none)
264 cfg_hide_edge_borders(vertical)
265 cfg_hide_edge_borders(horizontal)
266 cfg_hide_edge_borders(both)
267 EOT
268
269 is(parser_calls($config),
270    $expected,
271    'hide_edge_borders ok');
272
273 ################################################################################
274 # focus_follows_mouse
275 ################################################################################
276
277 $config = <<'EOT';
278 focus_follows_mouse yes
279 focus_follows_mouse no
280 EOT
281
282 $expected = <<'EOT';
283 cfg_focus_follows_mouse(yes)
284 cfg_focus_follows_mouse(no)
285 EOT
286
287 is(parser_calls($config),
288    $expected,
289    'focus_follows_mouse ok');
290
291 ################################################################################
292 # force_display_urgency_hint
293 ################################################################################
294
295 is(parser_calls('force_display_urgency_hint 300'),
296    "cfg_force_display_urgency_hint(300)\n",
297    'force_display_urgency_hint ok');
298
299 is(parser_calls('force_display_urgency_hint 500 ms'),
300    "cfg_force_display_urgency_hint(500)\n",
301    'force_display_urgency_hint ok');
302
303 is(parser_calls('force_display_urgency_hint 700ms'),
304    "cfg_force_display_urgency_hint(700)\n",
305    'force_display_urgency_hint ok');
306
307 $config = <<'EOT';
308 force_display_urgency_hint 300
309 force_display_urgency_hint 500 ms
310 force_display_urgency_hint 700ms
311 force_display_urgency_hint 700
312 EOT
313
314 $expected = <<'EOT';
315 cfg_force_display_urgency_hint(300)
316 cfg_force_display_urgency_hint(500)
317 cfg_force_display_urgency_hint(700)
318 cfg_force_display_urgency_hint(700)
319 EOT
320
321 is(parser_calls($config),
322    $expected,
323    'force_display_urgency_hint ok');
324
325 ################################################################################
326 # workspace
327 ################################################################################
328
329 $config = <<'EOT';
330 workspace 3 output VGA-1
331 workspace "4: output" output VGA-2
332 workspace bleh output LVDS1/I_1
333 EOT
334
335 $expected = <<'EOT';
336 cfg_workspace(3, VGA-1)
337 cfg_workspace(4: output, VGA-2)
338 cfg_workspace(bleh, LVDS1/I_1)
339 EOT
340
341 is(parser_calls($config),
342    $expected,
343    'workspace ok');
344
345 ################################################################################
346 # ipc-socket
347 ################################################################################
348
349 $config = <<'EOT';
350 ipc-socket /tmp/i3.sock
351 ipc_socket ~/.i3/i3.sock
352 EOT
353
354 $expected = <<'EOT';
355 cfg_ipc_socket(/tmp/i3.sock)
356 cfg_ipc_socket(~/.i3/i3.sock)
357 EOT
358
359 is(parser_calls($config),
360    $expected,
361    'ipc-socket ok');
362
363 ################################################################################
364 # colors
365 ################################################################################
366
367 $config = <<'EOT';
368 client.focused          #4c7899 #285577 #ffffff #2e9ef4
369 client.focused_inactive #333333 #5f676a #ffffff #484e50
370 client.unfocused        #333333 #222222 #888888 #292d2e
371 client.urgent           #2f343a #900000 #ffffff #900000
372 EOT
373
374 $expected = <<'EOT';
375 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4)
376 cfg_color(client.focused_inactive, #333333, #5f676a, #ffffff, #484e50)
377 cfg_color(client.unfocused, #333333, #222222, #888888, #292d2e)
378 cfg_color(client.urgent, #2f343a, #900000, #ffffff, #900000)
379 EOT
380
381 is(parser_calls($config),
382    $expected,
383    'colors ok');
384
385 ################################################################################
386 # Verify that errors don’t harm subsequent valid statements
387 ################################################################################
388
389 $config = <<'EOT';
390 hide_edge_border both
391 client.focused          #4c7899 #285577 #ffffff #2e9ef4
392 EOT
393
394 my $expected_all_tokens = <<'EOT';
395 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', 'bar', 'font', 'mode', 'floating_minimum_size', 'floating_maximum_size', 'floating_modifier', 'default_orientation', 'workspace_layout', 'new_window', 'new_float', 'hide_edge_borders', 'for_window', 'assign', 'focus_follows_mouse', 'force_focus_wrapping', 'force_xinerama', 'force-xinerama', 'workspace_auto_back_and_forth', 'fake_outputs', 'fake-outputs', 'force_display_urgency_hint', 'workspace', 'ipc_socket', 'ipc-socket', 'restart_state', 'popup_during_fullscreen', 'exec_always', 'exec', 'client.background', 'client.focused_inactive', 'client.focused', 'client.unfocused', 'client.urgent'
396 EOT
397
398 my $expected_end = <<'EOT';
399 ERROR: CONFIG: (in file <stdin>)
400 ERROR: CONFIG: Line   1: hide_edge_border both
401 ERROR: CONFIG:           ^^^^^^^^^^^^^^^^^^^^^
402 ERROR: CONFIG: Line   2: client.focused          #4c7899 #285577 #ffffff #2e9ef4
403 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4)
404 EOT
405
406 $expected = $expected_all_tokens . $expected_end;
407
408 is(parser_calls($config),
409    $expected,
410    'errors dont harm subsequent statements');
411
412 $config = <<'EOT';
413 hide_edge_borders FOOBAR
414 client.focused          #4c7899 #285577 #ffffff #2e9ef4
415 EOT
416
417 $expected = <<'EOT';
418 ERROR: CONFIG: Expected one of these tokens: 'none', 'vertical', 'horizontal', 'both', '1', 'yes', 'true', 'on', 'enable', 'active'
419 ERROR: CONFIG: (in file <stdin>)
420 ERROR: CONFIG: Line   1: hide_edge_borders FOOBAR
421 ERROR: CONFIG:                             ^^^^^^
422 ERROR: CONFIG: Line   2: client.focused          #4c7899 #285577 #ffffff #2e9ef4
423 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4)
424 EOT
425
426 is(parser_calls($config),
427    $expected,
428    'errors dont harm subsequent statements');
429
430
431 ################################################################################
432 # Error message with 2+2 lines of context
433 ################################################################################
434
435 $config = <<'EOT';
436 # i3 config file (v4)
437
438 font foobar
439
440 unknown qux
441
442 # yay
443 # this should not show up
444 EOT
445
446 my $expected_head = <<'EOT';
447 cfg_font(foobar)
448 EOT
449
450 my $expected_tail = <<'EOT';
451 ERROR: CONFIG: (in file <stdin>)
452 ERROR: CONFIG: Line   3: font foobar
453 ERROR: CONFIG: Line   4: 
454 ERROR: CONFIG: Line   5: unknown qux
455 ERROR: CONFIG:           ^^^^^^^^^^^
456 ERROR: CONFIG: Line   6: 
457 ERROR: CONFIG: Line   7: # yay
458 EOT
459
460 $expected = $expected_head . $expected_all_tokens . $expected_tail;
461
462 is(parser_calls($config),
463    $expected,
464    'error message (2+2 context) ok');
465
466 ################################################################################
467 # Error message with 0+0 lines of context
468 ################################################################################
469
470 $config = <<'EOT';
471 unknown qux
472 EOT
473
474 $expected_tail = <<'EOT';
475 ERROR: CONFIG: (in file <stdin>)
476 ERROR: CONFIG: Line   1: unknown qux
477 ERROR: CONFIG:           ^^^^^^^^^^^
478 EOT
479
480 $expected = $expected_all_tokens . $expected_tail;
481
482 is(parser_calls($config),
483    $expected,
484    'error message (0+0 context) ok');
485
486 ################################################################################
487 # Error message with 1+0 lines of context
488 ################################################################################
489
490 $config = <<'EOT';
491 # context before
492 unknown qux
493 EOT
494
495 $expected_tail = <<'EOT';
496 ERROR: CONFIG: (in file <stdin>)
497 ERROR: CONFIG: Line   1: # context before
498 ERROR: CONFIG: Line   2: unknown qux
499 ERROR: CONFIG:           ^^^^^^^^^^^
500 EOT
501
502 $expected = $expected_all_tokens . $expected_tail;
503
504 is(parser_calls($config),
505    $expected,
506    'error message (1+0 context) ok');
507
508 ################################################################################
509 # Error message with 0+1 lines of context
510 ################################################################################
511
512 $config = <<'EOT';
513 unknown qux
514 # context after
515 EOT
516
517 $expected_tail = <<'EOT';
518 ERROR: CONFIG: (in file <stdin>)
519 ERROR: CONFIG: Line   1: unknown qux
520 ERROR: CONFIG:           ^^^^^^^^^^^
521 ERROR: CONFIG: Line   2: # context after
522 EOT
523
524 $expected = $expected_all_tokens . $expected_tail;
525
526 is(parser_calls($config),
527    $expected,
528    'error message (0+1 context) ok');
529
530 ################################################################################
531 # Error message with 0+2 lines of context
532 ################################################################################
533
534 $config = <<'EOT';
535 unknown qux
536 # context after
537 # context 2 after
538 EOT
539
540 $expected_tail = <<'EOT';
541 ERROR: CONFIG: (in file <stdin>)
542 ERROR: CONFIG: Line   1: unknown qux
543 ERROR: CONFIG:           ^^^^^^^^^^^
544 ERROR: CONFIG: Line   2: # context after
545 ERROR: CONFIG: Line   3: # context 2 after
546 EOT
547
548 $expected = $expected_all_tokens . $expected_tail;
549
550 is(parser_calls($config),
551    $expected,
552    'error message (0+2 context) ok');
553
554 ################################################################################
555 # Error message within mode blocks
556 ################################################################################
557
558 $config = <<'EOT';
559 mode "yo" {
560     bindsym x resize shrink left
561     unknown qux
562 }
563 EOT
564
565 $expected = <<'EOT';
566 cfg_enter_mode(yo)
567 cfg_mode_binding(bindsym, (null), x, (null), resize shrink left)
568 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', '}'
569 ERROR: CONFIG: (in file <stdin>)
570 ERROR: CONFIG: Line   1: mode "yo" {
571 ERROR: CONFIG: Line   2:     bindsym x resize shrink left
572 ERROR: CONFIG: Line   3:     unknown qux
573 ERROR: CONFIG:               ^^^^^^^^^^^
574 ERROR: CONFIG: Line   4: }
575 EOT
576
577 is(parser_calls($config),
578    $expected,
579    'error message (mode block) ok');
580
581 ################################################################################
582 # Error message within bar blocks
583 ################################################################################
584
585 $config = <<'EOT';
586 bar {
587     output LVDS-1
588     unknown qux
589 }
590 EOT
591
592 $expected = <<'EOT';
593 cfg_bar_output(LVDS-1)
594 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'i3bar_command', 'status_command', 'socket_path', 'mode', 'modifier', 'position', 'output', 'tray_output', 'font', 'workspace_buttons', 'verbose', 'colors', '}'
595 ERROR: CONFIG: (in file <stdin>)
596 ERROR: CONFIG: Line   1: bar {
597 ERROR: CONFIG: Line   2:     output LVDS-1
598 ERROR: CONFIG: Line   3:     unknown qux
599 ERROR: CONFIG:               ^^^^^^^^^^^
600 ERROR: CONFIG: Line   4: }
601 cfg_bar_finish()
602 EOT
603
604 is(parser_calls($config),
605    $expected,
606    'error message (bar block) ok');
607
608 done_testing;