Debian Codegraph @

t/dsl_path_dirname.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4
5 # Dancer2 2.1.0 regression: the `path` (and `dirname`) DSL keywords silently
6 # drop their first argument when called with 2+ path segments.
7 #
8 # Root cause (Dancer2::Core::DSL, 2.1.0):
9 # sub _path_obj { shift and Path::Tiny::path(@_) }
10 # sub path { shift and _path_obj(@_)->stringify }
11 #
12 # path() already shifts off its DSL invocant before calling _path_obj(@_).
13 # _path_obj then shifts *again*, as if it too were called as a method, but
14 # it's a plain function call -- so it eats the first real path segment
15 # instead of an invocant. dirname() shares the same broken helper.
16 #
17 # Documented behavior (Dancer2::Manual::Keywords, "path"): "Concatenates
18 # multiple paths together" -- i.e. path('t','views') should be 't/views'.
19 # Confirmed correct under Dancer2 2.0.1; broken under 2.1.0.
20
21 package TestApp;
22 use Dancer2;
23 package main;
24
25 my $dsl = TestApp->dsl;
26
27 is( $dsl->path('t', 'views'), 't/views',
28 'path() with 2 segments keeps all segments (documented: concatenates)' );
29
30 is( $dsl->path('a', 'b', 'c'), 'a/b/c',
31 'path() with 3 segments keeps all segments' );
32
33 # Single-argument calls are even worse: _path_obj's spurious shift eats the
34 # only argument, leaving Path::Tiny::path() with zero args, which dies
35 # outright instead of returning a wrong-but-harmless string.
36 my $single_arg_result = eval { $dsl->dirname('t/views/x.tt') };
37 my $single_arg_error = $@;
38 ok( defined $single_arg_result && !$single_arg_error,
39 'dirname() with a single argument does not crash' )
40 or diag("dirname('t/views/x.tt') died: $single_arg_error");
41
42 is( $single_arg_result, 't/views',
43 'dirname() of a single-argument path is correct' ) if defined $single_arg_result;
44
45 # Knock-on effect: this is exactly how Dancer2::Plugin::FlashNote's test
46 # suite sets its views directory (t/01-default.t):
47 # setting views => path( 't', 'views' );
48 # Under the bug, `views` silently becomes just 'views' (the 't' is lost),
49 # so every template lookup 404s and every route handler that renders a
50 # template returns a 500 -- which is what caused ~114/160 FlashNote
51 # subtests to fail under 2.1.0 while the plugin's own code never changed.
52
53 done_testing();