File Coverage

blib/lib/App/Test/DWG/LibreDWG/DwgRead.pm
Criterion Covered Total %
statement 50 93 53.7
branch 1 20 5.0
condition 2 12 16.6
subroutine 14 17 82.3
pod 2 2 100.0
total 69 144 47.9


line stmt bran cond sub pod time code
1             package App::Test::DWG::LibreDWG::DwgRead;
2              
3 4     4   73700 use strict;
  4         24  
  4         113  
4 4     4   20 use warnings;
  4         6  
  4         108  
5              
6 4     4   2128 use Capture::Tiny qw(capture);
  4         120015  
  4         300  
7 4     4   2004 use File::Copy;
  4         9326  
  4         224  
8 4     4   2113 use File::Find::Rule;
  4         32496  
  4         40  
9 4     4   2136 use File::Find::Rule::DWG;
  4         113964  
  4         46  
10 4     4   233 use File::Path qw(mkpath);
  4         12  
  4         241  
11 4     4   1942 use File::Spec::Functions qw(catfile);
  4         3346  
  4         308  
12 4     4   30 use File::Temp qw(tempdir);
  4         14  
  4         177  
13 4     4   8021 use Getopt::Std;
  4         193  
  4         233  
14 4     4   1821 use IO::Barf qw(barf);
  4         2248  
  4         58  
15 4     4   210 use Readonly;
  4         8  
  4         3327  
16              
17             Readonly::Scalar our $DR => 'dwgread';
18              
19             our $VERSION = 0.02;
20              
21             # Constructor.
22             sub new {
23 2     2 1 1853 my ($class, @params) = @_;
24              
25             # Create object.
26 2         16 my $self = bless {}, $class;
27              
28             # Object.
29 2         8 return $self;
30             }
31              
32             # Run.
33             sub run {
34 1     1 1 3 my $self = shift;
35              
36             # Process arguments.
37 1         10 $self->{'_opts'} = {
38             'd' => undef,
39             'h' => 0,
40             'm' => undef,
41             'v' => 1,
42             };
43 1 50 33     6 if (! getopts('d:hm:v:', $self->{'_opts'}) || @ARGV < 1
      33        
44             || $self->{'_opts'}->{'h'}) {
45              
46 1         137 print STDERR "Usage: $0 [-d test_dir] [-h] [-m match_string] [-v level] [--version] directory\n";
47 1         16 print STDERR "\t-d test_dir\tTest directory (default is directory in system tmp).\n";
48 1         12 print STDERR "\t-h\t\tPrint help.\n";
49 1         12 print STDERR "\t-m match_string\tMatch string (default is not defined).\n";
50 1         12 print STDERR "\t-v level\tVerbosity level (default 1, min 0, max 9).\n";
51 1         11 print STDERR "\t--version\tPrint version.\n";
52 1         12 print STDERR "\tdirectory\tDirectory with DWG files to test.\n";
53 1         4 return 1;
54             }
55 0           $self->{'_directory'} = shift @ARGV;
56              
57 0 0         if ($self->{'_opts'}->{'v'} == 0) {
58 0           warn "Verbosity level 0 hasn't detection of ERRORs.\n";
59             }
60              
61 0           my $tmp_dir = $self->{'_opts'}->{'d'};
62 0 0 0       if (defined $tmp_dir && ! -d $tmp_dir) {
63 0           mkpath($tmp_dir);
64             }
65 0 0 0       if (! defined $tmp_dir || ! -d $tmp_dir) {
66 0           $tmp_dir = tempdir(CLEANUP => 1);
67             }
68 0           $self->{'_tmp_dir'} = $tmp_dir;
69              
70             # Verbose level.
71 0           my $v = '-v'.$self->{'_opts'}->{'v'};
72              
73 0           my $file_num = 1;
74 0           foreach my $dwg_file_in (File::Find::Rule->dwg->in($self->{'_directory'})) {
75              
76             # Copy DWG file to dir.
77 0           my $dwg_file_out = catfile($tmp_dir, $file_num.'.dwg');
78 0           copy($dwg_file_in, $dwg_file_out);
79              
80             # dwgread.
81 0           my $dwgread = "$DR $v $dwg_file_out";
82 0           $self->_exec($dwgread, $file_num.'-dwgread', $dwg_file_in);
83              
84 0           $file_num++;
85             }
86              
87 0           return 0;
88             }
89              
90             sub _exec {
91 0     0     my ($self, $command, $log_prefix, $dwg_file) = @_;
92              
93             my ($stdout, $stderr, $exit_code) = capture {
94 0     0     system($command);
95 0           };
96              
97 0 0         if ($exit_code) {
98 0           print STDERR "Cannot dwgread '$dwg_file'.\n";
99 0           print STDERR "\tCommand '$command' exit with $exit_code.\n";
100 0           return;
101             }
102              
103 0 0         if ($stdout) {
104 0           my $stdout_file = catfile($self->{'_tmp_dir'},
105             $log_prefix.'-stdout.log');
106 0           barf($stdout_file, $stdout);
107             }
108 0 0         if ($stderr) {
109 0           my $stderr_file = catfile($self->{'_tmp_dir'},
110             $log_prefix.'-stderr.log');
111 0           barf($stderr_file, $stderr);
112              
113 0 0         if (my @num = ($stderr =~ m/ERROR/gms)) {
114 0           print STDERR "dwgread '$dwg_file' has ".scalar @num." ERRORs\n";
115             }
116              
117 0 0         if (defined $self->{'_opts'}->{'m'}) {
118 0           foreach my $match_line ($self->_match_lines($stderr)) {
119 0           print $match_line."\n";
120             }
121             }
122             }
123              
124 0           return;
125             }
126              
127             sub _match_lines {
128 0     0     my ($self, $string) = @_;
129              
130 0           my @ret;
131 0           foreach my $line (split m/\n/ms, $string) {
132 0 0         if ($line =~ /$self->{'_opts'}->{'m'}/) {
133 0           push @ret, $line;
134             }
135             }
136              
137 0           return @ret;
138             }
139              
140             1;
141              
142              
143             __END__