-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_test.pl
More file actions
executable file
·88 lines (76 loc) · 2.28 KB
/
new_test.pl
File metadata and controls
executable file
·88 lines (76 loc) · 2.28 KB
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
#!/usr/bin/env perl
use v5.36;
use open ':std', ':encoding(UTF-8)';
use Getopt::Long::Descriptive;
use Path::Tiny;
use Sq -sig => 1;
# This makes the code Lazy. That means the code is basically just wrapped
# in a subroutine. But it has an intent over just a sub-ref. The idea is that
# this code is not executed immediately, but maybe at a later time. lazy {}
# returns a lazy object. This object ensures that when it is run, only runs
# a single time.
my $use = lazy {
my $folders =
Sq->fs->children('t')
->keep(call 'is_dir')
->rxs (qr{\At/}, sub { " + " })
->sort(by_str)
->join("\n");
return join("\n",
q(USAGE:),
qq(\t%c %o),
q(),
q(EXAMPLE:),
qq(\t\$ new_test.pl -f Seq -t hello),
qq(\tCreated 't/Seq/02-hello.t' ...),
q(),
q(FOLDERS:),
$folders,
q(),
q(OPTIONS:)
);
};
# Here lazy{} not really makes sense, because the `$use->force` is always evaluated.
# So we don't really need that. But what makes lazy different is that another new
# call to `$use->force` will not run the function twice. Code is only runned once
# and the result is saved/cached.
my ($opt, $usage) = describe_options(
$use->force,
['folder|f=s', 'folder inside t/ to create test-file', {default => '.'}],
['test|t=s', 'name of the test. Without number and .t', {required => 1}],
['help|h', 'Print this message', {shortcircuit => 1}],
);
$usage->die if $opt->help;
# get the maximum id from test-files so far
my $maximum_id =
Sq->fs
->children('t', $opt->folder )
->map(call 'basename' )
->rxm(qr/\A(\d+) .* \.t\z/xms )
->fsts
->max
->or(-1);
# Load DATA into array
my @content = <DATA>;
# file to create
my $basename = sprintf "%02d-%s.t", ($maximum_id + 1), $opt->test;
my $file = path('t', $opt->folder => $basename);
# abort when file exists
if ( -e $file ) {
die "Abort: Requested file already exists. Not created.\n";
}
# create test file from template
else {
$file->spew_utf8(@content);
printf "Created '%s' ...\n", $file;
}
__DATA__
#!perl
use 5.036;
use utf8;
use open ':std', ':encoding(UTF-8)';
use Sq -sig => 1;
use Sq::Parser;
use Sq::Test;
ok(1, 'Write a test');
done_testing;