script/mojopaste

filter: Statementstmt 95.9% 119 / 124 Branchbran 78.2% 36 / 46 Conditioncond 54.3% 31 / 57 MC/DCmcdc 32.1% 9 / 28 Subroutinesub 96.1% 25 / 26 Podpod n/a totaltotal 78.2% 220 / 281 CC 34 SCAR 16.7
CC
34
Cov
82%
__ANON__
15.9
_load_p
5.1
_chart
5.0
CRAP 5.3
↑ Summary blib/lib/App/mojopaste.pm »
1 #!perl
2 package App::mojopaste::Backend::File;
3 7 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Mojo::Util qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 my $ID = 0;
10  
11 7 ▶sub register {
Subroutine register: called CC=2 SCAR=6.9
12 7 my ($self, $app, $config) = @_;
13 7 my $dir = $app->config('paste_dir');
14 7 ▶ path($dir)->make_path unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 6 7
15 7 ▶ $app->helper('paste.load_p' => sub { _load_p($dir, @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
16 7 ▶ $app->helper('paste.save_p' => sub { _save_p($dir, @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
17 }
18  
19 12 ▶sub _load_p {
Subroutine _load_p: called CC=5 SCAR=16.2
20 12 my ($dir, $c, $id) = @_;
21 12 my @res = ('', '');
22  
23 eval {
24 12 ▶ die "Hacking attempt! paste_id=($id)" if !$id or $id =~ m!\W!;
Branch: 1 branch
BranchTrueFalseTotal
if not $id or $id =~ /\W/u 1 11 12
Condition66%2/3
not $id or $id =~ /\W/u
l!l&&r!l&&!r
0111
MC/DC50%1/2
not $id or $id =~ /\W/u
not $id $id =~ /\W/u
Truth table66%2/3
not(A) or B
A= not $idB= $id =~ /\W/u
ABresult
1X1
011
000
25 11 return Mojo::Promise->new->resolve(decode 'UTF-8', path($dir, $id)->slurp);
26 12 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { die "Hacking attempt! paste_id=($id)" if not $id or $id =~ /\W/u; return "Mojo::Promise"->new->resolve(decode("UTF-8", path($dir, $id)->slurp)) } } or do { return "Mojo::Promise"->new->reject($@ || "Paste not found") } 8 4 12
27 4 ▶ return Mojo::Promise->new->reject($@ || 'Paste not found');
Condition50%1/2
$@ || 'Paste not found'
l!l
40
MC/DC0%0/1
$@ || 'Paste not found'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
28 };
29 }
30  
31 3 ▶sub _save_p {
Subroutine _save_p: called CC=3 SCAR=12.3
32 3 my ($dir, $c, $text) = @_;
33 3 my $id = substr Mojo::Util::md5_sum($$ . time . $ID++), 0, 12;
34 3 my @res = ('', '');
35  
36 eval {
37 3 path($dir, $id)->spurt(encode 'UTF-8', $text);
38 3 return Mojo::Promise->new->resolve($id);
39 3 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { path($dir, $id)->spurt(encode("UTF-8", $text)); return "Mojo::Promise"->new->resolve($id) } } or do { return "Mojo::Promise"->new->reject($@ || "Unknown error") } 3 0 3
40 0 ▶ return Mojo::Promise->new->reject($@ || 'Unknown error');
Condition0%0/2
$@ || 'Unknown error'
l!l
00
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
41 };
42 }
43  
44 package main;
45 7 ▶use Mojolicious::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 7 ▶use Mojo::JSON 'true';
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 7 ▶plugin 'config' if $ENV{MOJO_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOJO_CONFIG"} 1 6 7
50 7 ▶app->config->{backend} ||= $ENV{PASTE_BACKEND} || 'File';
Condition40%2/5
app()->config->{"backend"} ||= $ENV{"PASTE_BACKEND"} || "File"
l!l&&r!l&&!r
070
$ENV{'PASTE_BACKEND'} || 'File'
l!l
07
MC/DC0%0/2
app()->config->{"backend"} ||= $ENV{"PASTE_BACKEND"} || "File"
app()->config->{"backend"} $ENV{'PASTE_BACKEND'}
Truth table0%0/3
A ||= B
A= app()->config->{"backend"}B= $ENV{'PASTE_BACKEND'}
ABresult
1X1
011
000
51 7 ▶app->config->{paste_dir} ||= $ENV{PASTE_DIR} || 'paste';
Condition60%3/5
app()->config->{"paste_dir"} ||= $ENV{"PASTE_DIR"} || "paste"
l!l&&r!l&&!r
160
$ENV{'PASTE_DIR'} || 'paste'
l!l
60
MC/DC0%0/2
app()->config->{"paste_dir"} ||= $ENV{"PASTE_DIR"} || "paste"
app()->config->{"paste_dir"} $ENV{'PASTE_DIR'}
Truth table66%2/3
A ||= B
A= app()->config->{"paste_dir"}B= $ENV{'PASTE_DIR'}
ABresult
1X1
011
000
52  
53 app->defaults(
54 brand_link => app->config('brand_link') || $ENV{PASTE_BRAND_LINK} || 'index',
55 brand_logo => app->config('brand_logo') // $ENV{PASTE_BRAND_LOGO} // '/images/logo.png',
56 brand_name => app->config('brand_name') // $ENV{PASTE_BRAND_NAME} // 'Mojopaste',
57 enable_charts => app->config('enable_charts') // $ENV{PASTE_ENABLE_CHARTS},
58 7 ▶ embed => 'description,graph,heading,nav',
Condition44%8/18
app()->config('brand_link') || $ENV{'PASTE_BRAND_LINK'} || 'index'
l!l
07
app()->config("brand_link") or $ENV{"PASTE_BRAND_LINK"}
l!l&&r!l&&!r
007
(app()->config("brand_logo") // $ENV{"PASTE_BRAND_LOGO"}) // "/images/logo.png"
l!l
07
app()->config("brand_logo") // $ENV{"PASTE_BRAND_LOGO"}
l!l&&r!l&&!r
007
(app()->config("brand_name") // $ENV{"PASTE_BRAND_NAME"}) // "Mojopaste"
l!l
07
app()->config("brand_name") // $ENV{"PASTE_BRAND_NAME"}
l!l&&r!l&&!r
007
app()->config("enable_charts") // $ENV{"PASTE_ENABLE_CHARTS"}
l!l&&r!l&&!r
025
MC/DC0%0/2
app()->config('brand_link') || $ENV{'PASTE_BRAND_LINK'} || 'index'
app()->config("brand_link") $ENV{"PASTE_BRAND_LINK"}
MC/DC0%0/2
(app()->config("brand_logo") // $ENV{"PASTE_BRAND_LOGO"}) // "/images/logo.png"
app()->config("brand_logo") $ENV{"PASTE_BRAND_LOGO"}
MC/DC0%0/2
(app()->config("brand_name") // $ENV{"PASTE_BRAND_NAME"}) // "Mojopaste"
app()->config("brand_name") $ENV{"PASTE_BRAND_NAME"}
MC/DC50%1/2
app()->config("enable_charts") // $ENV{"PASTE_ENABLE_CHARTS"}
app()->config("enable_charts") $ENV{"PASTE_ENABLE_CHARTS"}
Truth table33%1/3
A or B
A= app()->config("brand_link")B= $ENV{"PASTE_BRAND_LINK"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= app()->config("brand_logo")B= $ENV{"PASTE_BRAND_LOGO"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= app()->config("brand_name")B= $ENV{"PASTE_BRAND_NAME"}
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= app()->config("enable_charts")B= $ENV{"PASTE_ENABLE_CHARTS"}
ABresult
1X1
011
000
59 error => '',
60 paste => '',
61 placeholder => 'Enter your text here and then press the "Save" button above.',
62 title => 'Mojopaste',
63 );
64  
65 7 my $backend = app->config('backend');
66 7 ▶plugin $backend =~ /::/ ? $backend : "App::mojopaste::Backend::$backend";
Branch: 1 branch
BranchTrueFalseTotal
$backend =~ /::/u ? : 0 7 7
67  
68 4 ▶helper no_such_paste => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
69 4 my ($c, $err) = @_;
70 4 $c->app->log->debug("no_such_paste: $err");
71 4 $c->stash($_ => 'Could not find paste') for qw(error heading title);
72 4 $c->render(description => '', layout => 'mojopaste', status => 404);
73 7 };
74  
75 10 ▶helper set_title => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
76 10 my ($c, $prefix, $suffix) = @_;
77 10 ▶ my $brand_name = $c->stash('brand_name') || 'Mojopaste';
Condition100%2/2
$c->stash('brand_name') || 'Mojopaste'
l!l
91
MC/DC100%1/1
$c->stash('brand_name') || 'Mojopaste'
$c->stash('brand_name')
Truth table100%2/2
A
A= $c->stash('brand_name')
Aresult
11
00
78 10 ▶ $suffix = $suffix ? "$brand_name $suffix" : $brand_name;
Branch: 1 branch
BranchTrueFalseTotal
$suffix ? : 2 8 10
79 10 $prefix =~ s![\n\r]+! !g;
80 10 $prefix =~ s!^\W+!!g;
81 10 $prefix = substr $prefix, 0, 56 - length $suffix;
82 10 return $c->stash(title => "$prefix - $suffix");
83 7 };
84  
85 get(
86 7 ▶ '/' => {layout => 'mojopaste'} => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
87 7 my $c = shift;
88  
89 7 ▶ return $c->set_title("Create new paste") unless my $id = $c->param('edit');
Branch: 1 branch
BranchTrueFalseTotal
unless my $id = $c->param("edit") 4 3 7
90 1 ▶ return $c->render_later->paste->load_p($id)->then(sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
91 1 ▶ return $c->no_such_paste('Could not find paste') unless my $paste = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless my $paste = shift() 0 1 1
92 1 $c->set_title(substr($paste, 0, 80), 'edit');
93 1 $c->param(paste => $paste)->render;
94 3 ▶ })->catch(sub { $c->no_such_paste(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
95 },
96 7 'index'
97 );
98  
99 post(
100 '/' => {layout => 'mojopaste'},
101 5 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
102 5 my $c = shift;
103 5 ▶ my $paste = $c->param('paste') || '';
Condition100%2/2
$c->param('paste') || ''
l!l
32
MC/DC100%1/1
$c->param('paste') || ''
$c->param('paste')
Truth table100%2/2
A
A= $c->param('paste')
Aresult
11
00
104  
105 5 ▶ return $c->render('index', placeholder => 'You neeed to enter some characters!', status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $paste =~ /\w/u 2 3 5
106 unless $paste =~ /\w/;
107 3 ▶ return $c->render_later->paste->save_p($paste)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
108 3 $c->redirect_to('show', paste_id => shift);
109 0 ▶ })->catch(sub { $c->reply->exception(shift) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
110 }
111 7 );
112  
113 get(
114 '/:paste_id',
115 [format => ['html', 'txt']],
116 {format => undef},
117 7 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
118 7 my $c = shift;
119 7 ▶ my $format = $c->stash('format') || '';
Condition100%2/2
$c->stash('format') || ''
l!l
16
MC/DC100%1/1
$c->stash('format') || ''
$c->stash('format')
Truth table100%2/2
A
A= $c->stash('format')
Aresult
11
00
120  
121 6 ▶ $c->render_later->paste->load_p($c->stash('paste_id'))->then(sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
122 6 my $paste = shift;
123 6 ▶ if (!$paste) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $paste) { } 0 6 6
elsif ($c->param('raw') or $format eq 'txt') { } 2 4 6
124 0 $c->no_such_paste('Could not find paste');
125 }
126 ▶ elsif ($c->param('raw') or $format eq 'txt') {
Condition100%3/3
$c->param("raw") or $format eq "txt"
l!l&&r!l&&!r
114
MC/DC100%2/2
$c->param("raw") or $format eq "txt"
$c->param("raw") $format eq "txt"
Truth table100%3/3
A or B
A= $c->param("raw")B= $format eq "txt"
ABresult
1X1
011
000
127 2 $c->res->headers->content_type('text/plain; charset=utf-8');
128 2 $c->render(text => $paste);
129 }
130 else {
131 4 $c->set_title(substr($paste, 0, 80));
132 4 $c->res->headers->header('X-Plain-Text-URL' => $c->url_for(format => 'txt')->userinfo(undef)->to_abs);
133 4 ▶ $c->stash(embed => $c->param('embed')) if $c->param('embed');
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("embed") 1 3 4
134 4 $c->render(layout => 'mojopaste', paste => $paste);
135 }
136 7 ▶ })->catch(sub { $c->no_such_paste(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
137 },
138 7 'show'
139 );
140  
141 app->defaults('enable_charts') and get(
142 '/:paste_id/chart' => {layout => 'mojopaste'},
143 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.3
144 2 my $c = shift;
145 2 my $chart = {element => 'chart', data => [], hideHover => true, resize => true};
146 2 my ($heading, $description, $error) = ('', '', '');
147  
148 1 ▶ $c->render_later->paste->load_p($c->stash('paste_id'))->then(sub {
Subroutine __ANON__: called CC=11 SCAR=27.6
149 1 ▶ return $c->no_such_paste('Could not find paste') unless my $paste = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless my $paste = shift() 0 1 1
150  
151 1 ▶ while ($paste =~ s!^\s*(?://|\#)(.*)!!m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paste =~ s[^\s*(?://|\#)(.*)][]mu) 7 1 8
152 7 ▶ $description .= $1 if $heading;
Branch: 1 branch
BranchTrueFalseTotal
if $heading 5 2 7
153 7 ▶ $heading ||= $1;
Condition100%3/3
$heading ||= $1
l!l&&r!l&&!r
511
MC/DC100%2/2
$heading ||= $1
$heading $1
Truth table100%3/3
A ||= B
A= $headingB= $1
ABresult
1X1
011
000
154 }
155  
156 eval {
157 1 _chart($chart, grep { $_ =~ /\S/ } split /\r?\n/, $paste);
158 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _chart($chart, grep({$_ =~ /\S/u;} split(/\r?\n/u, $paste, 0))) } } or do { $error = $@ || "Unknown error"; $error =~ s/\s*at .*? line \d+.*//su } 1 0 1
159 0 ▶ $error = $@ || 'Unknown error';
Condition0%0/2
$@ || 'Unknown error'
l!l
00
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
160 0 $error =~ s!\s*at .*? line \d+.*!!s;
161 };
162  
163 1 ▶ $c->set_title($heading || $description || substr($paste, 0, 80), 'graph');
Condition33%2/6
$heading || $description || substr($paste, 0, 80)
l!l&&r!l&&!r
100
$heading or $description
l!l&&r!l&&!r
100
MC/DC0%0/3
$heading || $description || substr($paste, 0, 80)
$heading $description substr($paste, 0, 80)
Truth table25%1/4
A or B || C
A= $headingB= $descriptionC= substr($paste, 0, 80)
ABCresult
1XX1
01X1
0011
0000
164 1 ▶ $c->stash(embed => $c->param('embed')) if $c->param('embed');
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("embed") 0 1 1
165 1 ▶ $c->render(chart => $chart, description => $description // '', error => $error, heading => $heading);
Condition50%1/2
$description // ""
l!l
10
MC/DC0%0/1
$description // ""
$description
Truth table50%1/2
A
A= $description
Aresult
11
00
166 2 ▶ })->catch(sub { $c->no_such_paste(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
167 },
168 7 ▶ 'chart'
Branch: 1 branch
BranchTrueFalseTotal
app()->defaults("enable_charts") and get("/:paste_id/chart", {"layout", "mojopaste"}, sub { my $c = shift(); my $chart = {"element", "chart", "data", [], "hideHover", &true(), "resize", &true()}; my($heading, $description, $error) = ("", "", ""); $c->render_later->paste->load_p($c->stash("paste_id"))->then(sub { return $c->no_such_paste("Could not find paste") unless my $paste = shift(); while ($paste =~ s[^\s*(?://|\#)(.*)][]mu) { $description .= $1 if $heading; $heading ||= $1; } ; eval { do { _chart($chart, grep({$_ =~ /\S/u;} split(/\r?\n/u, $paste, 0))) } } or do { $error = $@ || "Unknown error"; $error =~ s/\s*at .*? line \d+.*//su }; $c->set_title($heading || $description || substr($paste, 0, 80), "graph"); $c->stash("embed", $c->param("embed")) if $c->param("embed"); $c->render("chart", $chart, "description", $description // "", "error", $error, "heading", $heading); } )->catch(sub { $c->no_such_paste(shift()); } ); } , "chart") 2 5 7
169 );
170  
171 7 app->start;
172  
173 1 ▶sub _chart {
Subroutine _chart: called CC=5 SCAR=16.2
174 1 my $chart = shift;
175 1 my $csv = Text::CSV->new;
176  
177 1 $csv->parse(shift @_); # heading
178 1 $chart->{ykeys} = [$csv->fields];
179 1 $chart->{xkey} = shift @{$chart->{ykeys}};
180 1 $chart->{labels} = $chart->{ykeys};
181 1 $chart->{pointStrokeColors} = '#222';
182  
183 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 1 4
184 3 ▶ die $csv->error_input unless $csv->parse(shift @_);
Branch: 1 branch
BranchTrueFalseTotal
unless $csv->parse(shift @_) 0 3 3
185 3 ▶ my @row = $csv->fields or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@row) = $csv->fields or next 3 0 3
186 3 push @{$chart->{data}}, {$chart->{xkey} => shift(@row), map { ($_ => 0 + shift @row) } @{$chart->{ykeys}}};
187 }
188  
189 1 ▶ die 'Could not parse CSV data.' unless @{$chart->{data}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$chart->{"data"};} 0 1 1
190 1 return $chart;
191 }
192  
193 =pod
194  
195 =encoding utf8
196  
197 =head1 NAME
198  
199 mojopaste - Pastebin application
200  
201 =head1 DESCRIPTION
202  
203 See L<App::mojopaste>.
204  
205 =head1 AUTHOR
206  
207 Jan Henning Thorsen - C<jhthorsen@cpan.org>
208  
209 =cut
210  
211 __DATA__
↑ Summary blib/lib/App/mojopaste.pm »