Skip to content

Commit 8a535f6

Browse files
committed
MQE-2669: bin/mftf run:failed process used a lot of RAM
- Added simple unit test to test configuration generator
1 parent dccf388 commit 8a535f6

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace tests\unit\Magento\FunctionalTestFramework\Console;
7+
8+
use Magento\FunctionalTestingFramework\Console\GenerateTestFailedCommand;
9+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
10+
use PHPUnit\Framework\MockObject\MockBuilder;
11+
use PHPUnit\Framework\TestCase;
12+
use Magento\FunctionalTestingFramework\Exceptions\FastFailException;
13+
use Magento\FunctionalTestingFramework\Console\GenerateTestsCommand;
14+
use ReflectionClass;
15+
16+
class GenerateTestFailedCommandTest extends BaseGenerateCommandTest
17+
{
18+
public function testSingleTestNoSuite(): void
19+
{
20+
$testFileReturn = [
21+
"tests/functional/tests/MFTF/_generated/default/SingleTestNoSuiteTest.php:SingleTestNoSuiteTest"
22+
];
23+
$expectedConfiguration = '{"tests":["SingleTestNoSuiteTest"],"suites":null}';
24+
25+
// Create a stub for the SomeClass class.
26+
$stub = $this->getMockBuilder(GenerateTestFailedCommand::class)
27+
->onlyMethods(["readFailedTestFile", "writeFailedTestToFile"])
28+
->getMock();
29+
// Configure the stub.
30+
$stub
31+
->method('readFailedTestFile')
32+
->willReturn($testFileReturn);
33+
$stub
34+
->method('writeFailedTestToFile')
35+
->willReturn(null);
36+
37+
// Run the real code
38+
$configuration = $stub->getFailedTestList("", "");
39+
$this->assertEquals($expectedConfiguration, $configuration);
40+
}
41+
}

src/Magento/FunctionalTestingFramework/Console/GenerateTestFailedCommand.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ class GenerateTestFailedCommand extends BaseGenerateCommand
2323
*/
2424
const DEFAULT_TEST_GROUP = 'default';
2525

26-
/**
27-
* @var string
28-
*/
29-
private $testsReRunFile;
30-
3126
/**
3227
* Configures the current command.
3328
*
@@ -54,9 +49,6 @@ protected function configure()
5449
*/
5550
protected function execute(InputInterface $input, OutputInterface $output): int
5651
{
57-
$this->testsFailedFile = $this->getTestsOutputDir() . self::FAILED_FILE;
58-
$this->testsReRunFile = $this->getTestsOutputDir() . "rerun_tests";
59-
6052
$force = $input->getOption('force');
6153
$debug = $input->getOption('debug') ?? MftfApplicationConfig::LEVEL_DEVELOPER; // for backward compatibility
6254
$allowSkipped = $input->getOption('allow-skipped');
@@ -71,7 +63,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7163
$allowSkipped
7264
);
7365

74-
$testConfiguration = $this->getFailedTestList();
66+
$testsFailedFile = $this->getTestsOutputDir() . self::FAILED_FILE;
67+
$testsReRunFile = $this->getTestsOutputDir() . "rerun_tests";
68+
$testConfiguration = $this->getFailedTestList($testsFailedFile, $testsReRunFile);
7569

7670
if ($testConfiguration === null) {
7771
// No failed tests found, no tests generated
@@ -98,16 +92,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9892
*
9993
* @return string
10094
*/
101-
private function getFailedTestList()
95+
public function getFailedTestList($testsFailedFile, $testsReRunFile)
10296
{
10397
$failedTestDetails = ['tests' => [], 'suites' => []];
10498

105-
if (realpath($this->testsFailedFile)) {
106-
$testList = $this->readFailedTestFile($this->testsFailedFile);
99+
$testList = $this->readFailedTestFile($testsFailedFile);
107100

101+
if (!empty($testList)) {
108102
foreach ($testList as $test) {
109103
if (!empty($test)) {
110-
$this->writeFailedTestToFile($test, $this->testsReRunFile);
104+
$this->writeFailedTestToFile($test, $testsReRunFile);
111105
$testInfo = explode(DIRECTORY_SEPARATOR, $test);
112106
$testName = explode(":", $testInfo[count($testInfo) - 1])[1];
113107
$suiteName = $testInfo[count($testInfo) - 2];
@@ -160,9 +154,12 @@ private function sanitizeSuiteName($suiteName)
160154
* @param string $filePath
161155
* @return array|boolean
162156
*/
163-
private function readFailedTestFile($filePath)
157+
public function readFailedTestFile($filePath)
164158
{
165-
return file($filePath, FILE_IGNORE_NEW_LINES);
159+
if (realpath($filePath)) {
160+
return file($filePath, FILE_IGNORE_NEW_LINES);
161+
}
162+
return "";
166163
}
167164

168165
/**
@@ -172,7 +169,7 @@ private function readFailedTestFile($filePath)
172169
* @param string $filePath
173170
* @return void
174171
*/
175-
private function writeFailedTestToFile($test, $filePath)
172+
public function writeFailedTestToFile($test, $filePath)
176173
{
177174
if (file_exists($filePath)) {
178175
if (strpos(file_get_contents($filePath), $test) === false) {

0 commit comments

Comments
 (0)