File Coverage

blib/lib/App/Test/DWG/LibreDWG/DwgRead.pm
Criterion Covered Total %
statement 49 83 59.0
branch 1 16 6.2
condition 2 12 16.6
subroutine 14 16 87.5
pod 2 2 100.0
total 68 129 52.7


line stmt bran cond sub pod time code
1             package App::Test::DWG::LibreDWG::DwgRead;
2              
3 4     4   72853 use strict;
  4         26  
  4         111  
4 4     4   21 use warnings;
  4         10  
  4         112  
5              
6 4     4   2163 use Capture::Tiny qw(capture);
  4         124357  
  4         265  
7 4     4   2030 use File::Copy;
  4         9655  
  4         217  
8 4     4   2127 use File::Find::Rule;
  4         33198  
  4         30  
9 4     4   2018 use File::Find::Rule::DWG;
  4         116342  
  4         46  
10 4     4   222 use File::Path qw(mkpath);
  4         9  
  4         221  
11 4     4   1852 use File::Spec::Functions qw(catfile);
  4         3687  
  4         300  
12 4     4   38 use File::Temp qw(tempdir);
  4         12  
  4         151  
13 4     4   8246 use Getopt::Std;
  4         196  
  4         232  
14 4     4   1893 use IO::Barf qw(barf);
  4         2260  
  4         58  
15 4     4   203 use Readonly;
  4         10  
  4         2874  
16              
17             Readonly::Scalar our $DR => 'dwgread';
18              
19             our $VERSION = 0.01;
20              
21             # Constructor.
22             sub new {
23 2     2 1 1638 my ($class, @params) = @_;
24              
25             # Create object.
26 2         9 my $self = bless {}, $class;
27              
28             # Object.
29 2         9 return $self;
30             }
31              
32             # Run.
33             sub run {
34 1     1 1 3 my $self = shift;
35              
36             # Process arguments.
37 1         9 $self->{'_opts'} = {
38             'd' => undef,
39             'h' => 0,
40             'v' => 1,
41             };
42 1 50 33     5 if (! getopts('d:hv:', $self->{'_opts'}) || @ARGV < 1
      33        
43             || $self->{'_opts'}->{'h'}) {
44              
45 1         136 print STDERR "Usage: $0 [-d test_dir] [-h] [-v level] [--version] directory\n";
46 1         16 print STDERR "\t-d test_dir\tTest directory (default is directory in system tmp).\n";
47 1         12 print STDERR "\t-h\t\tPrint help.\n";
48 1         13 print STDERR "\t-v level\tVerbosity level (default 1, min 0, max 9).\n";
49 1         11 print STDERR "\t--version\tPrint version.\n";
50 1         11 print STDERR "\tdirectory\tDirectory with DWG files to test.\n";
51 1         6 return 1;
52             }
53 0           $self->{'_directory'} = shift @ARGV;
54              
55 0 0         if ($self->{'_opts'}->{'v'} == 0) {
56 0           warn "Verbosity level 0 hasn't detection of ERRORs.\n";
57             }
58              
59 0           my $tmp_dir = $self->{'_opts'}->{'d'};
60 0 0 0       if (defined $tmp_dir && ! -d $tmp_dir) {
61 0           mkpath($tmp_dir);
62             }
63 0 0 0       if (! defined $tmp_dir || ! -d $tmp_dir) {
64 0           $tmp_dir = tempdir(CLEANUP => 1);
65             }
66 0           $self->{'_tmp_dir'} = $tmp_dir;
67              
68             # Verbose level.
69 0           my $v = '-v'.$self->{'_opts'}->{'v'};
70              
71 0           my $file_num = 1;
72 0           foreach my $dwg_file_in (File::Find::Rule->dwg->in($self->{'_directory'})) {
73              
74             # Copy DWG file to dir.
75 0           my $dwg_file_out = catfile($tmp_dir, $file_num.'.dwg');
76 0           copy($dwg_file_in, $dwg_file_out);
77              
78             # dwgread.
79 0           my $dwgread = "$DR $v $dwg_file_out";
80 0           $self->_exec($dwgread, $file_num.'-dwgread', $dwg_file_in);
81              
82 0           $file_num++;
83             }
84              
85 0           return 0;
86             }
87              
88             sub _exec {
89 0     0     my ($self, $command, $log_prefix, $dwg_file) = @_;
90              
91             my ($stdout, $stderr, $exit_code) = capture {
92 0     0     system($command);
93 0           };
94              
95 0 0         if ($exit_code) {
96 0           print STDERR "Cannot dwgread '$dwg_file'.\n";
97 0           print STDERR "\tCommand '$command' exit with $exit_code.\n";
98 0           return;
99             }
100              
101 0 0         if ($stdout) {
102 0           my $stdout_file = catfile($self->{'_tmp_dir'},
103             $log_prefix.'-stdout.log');
104 0           barf($stdout_file, $stdout);
105             }
106 0 0         if ($stderr) {
107 0           my $stderr_file = catfile($self->{'_tmp_dir'},
108             $log_prefix.'-stderr.log');
109 0           barf($stderr_file, $stderr);
110              
111 0 0         if (my @num = ($stderr =~ m/ERROR/gms)) {
112 0           print STDERR "dwgread '$dwg_file' has ".scalar @num." ERRORs\n";
113             }
114             }
115              
116 0           return;
117             }
118              
119             1;
120              
121              
122             __END__