Debian Codegraph @

t/classes/Dancer2-Core-Role-HasLocation/with.t

raw
1 use strict;
2 use warnings;
3 use Path::Tiny qw< path >;
4 use Test::More tests => 19;
5
6 {
7 package App;
8 use Moo;
9 with 'Dancer2::Core::Role::HasLocation';
10 }
11
12 note 'Defaults:'; {
13 my $app = App->new();
14 isa_ok( $app, 'App' );
15 can_ok( $app, qw<caller location> ); # attributes
16 can_ok( $app, '_build_location' ); # methods
17
18 ok(
19 $app->DOES('Dancer2::Core::Role::HasLocation'),
20 'App consumes Dancer2::Core::Role::HasLocation',
21 );
22
23 my $path = path(qw<
24 t classes Dancer2-Core-Role-HasLocation with.t
25 >)->stringify;
26
27 is(
28 path( $app->caller ),
29 $path,
30 'Default caller',
31 );
32
33 }
34
35 my $basedir = path( __FILE__ )->parent->stringify;
36
37 note 'With lib/ and bin/:'; {
38 my $app = App->new(
39 caller => path(
40 $basedir, qw<FakeDancerDir lib fake inner dir fakescript.pl>
41 )->stringify
42 );
43
44 isa_ok( $app, 'App' );
45
46 my $location = $app->location;
47 $location =~ s/\/$//;
48
49 my $path = path(
50 qw<t classes Dancer2-Core-Role-HasLocation FakeDancerDir>,
51 )->absolute->stringify;
52
53 is(
54 $location,
55 $path,
56 'Got correct location with lib/ and bin/',
57 );
58 }
59
60 note 'With .dancer file:'; {
61 my $app = App->new(
62 caller => path(
63 $basedir, qw<FakeDancerFile script.pl>
64 )->stringify
65 );
66
67 isa_ok( $app, 'App' );
68
69 my $location = $app->location;
70
71 my $path = path(
72 qw<t classes Dancer2-Core-Role-HasLocation FakeDancerFile>,
73 )->absolute->stringify;
74
75 is( $location, $path, 'Got correct location with .dancer file' );
76 }
77
78 # FakeDistRoot/ has lib/ and bin/, exactly like any Perl distribution root,
79 # like ~, like /usr and /usr/local. The real app roots are nested inside it.
80 # Before GH #1781 the upward walk stalled and never noticed FakeDistRoot;
81 # now that it walks properly, lib/+bin/ must not outrank real Dancer2
82 # artifacts found closer to the script.
83 note 'Does not walk past an app root into an enclosing lib/+bin/ dir:'; {
84 my %case = (
85 't' => 'config.yml, views/ and public/',
86 'configonly' => 'a config file alone',
87 'viewsonly' => 'a views/ directory alone',
88 );
89
90 foreach my $dir ( sort keys %case ) {
91 my $app = App->new(
92 caller => path(
93 $basedir, 'FakeDistRoot', $dir, 'fakescript.pl'
94 )->stringify
95 );
96
97 my $path = path(
98 qw<t classes Dancer2-Core-Role-HasLocation FakeDistRoot>, $dir,
99 )->absolute->stringify;
100
101 is(
102 $app->location,
103 $path,
104 "App root found by $case{$dir}, not the enclosing lib/+bin/ dir",
105 );
106 }
107 }
108
109 # A checkout or distribution root is a ceiling: whatever the app root is, it
110 # sits at or below that line, never in the directory above it. Built at
111 # runtime rather than shipped, because git will not track a path named .git.
112 note 'Stops at a project boundary rather than escaping above it:'; {
113 my $tmp = Path::Tiny->tempdir;
114
115 # plays the part of a home directory: lib/ and bin/, nothing Dancer2
116 $tmp->child($_)->mkpath for qw< lib bin >;
117
118 foreach my $marker ( qw< .git .hg Makefile.PL dist.ini cpanfile > ) {
119 my $checkout = $tmp->child("checkout$marker");
120 my $srcdir = $checkout->child('src');
121 $srcdir->mkpath;
122
123 # .git and .hg are directories in a normal checkout, the rest files
124 $marker =~ /^\./ ? $checkout->child($marker)->mkpath
125 : $checkout->child($marker)->touch;
126
127 my $script = $srcdir->child('fakescript.pl');
128 $script->touch;
129
130 my $app = App->new( caller => $script->stringify );
131
132 is(
133 $app->location,
134 $srcdir->realpath->stringify,
135 "$marker halts the walk instead of reaching the lib/+bin/ dir above",
136 );
137 }
138 }
139
140 note 'blib/ ignored:'; {
141 my $app = App->new(
142 caller => path(
143 $basedir, qw<FakeDancerDir blib lib fakescript.pl>
144 )->stringify
145 );
146
147 isa_ok( $app, 'App' );
148
149 my $location = $app->location;
150 $location =~ s/\/$//;
151
152 my $path = path(
153 qw<t classes Dancer2-Core-Role-HasLocation FakeDancerDir>,
154 )->absolute->stringify;
155
156 is( $location, $path, 'blib/ dir is ignored' );
157 }