File Coverage

blib/lib/TBX/Checker.pm
Criterion Covered Total %
statement 65 71 91.5
branch 10 18 55.5
condition 2 5 40.0
subroutine 14 15 93.3
pod 1 1 100.0
total 92 110 83.6


line stmt bran cond sub pod time code
1             #
2             # This file is part of TBX-Checker
3             #
4             # This software is copyright (c) 2013 by Alan K. Melby.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package TBX::Checker;
10 2     2   38739 use strict;
  2         5  
  2         87  
11 2     2   10 use warnings;
  2         4  
  2         149  
12 2     2   2426 use autodie;
  2         217887  
  2         12  
13 2     2   49504 use File::ShareDir 'dist_dir';
  2         52424  
  2         224  
14             use Exporter::Easy (
15 2         31 OK => [ qw(check) ],
16 2     2   8424 );
  2         8800  
17 2     2   17433 use Path::Tiny;
  2         45338  
  2         156  
18 2     2   26 use Carp;
  2         3  
  2         163  
19 2     2   14 use feature 'state';
  2         4  
  2         258  
20 2     2   8816 use Capture::Tiny 'capture_merged';
  2         101400  
  2         3327  
21             our $VERSION = '0.03'; # VERSION
22              
23             my $TBXCHECKER = path( dist_dir('TBX-Checker'),'tbxcheck-1_2_9.jar' );
24              
25             # ABSTRACT: Check TBX validity using TBXChecker
26              
27             #When run as a script instead of used as a module: check the input file and print the results
28             _run(@ARGV) unless caller;
29             sub _run {
30 0     0   0 my ($tbx) = @_;
31 0         0 my ($passed, $messages) = check($tbx);
32 0 0 0     0 ($passed && print 'ok!')
33             or print join (qq{\n}, @$messages);
34 0         0 return;
35             }
36              
37              
38             sub check {
39 6     6 1 23998 my ($data, @args) = @_;
40              
41 6 50       43 croak 'missing data argument. Usage: TBX::Checker::check($data, %args)'
42             unless $data;
43              
44 6         35 my $file = _get_file($data);
45             #due to TBXChecker bug, file must be relative to cwd
46 6         76 my $rel_file = $file->relative;
47 6         1789 my $arg_string = _get_arg_string(@args);
48              
49 6         57 my $command = qq{java -cp ".;$TBXCHECKER" org.ttt.salt.Main } .
50             qq{$arg_string "$rel_file"};
51              
52             # capture STDOUT and STDERR from jar call into $output
53 6     6   536 my $output = capture_merged {system($command)};
  6         52050  
54 6         11590 my @messages = split /\v+/, $output;
55 6         79 my $valid = _is_valid(\@messages);
56 6         185 return ($valid, \@messages);
57             }
58              
59             # get a Path::Tiny object for the file to give to the TBXChecker
60             sub _get_file {
61 6     6   13 my ($data) = @_;
62 6         14 my $file;
63             #pointers are string data
64 6 100       53 if(ref $data eq 'SCALAR'){
65 3         54 $file = Path::Tiny->tempfile;
66             #TODO: will this get encodings right?
67 3         5841 $file->append_raw($$data);
68             #everything else should be string paths
69             }else{
70 3         276 $file = path($data);
71 3 50       90 croak "$file doesn't exist!"
72             unless $file->exists;
73             }
74 6         1050 return $file;
75             }
76              
77             # process arguments and return the command to be run and the file
78             # being processed (so temp files aren't destroyed by leaving scope)
79             sub _get_arg_string {
80 6     6   48 my (%args) = @_;
81              
82             # check the parameters.
83             # TODO: use a module or something for param checking
84 6         17 state $allowed_params = [ qw(
85             loglevel lang country variant system version environment) ];
86 6         17 state $allowed_levels = [ qw(
87             OFF SEVERE WARNING INFO CONFIG FINE FINER FINEST ALL) ];
88 6         40 foreach my $param (keys %args){
89 14         49 croak "unknown paramter: $param"
90 2 50       10 unless grep { $_ eq $param } @$allowed_params;
91             }
92 6 100       37 if(exists $args{loglevel}){
93 2 50       10 grep { $_ eq $args{loglevel} } @$allowed_levels
  18         55  
94             or croak "Loglevel doesn't exist: $args{loglevel}";
95             }
96 6   100     61 $args{loglevel} ||= q{OFF};
97              
98             #combine the options into a string that TBXChecker will understand
99 6         19 return join q{ }, map {"--$_=$args{$_}"} keys %args;
  6         48  
100             }
101              
102             #return a boolean indicating the validity of the file, given the messages
103             #remove the message indicating that the file is valid (if it exists)
104             sub _is_valid {
105 6     6   40 my ($messages) = @_;
106             #locate index of "Valid file:" message
107 6         34 my $index = 0;
108 6         71 while($index < @$messages){
109 6 50       132 last if $$messages[$index] =~ /^Valid file: /;
110 6         21 $index++;
111             }
112             #if message not found, file was invalid
113 6 50       36 if($index > $#$messages){
114 6         30 return 0;
115             }
116             #remove message and return true
117 0           splice(@$messages, $index, 1);
118 0           return 1;
119             }
120              
121             1;
122              
123             __END__