File: TestsRunVue.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (114 lines) | stat: -rw-r--r-- 3,769 bytes parent folder | download
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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\TestRunner\Commands;

use Piwik\Plugin\ConsoleCommand;

class TestsRunVue extends ConsoleCommand
{
    protected function configure()
    {
        $this->setName('tests:run-vue');
        $this->setDescription('Run Vue component unit tests');
        $this->setHelp(<<<'EOF'
The <info>%command.name%</info> command run all Vue component tests by default:

<info>ddev matomo:console %command.name%</info>

You can also run only one Vue spec by adding a specific file path, file name, or a regex:

<info>ddev matomo:console %command.name% plugins/CoreHome/vue/src/Alert/Alert.spec.ts</info>

<info>ddev matomo:console %command.name% Alert.spec.ts</info>

You can run the Vue tests for a specific plugin:

<info>ddev matomo:console %command.name% --plugin=CoreHome</info>

Notes:
- if <info>--plugin</info> is provided, <info>specs</info> arguments are ignored and discovery is scoped to that plugin.
- if the plugin does not exist, the command exits with a non-zero status.

It's possible to run the tests serially with verbose output:

<info>ddev matomo:console %command.name% --run-in-band --verbose</info>

EOF
        );

        $this->addOptionalArgument(
            'specs',
            'One or more Vue spec file paths or test path regex fragments. Separate multiple values by a space.',
            null,
            true
        );
        $this->addRequiredValueOption('plugin', null, 'The plugin to run Vue tests for (eg CoreHome or plugins/CoreHome).');
        $this->addNoValueOption('run-in-band', null, 'Run Jest tests serially in a single process.');
    }

    protected function doExecute(): int
    {
        $input = $this->getInput();
        $output = $this->getOutput();

        $plugin = $input->getOption('plugin');
        $runInBand = $input->getOption('run-in-band');
        $verbose = $output->isVerbose();
        $specs = $input->getArgument('specs');

        $testOptions = [];

        if ($runInBand) {
            $testOptions[] = '--runInBand';
        }

        if ($verbose) {
            $testOptions[] = '--verbose';
        }

        $pluginEnv = '';
        if (!empty($plugin)) {
            $plugin = trim((string) $plugin);
            if (strpos($plugin, 'plugins/') !== 0) {
                $plugin = 'plugins/' . $plugin;
            }

            $pluginPath = PIWIK_INCLUDE_PATH . '/' . $plugin;
            if (!is_dir($pluginPath)) {
                $output->writeln('<error>Plugin path not found: ' . $plugin . '</error>');
                $output->writeln('<error>Use --plugin=CoreHome or --plugin=plugins/CoreHome.</error>');
                return 1;
            }

            $pluginPattern = preg_quote($plugin, '/') . '\/vue\/.*\.spec\.[tj]s$';
            $testOptions[] = '--testPathPattern=' . escapeshellarg($pluginPattern);

            if (!empty($specs)) {
                $output->writeln('<comment>Ignoring specs arguments because --plugin scopes test discovery.</comment>');
            }

            $pluginEnv = 'MATOMO_CURRENT_PLUGIN=' . escapeshellarg($plugin) . ' ';
        } elseif (!empty($specs)) {
            $pattern = implode('|', $specs);
            $testOptions[] = '--testPathPattern=' . escapeshellarg($pattern);
        }

        $cmd = "cd '" . PIWIK_INCLUDE_PATH . "' && " . $pluginEnv . 'npm test';
        if (!empty($testOptions)) {
            $cmd .= ' -- ' . implode(' ', $testOptions);
        }

        $output->writeln('Executing command: <info>' . $cmd . '</info>');
        $output->writeln('');

        passthru($cmd, $returnCode);
        return $returnCode;
    }
}