File Coverage

blib/lib/App/Test/DWG/LibreDWG/DwgRead.pm
Criterion Covered Total %
statement 53 108 49.0
branch 1 30 3.3
condition 2 12 16.6
subroutine 14 17 82.3
pod 2 2 100.0
total 72 169 42.6


line stmt bran cond sub pod time code
1             package App::Test::DWG::LibreDWG::DwgRead;
2              
3 4     4   208088 use strict;
  4         9  
  4         165  
4 4     4   21 use warnings;
  4         7  
  4         230  
5              
6 4     4   2659 use Capture::Tiny qw(capture);
  4         157246  
  4         326  
7 4     4   2286 use File::Copy;
  4         23470  
  4         342  
8 4     4   2495 use File::Find::Rule;
  4         48715  
  4         42  
9 4     4   2720 use File::Find::Rule::DWG;
  4         83624  
  4         60  
10 4     4   301 use File::Path qw(mkpath);
  4         10  
  4         265  
11 4     4   2533 use File::Spec::Functions qw(catfile);
  4         4024  
  4         368  
12 4     4   31 use File::Temp qw(tempdir);
  4         7  
  4         203  
13 4     4   2318 use Getopt::Std;
  4         11756  
  4         315  
14 4     4   2103 use IO::Barf qw(barf);
  4         3257  
  4         80  
15 4     4   268 use Readonly;
  4         8  
  4         5253  
16              
17             Readonly::Scalar our $DR => 'dwgread';
18              
19             our $VERSION = 0.06;
20              
21             # Constructor.
22             sub new {
23 2     2 1 657948 my ($class, @params) = @_;
24              
25             # Create object.
26 2         7 my $self = bless {}, $class;
27              
28             # Object.
29 2         15 return $self;
30             }
31              
32             # Run.
33             sub run {
34 1     1 1 3 my $self = shift;
35              
36             # Process arguments.
37 1         24 $self->{'_opts'} = {
38             'd' => undef,
39             'f' => 0,
40             'h' => 0,
41             'i' => 0,
42             'm' => undef,
43             's' => 0,
44             'v' => 1,
45             };
46 1 50 33     8 if (! getopts('d:fhim:sv:', $self->{'_opts'}) || @ARGV < 1
      33        
47             || $self->{'_opts'}->{'h'}) {
48              
49 1         162 print STDERR "Usage: $0 [-d test_dir] [-f] [-h] [-i] [-m match_string] [-s] [-v level] [--version] directory\n";
50 1         33 print STDERR "\t-d test_dir\tTest directory (default is directory in system tmp).\n";
51 1         15 print STDERR "\t-f\t\tPrint file.\n";
52 1         14 print STDERR "\t-h\t\tPrint help.\n";
53 1         13 print STDERR "\t-i\t\tIgnore errors.\n";
54 1         57 print STDERR "\t-m match_string\tMatch string (default is not defined).\n";
55 1         15 print STDERR "\t-s\t\tSelect files which are symlinks too.\n";
56 1         12 print STDERR "\t-v level\tVerbosity level (default 1, min 0, max 9).\n";
57 1         13 print STDERR "\t--version\tPrint version.\n";
58 1         13 print STDERR "\tdirectory\tDirectory with DWG files to test.\n";
59 1         5 return 1;
60             }
61 0           $self->{'_directory'} = shift @ARGV;
62              
63 0 0         if ($self->{'_opts'}->{'v'} == 0) {
64 0           warn "Verbosity level 0 hasn't detection of ERRORs.\n";
65             }
66              
67 0           my $tmp_dir = $self->{'_opts'}->{'d'};
68 0 0 0       if (defined $tmp_dir && ! -d $tmp_dir) {
69 0           mkpath($tmp_dir);
70             }
71 0 0 0       if (! defined $tmp_dir || ! -d $tmp_dir) {
72 0           $tmp_dir = tempdir(CLEANUP => 1);
73             }
74 0           $self->{'_tmp_dir'} = $tmp_dir;
75              
76             # Verbose level.
77 0           my $v = '-v'.$self->{'_opts'}->{'v'};
78              
79 0           my @selected_files;
80 0 0         if ($self->{'_opts'}->{'s'}) {
81 0           @selected_files = File::Find::Rule->in($self->{'_directory'});
82             } else {
83 0           @selected_files = File::Find::Rule->dwg->not_symlink->in($self->{'_directory'});
84             }
85 0           my $file_num = 1;
86 0           foreach my $dwg_file_in (@selected_files) {
87              
88             # Copy DWG file to dir.
89 0           my $dwg_file_out = catfile($tmp_dir, $file_num.'.dwg');
90 0           copy($dwg_file_in, $dwg_file_out);
91              
92             # dwgread.
93 0           my $dwgread = "$DR $v $dwg_file_out";
94 0           $self->_exec($dwgread, $file_num.'-dwgread', $dwg_file_in);
95              
96             # tmp directory cleanup immediately.
97 0 0         if (! defined $self->{'_opts'}->{'d'}) {
98 0           my $tmp_glob_file = catfile($tmp_dir, $file_num);
99 0           my @glob_files = glob $tmp_glob_file.'*';
100 0           unlink @glob_files;
101             }
102              
103 0           $file_num++;
104             }
105              
106 0           return 0;
107             }
108              
109             sub _exec {
110 0     0     my ($self, $command, $log_prefix, $dwg_file) = @_;
111              
112             my ($stdout, $stderr, $exit_code) = capture {
113 0     0     system($command);
114 0           };
115              
116 0 0         if ($exit_code) {
117 0 0         if (! $self->{'_opts'}->{'i'}) {
118 0           print STDERR "Cannot dwgread '$dwg_file'.\n";
119 0           print STDERR "\tCommand '$command' exit with $exit_code.\n";
120             }
121 0           return;
122             }
123              
124 0 0         if ($stdout) {
125 0           my $stdout_file = catfile($self->{'_tmp_dir'},
126             $log_prefix.'-stdout.log');
127 0           barf($stdout_file, $stdout);
128             }
129 0 0         if ($stderr) {
130 0           my $stderr_file = catfile($self->{'_tmp_dir'},
131             $log_prefix.'-stderr.log');
132 0           barf($stderr_file, $stderr);
133              
134             # Report errors.
135 0 0         if (! $self->{'_opts'}->{'i'}) {
136 0 0         if (my @num = ($stderr =~ m/ERROR/gms)) {
137 0           print STDERR "dwgread '$dwg_file' has ".scalar @num." ERRORs\n";
138             }
139             }
140              
141 0 0         if (defined $self->{'_opts'}->{'m'}) {
142 0           foreach my $match_line ($self->_match_lines($stderr)) {
143 0 0         if ($self->{'_opts'}->{'f'}) {
144 0           print $dwg_file.': ';
145             }
146 0           print $match_line."\n";
147             }
148             }
149             }
150              
151 0           return;
152             }
153              
154             sub _match_lines {
155 0     0     my ($self, $string) = @_;
156              
157 0           my @ret;
158 0           foreach my $line (split m/\n/ms, $string) {
159 0 0         if ($line =~ /$self->{'_opts'}->{'m'}/) {
160 0           push @ret, $line;
161             }
162             }
163              
164 0           return @ret;
165             }
166              
167             1;
168              
169              
170             __END__