annotate Roary/t/lib/TestHelper.pm @ 0:c47a5f61bc9f draft

Uploaded
author dereeper
date Fri, 14 May 2021 20:27:06 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
1 package TestHelper;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
2 use Moose::Role;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
3 use Test::Most;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
4 use Data::Dumper;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
5 use File::Slurper qw(read_lines read_text);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
6 use Test::Files;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
7 use Test::Output;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
8
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
9 $ENV{PATH} .= ":./bin";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
10
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
11 sub compare_files {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
12 my ( $actual_file, $expected_file, $comment ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
13 my @actual_lines = sort( read_lines($actual_file) );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
14 my @expected_lines = sort( read_lines($expected_file) );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
15 is_deeply( \@actual_lines, \@expected_lines, $comment );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
16 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
17
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
18 sub compare_groups_files {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
19 my ( $actual_file, $expected_file, $comment ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
20 my @actual_lines = sort( read_lines($actual_file) );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
21 my @expected_lines = sort( read_lines($expected_file) );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
22
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
23 my @actual_sorted_lines;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
24 for my $line (@actual_lines) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
25 my @line_details = split( / /, $line );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
26 shift @line_details;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
27 my @sorted = sort(@line_details);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
28 push( @actual_sorted_lines, \@sorted );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
29 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
30
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
31 my @expected_sorted_lines;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
32 for my $line (@expected_lines) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
33 my @line_details = split( / /, $line );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
34 shift @line_details;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
35 my @sorted = sort(@line_details);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
36 push( @expected_sorted_lines, \@sorted );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
37 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
38 is_deeply( \@actual_sorted_lines, \@expected_sorted_lines, $comment );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
39 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
40
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
41
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
42 sub stdout_should_have
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
43 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
44 my ( $script_name, $parameters, $expected ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
45 my @input_args = split( " ", $parameters );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
46 open OLDERR, '>&STDERR';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
47 eval("use $script_name ;");
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
48 my $returned_values = 0;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
49 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
50 local *STDERR;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
51 open STDERR, '>/dev/null' or warn "Can't open /dev/null: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
52 stdout_like { eval("$script_name->new(args => \\\@input_args, script_name => '$script_name')->run;"); } qr/$expected/, "got expected text $expected for $parameters";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
53 close STDERR;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
54 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
55 open STDERR, '>&OLDERR' or die "Can't restore stderr: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
56 close OLDERR or die "Can't close OLDERR: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
57 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
58
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
59
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
60 sub stdout_should_not_have
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
61 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
62 my ( $script_name, $parameters, $expected ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
63 my @input_args = split( " ", $parameters );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
64 open OLDERR, '>&STDERR';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
65 eval("use $script_name ;");
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
66 my $returned_values = 0;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
67 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
68 local *STDERR;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
69 open STDERR, '>/dev/null' or warn "Can't open /dev/null: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
70 stdout_unlike { eval("$script_name->new(args => \\\@input_args, script_name => '$script_name')->run;"); } qr/$expected/, "got expected text $expected for $parameters";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
71 close STDERR;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
72 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
73 open STDERR, '>&OLDERR' or die "Can't restore stderr: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
74 close OLDERR or die "Can't close OLDERR: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
75 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
76
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
77
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
78
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
79 sub stderr_should_not_have
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
80 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
81 my ( $script_name, $parameters, $expected ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
82 my @input_args = split( " ", $parameters );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
83 open OLDOUT, '>&STDOUT';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
84 eval("use $script_name ;");
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
85 my $returned_values = 0;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
86 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
87 local *STDOUT;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
88 open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
89 stderr_unlike { eval("$script_name->new(args => \\\@input_args, script_name => '$script_name')->run;"); } qr/$expected/, "got expected text $expected for $parameters";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
90 close STDOUT;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
91 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
92 open STDOUT, '>&OLDOUT' or die "Can't restore stdout: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
93 close OLDOUT or die "Can't close OLDOUT: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
94 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
95
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
96 sub stderr_should_have
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
97 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
98 my ( $script_name, $parameters, $expected ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
99 my @input_args = split( " ", $parameters );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
100 open OLDOUT, '>&STDOUT';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
101 eval("use $script_name ;");
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
102 my $returned_values = 0;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
103 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
104 local *STDOUT;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
105 open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
106 stderr_like { eval("$script_name->new(args => \\\@input_args, script_name => '$script_name')->run;"); } qr/$expected/, "got expected text $expected for $parameters";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
107 close STDOUT;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
108 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
109 open STDOUT, '>&OLDOUT' or die "Can't restore stdout: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
110 close OLDOUT or die "Can't close OLDOUT: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
111 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
112
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
113
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
114 sub mock_execute_script_and_check_output {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
115 my ( $script_name, $scripts_and_expected_files, $columns_to_exclude ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
116
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
117 system('touch empty_file');
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
118
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
119 open OLDOUT, '>&STDOUT';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
120 open OLDERR, '>&STDERR';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
121 eval("use $script_name ;");
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
122 my $returned_values = 0;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
123 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
124 local *STDOUT;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
125 open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
126 local *STDERR;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
127 open STDERR, '>/dev/null' or warn "Can't open /dev/null: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
128
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
129 for my $script_parameters ( sort keys %$scripts_and_expected_files ) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
130 my $full_script = $script_parameters;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
131 my @input_args = split( " ", $full_script );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
132
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
133 my $cmd = "$script_name->new(args => \\\@input_args, script_name => '$script_name')->run;";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
134 eval($cmd);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
135 warn $@ if $@;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
136
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
137 my $actual_output_file_name = $scripts_and_expected_files->{$script_parameters}->[0];
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
138 my $expected_output_file_name = $scripts_and_expected_files->{$script_parameters}->[1];
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
139 ok( -e $actual_output_file_name, "Actual output file exists $actual_output_file_name $script_parameters" );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
140 if ( defined($columns_to_exclude) ) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
141 is(
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
142 _exclude_variable_columns_from_spreadsheet( $actual_output_file_name, $columns_to_exclude ),
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
143 _exclude_variable_columns_from_spreadsheet( $expected_output_file_name, $columns_to_exclude ),
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
144 'Actual and expected match output excluding variable columns'
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
145 );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
146 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
147 else {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
148 compare_ok( $actual_output_file_name, $expected_output_file_name,
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
149 "Actual and expected output match for '$script_parameters'" );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
150
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
151 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
152 unlink($actual_output_file_name);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
153 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
154 close STDOUT;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
155 close STDERR;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
156 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
157
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
158 # Restore stdout.
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
159 open STDOUT, '>&OLDOUT' or die "Can't restore stdout: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
160 open STDERR, '>&OLDERR' or die "Can't restore stderr: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
161
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
162 # Avoid leaks by closing the independent copies.
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
163 close OLDOUT or die "Can't close OLDOUT: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
164 close OLDERR or die "Can't close OLDERR: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
165 unlink('empty_file');
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
166 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
167
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
168 sub mock_execute_script_and_check_output_sorted_groups {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
169 my ( $script_name, $scripts_and_expected_files, $columns_to_exclude ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
170
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
171 system('touch empty_file');
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
172
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
173 open OLDOUT, '>&STDOUT';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
174 open OLDERR, '>&STDERR';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
175 eval("use $script_name ;");
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
176 my $returned_values = 0;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
177 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
178 local *STDOUT;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
179 open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
180 local *STDERR;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
181 open STDERR, '>/dev/null' or warn "Can't open /dev/null: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
182
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
183 for my $script_parameters ( sort keys %$scripts_and_expected_files ) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
184 my $full_script = $script_parameters;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
185 my @input_args = split( " ", $full_script );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
186
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
187 my $cmd = "$script_name->new(args => \\\@input_args, script_name => '$script_name')->run;";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
188 eval($cmd);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
189 warn $@ if $@;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
190
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
191 my $actual_output_file_name = $scripts_and_expected_files->{$script_parameters}->[0];
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
192
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
193 my $expected_output_file_name = $scripts_and_expected_files->{$script_parameters}->[1];
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
194 ok( -e $actual_output_file_name, "Actual output file exists $actual_output_file_name $script_parameters" );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
195 if ( defined($columns_to_exclude) ) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
196 my @actual_content_sorted =
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
197 sort( split( /\n/, _exclude_variable_columns_from_spreadsheet( $actual_output_file_name, $columns_to_exclude ) ) );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
198 my @expected_content_sorted =
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
199 sort( split( /\n/, _exclude_variable_columns_from_spreadsheet( $expected_output_file_name, $columns_to_exclude ) ) );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
200 is_deeply( \@actual_content_sorted, \@expected_content_sorted,
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
201 'Actual and expected match output excluding variable columns' );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
202 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
203 else {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
204 compare_groups_files( $actual_output_file_name, $expected_output_file_name,
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
205 "Actual and expected sorted output match for '$script_parameters'" );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
206 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
207 unlink($actual_output_file_name);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
208 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
209 close STDOUT;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
210 close STDERR;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
211 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
212
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
213 # Restore stdout.
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
214 open STDOUT, '>&OLDOUT' or die "Can't restore stdout: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
215 open STDERR, '>&OLDERR' or die "Can't restore stderr: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
216
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
217 # Avoid leaks by closing the independent copies.
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
218 close OLDOUT or die "Can't close OLDOUT: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
219 close OLDERR or die "Can't close OLDERR: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
220 unlink('empty_file');
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
221 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
222
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
223 sub mock_execute_script_and_check_output_sorted {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
224 my ( $script_name, $scripts_and_expected_files, $columns_to_exclude ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
225
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
226 system('touch empty_file');
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
227
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
228 open OLDOUT, '>&STDOUT';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
229 open OLDERR, '>&STDERR';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
230 eval("use $script_name ;");
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
231 my $returned_values = 0;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
232 {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
233 local *STDOUT;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
234 open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
235 local *STDERR;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
236 open STDERR, '>/dev/null' or warn "Can't open /dev/null: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
237
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
238 for my $script_parameters ( sort keys %$scripts_and_expected_files ) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
239 my $full_script = $script_parameters;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
240 my @input_args = split( " ", $full_script );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
241
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
242 my $cmd = "$script_name->new(args => \\\@input_args, script_name => '$script_name')->run;";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
243 eval($cmd);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
244 warn $@ if $@;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
245
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
246 my $actual_output_file_name = $scripts_and_expected_files->{$script_parameters}->[0];
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
247
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
248 my $expected_output_file_name = $scripts_and_expected_files->{$script_parameters}->[1];
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
249 ok( -e $actual_output_file_name, "Actual output file exists $actual_output_file_name $script_parameters" );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
250 if ( defined($columns_to_exclude) ) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
251 my @actual_content_sorted =
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
252 sort( split( /\n/, _exclude_variable_columns_from_spreadsheet( $actual_output_file_name, $columns_to_exclude ) ) );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
253 my @expected_content_sorted =
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
254 sort( split( /\n/, _exclude_variable_columns_from_spreadsheet( $expected_output_file_name, $columns_to_exclude ) ) );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
255 is_deeply( \@actual_content_sorted, \@expected_content_sorted,
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
256 'Actual and expected match output excluding variable columns' );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
257 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
258 else {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
259 compare_groups_files( $actual_output_file_name, $expected_output_file_name,
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
260 "Actual and expected sorted output match for '$script_parameters'" );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
261 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
262 unlink($actual_output_file_name);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
263 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
264 close STDOUT;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
265 close STDERR;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
266 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
267
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
268 # Restore stdout.
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
269 open STDOUT, '>&OLDOUT' or die "Can't restore stdout: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
270 open STDERR, '>&OLDERR' or die "Can't restore stderr: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
271
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
272 # Avoid leaks by closing the independent copies.
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
273 close OLDOUT or die "Can't close OLDOUT: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
274 close OLDERR or die "Can't close OLDERR: $!";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
275 unlink('empty_file');
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
276 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
277
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
278 sub compare_tab_files_with_variable_coordinates {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
279 my ( $actual_file, $expected_file ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
280 ok( -e $actual_file, 'File exists' . $actual_file );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
281
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
282 is(
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
283 _filter_coordinates_from_string($actual_file),
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
284 _filter_coordinates_from_string($expected_file),
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
285 'file contents the same for ' . $actual_file
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
286 );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
287 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
288
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
289 sub _filter_coordinates_from_string {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
290 my ($file_name) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
291 my $file_contents = read_text($file_name);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
292 my @lines = split( /\n/, $file_contents );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
293 my $modified_file_contents = '';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
294 for my $line ( sort @lines ) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
295 next if ( $line =~ /(variation|misc_feature|feature)/ );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
296 $line =~ s!group_[\d]+!group_XX!gi;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
297 $modified_file_contents .= $line . "\n";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
298 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
299 return $modified_file_contents;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
300 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
301
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
302 sub _exclude_variable_columns_from_spreadsheet {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
303 my ( $file_name, $columns_to_exclude ) = @_;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
304 my $file_contents = read_text($file_name);
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
305 my @lines = split( /\n/, $file_contents );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
306 my $modified_file_contents = '';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
307
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
308 for ( my $i = 0 ; $i < @lines ; $i++ ) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
309 my @cells = split( /,/, $lines[$i] );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
310
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
311 for my $col_number ( @{$columns_to_exclude} ) {
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
312 next unless ( defined( $cells[$col_number] ) );
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
313 $cells[$col_number] = '';
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
314 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
315 $modified_file_contents .= join( ',', @cells ) . "\n";
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
316 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
317
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
318 return $modified_file_contents;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
319 }
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
320
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
321 no Moose;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
322 1;
c47a5f61bc9f Uploaded
dereeper
parents:
diff changeset
323