-
Notifications
You must be signed in to change notification settings - Fork 34
/
create.t
459 lines (372 loc) · 17.7 KB
/
create.t
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use Statocles::Base 'Test';
my $SHARE_DIR = path( __DIR__, '..', 'share' );
use Statocles::Command;
use Capture::Tiny qw( capture );
use YAML;
use File::Share qw( dist_dir );
my ( undef, undef, undef, $day, $mon, $year ) = localtime;
$year += 1900;
$mon += 1;
my @date_parts = (
sprintf( '%04i', $year ),
sprintf( '%02i', $mon ),
sprintf( '%02i', $day ),
);
my $create_dir = path( dist_dir( 'Statocles' ), 'create' );
subtest 'create a site' => sub {
my $cwd = cwd;
subtest 'project site with file deploy' => sub {
my $tmp = tempdir;
chdir $tmp;
my $in = $SHARE_DIR->child( qw( create project_file_in.txt ) )->openr_utf8;
local *STDIN = $in;
my @args = ( 'create', 'example.com' );
my ( $out, $err, $exit ) = capture { Statocles::Command->main( @args ) };
is $exit, 0;
ok !$err, 'nothing on stderr' or diag "STDERR: $err";
eq_or_diff $out, $SHARE_DIR->child( qw( create project_file_out.txt ) )->slurp_utf8;
my $expect_config = site_config();
$expect_config->{site}{args}{nav}{main}[0]{href} = "/blog";
$expect_config->{site}{args}{index} = "/page";
$expect_config->{site}{args}{base_url} = "http://example.com";
$expect_config->{deploy}{class} = "Statocles::Deploy::File";
$expect_config->{deploy}{args}{path} = ".";
ok $tmp->child( 'example.com', 'site.yml' )->is_file, 'site.yml file exists';
my $config = $tmp->child( 'example.com', 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';
ok $tmp->child( 'example.com', 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'example.com', 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
$create_dir->child( 'blog', 'post.markdown' )->slurp,
'first post exists';
ok $tmp->child( 'example.com', 'static' )->is_dir, 'static dir exists';
ok $tmp->child( 'example.com', 'page' )->is_dir, 'page dir exists';
eq_or_diff $tmp->child( 'example.com', 'page', 'index.markdown' )->slurp,
$create_dir->child( 'page', 'index.markdown' )->slurp,
'first page exists';
ok !$tmp->child( 'example.com', 'theme' )->exists, 'theme dir does not exists';
subtest 'build the site' => \&test_site_build, $tmp->child( 'example.com' );
chdir $cwd;
};
subtest 'do nothing at all' => sub {
my $tmp = tempdir;
chdir $tmp;
my $in = $SHARE_DIR->child( qw( create none_in.txt ) )->openr_utf8;
local *STDIN = $in;
my @args = ( 'create', 'http://example.com' );
my ( $out, $err, $exit ) = capture { Statocles::Command->main( @args ) };
is $exit, 0;
ok !$err, 'nothing on stderr' or diag "STDERR: $err";
eq_or_diff $out, $SHARE_DIR->child( qw( create none_out.txt ) )->slurp_utf8;
my $expect_config = site_config();
$expect_config->{site}{args}{base_url} = "http://example.com";
$expect_config->{deploy}{class} = 'Statocles::Deploy::File';
$expect_config->{deploy}{args}{path} = '.';
ok $tmp->child( 'example.com', 'site.yml' )->is_file, 'site.yml file exists';
my $config = $tmp->child( 'example.com', 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';
ok $tmp->child( 'example.com', 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'example.com', 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
$create_dir->child( 'blog', 'post.markdown' )->slurp,
'first post exists';
ok $tmp->child( 'example.com', 'static' )->is_dir, 'static dir exists';
ok $tmp->child( 'example.com', 'page' )->is_dir, 'page dir exists';
eq_or_diff $tmp->child( 'example.com', 'page', 'index.markdown' )->slurp,
$create_dir->child( 'page', 'index.markdown' )->slurp,
'first page exists';
ok !$tmp->child( 'example.com', 'theme' )->exists, 'theme dir does not exists';
subtest 'build the site' => \&test_site_build, $tmp->child( 'example.com' );
chdir $cwd;
};
subtest 'site in the current directory' => sub {
my $tmp = tempdir;
chdir $tmp;
my $in = $SHARE_DIR->child( qw( create project_file_with_base_in.txt ) )->openr_utf8;
local *STDIN = $in;
my @args = ( 'create' );
my ( $out, $err, $exit ) = capture { Statocles::Command->main( @args ) };
is $exit, 0;
ok !$err, 'nothing on stderr' or diag "STDERR: $err";
eq_or_diff $out, $SHARE_DIR->child( qw( create project_file_with_base_out.txt ) )->slurp_utf8;
my $expect_config = site_config();
# Missing http:// gets added
$expect_config->{site}{args}{base_url} = "http://example.com";
$expect_config->{site}{args}{nav}{main}[0]{href} = "/blog";
$expect_config->{site}{args}{index} = "/page";
$expect_config->{deploy}{class} = "Statocles::Deploy::File";
$expect_config->{deploy}{args}{path} = ".";
ok $tmp->child( 'site.yml' )->is_file, 'site.yml file exists';
my $config = $tmp->child( 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';
ok $tmp->child( 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
$create_dir->child( 'blog', 'post.markdown' )->slurp,
'first post exists';
ok $tmp->child( 'static' )->is_dir, 'static dir exists';
ok $tmp->child( 'page' )->is_dir, 'page dir exists';
eq_or_diff $tmp->child( 'page', 'index.markdown' )->slurp,
$create_dir->child( 'page', 'index.markdown' )->slurp,
'first page exists';
ok !$tmp->child( 'theme' )->exists, 'theme dir does not exists';
subtest 'build the site' => \&test_site_build, $tmp->child( 'example.com' );
chdir $cwd;
};
subtest 'https base url' => sub {
my $tmp = tempdir;
chdir $tmp;
my $in = $SHARE_DIR->child( qw( create project_file_with_https_base_in.txt ) )->openr_utf8;
local *STDIN = $in;
my @args = ( 'create' );
my ( $out, $err, $exit ) = capture { Statocles::Command->main( @args ) };
is $exit, 0;
ok !$err, 'nothing on stderr' or diag "STDERR: $err";
eq_or_diff $out, $SHARE_DIR->child( qw( create project_file_with_https_base_out.txt ) )->slurp_utf8;
my $expect_config = site_config();
$expect_config->{site}{args}{base_url} = "https://example.com";
$expect_config->{site}{args}{nav}{main}[0]{href} = "/blog";
$expect_config->{site}{args}{index} = "/page";
$expect_config->{deploy}{class} = "Statocles::Deploy::File";
$expect_config->{deploy}{args}{path} = ".";
ok $tmp->child( 'site.yml' )->is_file, 'site.yml file exists';
my $config = $tmp->child( 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';
ok $tmp->child( 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
$create_dir->child( 'blog', 'post.markdown' )->slurp,
'first post exists';
ok $tmp->child( 'static' )->is_dir, 'static dir exists';
ok $tmp->child( 'page' )->is_dir, 'page dir exists';
eq_or_diff $tmp->child( 'page', 'index.markdown' )->slurp,
$create_dir->child( 'page', 'index.markdown' )->slurp,
'first page exists';
ok !$tmp->child( 'theme' )->exists, 'theme dir does not exists';
subtest 'build the site' => \&test_site_build, $tmp->child( 'example.com' );
chdir $cwd;
};
subtest 'base url and directory on the command-line' => sub {
my $tmp = tempdir;
chdir $tmp;
my $in = $SHARE_DIR->child( qw( create none_in.txt ) )->openr_utf8;
local *STDIN = $in;
my @args = ( 'create', 'http://site.example.com', 'example.com' );
my ( $out, $err, $exit ) = capture { Statocles::Command->main( @args ) };
is $exit, 0;
ok !$err, 'nothing on stderr' or diag "STDERR: $err";
eq_or_diff $out, $SHARE_DIR->child( qw( create none_out.txt ) )->slurp_utf8;
my $expect_config = site_config();
$expect_config->{site}{args}{base_url} = "http://site.example.com";
$expect_config->{deploy}{class} = 'Statocles::Deploy::File';
$expect_config->{deploy}{args}{path} = '.';
ok $tmp->child( 'example.com', 'site.yml' )->is_file, 'site.yml file exists';
my $config = $tmp->child( 'example.com', 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';
ok $tmp->child( 'example.com', 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'example.com', 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
$create_dir->child( 'blog', 'post.markdown' )->slurp,
'first post exists';
ok $tmp->child( 'example.com', 'static' )->is_dir, 'static dir exists';
ok $tmp->child( 'example.com', 'page' )->is_dir, 'page dir exists';
eq_or_diff $tmp->child( 'example.com', 'page', 'index.markdown' )->slurp,
$create_dir->child( 'page', 'index.markdown' )->slurp,
'first page exists';
ok !$tmp->child( 'example.com', 'theme' )->exists, 'theme dir does not exists';
subtest 'build the site' => \&test_site_build, $tmp->child( 'example.com' );
chdir $cwd;
};
chdir $cwd;
};
subtest 'git deploy' => sub {
if ( !eval { require Statocles::Deploy::Git; 1 } ) {
pass "No test: Statocles::Deploy::Git failed to load: $@";
return;
}
my $git_version = Statocles::Deploy::Git->_git_version;
diag "Git version: $git_version";
if ( $git_version < 1.007002 ) {
pass "No test: Git 1.7.2 or higher required";
return;
}
require Git::Repository;
my $cwd = cwd;
subtest 'basic blog site with git' => sub {
my $tmp = tempdir;
chdir $tmp;
my $in = $SHARE_DIR->child( qw( create basic_blog_in.txt ) )->openr_utf8;
local *STDIN = $in;
my @args = ( 'create', 'www.example.com' );
my ( $out, $err, $exit ) = capture { Statocles::Command->main( @args ) };
is $exit, 0;
ok !$err, 'nothing on stderr' or diag "STDERR: $err";
eq_or_diff $out, $SHARE_DIR->child( qw( create basic_blog_out.txt ) )->slurp_utf8;
ok $tmp->child( 'www.example.com', 'site.yml' )->is_file, 'site.yml file exists';
my $expect_config = site_config();
$expect_config->{site}{args}{base_url} = "http://www.example.com";
$expect_config->{deploy}{class} = 'Statocles::Deploy::Git';
$expect_config->{deploy}{args}{branch} = 'master';
$expect_config->{theme}{args}{store} = 'theme';
my $config = $tmp->child( 'www.example.com', 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';
ok $tmp->child( 'www.example.com', 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'www.example.com', 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
$create_dir->child( 'blog', 'post.markdown' )->slurp,
'first post exists';
ok $tmp->child( 'www.example.com', 'static' )->is_dir, 'static dir exists';
ok $tmp->child( 'www.example.com', 'page' )->is_dir, 'page dir exists';
eq_or_diff $tmp->child( 'www.example.com', 'page', 'index.markdown' )->slurp,
$create_dir->child( 'page', 'index.markdown' )->slurp,
'first page exists';
ok $tmp->child( 'www.example.com', 'theme' )->is_dir, 'theme dir exists';
ok $tmp->child( 'www.example.com', '.git' )->is_dir, 'git repository created';
eq_or_diff $tmp->child( 'www.example.com', '.gitignore' )->slurp,
qq{\n.statocles\n},
'gitignore is created',
;
subtest 'build the site' => \&test_site_build, $tmp->child( 'www.example.com' );
chdir $cwd;
};
subtest 'init in existing git repo' => sub {
my $tmp = tempdir;
chdir $tmp;
Git::Repository->run( 'init' );
$tmp->child( '.gitignore' )->spew( "some\n\nexisting\npaths\n" );
my $in = $SHARE_DIR->child( qw( create basic_blog_in.txt ) )->openr_utf8;
local *STDIN = $in;
my @args = ( 'create', 'http://example.com', '.' );
my ( $out, $err, $exit ) = capture { Statocles::Command->main( @args ) };
is $exit, 0;
ok !$err, 'nothing on stderr' or diag "STDERR: $err";
eq_or_diff $out, $SHARE_DIR->child( qw( create basic_blog_out.txt ) )->slurp_utf8;
ok $tmp->child( 'site.yml' )->is_file, 'site.yml file exists';
my $expect_config = site_config();
$expect_config->{site}{args}{base_url} = "http://example.com";
$expect_config->{deploy}{class} = 'Statocles::Deploy::Git';
$expect_config->{deploy}{args}{branch} = 'master';
$expect_config->{theme}{args}{store} = 'theme';
my $config = $tmp->child( 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';
ok $tmp->child( 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
$create_dir->child( 'blog', 'post.markdown' )->slurp,
'first post exists';
ok $tmp->child( 'static' )->is_dir, 'static dir exists';
ok $tmp->child( 'page' )->is_dir, 'page dir exists';
eq_or_diff $tmp->child( 'page', 'index.markdown' )->slurp,
$create_dir->child( 'page', 'index.markdown' )->slurp,
'first page exists';
ok $tmp->child( 'theme' )->is_dir, 'theme dir exists';
ok $tmp->child( '.git' )->is_dir, 'git repository still exists';
eq_or_diff $tmp->child( '.gitignore' )->slurp,
qq{some\n\nexisting\npaths\n\n.statocles\n},
'gitignore is added to',
;
subtest 'build the site' => \&test_site_build, $tmp;
chdir $cwd;
};
};
done_testing;
sub test_site_build {
my ( $tmp ) = @_;
my $cwd = cwd;
chdir $tmp;
my ( $out, $err, $exit );
eval {
( $out, $err, $exit ) = capture { Statocles::Command->main( 'build' ) };
};
ok !$@, 'completed successfully' or diag $@;
is $exit, 0;
ok !$err, 'nothing on stderr' or diag "STDERR: $err";
ok !$out, 'nothing on stdout' or diag "STDOUT: $out";
chdir $cwd;
}
sub site_config {
return {
site => {
class => 'Statocles::Site',
args => {
title => 'My Statocles Site',
nav => {
main => [
{
text => 'Blog',
href => '/',
},
],
},
theme => { '$ref' => 'theme' },
apps => {
blog => { '$ref' => 'blog_app' },
page => { '$ref' => 'page_app' },
static => { '$ref' => 'static_app' },
},
index => '/blog',
deploy => { '$ref' => 'deploy' },
plugins => {
link_check => {
'$class' => 'Statocles::Plugin::LinkCheck',
},
},
},
},
blog_app => {
class => 'Statocles::App::Blog',
args => {
store => 'blog',
url_root => '/blog',
},
},
page_app => {
class => 'Statocles::App::Basic',
args => {
store => 'page',
url_root => '/page',
},
},
static_app => {
class => 'Statocles::App::Basic',
args => {
store => 'static',
url_root => '/static',
},
},
theme => {
class => 'Statocles::Theme',
args => {
store => '::default',
},
},
};
}