summaryrefslogtreecommitdiffstats
path: root/scripts/generic/t/01-testrunner-testcocoon.t
blob: af222e5c64e4d3677ada1b2d4957291301be38ca (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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;

=head1 NAME

01-testrunner-testcocoon.t - test testrunner's 'testcocoon' plugin for code coverage analysis.

=head1 SYNOPSIS

  perl ./01-testrunner-testcocoon.t

This test will run the testrunner.pl script with some fake testcocoon processes
and verify that the testcocoon plugin generates the expected output.

=cut

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

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 => catfile(File::Spec->tmpdir);

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

# Commands used multiple times

Readonly my $MYTEST_DIR => qr{${TEMPDIR_TEMPLATE}${DS}module${DS}mytest}sm;

Readonly my $GLOBAL_TEST =>
    qr{$MYTEST_DIR${DS}mytest_global\.csmes}sm;

Readonly my $COVERAGERUNNER_CMMERGE_OTHERCSMES =>
    qr{\+ cmmerge --append --output=$GLOBAL_TEST $MYTEST_DIR${DS}mytest\.csmes.*};
Readonly my $COVERAGERUNNER_CMMERGE_PLUGIN_WITH_TEST =>
    qr{\+ cmmerge --append --output=$GLOBAL_TEST ${TEMPDIR_TEMPLATE}${DS}(?:module${DS}\.\.${DS})?qtbase${DS}plugins${DS}plugin1${DS}plugin1\.csmes.*};
Readonly my $COVERAGERUNNER_CMCSEXEIMPORT =>
    qr{\+ cmcsexeimport --csmes=$GLOBAL_TEST --csexe=$MYTEST_DIR${DS}myother\.csexe --title=tc_mytest --policy=merge.*};
Readonly my $COVERAGERUNNER_CMCSEXEIMPORT_OTHER =>
    qr{\+ cmcsexeimport --csmes=$GLOBAL_TEST --csexe=$MYTEST_DIR${DS}mytest\.csexe --title=tc_mytest --policy=merge.*};
Readonly my $COVERAGERUNNER_CMMERGE_TEST_WITH_GLOBAL =>
    qr{\+ cmmerge --append --output=${TEMPDIR_TEMPLATE}${DS}tests\.csmes $GLOBAL_TEST.*};

# Missing --testcocoon-tests-output required option
Readonly my $MISSING_REQUIRED_TESTS_OUTPUT =>
    qr{internal error: .*${DS}testcocoon\.pm loaded OK, but QtQA::App::TestRunner::Plugin::testcocoon could not be instantiated: Missing required '--testcocoon-tests-output' option at .*${DS}testcocoon\.pm line .*};

# Missing --testcocoon-qt-gitmodule-dir required option
Readonly my $MISSING_REQUIRED_GITDIR =>
    qr{internal error: .*${DS}testcocoon\.pm loaded OK, but QtQA::App::TestRunner::Plugin::testcocoon could not be instantiated: Invalid or missing required '--testcocoon-qt-gitmodule-dir' option at .*${DS}testcocoon\.pm line .*};

# Missing --testcocoon-qt-gitmodule required option
Readonly my $MISSING_REQUIRED_MODULE =>
    qr{internal error: .*${DS}testcocoon\.pm loaded OK, but QtQA::App::TestRunner::Plugin::testcocoon could not be instantiated: Missing required '--testcocoon-qt-gitmodule' option at .*${DS}testcocoon\.pm line .*};

# Cmmerge other csmes
Readonly my $CMMERGE_OTHERCSMES =>
    qr{test success.*
$COVERAGERUNNER_CMMERGE_OTHERCSMES}sm;

# Cmmerge with a plugin
Readonly my $CMMERGE_PLUGIN_WITH_TEST =>
    qr{$CMMERGE_OTHERCSMES
cmmerge success other csmes.*
$COVERAGERUNNER_CMMERGE_PLUGIN_WITH_TEST}sm;

# Cmcsexeimport
Readonly my $CMCSEXEIMPORT =>
    qr{$CMMERGE_PLUGIN_WITH_TEST
cmmerge success plugin with test.*
$COVERAGERUNNER_CMCSEXEIMPORT}sm;

# Cmcsexeimport other
Readonly my $CMCSEXEIMPORT_OTHER =>
    qr{$CMCSEXEIMPORT
cmcsexeimport success.*
$COVERAGERUNNER_CMCSEXEIMPORT_OTHER}sm;

# Cmmerge test into global
Readonly my $CMMERGE_TEST_INTO_GLOBAL =>
    qr{$CMCSEXEIMPORT_OTHER
cmcsexeimport other success.*
$COVERAGERUNNER_CMMERGE_TEST_WITH_GLOBAL}sm;

# Successful run
Readonly my $TESTSCRIPT_SUCCESS =>
    qr{$CMMERGE_TEST_INTO_GLOBAL
cmmerge success test into global.*}sm;

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/../testrunner.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_plugin.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        =>  'mytest',
        directory   =>  $tempdir,
        sequence    =>  [
           { exitcode => 0, stdout => "test success\n"},
        ],
    );

    create_mock_command(
        name        =>  'cmmerge',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmmerge success other csmes\n"},
            { exitcode => 0, stdout => "cmmerge success plugin with test\n"},
            { exitcode => 0, stdout => "cmmerge success test into global\n"},
        ],
    );

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

    my $initdir = getcwd;
    my $testdir = catfile( $tempdir, 'module', 'mytest');
    chdir($testdir);

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

    test_run({
        testname         => 'test success',
        args             => [ '--plugin', 'testcocoon', '--testcocoon-tests-output', $tests_csmes, '--testcocoon-qt-gitmodule-dir', $module_gitdir, '--testcocoon-qt-gitmodule', 'module', '--', "$tempdir/mytest",'testname' ],
        expected_success => 1,
        expected_stdout  => $TESTSCRIPT_SUCCESS,
        expected_stderr  => q{},
    });

    chdir($initdir);

    return;
}

sub test_invalid_tests_output_arg
{
    my $tempdir = tempdir( 'testcocoon_plugin.XXXXXX', TMPDIR => 1, CLEANUP => 1 );

    test_run({
        testname         => 'missing test output',
        args             => [ '--plugin', 'testcocoon', '--testcocoon-qt-gitmodule-dir', 'fake_gitmodule_dir', '--', "$tempdir/mytest",'testname' ],
        expected_success => 0,
        expected_stdout  => q{},
        expected_stderr  => $MISSING_REQUIRED_TESTS_OUTPUT,
    });

    return;
}

sub test_invalid_qt_gitmodule_dir
{
    my $tempdir = tempdir( 'testcocoon_plugin.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
    my $tests_csmes = catfile( $tempdir, 'tests.csmes' );

    test_run({
        testname         => 'missing git module dir',
        args             => [ '--plugin', 'testcocoon', '--testcocoon-tests-output', $tests_csmes, '--', "$tempdir/mytest",'testname' ],
        expected_success => 0,
        expected_stdout  => q{},
        expected_stderr  => $MISSING_REQUIRED_GITDIR,
    });

    return;
}

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

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

    test_run({
        testname         => 'missing git module name',
        args             => [ '--plugin', 'testcocoon', '--testcocoon-qt-gitmodule-dir', $module_gitdir, '--testcocoon-tests-output', $tests_csmes, '--', "$tempdir/mytest",'testname' ],
        expected_success => 0,
        expected_stdout  => q{},
        expected_stderr  => $MISSING_REQUIRED_MODULE,
    });

    return;
}

sub test_merge_other_csmes_failure
{
    my $tempdir = tempdir( 'testcocoon_plugin.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        =>  'mytest',
        directory   =>  $tempdir,
        sequence    =>  [
           { exitcode => 0, stdout => "test success\n"},
        ],
    );

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

    my $initdir = getcwd;
    my $testdir = catfile( $tempdir, 'module', 'mytest');
    chdir($testdir);

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

    test_run({
        testname         => 'cmmerge other csmes failure',
        args             => [ '--plugin', 'testcocoon', '--testcocoon-tests-output', $tests_csmes, '--testcocoon-qt-gitmodule-dir', $module_gitdir, '--testcocoon-qt-gitmodule', 'module', '--', "$tempdir/mytest",'testname' ],
        expected_success => 0,
        expected_stdout  => $CMMERGE_OTHERCSMES,
        expected_stderr  => qr{cmmerge failure other csmes\n},
    });

    chdir($initdir);

    return;
}

sub test_cmmerge_plugin_with_test_failure
{
    my $tempdir = tempdir( 'testcocoon_plugin.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        =>  'mytest',
        directory   =>  $tempdir,
        sequence    =>  [
           { exitcode => 0, stdout => "test success\n"},
        ],
    );

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

    my $initdir = getcwd;
    my $testdir = catfile( $tempdir, 'module', 'mytest');
    chdir($testdir);

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

    test_run({
        testname         => 'cmmerge into global plugins failure',
        args             => [ '--plugin', 'testcocoon', '--testcocoon-tests-output', $tests_csmes, '--testcocoon-qt-gitmodule-dir', $module_gitdir, '--testcocoon-qt-gitmodule', 'module', '--', "$tempdir/mytest",'testname' ],
        expected_success => 0,
        expected_stdout  => $CMMERGE_PLUGIN_WITH_TEST,
        expected_stderr  => qr{cmmerge failure plugin\n},
    });

    chdir($initdir);

    return;
}

sub test_cmcsexeimport_failure
{
    my $tempdir = tempdir( 'testcocoon_plugin.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        =>  'mytest',
        directory   =>  $tempdir,
        sequence    =>  [
           { exitcode => 0, stdout => "test success\n"},
        ],
    );

    create_mock_command(
        name        =>  'cmmerge',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmmerge success other csmes\n"},
            { exitcode => 0, stdout => "cmmerge success plugin with test\n"},
        ],
    );

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

    my $initdir = getcwd;
    my $testdir = catfile( $tempdir, 'module', 'mytest');
    chdir($testdir);

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

    test_run({
        testname         => 'cmcsexeimport failure',
        args             => [ '--plugin', 'testcocoon', '--testcocoon-tests-output', $tests_csmes, '--testcocoon-qt-gitmodule-dir', $module_gitdir, '--testcocoon-qt-gitmodule', 'module', '--', "$tempdir/mytest",'testname' ],
        expected_success => 0,
        expected_stdout  => $CMCSEXEIMPORT,
        expected_stderr  => qr{cmcsexeimport failure\n},
    });

    chdir($initdir);

    return;
}

sub test_cmmerge_test_into_global_failure
{
    my $tempdir = tempdir( 'testcocoon_plugin.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        =>  'mytest',
        directory   =>  $tempdir,
        sequence    =>  [
           { exitcode => 0, stdout => "test success\n"},
        ],
    );

    create_mock_command(
        name        =>  'cmmerge',
        directory   =>  $tempdir,
        sequence    =>  [
            { exitcode => 0, stdout => "cmmerge success other csmes\n"},
            { exitcode => 0, stdout => "cmmerge success plugin with test\n"},
            { exitcode => 4, stderr => "cmmerge failure test into global\n"},
        ],
    );

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

    my $initdir = getcwd;
    my $testdir = catfile( $tempdir, 'module', 'mytest');
    chdir($testdir);

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

    test_run({
        testname         => 'cmmerge test into global failure',
        args             => [ '--plugin', 'testcocoon', '--testcocoon-tests-output', $tests_csmes, '--testcocoon-qt-gitmodule-dir', $module_gitdir, '--testcocoon-qt-gitmodule', 'module', '--', "$tempdir/mytest",'testname' ],
        expected_success => 0,
        expected_stdout  => $CMMERGE_TEST_INTO_GLOBAL,
        expected_stderr  => qr{cmmerge failure test into global\n},
    });

    chdir($initdir);

    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 "module" path
    my $gitmodule_path = catfile($tempdir, 'module');
    if (! -d $gitmodule_path && ! mkpath( $gitmodule_path )) {
        die "mkpath $gitmodule_path: $!";
    }

    # Create "mytest" path
    my $mytest_path = catfile($gitmodule_path, 'mytest');
    if (! -d $mytest_path && ! mkpath( $mytest_path )) {
        die "mkpath $mytest_path: $!";
    }

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

    my $plugin1_path = catfile($plugins_path, 'plugin1');
    if (! -d $plugin1_path && ! mkpath( $plugin1_path )) {
        die "mkpath $plugin1_path: $!";
    }

    # Create csmes for 1 plugin
    open(my $plugin1, ">", "$plugin1_path/plugin1.csmes") or die $!;
    close($plugin1);

    #  Create current test csexe and csmes
    open(my $mytestcsmes, ">", "$mytest_path/mytest.csmes") or die $!;
    close($mytestcsmes);
    open(my $mytestcsexe, ">", "$mytest_path/mytest.csexe") or die $!;
    close($mytestcsexe);
    open(my $myothercsmes, ">", "$mytest_path/myother.csmes") or die $!;
    close($myothercsmes);
    open(my $myothercsexe, ">", "$mytest_path/myother.csexe") or die $!;
    close($myothercsexe);
    open(my $moccsexe, ">", "$mytest_path/moc.csexe") or die $!;
    close($moccsexe);

    # Create a tests result database csmes
    open(my $test, ">", "$tempdir/tests.csmes") or die $!;
    close($test);

    return;
}

sub run
{
    test_success;
    test_invalid_tests_output_arg;
    test_invalid_qt_gitmodule_dir;
    test_merge_other_csmes_failure;
    test_cmmerge_plugin_with_test_failure;
    test_cmcsexeimport_failure;
    test_cmmerge_test_into_global_failure;
    test_missing_qt_gitmodule;

    done_testing;

    return;
}

run if (!caller);
1;