-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathGenerateTestFailedCommand.php
186 lines (170 loc) · 6.01 KB
/
GenerateTestFailedCommand.php
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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento\FunctionalTestingFramework\Console;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\Util\Path\FilePathFormatter;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Symfony\Component\Console\Input\InputOption;
class GenerateTestFailedCommand extends BaseGenerateCommand
{
/**
* Default Test group to signify not in suite
*/
const DEFAULT_TEST_GROUP = 'default';
/**
* Configures the current command.
*
* @return void
*/
protected function configure()
{
$this->setName('generate:failed')
->setDescription('Generate a set of tests failed');
parent::configure();
}
/**
* Executes the current command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return integer
* @throws \Exception
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$force = $input->getOption('force');
$debug = $input->getOption('debug') ?? MftfApplicationConfig::LEVEL_DEVELOPER; // for backward compatibility
$allowSkipped = $input->getOption('allow-skipped');
$verbose = $output->isVerbose();
// Create Mftf Configuration
MftfApplicationConfig::create(
$force,
MftfApplicationConfig::EXECUTION_PHASE,
$verbose,
$debug,
$allowSkipped
);
$testsFailedFile = $this->getTestsOutputDir() . self::FAILED_FILE;
$testsReRunFile = $this->getTestsOutputDir() . "rerun_tests";
$testConfiguration = $this->getFailedTestList($testsFailedFile, $testsReRunFile);
if ($testConfiguration === null) {
// No failed tests found, no tests generated
$this->removeGeneratedDirectory($output, $verbose);
return 0;
}
$command = $this->getApplication()->find('generate:tests');
$args = [
'--tests' => $testConfiguration,
'--force' => $force,
'--remove' => true,
'--debug' => $debug,
'--allow-skipped' => $allowSkipped,
'-v' => $verbose
];
$command->run(new ArrayInput($args), $output);
$output->writeln("Test Failed Generated, now run:failed command");
return 0;
}
/**
* Returns a json string of tests that failed on the last run
*
* @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getFailedTestList($testsFailedFile, $testsReRunFile)
{
$failedTestDetails = ['tests' => [], 'suites' => []];
$testList = $this->readFailedTestFile($testsFailedFile);
if (!empty($testList)) {
foreach ($testList as $test) {
if (!empty($test)) {
$this->writeFailedTestToFile($test, $testsReRunFile);
$testInfo = explode(DIRECTORY_SEPARATOR, $test);
$testName = isset($testInfo[count($testInfo) - 1][1])
? explode(":", $testInfo[count($testInfo) - 1])[1]
: [];
$suiteName = isset($testInfo[count($testInfo) - 2])
? $testInfo[count($testInfo) - 2]
: [];
if ($suiteName === self::DEFAULT_TEST_GROUP) {
array_push($failedTestDetails['tests'], $testName);
} else {
$suiteName = $this->sanitizeSuiteName($suiteName);
$failedTestDetails['suites'] = array_merge_recursive(
$failedTestDetails['suites'],
[$suiteName => [$testName]]
);
}
}
}
}
if (empty($failedTestDetails['tests']) & empty($failedTestDetails['suites'])) {
return null;
}
if (empty($failedTestDetails['tests'])) {
$failedTestDetails['tests'] = null;
}
if (empty($failedTestDetails['suites'])) {
$failedTestDetails['suites'] = null;
}
$testConfigurationJson = json_encode($failedTestDetails);
return $testConfigurationJson;
}
/**
* Trim potential suite_parallel_0_G to suite_parallel
*
* @param string $suiteName
* @return string
*/
private function sanitizeSuiteName($suiteName)
{
$suiteNameArray = explode("_", $suiteName);
if (array_pop($suiteNameArray) === 'G') {
if (is_numeric(array_pop($suiteNameArray))) {
$suiteName = implode("_", $suiteNameArray);
}
}
return $suiteName;
}
/**
* Returns an array of tests read from the failed test file in _output
*
* @param string $filePath
* @return array|boolean
*/
public function readFailedTestFile($filePath)
{
if (realpath($filePath)) {
return file($filePath, FILE_IGNORE_NEW_LINES);
}
return "";
}
/**
* Writes the test name to a file if it does not already exist
*
* @param string $test
* @param string $filePath
* @return void
*/
public function writeFailedTestToFile($test, $filePath)
{
if (file_exists($filePath)) {
if (strpos(file_get_contents($filePath), $test) === false) {
file_put_contents($filePath, "\n" . $test, FILE_APPEND);
}
} else {
file_put_contents($filePath, $test . "\n");
}
}
}