| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use File::Temp qw< tempdir >; |
| 6 |
use Path::Tiny qw< path >; |
| 7 |
use Capture::Tiny qw< capture >; |
| 8 |
use Config; |
| 9 |
use File::Which qw< which >; |
| 10 |
use TAP::Harness; |
| 11 |
|
| 12 |
# End-to-end test for the `dancer2 gen` scaffold: it runs the real command as a |
| 13 |
# separate process against share/skel, then compiles and runs what came out. |
| 14 |
# |
| 15 |
# It runs the command out of *this* source tree, never an installed Dancer2: |
| 16 |
# -s share/skel points at the skeleton in the repo rather than the dist_dir |
| 17 |
# -x skips the "is there a newer Dancer2 on CPAN?" check, which |
| 18 |
# needs the network and warns when it cannot reach it -- but |
| 19 |
# only if this build still has that option, see below |
| 20 |
# The generated application only knows about Dancer2 through PERL5LIB, which is |
| 21 |
# set to this repo's lib for every child process below. |
| 22 |
|
| 23 |
my $repo = path('.')->absolute; |
| 24 |
my $script = $repo->child('script/dancer2'); |
| 25 |
my $skel = $repo->child('share/skel'); |
| 26 |
|
| 27 |
-f $script && -d $skel |
| 28 |
or plan skip_all => 'must be run from the distribution root'; |
| 29 |
|
| 30 |
my $lib = $repo->child('lib')->stringify; |
| 31 |
|
| 32 |
# Whether the generator understands -x has to be asked, not assumed. Debian |
| 33 |
# patches the CPAN version check out of Dancer2::CLI::Gen as a privacy measure |
| 34 |
# (debian/patches/no-phone-home.patch), and the option whose only job is to |
| 35 |
# switch that check off goes with it, so on a build made from those sources |
| 36 |
# passing -x aborts the generator with "Unknown option: x" before it writes a |
| 37 |
# single file -- and every assertion in this file then fails, pointing at the |
| 38 |
# scaffold rather than at the flag. Where the option is gone, so is the network |
| 39 |
# access it exists to suppress, so dropping it loses nothing. |
| 40 |
my @no_check = _no_check_option(); |
| 41 |
|
| 42 |
# Ask the command itself what it accepts. Osprey spells the long form with |
| 43 |
# dashes or underscores depending on its version, so match the short form, and |
| 44 |
# accept either spelling of the long one. |
| 45 |
sub _no_check_option { |
| 46 |
my ( $stdout, $stderr ) = capture { |
| 47 |
local $ENV{'PERL5LIB'} = _prepend_lib(); |
| 48 |
system( $Config{'perlpath'}, "-I$lib", "$script", 'gen', '--help' ); |
| 49 |
}; |
| 50 |
|
| 51 |
my $usage = "$stdout$stderr"; |
| 52 |
return '-x' if $usage =~ /(?:^|\s)-x\b/ || $usage =~ /--no.check\b/; |
| 53 |
|
| 54 |
note 'this dancer2 gen has no -x: running without it, on the assumption ' |
| 55 |
. 'that the version check it skips has been patched out too'; |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
# Run the generator. Returns the exit status and both output streams; the |
| 60 |
# generator is chatty on STDOUT ("+ path" per file written), and none of that |
| 61 |
# should reach this suite's own output. |
| 62 |
sub gen { |
| 63 |
my @args = @_; |
| 64 |
my ( $stdout, $stderr, $status ) = capture { |
| 65 |
local $ENV{'PERL5LIB'} = _prepend_lib(); |
| 66 |
system( $Config{'perlpath'}, "-I$lib", "$script", 'gen', |
| 67 |
'-s', "$skel", @no_check, '--overwrite', @args ); |
| 68 |
}; |
| 69 |
return { |
| 70 |
status => $status, |
| 71 |
stdout => $stdout, |
| 72 |
stderr => _without_version_check_warning($stderr), |
| 73 |
}; |
| 74 |
} |
| 75 |
|
| 76 |
# With no -x to pass, a build that kept the version check runs it, and it warns |
| 77 |
# when it cannot reach metacpan -- which is the normal state of a package build |
| 78 |
# daemon. That warning is the generator saying something this test has no way |
| 79 |
# to switch off in such a build, so it is dropped here rather than failing the |
| 80 |
# "says nothing on STDERR" assertion. Only those two lines go; anything else |
| 81 |
# the generator writes to STDERR is still caught. Builds that do have -x are |
| 82 |
# left alone, so the assertion stays strict where it can be. |
| 83 |
sub _without_version_check_warning { |
| 84 |
my $stderr = shift; |
| 85 |
return $stderr if @no_check; |
| 86 |
$stderr =~ s{\s*Couldn't determine latest version of Dancer2\b[^\n]*\n[^\n]*\n\s*}{}s; |
| 87 |
return $stderr; |
| 88 |
} |
| 89 |
|
| 90 |
# This repo's lib has to come first so the child sees *this* Dancer2, but it |
| 91 |
# must not replace whatever PERL5LIB already carries -- under local::lib (as |
| 92 |
# used in development and CI) that variable is how the child finds Dancer2's |
| 93 |
# own dependencies (e.g. CLI::Osprey), and clobbering it makes every child |
| 94 |
# process below fail before it even gets to the code under test. |
| 95 |
sub _prepend_lib { |
| 96 |
return join $Config{'path_sep'}, $lib, |
| 97 |
( defined $ENV{'PERL5LIB'} && length $ENV{'PERL5LIB'} ? $ENV{'PERL5LIB'} : () ); |
| 98 |
} |
| 99 |
|
| 100 |
# Read a generated file, or give back the empty string if the generator never |
| 101 |
# wrote it. A missing file is already reported by the file-presence subtest; |
| 102 |
# this keeps the content assertions from dying and taking the rest of the run |
| 103 |
# down with them, so one broken thing produces one clear list of failures. |
| 104 |
sub slurp { |
| 105 |
my $file = shift; |
| 106 |
return $file->is_file ? $file->slurp_utf8 : ''; |
| 107 |
} |
| 108 |
|
| 109 |
# The application the scaffold has to produce, listed here rather than derived |
| 110 |
# from share/skel -- deriving it would make the check vacuous, since a file |
| 111 |
# deleted from the skeleton would also vanish from the expectation. Every entry |
| 112 |
# is something the generated app needs to start, serve its one route, or be |
| 113 |
# packaged. |
| 114 |
my @required = qw< |
| 115 |
bin/app.psgi |
| 116 |
config.yml |
| 117 |
cpanfile |
| 118 |
views/index.tt |
| 119 |
views/layouts/main.tt |
| 120 |
public/404.html |
| 121 |
public/500.html |
| 122 |
public/css/style.css |
| 123 |
public/dispatch.cgi |
| 124 |
public/dispatch.fcgi |
| 125 |
t/001_base.t |
| 126 |
t/002_index_route.t |
| 127 |
environments/development.yml |
| 128 |
environments/production.yml |
| 129 |
.dancer |
| 130 |
Makefile.PL |
| 131 |
MANIFEST |
| 132 |
MANIFEST.SKIP |
| 133 |
>; |
| 134 |
|
| 135 |
my $tmp = tempdir( CLEANUP => 1 ); |
| 136 |
my $run = gen( '-a', 'MyApp::App', '-d', 'myapp', '--path', $tmp ); |
| 137 |
my $app = path( $tmp, 'myapp' ); |
| 138 |
|
| 139 |
subtest 'the generator runs cleanly and writes where it was told' => sub { |
| 140 |
is( $run->{'status'}, 0, 'dancer2 gen exits successfully' ) |
| 141 |
or diag "STDERR:\n$run->{stderr}\nSTDOUT:\n$run->{stdout}"; |
| 142 |
is( $run->{'stderr'}, '', 'and says nothing on STDERR' ); |
| 143 |
|
| 144 |
ok( $app->is_dir, "--path and -d put the application in $app" ); |
| 145 |
like( $run->{'stdout'}, qr/Your new application is ready/, |
| 146 |
'the run ends with the how-to-run banner' ); |
| 147 |
}; |
| 148 |
|
| 149 |
subtest 'every file the generated application needs is present' => sub { |
| 150 |
# This is the subtest that goes red if share/skel loses something. |
| 151 |
for my $file (@required) { |
| 152 |
ok( $app->child($file)->is_file, "generated $file" ); |
| 153 |
} |
| 154 |
|
| 155 |
# The skeleton marks these two with a leading '+' to request the exec bit; |
| 156 |
# the '+' must not survive into the generated name. |
| 157 |
ok( !$app->child('bin/+app.psgi')->exists, |
| 158 |
'the + marker is stripped from the generated filename' ); |
| 159 |
|
| 160 |
SKIP: { |
| 161 |
skip 'no meaningful exec bit on this platform', 2 |
| 162 |
if $^O eq 'MSWin32'; |
| 163 |
ok( -x $app->child('bin/app.psgi')->stringify, |
| 164 |
'bin/app.psgi is executable' ); |
| 165 |
ok( -x $app->child('public/dispatch.cgi')->stringify, |
| 166 |
'public/dispatch.cgi is executable' ); |
| 167 |
} |
| 168 |
}; |
| 169 |
|
| 170 |
subtest '-a decides the package name and the module path' => sub { |
| 171 |
my $module = $app->child('lib/MyApp/App.pm'); |
| 172 |
ok( $module->is_file, 'the module is written at the path -a implies' ); |
| 173 |
|
| 174 |
my $source = slurp($module); |
| 175 |
like( $source, qr/^package MyApp::App;/m, 'and declares that package' ); |
| 176 |
ok( length $source && $source !~ /AppFile/, |
| 177 |
'no trace of the skeleton\'s placeholder package name' ); |
| 178 |
|
| 179 |
# The same name has to reach the files that refer to the app by name, or |
| 180 |
# the generated app starts but cannot be packaged or served. |
| 181 |
like( slurp( $app->child('bin/app.psgi') ), qr/\buse MyApp::App\b/, |
| 182 |
'the .psgi entry point loads the generated module' ); |
| 183 |
like( slurp( $app->child('Makefile.PL') ), qr/NAME\s*=>\s*'MyApp::App'/, |
| 184 |
'Makefile.PL names the generated module' ); |
| 185 |
|
| 186 |
# Template tokens are [d2% ... %2d]; any left behind means a variable was |
| 187 |
# never substituted. |
| 188 |
for my $file (qw< bin/app.psgi Makefile.PL config.yml >) { |
| 189 |
my $content = slurp( $app->child($file) ); |
| 190 |
ok( length $content && $content !~ /\Q[d2%\E/, |
| 191 |
"no unsubstituted template token left in $file" ); |
| 192 |
} |
| 193 |
}; |
| 194 |
|
| 195 |
subtest 'the generated application compiles' => sub { |
| 196 |
my ( $stdout, $stderr, $status ) = capture { |
| 197 |
system( $Config{'perlpath'}, "-I$lib", |
| 198 |
'-I' . $app->child('lib')->stringify, |
| 199 |
'-c', $app->child('lib/MyApp/App.pm')->stringify ); |
| 200 |
}; |
| 201 |
|
| 202 |
is( $status, 0, 'perl -c on the generated module succeeds' ) |
| 203 |
or diag "STDERR:\n$stderr"; |
| 204 |
like( $stderr, qr/syntax OK/, 'and perl says so' ); |
| 205 |
}; |
| 206 |
|
| 207 |
subtest 'the generated application passes its own bundled tests' => sub { |
| 208 |
# The app's tests are run from inside the generated directory, the way its |
| 209 |
# author would run them. Using TAP::Harness (rather than shelling out to |
| 210 |
# prove) ensures the child perls are invoked via $^X -- the same interpreter |
| 211 |
# that is running this test suite -- so they see the same @INC and the same |
| 212 |
# XS extensions. Shelling out to prove lets PATH decide which perl to use, |
| 213 |
# which on smokers with multiple perls installed can differ from this one, |
| 214 |
# producing "Can't locate Module::Runtime" or XS handshake mismatches. |
| 215 |
my ( $stdout, $stderr, $status ) = capture { |
| 216 |
local $ENV{'PERL5LIB'} = _prepend_lib(); |
| 217 |
my $cwd = path('.')->absolute; |
| 218 |
chdir $app->stringify or die "cannot chdir to $app: $!"; |
| 219 |
my $harness = TAP::Harness->new({ lib => ['lib'] }); |
| 220 |
my $aggregator = $harness->runtests(glob 't/*.t'); |
| 221 |
chdir $cwd->stringify or die "cannot chdir back to $cwd: $!"; |
| 222 |
$aggregator->has_errors ? 1 : 0; |
| 223 |
}; |
| 224 |
|
| 225 |
is( $status, 0, 'the generated app passes its own tests' ) |
| 226 |
or diag "STDOUT:\n$stdout\nSTDERR:\n$stderr"; |
| 227 |
like( $stdout, qr/Result: PASS/, 'the harness agrees' ); |
| 228 |
}; |
| 229 |
|
| 230 |
subtest 'the application directory is named after the dashed app name (fixed)' => sub { |
| 231 |
# This used to leave the directory named after the application verbatim -- |
| 232 |
# colons and all -- even though the generator computes a dashed name for |
| 233 |
# exactly this purpose. Without -d, that dashed name is now what the |
| 234 |
# directory option falls back to, so the directory and the Makefile.PL it |
| 235 |
# contains agree with each other. |
| 236 |
my $dir = tempdir( CLEANUP => 1 ); |
| 237 |
my $res = gen( '-a', 'Other::App', '--path', $dir ); |
| 238 |
|
| 239 |
is( $res->{'status'}, 0, 'generating without -d still succeeds' ); |
| 240 |
ok( path( $dir, 'Other-App' )->is_dir, |
| 241 |
'the directory is named Other-App, dashed' ); |
| 242 |
ok( !path( $dir, 'Other::App' )->exists, |
| 243 |
'and the raw, colon-bearing name is not used' ); |
| 244 |
|
| 245 |
# The dashed name also reaches the generated Makefile.PL -- the two |
| 246 |
# spellings inside one generated application now agree. |
| 247 |
like( |
| 248 |
slurp( path( $dir, 'Other-App', 'Makefile.PL' ) ), |
| 249 |
qr/FILES\s*=>\s*'Other-App-\*'/, |
| 250 |
'and Makefile.PL cleans the same dashed name', |
| 251 |
); |
| 252 |
|
| 253 |
# An explicit -d must still be honoured verbatim, colons or not -- the |
| 254 |
# fallback only kicks in when the option is absent. |
| 255 |
my $res2 = gen( '-a', 'Other::App', '-d', 'Other::App', '--path', $dir ); |
| 256 |
is( $res2->{'status'}, 0, 'generating with an explicit -d still succeeds' ); |
| 257 |
ok( path( $dir, 'Other::App' )->is_dir, |
| 258 |
'and an explicit -d is honoured verbatim, even with colons in it' ); |
| 259 |
}; |
| 260 |
|
| 261 |
subtest 'MANIFEST.SKIP gets the relative dashed name (fixed)' => sub { |
| 262 |
# This used to build the appended line from the full filesystem path the |
| 263 |
# app was generated into, so it could never match anything -- MANIFEST.SKIP |
| 264 |
# patterns are matched against distribution-root-relative paths. The line |
| 265 |
# is now the dashed distribution name, matching what Makefile.PL cleans. |
| 266 |
my @lines = split /\n/, slurp( $app->child('MANIFEST.SKIP') ); |
| 267 |
my $appended = @lines ? $lines[-1] : ''; |
| 268 |
|
| 269 |
is( $appended, '^MyApp-App-', 'the appended pattern is the relative dashed name' ); |
| 270 |
unlike( $appended, qr{^\^/}, 'and is not anchored to an absolute filesystem path' ); |
| 271 |
|
| 272 |
# No line in the file should carry an absolute path -- that was the shape |
| 273 |
# of the bug, so make sure it is gone everywhere, not just the last line. |
| 274 |
ok( !( grep { m{^\^?/} } @lines ), 'no line in MANIFEST.SKIP is an absolute path' ); |
| 275 |
|
| 276 |
# The rest of the file is what a MANIFEST.SKIP is supposed to look like, |
| 277 |
# which is what makes the appended line fit in rather than stand out. |
| 278 |
ok( ( grep { $_ eq '^\.gitignore' || $_ eq '^.gitignore' } @lines ), |
| 279 |
'the other patterns are relative, as MANIFEST.SKIP expects' ); |
| 280 |
}; |
| 281 |
|
| 282 |
subtest 'the skeleton environment configs are in git (fixed)' => sub { |
| 283 |
# This was caused by share/.gitignore: shipped as data -- Dancer2::CLI::Gen |
| 284 |
# copies it into a generated app when -g is given -- but because it sat |
| 285 |
# inside share/ under that name, git also applied its patterns to this |
| 286 |
# repository's own tree, and one of them was 'environments/'. That template |
| 287 |
# now lives at share/gitignore instead (a name git does not honour as an |
| 288 |
# ignore file), so the skeleton's environment configs can be committed, and |
| 289 |
# a fresh clone generates them. They also belong in @required above now. |
| 290 |
my $probe = 'share/skel/default/environments/development.yml'; |
| 291 |
|
| 292 |
# Being inside a work tree is not enough. 'dzil test' builds into |
| 293 |
# .build/XXXX *within* this checkout and runs the suite from there, so |
| 294 |
# --is-inside-work-tree succeeds while 'git ls-files <path>' -- which |
| 295 |
# resolves its pathspec relative to the current directory -- names a |
| 296 |
# build artifact rather than the source file, and reports nothing. |
| 297 |
# Ask git where the top of the tree is and query from there instead, |
| 298 |
# skipping where there is no git at all (a released tarball on a smoker). |
| 299 |
my ( $toplevel, undef, $rev_status ) = capture { |
| 300 |
system( 'git', 'rev-parse', '--show-toplevel' ); |
| 301 |
}; |
| 302 |
$rev_status == 0 |
| 303 |
or plan skip_all => 'not a git checkout, so tracking cannot be checked'; |
| 304 |
chomp $toplevel; |
| 305 |
|
| 306 |
-d path( $toplevel, 'share/skel/default' ) |
| 307 |
or plan skip_all => "git top level $toplevel is not this source tree"; |
| 308 |
|
| 309 |
my ($tracked) = capture { |
| 310 |
system( 'git', '-C', $toplevel, 'ls-files', |
| 311 |
'share/skel/default/environments' ); |
| 312 |
}; |
| 313 |
like( $tracked, qr{development\.yml}, 'development.yml is tracked by git' ); |
| 314 |
like( $tracked, qr{production\.yml}, 'production.yml is tracked by git' ); |
| 315 |
|
| 316 |
# And the generator actually produced them for the app built at the top of |
| 317 |
# this file -- already covered by @required above, but asserted here too |
| 318 |
# since this is the subtest that explains why. |
| 319 |
ok( $app->child('environments/development.yml')->is_file, |
| 320 |
'environments/development.yml is generated' ); |
| 321 |
ok( $app->child('environments/production.yml')->is_file, |
| 322 |
'environments/production.yml is generated' ); |
| 323 |
}; |
| 324 |
|
| 325 |
subtest '-g creates a git repository with an initial commit (fixed)' => sub { |
| 326 |
# _check_git used to die with "Can't locate object method \"absolute\" |
| 327 |
# via package ..." -- it called ->absolute on $vars->{apppath}, a plain |
| 328 |
# string, instead of using $vars->{appdir}, which run() had already made |
| 329 |
# absolute for exactly this purpose. That happened after the application |
| 330 |
# was written but before git ever ran, so the user was left with a |
| 331 |
# generated app, no repository, and a stack-shaped error instead of the |
| 332 |
# how-to-run banner. -r implies -g and shares the same code path, so a |
| 333 |
# working -g is what -r's remote-adding also depends on. |
| 334 |
which('git') |
| 335 |
or plan skip_all => 'no usable git binary found'; |
| 336 |
|
| 337 |
my $dir = tempdir( CLEANUP => 1 ); |
| 338 |
|
| 339 |
# git needs a name and email to commit, and may consult other bits of |
| 340 |
# config (e.g. commit.gpgsign) that vary developer to developer and could |
| 341 |
# make this hang or fail for reasons that have nothing to do with the code |
| 342 |
# under test. Rather than rely on -- or fight with -- whatever is in the |
| 343 |
# developer's own ~/.gitconfig, give the child processes below a HOME of |
| 344 |
# their own (so no global or user config is read at all) and supply the |
| 345 |
# commit identity git itself reads via the environment. |
| 346 |
my $git_home = tempdir( CLEANUP => 1 ); |
| 347 |
local $ENV{'HOME'} = $git_home; |
| 348 |
local $ENV{'XDG_CONFIG_HOME'} = $git_home; |
| 349 |
local $ENV{'GIT_AUTHOR_NAME'} = 'Dancer2 Test Suite'; |
| 350 |
local $ENV{'GIT_AUTHOR_EMAIL'} = '[email protected]'; |
| 351 |
local $ENV{'GIT_COMMITTER_NAME'} = 'Dancer2 Test Suite'; |
| 352 |
local $ENV{'GIT_COMMITTER_EMAIL'} = '[email protected]'; |
| 353 |
|
| 354 |
my $res = gen( '-a', 'Git::App', '-d', 'gitapp', '--path', $dir, '-g' ); |
| 355 |
my $gitapp = path( $dir, 'gitapp' ); |
| 356 |
|
| 357 |
is( $res->{'status'}, 0, 'dancer2 gen -g exits successfully' ) |
| 358 |
or diag "STDERR:\n$res->{stderr}\nSTDOUT:\n$res->{stdout}"; |
| 359 |
like( $res->{'stdout'}, qr/Your new application is ready/, |
| 360 |
'and still ends with the how-to-run banner, not a stack trace' ); |
| 361 |
|
| 362 |
ok( $gitapp->child('.git')->is_dir, 'a git repository was created' ); |
| 363 |
|
| 364 |
my $git_dir = $gitapp->child('.git')->stringify; |
| 365 |
my ( $log, undef, $log_status ) = capture { |
| 366 |
system( 'git', '--git-dir', $git_dir, 'log', '--oneline' ); |
| 367 |
}; |
| 368 |
is( $log_status, 0, 'the repository has at least one commit' ); |
| 369 |
like( $log, qr/Initial commit of Git::App by Dancer2/, |
| 370 |
'and it is the generator\'s initial commit' ); |
| 371 |
|
| 372 |
my ( $tracked, undef, $ls_status ) = capture { |
| 373 |
system( 'git', '--git-dir', $git_dir, '--work-tree', $gitapp->stringify, |
| 374 |
'ls-files' ); |
| 375 |
}; |
| 376 |
is( $ls_status, 0, 'can list the files the initial commit contains' ); |
| 377 |
like( $tracked, qr{bin/app\.psgi}, 'the generated application files are in it' ); |
| 378 |
like( $tracked, qr{lib/Git/App\.pm}, 'including the generated module' ); |
| 379 |
}; |
| 380 |
|
| 381 |
done_testing(); |