summaryrefslogtreecommitdiffstats
path: root/scripts/generic/t/01-coveragerunner_testcocoon.t
blob: d651346120d0bffe55846fd1b0e73ba44d1e13d7 (plain)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use utf8;
use Readonly;

=head1 NAME

01-coveragerunner_testcocoon.t - basic test for coveragerunner_testcocoon.pl

=head1 SYNOPSIS

  perl ./01-coveragerunner_testcocoon.t

This test will run the coveragerunner_testcocoon.pl script with a few different
types of subprocesses and verify that behavior is as expected.

=cut

use Encode;
use English qw( -no_match_vars );
use FindBin;
use Readonly;
use Test::More;
use Capture::Tiny qw( capture );
use Env::Path;
use File::Basename;
use File::Copy;
use File::Find::Rule;
use File::Path qw( mkpath );
use File::Spec::Functions;
use File::Temp qw( tempdir );

use lib "$FindBin::Bin/../../lib/perl5";
use QtQA::Test::More qw( is_or_like create_mock_command );

# Directory separator, quoted for regex
Readonly my $DS => ($OSNAME eq 'MSWin32') ? q{\\\\} : q{/};

# Tempdir testcocoon for tests template
Readonly my $TEMPDIR_TOPLEVEL => File::Spec->tmpdir;

Readonly my $TEMPDIR_TEMPLATE =>
    qr{\Q$TEMPDIR_TOPLEVEL\E${DS}testcocoon_runner\..{6}}sm;

# Preamble always printed out at the beginning
Readonly my $COVERAGERUNNER_PREAMBLE =>
    qr{Gather all library files covered in a global database\sList of all source files in coverage.*};

# Reused paths to csmes for the libraries
# Note for this (and others), the module/.. part is optional,
# because that may be redundant and File::Find may already clean up that
# before returning.
Readonly my $LIST_CSMESLIB =>
    qr{${TEMPDIR_TEMPLATE}${DS}(?:module${DS}\.\.${DS})?qtbase${DS}lib${DS}lib\.csmes.*
${TEMPDIR_TEMPLATE}${DS}(?:module${DS}\.\.${DS})?qtbase${DS}lib${DS}lib2\.csmes.*}sm;

# First merge command
Readonly my $COVERAGERUNNER_CMMERGE_SRC =>
    qr{\+ cmmerge --append --output=${TEMPDIR_TEMPLATE}${DS}module_coverage_src-[0-9]{8}-[0-9]{4}\.csmes ${TEMPDIR_TEMPLATE}${DS}(?:module${DS}\.\.${DS})?qtbase${DS}lib${DS}lib2\.csmes.*};

# Second merge command
Readonly my $COVERAGERUNNER_CMMERGE_GLOBAL =>
    qr{\+ cmmerge --append --output=${TEMPDIR_TEMPLATE}${DS}module_coverage_global-[0-9]{8}-[0-9]{4}\.csmes ${TEMPDIR_TEMPLATE}${DS}module_coverage_unittests-[0-9]{8}-[0-9]{4}\.csmes.*};

# Cmreport command
Readonly my $COVERAGERUNNER_CMREPORT =>
    qr{\+ cmreport --csmes=${TEMPDIR_TEMPLATE}${DS}module_coverage_global-[0-9]{8}-[0-9]{4}\.csmes --xml=${TEMPDIR_TEMPLATE}${DS}module_coverage_report-[0-9]{8}-[0-9]{4}\.xml --select=.* --source=all --source-sort=name --global=all.*};

# gzip command
Readonly my $COVERAGERUNNER_GZIP =>
    qr{\+ gzip ${TEMPDIR_TEMPLATE}${DS}module_coverage_global-[0-9]{8}-[0-9]{4}\.csmes.*};

# xml2html_testcocoon command
Readonly my $COVERAGERUNNER_XML2HTML =>
    qr{\+ xml2html_testcocoon --xml ${TEMPDIR_TEMPLATE}${DS}module_coverage_report-[0-9]{8}-[0-9]{4}\.xml --module module --output ${TEMPDIR_TEMPLATE}.*};

# Standard output up to the first merge (gathering the lib and plugins csmes)
Readonly my $FIRST_CMMERGE =>
    qr{$COVERAGERUNNER_PREAMBLE
$LIST_CSMESLIB
$COVERAGERUNNER_CMMERGE_SRC}sm;

# Standard output up to the second merge (gathering the lib and plugins csmes)
Readonly my $SECOND_CMMERGE =>
    qr{$FIRST_CMMERGE
cmmerge success lib.*
End of list.*
$COVERAGERUNNER_CMMERGE_GLOBAL}sm;

# Standard output until cmreport command
Readonly my $CMREPORT =>
    qr{$SECOND_CMMERGE
cmmerge success tests.*
$COVERAGERUNNER_CMREPORT}sm;

# Standard output until gzip
Readonly my $GZIP =>
    qr{$CMREPORT
cmreport success.*
$COVERAGERUNNER_GZIP}sm;

# Standard output until xml2html_testcocoon
Readonly my $XML2HTML =>
    qr{$GZIP
gzip success.*
$COVERAGERUNNER_XML2HTML}sm;

# Successful run
Readonly my $TESTSCRIPT_SUCCESS =>
    qr{$XML2HTML
xml2html_testcocoon success.*}sm;

# Invalid --qtcoverage-test-ouput required argument
Readonly my $INVALID_TESTS_OUTPUT =>
    qr{${TEMPDIR_TEMPLATE}${DS}tests_invalid\.csmes does not exist. Either the tests have not been run or coverage was not enabled at build time};

# Run coveragerunner_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 $testname          =   $params_ref->{ testname }          || q{};

    my $status;
    my ($output, $error) = capture {
        $status = system( 'perl', "$FindBin::Bin/../coveragerunner_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" );

    return;
}

sub test_success
{
    my $tempdir = tempdir( 'testcocoon_runner.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
    init_test_env($tempdir);

    # Put our mock command first in PATH
    local $ENV{ PATH } = $ENV{ PATH };
    Env::Path->PATH->Prepend( $tempdir );

    create_mock_command(
        name        =>  'cmmerge',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmmerge success lib\n"},
            { exitcode => 0, stdout => "cmmerge success tests\n"},
        ],
    );

    create_mock_command(
        name        =>  'cmreport',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmreport success\n"},
        ],
    );

    create_mock_command(
        name        =>  'gzip',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "gzip success\n"},
        ],
    );

    local $ENV{ TESTING_COVERAGERUNNER } = 1;
    create_mock_command(
        name        =>  'xml2html_testcocoon',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "xml2html_testcocoon success\n"},
        ],
    );

    my $module_gitdir = catfile( $tempdir, 'module');
    my $test_csmes = catfile( $tempdir, 'tests.csmes' );

    test_run({
        args                =>  [ '--qt-gitmodule-dir', $module_gitdir, '--qt-gitmodule', 'module', '--qtcoverage-tests-output', $test_csmes, 'testname'],
        expected_stdout     =>  $TESTSCRIPT_SUCCESS,
        expected_stderr     =>  q{},
        expected_success    =>  1,
        testname            =>  'basic test success',
    });

    return;
}

sub test_first_merge_failure
{
    my $tempdir = tempdir( 'testcocoon_runner.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
    init_test_env($tempdir);

    # Put our mock command first in PATH
    local $ENV{ PATH } = $ENV{ PATH };
    Env::Path->PATH->Prepend( $tempdir );

    create_mock_command(
        name        =>  'cmmerge',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 1, stderr => "first cmmerge failure\n", stdout => ""},
        ],
    );

    my $module_gitdir = catfile( $tempdir, 'module');
    my $test_csmes = catfile( $tempdir, 'tests.csmes' );

    test_run({
        args                =>  [ '--qt-gitmodule-dir', $module_gitdir, '--qt-gitmodule', 'module', '--qtcoverage-tests-output', $test_csmes, 'testname'],
        expected_stdout     =>  $FIRST_CMMERGE,
        expected_stderr     =>  qr{first cmmerge failure\n}sm,
        expected_success    =>  0,
        testname            =>  'test first merge failed',
    });

    return;
}

sub test_second_merge_failure
{
    my $tempdir = tempdir( 'testcocoon_runner.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
    init_test_env($tempdir);

    # Put our mock command first in PATH
    local $ENV{ PATH } = $ENV{ PATH };
    Env::Path->PATH->Prepend( $tempdir );

    create_mock_command(
        name        =>  'cmmerge',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmmerge success lib\n"},
            { exitcode => 2, stderr => "second cmmerge failure\n", stdout => ""},
        ],
    );

    my $module_gitdir = catfile( $tempdir, 'module');
    my $test_csmes = catfile( $tempdir, 'tests.csmes' );

    test_run({
        args                =>  [ '--qt-gitmodule-dir', $module_gitdir, '--qt-gitmodule', 'module', '--qtcoverage-tests-output', $test_csmes, 'testname'],
        expected_stdout     =>  $SECOND_CMMERGE,
        expected_stderr     =>  qr{second cmmerge failure\n}sm,
        expected_success    =>  0,
        testname            =>  'test second merge failed',
    });

    return;
}

sub test_cmreport_failure
{
    my $tempdir = tempdir( 'testcocoon_runner.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
    init_test_env($tempdir);

    # Put our mock command first in PATH
    local $ENV{ PATH } = $ENV{ PATH };
    Env::Path->PATH->Prepend( $tempdir );

    create_mock_command(
        name        =>  'cmmerge',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmmerge success lib\n"},
            { exitcode => 0, stdout => "cmmerge success tests\n"},
        ],
    );

    create_mock_command(
        name        =>  'cmreport',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 3, stderr => "cmreport failure\n", stdout => ""},
        ],
    );

    my $module_gitdir = catfile( $tempdir, 'module');
    my $test_csmes = catfile( $tempdir, 'tests.csmes' );

    test_run({
        args                =>  [ '--qt-gitmodule-dir', $module_gitdir, '--qt-gitmodule', 'module', '--qtcoverage-tests-output', $test_csmes, 'testname'],
        expected_stdout     =>  $CMREPORT,
        expected_stderr     =>  qr{cmreport failure\n}sm,
        expected_success    =>  0,
        testname            =>  'test cmreport failure',
    });

    return;
}

sub test_gzip_failure
{
    my $tempdir = tempdir( 'testcocoon_runner.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
    init_test_env($tempdir);

    # Put our mock command first in PATH
    local $ENV{ PATH } = $ENV{ PATH };
    Env::Path->PATH->Prepend( $tempdir );

    create_mock_command(
        name        =>  'cmmerge',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmmerge success lib\n"},
            { exitcode => 0, stdout => "cmmerge success tests\n"},
        ],
    );

    create_mock_command(
        name        =>  'cmreport',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmreport success\n"},
        ],
    );

    create_mock_command(
        name        =>  'gzip',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 4, stderr => "gzip failure\n", stdout => ""},
        ],
    );

    my $module_gitdir = catfile( $tempdir, 'module');
    my $test_csmes = catfile( $tempdir, 'tests.csmes' );

    test_run({
        args                =>  [ '--qt-gitmodule-dir', $module_gitdir, '--qt-gitmodule', 'module', '--qtcoverage-tests-output', $test_csmes, 'testname'],
        expected_stdout     =>  $GZIP,
        expected_stderr     =>  qr{gzip failure\n}sm,
        expected_success    =>  0,
        testname            =>  'test gzip failure',
    });

    return;
}

sub test_xml2html_failure
{
    my $tempdir = tempdir( 'testcocoon_runner.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
    init_test_env($tempdir);

    # Put our mock command first in PATH
    local $ENV{ PATH } = $ENV{ PATH };
    Env::Path->PATH->Prepend( $tempdir );

    create_mock_command(
        name        =>  'cmmerge',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmmerge success lib\n"},
            { exitcode => 0, stdout => "cmmerge success tests\n"},
        ],
    );

    create_mock_command(
        name        =>  'cmreport',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmreport success\n"},
        ],
    );

    create_mock_command(
        name        =>  'gzip',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "gzip success\n"},
        ],
    );

    local $ENV{ TESTING_COVERAGERUNNER } = 1;
    create_mock_command(
        name        =>  'xml2html_testcocoon',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 5, stderr => "xml2html_testcocoon failure\n"},
        ],
    );

    my $module_gitdir = catfile( $tempdir, 'module');
    my $test_csmes = catfile( $tempdir, 'tests.csmes' );

    test_run({
        args                =>  [ '--qt-gitmodule-dir', $module_gitdir, '--qt-gitmodule', 'module', '--qtcoverage-tests-output', $test_csmes, 'testname'],
        expected_stdout     =>  $XML2HTML,
        expected_stderr     =>  qr{xml2html_testcocoon failure\n}sm,
        expected_success    =>  0,
        testname            =>  'test xml2html failure',
    });

    return;
}

sub test_invalid_qtcoverage_tests_output
{
    my $tempdir = tempdir( 'testcocoon_runner.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
    init_test_env($tempdir);

    my $test_invalid_csmes = catfile( $tempdir, 'tests_invalid.csmes' );

    test_run({
        args                =>  [ '--qt-gitmodule-dir', $tempdir, '--qt-gitmodule', 'module', '--qtcoverage-tests-output', $test_invalid_csmes, 'testname'],
        expected_stdout     =>  q{},
        expected_stderr     =>  $INVALID_TESTS_OUTPUT,
        expected_success    =>  0,
        testname            =>  'invalid csmes output file',
    });

    return;
}

sub init_test_env
{
    my ($tempdir) = @_;

    # Create qtbase path
    my $qtbase_path = catfile($tempdir, 'qtbase');
    if (! -d $qtbase_path && ! mkpath( $qtbase_path )) {
        die "mkpath $qtbase_path: $!";
    }

    # Create plugins path
    my $plugins_path = catfile($qtbase_path, 'plugins');
    if (! -d $plugins_path && ! mkpath( $plugins_path )) {
        die "mkpath $plugins_path: $!";
    }

    # Create "module" path
    my $module_gitdir = catfile( $tempdir, 'module');
    if (! -d $module_gitdir && ! mkpath( $module_gitdir )) {
        die "mkpath $module_gitdir: $!";
    }

    # Create lib path
    my $lib_path = catfile($qtbase_path, 'lib');
    if (! -d $lib_path && ! mkpath( $lib_path )) {
        die "mkpath $lib_path: $!";
    }
    # Create a csmes in lib folder
    open(my $lib, ">", "$lib_path/lib.csmes") or die $!;
    close($lib);
    open(my $lib2, ">", "$lib_path/lib2.csmes") or die $!;
    close($lib2);
    # Create a tests result database csmes
    open(my $test, ">", "$tempdir/tests.csmes") or die $!;
    close($test);

    return;
}

sub run
{
    test_success;
    test_first_merge_failure;
    test_second_merge_failure;
    test_cmreport_failure;
    test_gzip_failure;
    test_invalid_qtcoverage_tests_output;
    test_xml2html_failure;

    done_testing;

    return;
}

run if (!caller);
1;