line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Command::swat; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.0.5'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
22442
|
use Mojo::Base 'Mojolicious::Command'; |
|
1
|
|
|
|
|
12845
|
|
|
1
|
|
|
|
|
6
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
139038
|
use re 'regexp_pattern'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
166
|
|
8
|
1
|
|
|
1
|
|
3553
|
use Getopt::Long qw(GetOptionsFromArray); |
|
1
|
|
|
|
|
12409
|
|
|
1
|
|
|
|
|
4
|
|
9
|
1
|
|
|
1
|
|
168
|
use File::Path qw(make_path); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
895
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has description => 'Generates swat tests scaffolding for mojo application'; |
12
|
|
|
|
|
|
|
has usage => sub { shift->extract_usage }; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub run { |
15
|
|
|
|
|
|
|
|
16
|
0
|
|
|
0
|
1
|
|
my ($self, @args) = @_; |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
|
GetOptionsFromArray \@args, 'f|force' => \my $force; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
mkdir 'swat'; |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
|
my $hostname_file = 'swat/host'; |
23
|
|
|
|
|
|
|
|
24
|
0
|
0
|
|
|
|
|
open(my $fh, '>', $hostname_file) or die "Could not open file $hostname_file: $!"; |
25
|
0
|
|
|
|
|
|
print $fh "http://127.0.0.1:3000"; |
26
|
0
|
|
|
|
|
|
close $fh; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
my $rows = []; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
_walk($_, 0, $rows, 0) for @{$self->app->routes->children}; |
|
0
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
ROUTE: for my $i (@$rows){ |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
my $http_method = $i->[1]; |
36
|
0
|
|
|
|
|
|
my $route = $i->[0]; |
37
|
|
|
|
|
|
|
|
38
|
0
|
0
|
|
|
|
|
unless ($http_method=~/GET|POST|DELETE|PUT/i){ |
39
|
0
|
|
|
|
|
|
print "sorry, swat does not support $http_method methods yet \n"; |
40
|
0
|
|
|
|
|
|
next ROUTE; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $filename = "swat/$route/"; |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
0
|
|
|
|
if (-e $filename and !$force){ |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
print "skip route $route - swat test already exist, use --force to override existed routes \n"; |
48
|
0
|
|
|
|
|
|
next ROUTE; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
} else { |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
print "generate swat route for $route ... \n"; |
53
|
0
|
|
|
|
|
|
make_path("swat/$route"); |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
print "generate swat data for $http_method $route ... \n"; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
$filename.=lc($http_method); $filename.=".txt"; |
|
0
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!"; |
59
|
0
|
|
|
|
|
|
print $fh "200 OK\n"; |
60
|
0
|
|
|
|
|
|
close $fh; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _walk { |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
0
|
|
|
my ($route, $depth, $rows, $verbose) = @_; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Pattern |
72
|
0
|
|
|
|
|
|
my $prefix = ''; |
73
|
0
|
0
|
|
|
|
|
if (my $i = $depth * 2) { $prefix .= ' ' x $i . '+' } |
|
0
|
|
|
|
|
|
|
74
|
0
|
|
0
|
|
|
|
push @$rows, my $row = [$prefix . ($route->pattern->unparsed || '/')]; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Flags |
77
|
0
|
|
|
|
|
|
my @flags; |
78
|
0
|
0
|
|
|
|
|
push @flags, @{$route->over || []} ? 'C' : '.'; |
|
0
|
0
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
push @flags, (my $partial = $route->partial) ? 'D' : '.'; |
80
|
0
|
0
|
|
|
|
|
push @flags, $route->inline ? 'U' : '.'; |
81
|
0
|
0
|
|
|
|
|
push @flags, $route->is_websocket ? 'W' : '.'; |
82
|
0
|
0
|
|
|
|
|
push @$row, join('', @flags) if $verbose; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Methods |
85
|
0
|
|
|
|
|
|
my $via = $route->via; |
86
|
0
|
0
|
|
|
|
|
push @$row, !$via ? '*' : uc join ',', @$via; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Name |
89
|
0
|
|
|
|
|
|
my $name = $route->name; |
90
|
0
|
0
|
|
|
|
|
push @$row, $route->has_custom_name ? qq{"$name"} : $name; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Regex (verbose) |
93
|
0
|
|
|
|
|
|
my $pattern = $route->pattern; |
94
|
0
|
|
0
|
|
|
|
$pattern->match('/', $route->is_endpoint && !$partial); |
95
|
0
|
|
|
|
|
|
my $regex = (regexp_pattern $pattern->regex)[0]; |
96
|
0
|
|
|
|
|
|
my $format = (regexp_pattern($pattern->format_regex))[0]; |
97
|
0
|
0
|
|
|
|
|
push @$row, $regex, $format ? $format : '' if $verbose; |
|
|
0
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
$depth++; |
100
|
0
|
|
|
|
|
|
_walk($_, $depth, $rows, $verbose) for @{$route->children}; |
|
0
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
$depth--; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
1; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
__END__ |