blib/lib/Liveman.pm

filter: Statementstmt 63.8% 189 / 296 Branchbran 28.0% 37 / 132 Conditioncond 21.4% 6 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 79.3% 23 / 29 Podpod 90.0% 9 / 10 totaltotal 51.5% 264 / 512 CC 80 SCAR 42.9
CC
80
Cov
51%
_to_testing
246.3
transforms
96.4
trans
90.0
CRAP 72.7
↑ Summary blib/lib/Liveman/Project.pm »
1 package Liveman;
2 3 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 our $VERSION = "4.2";
5  
6 3 ▶use File::Basename qw/dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Find::Wanted qw/find_wanted/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Spec qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Slurper qw/read_text write_text/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Path qw/mkpath rmtree/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Locale::PO qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Markdown::To::POD qw/markdown_to_pod/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Term::ANSIColor qw/colored/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Text::Trim qw/trim/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Liveman::Cpanfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Liveman::MinillaPod2Markdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Конструктор
19 4 ▶sub new {
Subroutine new: called CC=3 SCAR=12.3
Pod documented
20 4 my $cls = shift;
21 4 my $self = bless {@_}, $cls;
22 0 ▶ delete $self->{files} if $self->{files} && !scalar @{$self->{files}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"files"} and not scalar @{$self->{"files"};} 0 4 4
Condition33%1/3
$self->{"files"} and not scalar @{$self->{"files"};}
!ll&&!rl&&r
400
MC/DC0%0/2
$self->{"files"} and not scalar @{$self->{"files"};}
$self->{"files"} not scalar @{$self->{"files"};}
Truth table33%1/3
A and not(B)
A= $self->{"files"}B= not scalar @{$self->{"files"};}
ABresult
0X0
100
111
23 4 $self->{pod2markdown} = Liveman::MinillaPod2Markdown->new;
24 4 $self
25 }
26  
27 # Получить путь к тестовому файлу из пути к md-файлу
28 5 ▶sub test_path {
Subroutine test_path: called CC=1 SCAR=0.0
Pod documented
29 5 my ($self, $md) = @_;
30  
31 5 my ($volume, $chains) = File::Spec->splitpath($md, 1);
32 5 my @dirs = File::Spec->splitdir($chains);
33  
34 5 shift @dirs; # Удаляем lib
35 5 $dirs[$#dirs] =~ s!\.md$!\.t!;
36  
37 5 my $md = File::Spec->catfile("t", map { lcfirst($_) =~ s/[A-Z]/"-" . lc $&/gre } @dirs);
38  
39 5 $md
40 }
41  
42 # Трансформирует md-файлы
43 2 ▶sub transforms {
Subroutine transforms: called CC=17 SCAR=45.7
Pod documented
44 2 my ($self) = @_;
45 2 ▶ my $mds = $self->{files} // [ find_wanted(sub { /\.md$/ }, "lib") ];
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
$self->{"files"} // [find_wanted(sub { /\.md$/u; } , "lib")]
l!l
02
MC/DC0%0/1
$self->{"files"} // [find_wanted(sub { /\.md$/u; } , "lib")]
$self->{"files"}
Truth table50%1/2
A
A= $self->{"files"}
Aresult
11
00
46  
47 2 $self->{count} = 0;
48  
49 2 ▶ if($self->{compile_force}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'compile_force'}) { } 1 1 2
50 1 $self->transform($_) for @$mds;
51 } else {
52 1 for my $md (@$mds) {
53 1 my $test = $self->test_path($md);
54 1 my $mdmtime = (stat $md)[9];
55 1 ▶ die "Not exists file $md!" if !$mdmtime;
Branch: 1 branch
BranchTrueFalseTotal
unless $mdmtime 0 1 1
56 1 ▶ $self->transform($md, $test) if !-e $test
Branch: 1 branch
BranchTrueFalseTotal
if not -e $test or $mdmtime > (stat $test)[9] 0 1 1
Condition33%1/3
not -e $test or $mdmtime > (stat $test)[9]
l!l&&r!l&&!r
001
MC/DC0%0/2
not -e $test or $mdmtime > (stat $test)[9]
not -e $test $mdmtime > (stat $test)[9]
Truth table33%1/3
not(A) or B
A= not -e $testB= $mdmtime > (stat $test)[9]
ABresult
1X1
011
000
57 || $mdmtime > (stat $test)[9];
58 }
59 }
60  
61 # minil.toml и README.md
62 2 ▶ if(-f "minil.toml" && -r "minil.toml") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "minil.toml" and -r "minil.toml") 0 2 2
Condition33%1/3
-f "minil.toml" and -r "minil.toml"
!ll&&!rl&&r
200
MC/DC0%0/2
-f "minil.toml" and -r "minil.toml"
-f "minil.toml" -r "minil.toml"
Truth table33%1/3
A and B
A= -f "minil.toml"B= -r "minil.toml"
ABresult
0X0
100
111
63 0 my $is_copy; my $name;
64 0 eval {
65 0 my $minil = read_text("minil.toml");
66 0 ($name) = $minil =~ /^name\s*=\s*"([\w:-]+)"/m;
67 0 $name =~ s!(-|::)!/!g;
68 0 $name = "lib/$name.md";
69 0 ▶ if(-f $name && -r $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $name and -r $name) 0 0 0
Condition0%0/3
-f $name and -r $name
!ll&&!rl&&r
000
MC/DC0%0/2
-f $name and -r $name
-f $name -r $name
Truth table0%0/3
A and B
A= -f $nameB= -r $name
ABresult
0X0
100
111
70 0 ▶ if(!-e "README.md" || $self->{compile_force} || (stat $name)[9] > (stat "README.md")[9]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e "README.md" or $self->{"compile_force"} or (stat $name)[9] > (stat "README.md")[9]) 0 0 0
Condition0%0/6
not -e "README.md" or $self->{"compile_force"} or (stat $name)[9] > (stat "README.md")[9]
l!l&&r!l&&!r
000
not -e "README.md" or $self->{"compile_force"}
l!l&&r!l&&!r
000
MC/DC0%0/3
not -e "README.md" or $self->{"compile_force"} or (stat $name)[9] > (stat "README.md")[9]
not -e "README.md" $self->{"compile_force"} (stat $name)[9] > (stat "README.md")[9]
Truth table0%0/4
not(A) or B or C
A= not -e "README.md"B= $self->{"compile_force"}C= (stat $name)[9] > (stat "README.md")[9]
ABCresult
1XX1
01X1
0011
0000
71 0 my $readme = $self->{pod2markdown}->parse_from_file($name =~ s/\.\w+$/.pm/r)->as_markdown;
72 0 write_text "README.md", $readme;
73 0 $is_copy = 1;
74 }
75 }
76 };
77 0 ▶ if($@) {warn colored("minil.toml", 'red') . ": $@"}
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif ($is_copy) { } 0 0 0
78 elsif($is_copy) {
79 0 print colored(" ^‥^", "bright_black"), " $name ", colored("-->", "bright_black"), " README.md ", colored("...", "bright_white"), " ", colored("ok", "bright_green"), "\n";
80 }
81 }
82  
83 $self
84 2 }
85  
86 # Эскейпинг для qr!!
87 0 ▶sub _qr_esc($) {
Subroutine _qr_esc: not called CC=1 SCAR=6.9
88 0 $_[0] =~ s/!/\\!/gr
89 }
90  
91 # Эскейпинг для строки в двойных кавычках
92 0 ▶sub _qq_esc($) {
Subroutine _qq_esc: not called CC=1 SCAR=6.9
93 0 $_[0] =~ s!"!\\"!gr
94 }
95  
96 # Эскейпинг для строки в одинарных кавычках
97 6 ▶sub _q_esc($) {
Subroutine _q_esc: called CC=1 SCAR=0.0
98 6 $_[0] =~ s!'!\\'!gr
99 }
100  
101 # Строка кода для тестирования
102 3 ▶sub _to_testing {
Subroutine _to_testing: called CC=19 SCAR=55.1
103 3 my ($line, %x) = @_;
104  
105 3 ▶ return $x{code} if $x{code} =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $x{"code"} =~ /^\s*#/u 0 3 3
106  
107 3 my $expected = $x{expected};
108 3 my $q = _q_esc($line =~ s!\s*$!!r);
109 3 my $code = trim($x{code});
110 # clear делается, чтобы освободить ресурсы
111 3 my $clear = 'undef $::_g0; undef $::_e0;';
112
113 # local делается для того, чтобы в AnyEvent, Coro или Thread переменные не пересекались
114  
115 0 ▶ if(exists $x{is_deeply}) { "local (\$::_g0 = do {$code}, \$::_e0 = do {$expected}); ::is_deeply \$::_g0, \$::_e0, '$q' or ::diag ::_struct_diff(\$::_g0, \$::_e0); $clear\n" }
Branch: 17 branches
BranchTrueFalseTotal
if (exists $x{'is_deeply'}) { } 0 3 3
elsif (exists $x{'is'}) { } 3 0 3
elsif (exists $x{'qqis'}) { } 0 0 0
elsif (exists $x{'qis'}) { } 0 0 0
elsif (exists $x{'file'}) { } 0 0 0
elsif (exists $x{'like'}) { } 0 0 0
elsif (exists $x{'unlike'}) { } 0 0 0
elsif (exists $x{'qqbegins'}) { } 0 0 0
elsif (exists $x{'qqends'}) { } 0 0 0
elsif (exists $x{'qqinners'}) { } 0 0 0
elsif (exists $x{'begins'}) { } 0 0 0
elsif (exists $x{'ends'}) { } 0 0 0
elsif (exists $x{'inners'}) { } 0 0 0
elsif (exists $x{'error'}) { } 0 0 0
elsif (exists $x{'qqerror'}) { } 0 0 0
elsif (exists $x{'qrerror'}) { } 0 0 0
elsif (exists $x{'unqrerror'}) { } 0 0 0
116 3 elsif(exists $x{is}) { "local (\$::_g0 = do {$code}, \$::_e0 = do {$expected}); ::ok defined(\$::_g0) == defined(\$::_e0) && \$::_g0 eq \$::_e0, '$q' or ::diag ::_struct_diff(\$::_g0, \$::_e0); $clear\n" }
117 0 elsif(exists $x{qqis}) { "local (\$::_g0 = do {$code}, \$::_e0 = \"${\_qq_esc $expected}\"); ::ok \$::_g0 eq \$::_e0, '$q' or ::diag ::_string_diff(\$::_g0, \$::_e0); $clear\n" }
118 0 elsif(exists $x{qis}) { "local (\$::_g0 = do {$code}, \$::_e0 = '${\_q_esc $expected}'); ::ok \$::_g0 eq \$::_e0, '$q' or ::diag ::_string_diff(\$::_g0, \$::_e0); $clear\n" }
119 0 elsif(exists $x{file}) { "{ my \$s = '${\_q_esc $x{file}}'; open my \$__f__, '<:encoding(UTF-8)', \$s or die \"Read \$s: \$!\"; my \$got = join '', <\$__f__>; close \$__f__; my \$expected = '${\_q_esc $expected}'; ::ok \$got eq \$expected, '$q' or ::diag ::_string_diff(\$got, \$expected) }\n" }
120 0 elsif(exists $x{like}) { "::like scalar do {$code}, qr{${\_qr_esc $expected}}, '$q'; $clear\n" }
121 0 elsif(exists $x{unlike}) { "::unlike scalar do {$code}, qr{${\_qr_esc $expected}}, '$q'; $clear\n" }
122 0 elsif(exists $x{qqbegins}) { "local (\$::_g0 = do {$code}, \$::_e0 = \"${\_qq_esc $expected}\"); ::ok \$::_g0 =~ /^\${\\quotemeta \$::_e0}/, '$q' or ::diag ::_string_diff(\$::_g0, \$::_e0, 1); $clear\n" }
123 0 elsif(exists $x{qqends}) { "local (\$::_g0 = do {$code}, \$::_e0 = \"${\_qq_esc $expected}\"); ::ok \$::_g0 =~ /\${\\quotemeta \$::_e0}\$/, '$q' or ::diag ::_string_diff(\$::_g0, \$::_e0, 1); $clear\n" }
124 0 elsif(exists $x{qqinners}) { "local (\$::_g0 = do {$code}, \$::_e0 = \"${\_qq_esc $expected}\"); ::ok \$::_g0 =~ quotemeta \$::_e0, '$q' or ::diag ::_string_diff(\$::_g0, \$::_e0, 0); $clear\n" }
125 0 elsif(exists $x{begins}) { "local (\$::_g0 = do {$code}, \$::_e0 = '${\_q_esc $expected}'); ::ok \$::_g0 =~ /^\${\\quotemeta \$::_e0}/, '$q' or ::diag ::_string_diff(\$::_g0, \$::_e0, 1); $clear\n" }
126 0 elsif(exists $x{ends}) { "local (\$::_g0 = do {$code}, \$::_e0 = '${\_q_esc $expected}'); ::ok \$::_g0 =~ /\${\\quotemeta \$::_e0}\$/, '$q' or ::diag ::_string_diff(\$::_g0, \$::_e0, -1); $clear\n" }
127 0 elsif(exists $x{inners}) { "local (\$::_g0 = do {$code}, \$::_e0 = '${\_q_esc $expected}'); ::ok \$::_g0 =~ quotemeta \$::_e0, '$q' or ::diag ::_string_diff(\$::_g0, \$::_e0, 0); $clear\n" }
128 0 elsif(exists $x{error}) { "eval {$code}; local (\$::_g0 = \$\@, \$::_e0 = '${\_q_esc $expected}'); ok defined(\$::_g0) && \$::_g0 =~ /^\${\\quotemeta \$::_e0}/, '$q' or ::diag ::_string_diff(\$::_g0, \$::_e0, 1); $clear\n" }
129 0 elsif(exists $x{qqerror}) { "eval {$code}; local (\$::_g0 = \$\@, \$::_e0 = \"${\_qq_esc $expected}\"); ok defined(\$::_g0) && \$::_g0 =~ /^\${\\quotemeta \$::_e0}/, '$q' or ::diag ::_string_diff(\$::_g0, \$::_e0, 1); $clear\n" }
130 0 elsif(exists $x{qrerror}) { "eval {$code}; local (\$::_g0 = \$\@, \$::_e0 = qr{${\_qr_esc $expected}}); ok defined(\$::_g0) && \$::_g0 =~ \$::_e0, '$q' or ::diag defined(\$::_g0)? \"Got:\$::_g0\": 'Got is undef'; $clear\n" }
131 0 elsif(exists $x{unqrerror}) { "eval {$code}; local (\$::_g0 = \$\@, \$::_e0 = qr{${\_qr_esc $expected}}); ok defined(\$::_g0) && \$::_g0 !~ \$::_e0, '$q' or ::diag defined(\$::_g0)? \"Got:\$::_g0\": 'Got is undef'; $clear\n" }
132 else { # Что-то ужасное вырвалось на волю!
133 0 "???"
134 }
135 }
136  
137 # Обрезает строки вначале и все пробельные символы в конце
138 0 ▶sub _first_line_trim ($) {
Subroutine _first_line_trim: not called CC=1 SCAR=6.9
139 0 local ($_) = @_;
140 0 s/^([\t ]*\n)*//;
141 0 s/\s*$//;
142 0 $_
143 }
144  
145 # Преобразует из строчного формата
146 0 ▶sub _from_str ($) {
Subroutine _from_str: not called CC=3 SCAR=24.8
147 0 local ($_) = @_;
148 0 s/^"(.*)"$/$1/s;
149 0 ▶ s/\\(.)/ $1 eq "n"? "\n": $1 eq "t"? "\t": $1 /ge;
Branch: 2 branches
BranchTrueFalseTotal
$1 eq 'n' ? : 0 0 0
$1 eq 't' ? : 0 0 0
150 0 $_
151 }
152  
153 # Загрузка po
154 3 ▶sub load_po {
Subroutine load_po: called CC=4 SCAR=14.1
Pod documented
155 3 my ($self, $md, $from, $to) = @_;
156  
157 3 @$self{qw/from to/} = ($from, $to);
158  
159 3 ▶ return $self unless $md;
Branch: 1 branch
BranchTrueFalseTotal
unless $md 0 3 3
160  
161 3 my ($volume, $chains) = File::Spec->splitpath($md, 1);
162 3 my @dirs = File::Spec->splitdir($chains);
163 3 $dirs[0] = 'i18n'; # Удаляем lib
164 3 $dirs[$#dirs] =~ s!\.md$!\.$from-$to.po!;
165  
166 3 $self->{po_file} = File::Spec->catfile(@dirs);
167 3 my $i18n = File::Spec->catfile(@dirs[0..$#dirs-1]);
168 3 mkpath($i18n);
169  
170 3 my $manager = $self->{po_manager} = Locale::PO->new;
171 3 ▶ my $po = -e $self->{po_file}? $manager->load_file_ashash($self->{po_file}, "utf8"): {};
Branch: 1 branch
BranchTrueFalseTotal
-e $self->{'po_file'} ? : 0 3 3
172  
173 3 my %po;
174 3 my $lineno = 0;
175 3 for(keys %$po) {
176 0 my $val = $po->{$_};
177 0 $po{_first_line_trim(_from_str($_))} = $val;
178 }
179
180 3 $self->{po} = \%po;
181  
182 3 $self
183 }
184  
185 # Сохранение po
186 3 ▶sub save_po {
Subroutine save_po: called CC=2 SCAR=11.6
Pod documented
187 3 my ($self) = @_;
188
189 3 ▶ return $self unless $self->{from};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"from"} 3 0 3
190  
191 0 my @po = grep $_->{__used}, sort { $a->{loaded_line_number} <=> $b->{loaded_line_number} } values %{$self->{po}};
192  
193 0 $self->{po_manager}->save_file_fromarray($self->{po_file}, \@po, "utf8");
194  
195 0 $self
196 }
197  
198 # Функция переводит текст с одного языка на другой используя утилиту trans
199 0 ▶sub trans {
Subroutine trans: not called CC=9 SCAR=45.0
Pod documented
200 0 my ($self, $text, $lineno) = @_;
201  
202 0 $text = _first_line_trim($text);
203  
204 0 ▶ return $text if $text eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "" 0 0 0
205 0 ▶ return $text if $self->{from} eq "ru" && $text =~ /^[\x00-\x7F]*$/a;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"from"} eq "ru" and $text =~ /^[\x00-\x7F]*$/a 0 0 0
Condition0%0/3
$self->{"from"} eq "ru" and $text =~ /^[\x00-\x7F]*$/a
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"from"} eq "ru" and $text =~ /^[\x00-\x7F]*$/a
$self->{"from"} eq "ru" $text =~ /^[\x00-\x7F]*$/a
Truth table0%0/3
A and B
A= $self->{"from"} eq "ru"B= $text =~ /^[\x00-\x7F]*$/a
ABresult
0X0
100
111
206  
207 0 my $po = $self->{po}{$text};
208 0 ▶ $po->{__used} = 1, $po->loaded_line_number($lineno), return _from_str($po->msgstr) if defined $po;
Branch: 1 branch
BranchTrueFalseTotal
if defined $po 0 0 0
209  
210 0 my $dir = File::Spec->catfile(File::Spec->tmpdir, ".liveman");
211 0 mkpath($dir);
212 0 my $trans_from = File::Spec->catfile($dir, $self->{from});
213 0 my $trans_to = File::Spec->catfile($dir, $self->{to});
214  
215 0 write_text($trans_from, $text);
216  
217 0 my @progress = qw/\\ | \/ -/;
218 0 print $progress[$self->{trans_i}++ % @progress], "\033[D";
219  
220 0 my $cmd = "trans -no-auto -b $self->{from}:$self->{to} < $trans_from > $trans_to";
221 0 ▶ if(system $cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if (system $cmd) 0 0 0
222 0 ▶ die "$cmd: failed to execute: $!" if $? == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $? == -1 0 0 0
223 0 ▶ die printf "%s: child died with signal %d, %s coredump",
Branch: 2 branches
BranchTrueFalseTotal
if $? & 127 0 0 0
$? & 128 ? : 0 0 0
224 $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'
225 if $? & 127;
226 0 die printf "%s: child exited with value %d", $cmd, $? >> 8;
227 }
228  
229 0 my $trans = _first_line_trim(read_text($trans_to));
230  
231 0 $po = Locale::PO->new(
232 -msgid => $text,
233 -msgstr => $trans,
234 -loaded_line_number => $lineno,
235 );
236  
237 0 $po->{__used} = 1;
238 0 $self->{po}{$text} = $po;
239  
240 0 $trans
241 }
242  
243 # Заголовки не переводим
244 # Так же разбиваем по параграфам
245 0 ▶sub trans_paragraph {
Subroutine trans_paragraph: not called CC=2 SCAR=17.9
Pod documented
246 0 my ($self, $paragraph, $lineno) = @_;
247  
248 join "", map {
249 0 ▶ /^(#|\s*$)/n ? $_: join "", "\n", $self->trans(_first_line_trim($_), $lineno += 0.001), "\n\n"
Branch: 1 branch
BranchTrueFalseTotal
/^(#|\s*$)/nu ? : 0 0 0
250 } split /((?:[\t\ ]*\n){2,})/, $paragraph
251 }
252  
253 # Переводит markdown в pod
254 3 ▶sub markdown2pod {
Subroutine markdown2pod: called CC=1 SCAR=0.0
Pod undocumented
255 3 my ($self, $markdown) = @_;
256 3 local $_ = markdown_to_pod($markdown);
257 3 s/([\t ])(<[\w:]+>)/$1L$2/g;
258 3 s!L+<https://metacpan.org/pod/([\w:]+)>!L<$1>!ag;
259 3 $_
260 }
261  
262 3 our $TEST_HEAD = << 'END';
263 use common::sense;
264 use open qw/:std :utf8/;
265  
266 use Carp qw//;
267 use Cwd qw//;
268 use File::Basename qw//;
269 use File::Find qw//;
270 use File::Slurper qw//;
271 use File::Spec qw//;
272 use File::Path qw//;
273 use Scalar::Util qw//;
274  
275 use Test::More 0.98;
276  
277 use String::Diff qw//;
278 use Data::Dumper qw//;
279 use Term::ANSIColor qw//;
280  
281 BEGIN {
282 $SIG{__DIE__} = sub {
283 my ($msg) = @_;
284 if(ref $msg) {
285 $msg->{STACKTRACE} = Carp::longmess "?" if "HASH" eq Scalar::Util::reftype $msg;
286 die $msg;
287 } else {
288 die Carp::longmess defined($msg)? $msg: "undef"
289 }
290 };
291
292 my $t = File::Slurper::read_text(__FILE__);
293
294 my @dirs = File::Spec->splitdir(File::Basename::dirname(Cwd::abs_path(__FILE__)));
295 my $project_dir = File::Spec->catfile(@dirs[0..$#dirs-%(T_DIRS)]);
296 my $project_name = $dirs[$#dirs-%(T_DIRS)];
297 my @test_dirs = @dirs[$#dirs-%(T_DIRS)+2 .. $#dirs];
298  
299 $ENV{TMPDIR} = $ENV{LIVEMAN_TMPDIR} if exists $ENV{LIVEMAN_TMPDIR};
300  
301 my $dir_for_tests = File::Spec->catfile(File::Spec->tmpdir, ".liveman", $project_name, join("!", @test_dirs, File::Basename::basename(__FILE__)));
302
303 File::Find::find(sub { chmod 0700, $_ if !/^\.{1,2}\z/ }, $dir_for_tests), File::Path::rmtree($dir_for_tests) if -e $dir_for_tests;
304 File::Path::mkpath($dir_for_tests);
305
306 chdir $dir_for_tests or die "chdir $dir_for_tests: $!";
307
308 push @INC, "$project_dir/lib", "lib";
309
310 $ENV{PROJECT_DIR} = $project_dir;
311 $ENV{DIR_FOR_TESTS} = $dir_for_tests;
312
313 while($t =~ /^#\@> (.*)\n((#>> .*\n)*)#\@< EOF\n/gm) {
314 my ($file, $code) = ($1, $2);
315 $code =~ s/^#>> //mg;
316 File::Path::mkpath(File::Basename::dirname($file));
317 File::Slurper::write_text($file, $code);
318 }
319 }
320  
321 my $white = Term::ANSIColor::color('BRIGHT_WHITE');
322 my $red = Term::ANSIColor::color('BRIGHT_RED');
323 my $green = Term::ANSIColor::color('BRIGHT_GREEN');
324 my $reset = Term::ANSIColor::color('RESET');
325 my @diff = (
326 remove_open => "$white\[$red",
327 remove_close => "$white]$reset",
328 append_open => "$white\{$green",
329 append_close => "$white}$reset",
330 );
331  
332 sub _string_diff {
333 my ($got, $expected, $chunk) = @_;
334 $got = substr($got, 0, length $expected) if $chunk == 1;
335 $got = substr($got, -length $expected) if $chunk == -1;
336 String::Diff::diff_merge($got, $expected, @diff)
337 }
338  
339 sub _struct_diff {
340 my ($got, $expected) = @_;
341 String::Diff::diff_merge(
342 Data::Dumper->new([$got], ['diff'])->Indent(0)->Useqq(1)->Dump,
343 Data::Dumper->new([$expected], ['diff'])->Indent(0)->Useqq(1)->Dump,
344 @diff
345 )
346 }
347  
348 END
349  
350 # Трансформирует md-файл в тест и документацию
351 3 ▶sub transform {
Subroutine transform: called CC=18 SCAR=30.9
Pod documented
352 3 my ($self, $md, $test) = @_;
353 3 local $_;
354 3 ▶ $test //= $self->test_path($md);
Condition33%1/3
$test //= $self->test_path($md)
l!l&&r!l&&!r
030
MC/DC0%0/2
$test //= $self->test_path($md)
$test $self->test_path($md)
Truth table33%1/3
A //= B
A= $testB= $self->test_path($md)
ABresult
1X1
011
000
355  
356 3 print colored(" ^‥^", "bright_black"), " $md ", colored("-->", "bright_black"), " $test ", colored("...", "bright_white"), " ";
357  
358 3 my $markdown = read_text($md);
359  
360 3 my $options;
361 0 $markdown =~ s/^!(.*)\n/$options = $1; "\n"/e;
362 3 my ($from, $to) = $options =~ /(\w+):(\w+)/;
363 3 $self->load_po($md, $from, $to);
364  
365 3 my @pod; my @test; my $title = 'Start'; my $close_subtest; my $use_title = 1;
366  
367 3 my @text = split /^(```\w*[ \t]*(?:\n|\z))/mo, $markdown;
368  
369 3 ▶ for(my $i=0; $i<@text; $i+=4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @text) 3 3 6
370 3 $text[$i] =~ s!([ \t])<(\w+(?:::\w+)*)>!${1}[$2](https://metacpan.org/pod/$2)!g;
371  
372 # mark - текст, sec1 - ```perl, code - код, sec2 - ```
373 3 my ($mark, $sec1, $code, $sec2) = @text[$i..$i+4];
374  
375 3 ▶ push @pod, $self->markdown2pod($from? $self->trans_paragraph($mark, $i): $mark);
Branch: 1 branch
BranchTrueFalseTotal
$from ? : 0 3 3
376 3 push @test, $mark =~ s/^/# /rmg;
377  
378 3 ▶ last unless defined $sec1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sec1 0 3 3
379 3 ▶ $i--, $sec2 = $code, $code = "" if $code =~ /^```[ \t]*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /^```[ \t]*$/u 0 3 3
380  
381 3 ▶ die "=== mark ===\n$mark\n=== sec1 ===\n$sec1\n=== code ===\n$code\n=== sec2 ===\n$sec2\n\nsec2 ne ```" if $sec2 ne "```\n";
Branch: 1 branch
BranchTrueFalseTotal
if $sec2 ne "```\n" 0 3 3
382  
383 3 ▶ $title = trim($1) while $mark =~ /^#+[ \t]+(.*)/gm;
Branch: 1 branch
BranchTrueFalseTotal
$mark =~ /^#+[ \t]+(.*)/gmu and do { $title = trim($1) } 0 3 3
384  
385 3 push @pod, "\n", ($code =~ s/^/\t/gmr), "\n";
386  
387 3 my ($infile, $is) = $mark =~ /^(?:File|Файл)[ \t]+(.*?)([\t ]+(?:is|является))?:[\t ]*\n\z/m;
388 3 ▶ if($infile) {
Branch: 2 branches
BranchTrueFalseTotal
if ($infile) { } 0 3 3
elsif ($sec1 =~ /^```(?:perl)?[ \t]*$/u) { } 3 0 3
389 0 my $real_code = $code =~ s/^\\(```\w*[\t ]*$)/$1/mgro;
390 0 ▶ if($is) { # тестируем, что текст совпадает
Branch: 1 branch
BranchTrueFalseTotal
if ($is) { } 0 0 0
391 #push @test, "\n{ my \$s = '${\_q_esc($infile)}'; open my \$__f__, '<:encoding(UTF-8)', \$s or die \"Read \$s: \$!\"; my \$n = join '', <\$__f__>; close \$__f__; ::is \$n, '${\_q_esc($real_code)}', \"File \$s\"; }\n";
392 0 push @test, _to_testing("File $infile", file => $infile, expected => $real_code);
393 }
394 else { # записываем тект в файл
395 #push @test, "\n{ my \$s = main::_mkpath_('${\_q_esc($infile)}'); open my \$__f__, '>:encoding(UTF-8)', \$s or die \"Read \$s: \$!\"; print \$__f__ '${\_q_esc($real_code)}'; close \$__f__ }\n";
396 0 push @test, "#\@> $infile\n", $real_code =~ s/^/#>> /rgm, "#\@< EOF\n";
397 }
398 } elsif($sec1 =~ /^```(?:perl)?[ \t]*$/) {
399  
400 3 ▶ if($use_title ne $title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_title ne $title) 3 0 3
401 3 ▶ push @test, "::done_testing; }; " if $close_subtest;
Branch: 1 branch
BranchTrueFalseTotal
if $close_subtest 0 3 3
402 3 $close_subtest = 1;
403 3 push @test, "subtest '${\ _q_esc($title)}' => sub { ";
404 3 $use_title = $title;
405 }
406  
407 3 my $test = $code =~ s{^ (?<code> .* ) \# [\ \t]* (
408 (?<is_deeply> --> | ⟶ )
409 |(?<is> -> | → )
410 |(?<qqis> => | ⇒ )
411 |(?<qis> \\> | ↦ )
412 |(?<qqbegins> \^=> | ⤇ )
413 |(?<qqends> \$=> | ➾ )
414 |(?<qqinners> \*=> | ⥴ )
415 |(?<begins> \^-> | ↣ )
416 |(?<ends> \$-> | ⇥ )
417 |(?<inners> \*-> | ⥵ )
418 |(?<error> \@-> | ↯ )
419 |(?<qqerror> \@=> | ⤯ )
420 |(?<qrerror> \@~> | ⇝ )
421 |(?<unqrerror> <~\@ | ⇜ )
422 |(?<like> ~> | ↬ )
423 |(?<unlike> <~ | ↫ )
424 ) [\ \t]* (?<expected> .+? ) [\ \t]* \n
425 3 }{ _to_testing($&, %+) }xgrme;
426 3 push @test, "\n", $test, "\n";
427 }
428 else {
429 0 push @test, "\n", $code =~ s/^/# /rmg, "\n";
430 }
431 }
432  
433 3 ▶ push @test, "\n\t::done_testing;\n\};\n" if $close_subtest;
Branch: 1 branch
BranchTrueFalseTotal
if $close_subtest 3 0 3
434 3 push @test, "\n::done_testing;\n";
435  
436 3 my $test_head = $TEST_HEAD =~ y!\r\n! !r;
437
438 3 my $test_dir = File::Basename::dirname($test);
439 3 mkpath $test_dir;
440 3 my @test_dirs = File::Spec->splitdir($test_dir);
441 3 my %inter = (
442 T_DIRS => scalar @test_dirs,
443 );
444 3 $test_head =~ s/%\((\w+)\)/$inter{$1}/ge;
445  
446 3 write_text $test, join "", $test_head, @test;
447  
448 # Меняем версию:
449 3 my $v = uc "version";
450 3 my ($version) = $markdown =~ /^#[ \t]+$v\s+([\w\.-]{1,32})\s/m;
451
452 # Трансформируем модуль (pod и версия):
453 3 my $pod_doc = join "", @pod;
454 3 my $pm = $md =~ s/\.md$/.pm/r;
455 3 ▶ if (-e $pm) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $pm) { } 1 2 3
456 1 my $module = read_text $pm;
457 1 ▶ $module =~ s!^(our\s*\$$v\s*=\s*)["']?[\w.-]{1,32}["']?!$1"$version"!m if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 0 1 1
458 1 $module =~ s!(\s*\n__END__[\t ]*\n.*)?$!\n\n__END__\n\n=encoding utf-8\n\n$pod_doc!sn;
459 1 write_text $pm, $module;
460 } else {
461 2 my $pod = $md =~ s/\.md$/.pod/r;
462 2 write_text $pod, $pod_doc;
463 }
464  
465 3 $self->{count}++;
466  
467 3 $self->save_po;
468  
469 3 my $mark = join "", @text;
470 3 ▶ $mark =~ s/^/!$options/ if $options;
Branch: 1 branch
BranchTrueFalseTotal
if $options 0 3 3
471 3 ▶ write_text($md, $mark) if $mark ne $markdown;
Branch: 1 branch
BranchTrueFalseTotal
if $mark ne $markdown 0 3 3
472  
473 3 print colored("ok", "bright_green"), "\n";
474  
475 3 $self
476 }
477  
478 # Запустить тесты
479 1 ▶sub tests {
Subroutine tests: called CC=12 SCAR=29.5
Pod documented
480 1 my ($self) = @_;
481  
482 1 ▶ local $ENV{LIVEMAN_TMPDIR} = Cwd::abs_path(File::Spec->catfile($ENV{TMPDIR} // '/tmp'));
Condition50%1/2
$ENV{"TMPDIR"} // "/tmp"
l!l
01
MC/DC0%0/1
$ENV{"TMPDIR"} // "/tmp"
$ENV{"TMPDIR"}
Truth table50%1/2
A
A= $ENV{"TMPDIR"}
Aresult
11
00
483 1 mkpath($ENV{LIVEMAN_TMPDIR});
484  
485 1 my $cover = "/usr/bin/site_perl/cover";
486 1 ▶ $cover = 'cover' if !-e $cover;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $cover 1 0 1
487  
488 1 my $yath; my $prove;
489 1 my $use_prove = $self->{prove};
490 1 ▶ if($use_prove) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_prove) { } 1 0 1
491 1 $prove = "/usr/bin/site_perl/prove";
492 1 ▶ $prove = 'prove' if !-e $prove;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $prove 1 0 1
493 } else {
494 0 $yath = "/usr/bin/site_perl/yath";
495 0 ▶ $yath = 'yath' if !-e $yath;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $yath 0 0 0
496 }
497
498 1 my $options = $self->{options};
499  
500 1 ▶ if($self->{files}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"files"}) 0 1 1
501 0 my @tests = map $self->test_path($_), @{$self->{files}};
502 0 local $, = " ";
503 0 ▶ $self->{exit_code} = system $use_prove
Branch: 1 branch
BranchTrueFalseTotal
$use_prove ? : 0 0 0
504 ? "$prove -Ilib $options @tests"
505 : "$yath test -j4 $options @tests";
506 0 return $self;
507 }
508  
509 1 my $perl5opt = $ENV{PERL5OPT};
510 {
511 1 local $ENV{PERL5OPT};
512 1 system "$cover -delete";
513 1 ▶ if($use_prove) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_prove) { } 1 0 1
514 1 local $ENV{PERL5OPT} = "$perl5opt -MDevel::Cover";
515 1 $self->{exit_code} = system "$prove -Ilib -r t $options";
516 #$self->{exit_code} = system "prove --exec 'echo `pwd`/lib && perl -MDevel::Cover -I`pwd`/lib' -r t";
517 } else {
518 0 $self->{exit_code} = system "$yath test -j4 --cover $options";
519 }
520 1 ▶ return $self if $self->{exit_code};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"exit_code"} 0 1 1
521 1 system "$cover -report html_basic";
522 1 ▶ system "(opera cover_db/coverage.html || xdg-open cover_db/coverage.html) &> /dev/null" if $self->{open};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"open"} 0 1 1
523 }
524  
525 1 require Liveman::CoverBadge;
526 1 eval {
527 1 Liveman::CoverBadge->new->load->save;
528 };
529 1 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 0 1
530  
531 1 return $self;
532 }
533  
534 3 1;
535  
536 __END__
↑ Summary blib/lib/Liveman/Project.pm »