1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use utf8;
use Readonly;
use File::Basename qw(basename);
=head1 NAME
01-xml2html_testcocoon.t - basic tests for xml2html_testcocoon.pl
=head1 SYNOPSIS
perl ./01-xml2html_testcocoon.t
This test will run the xml2html_testcocoon.pl script with a few different
inputs and verify that behavior is as expected.
=cut
use Encode;
use English qw( -no_match_vars );
use FindBin;
use Test::More;
use Capture::Tiny qw( capture );
use File::Find::Rule;
use File::Slurp qw( read_file write_file);
use File::Spec::Functions;
use File::Temp qw( tempdir );
use lib "$FindBin::Bin/../../lib/perl5";
use QtQA::Test::More qw( is_or_like );
# Run xml2html_testcocoon
sub test_run
{
my ($params_ref) = @_;
my @args = @{$params_ref->{ args }};
my $expected_stdout = $params_ref->{ expected_stdout };
my $expected_stderr = $params_ref->{ expected_stderr };
my $expected_success = $params_ref->{ expected_success };
my $expected_logfile_list = $params_ref->{ expected_logfile };
my $expected_logtext_list = $params_ref->{ expected_logtext } // [];
my $testname = $params_ref->{ testname } || q{};
my $status;
my ($output, $error) = capture {
$status = system( 'perl', "$FindBin::Bin/../xml2html_testcocoon.pl", @args );
};
if ($expected_success) {
is ( $status, 0, "$testname exits zero" );
} else {
isnt( $status, 0, "$testname exits non-zero" );
}
is_or_like( $output, $expected_stdout, "$testname output looks correct" );
is_or_like( $error, $expected_stderr, "$testname error looks correct" );
# The rest of the verification steps are only applicable if a log file is expected and created
return if (!$expected_logfile_list);
my $countLogFiles = @{$expected_logfile_list};
my $countLogExpectedText = @{$expected_logtext_list};
for (my $i = 0; $i < $countLogFiles; $i++) {
my $expected_logfile = $expected_logfile_list->[$i];
my $expected_logtext = "";
if ($countLogExpectedText eq $countLogFiles) { # only if the counts match we use the expected texts from the list.
$expected_logtext = $expected_logtext_list->[$i];
}
return if (!ok( -e $expected_logfile, "$testname created $expected_logfile" ));
my $logtext = read_file( $expected_logfile ); # dies on error
is_or_like( $logtext, $expected_logtext, "$testname " . basename($expected_logfile) . " is as expected" );
}
return;
}
sub test_success
{
my $tempdir = tempdir( 'testcocoon_xml2html.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
my $xml_file = init_test_env($tempdir);
my $created_report = [
catfile($tempdir, 'module_report.html'),
catfile($tempdir, 'files', 'folder_folder_module.html'),
catfile($tempdir, 'files', 'tests_module_failed.html'),
catfile($tempdir, 'files', 'tests_module_passed.html'),
catfile($tempdir, 'files', 'tests_module_unknown.html'),
catfile($tempdir, 'files', 'untested_sources_module.html')
];
my $expected_text = get_html_report_content();
my $include_path = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', 'module', 'src');
test_run({
args => [ '--xml', $xml_file, '--module', 'module', '--output', $tempdir , '--include', $include_path],
expected_stdout => q{},
expected_stderr => q{},
expected_success => 1,
testname => 'basic test success',
expected_logfile => $created_report,
expected_logtext => $expected_text,
});
return;
}
sub test_invalid_xml
{
my $invalid_xml_file = 'invalid.xml';
test_run({
args => [ '--xml', $invalid_xml_file, '--module', 'module', '--output', 'fake', '--include', 'fake'],
expected_stdout => q{},
expected_stderr => qr{Missing or invalid required '--xml' option.*},
expected_success => 0,
testname => 'test invalid xml',
});
return;
}
sub test_missing_module
{
my $tempdir = tempdir( 'testcocoon_xml2html.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
my $xml_file = init_test_env($tempdir);
test_run({
args => [ '--xml', $xml_file, '--output', 'fake', '--include', 'fake'],
expected_stdout => q{},
expected_stderr => qr{Missing required '--module' option.*},
expected_success => 0,
testname => 'test missing module',
});
return;
}
sub test_missing_output
{
my $tempdir = tempdir( 'testcocoon_xml2html.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
my $xml_file = init_test_env($tempdir);
test_run({
args => [ '--xml', $xml_file, '--module', 'module', '--include', 'fake'],
expected_stdout => q{},
expected_stderr => qr{Missing required '--output' option.*},
expected_success => 0,
testname => 'test missing output',
});
return;
}
sub test_invalid_output
{
my $tempdir = tempdir( 'testcocoon_xml2html.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
my $xml_file = init_test_env($tempdir);
test_run({
args => [ '--xml', $xml_file, '--module', 'module', '--output', '/\invalid_path' ,'--include', 'fake'],
expected_stdout => q{},
expected_stderr => qr{mkdir /\\invalid_path: *},
expected_success => 0,
testname => 'test invalid output',
});
return;
}
sub test_missing_include
{
my $tempdir = tempdir( 'testcocoon_xml2html.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
my $xml_file = init_test_env($tempdir);
test_run({
args => [ '--xml', $xml_file, '--module', 'module', '--module', 'module', '--output', $tempdir],
expected_stdout => q{},
expected_stderr => qr{Missing required '--include' option.*},
expected_success => 0,
testname => 'test missing include',
});
return;
}
sub init_test_env
{
my ($tempdir) = @_;
# Create xml file from template
my $xml_file_template = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', 'module_test_template.xml');
my $content = read_file($xml_file_template);
my $path_files = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', 'module', 'src');
my $path_3rdparty = catfile($path_files, '3rdparty', '3rdparty.c');
my $path_source = catfile($path_files, 'folder', 'source.cpp');
my $path_source2 = catfile($path_files, 'folder', 'source2.cpp');
my $path2_source2 = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', 'module2', 'src', 'folder2', 'source2.cpp');
$content =~ s,%path_3rdparty%,$path_3rdparty,g;
$content =~ s,%path_source%,$path_source,g;
$content =~ s,%path_source2%,$path_source2,g;
$content =~ s,%path2_source2%,$path2_source2,g;
my $xml_file = catfile($tempdir, 'module_test_20120131-1042.xml');
write_file($xml_file, $content);
return $xml_file;
}
sub get_html_report_content
{
# Create html content from templates
my $list_html_content = [];
my $path_source = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', 'module', 'src');
my $thirdrdparty = catfile('3rdparty', '3rdparty.c');
# Main html report
my $html_report_template = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', "module_report_template.html");
my $content = read_file($html_report_template);
$content =~ s,%path_src%,$path_source,g;
$content =~ s,%3rdparty%,$thirdrdparty,g;
push @{$list_html_content}, $content;
# Files - folder
my $html_folder_template = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', 'files', "folder_folder_module_template.html");
my $content2 = read_file($html_folder_template);
$content2 =~ s,%path_src%,$path_source,g;
my $source = catfile('folder', 'source.cpp');
my $source2 = catfile('folder', 'source2.cpp');
$content2 =~ s,%source%,$source,g;
$content2 =~ s,%source2%,$source2,g;
push @{$list_html_content}, $content2;
my $html_failed_tests_template = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', 'files', "tests_module_failed_template.html");
my $content3 = read_file($html_failed_tests_template);
push @{$list_html_content}, $content3;
my $html_passed_tests_template = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', 'files', "tests_module_passed_template.html");
my $content4 = read_file($html_passed_tests_template);
push @{$list_html_content}, $content4;
my $html_unknown_tests_template = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', 'files', "tests_module_unknown_template.html");
my $content5 = read_file($html_unknown_tests_template);
push @{$list_html_content}, $content5;
my $html_untested_template = catfile($FindBin::Bin, 'data', 'parsed-xml-testcocoon', 'files', "untested_sources_module_template.html");
my $content6 = read_file($html_untested_template);
my $path_untested = catfile($path_source, 'folder', 'untested.cpp');
$content6 =~ s,%path_untested%,$path_untested,g;
push @{$list_html_content}, $content6;
return $list_html_content;
}
sub run
{
test_success;
test_invalid_xml;
test_missing_module;
test_missing_output;
test_invalid_output;
test_missing_include;
done_testing;
return;
}
run if (!caller);
1;
|