Skip to content

MQE-1031: Empty files are flagged during generation or dealt with gracefully #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ coverage/
.vscode
codeception.yml
dev/tests/functional/MFTF.suite.yml
dev/tests/functional/_output
dev/tests/functional/_output
dev/tests/mftf.log
8 changes: 8 additions & 0 deletions bin/all-checks.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:: Copyright © Magento, Inc. All rights reserved.
:: See COPYING.txt for license details.

@echo off
call bin\static-checks.bat

@echo off
call bin\phpunit-checks.bat
41 changes: 41 additions & 0 deletions bin/copyright-check.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
:: Copyright © Magento, Inc. All rights reserved.
:: See COPYING.txt for license details.

@echo off
SETLOCAL EnableDelayedExpansion
SET BLACKLIST_FILE=bin/blacklist.txt
SET i=0

FOR /F %%x IN ('git ls-tree --full-tree -r --name-only HEAD') DO (
SET GOOD_EXT=
if "%%~xx"==".php" set GOOD_EXT=1
if "%%~xx"==".xml" set GOOD_EXT=1
if "%%~xx"==".xsd" set GOOD_EXT=1
IF DEFINED GOOD_EXT (
SET BLACKLISTED=
FOR /F "tokens=* skip=5" %%f IN (%BLACKLIST_FILE%) DO (
SET LINE=%%x
IF NOT "!LINE!"=="!LINE:%%f=!" (
SET BLACKLISTED=1
)
)
IF NOT DEFINED BLACKLISTED (
FIND "Copyright © Magento, Inc. All rights reserved." %%x >nul
IF ERRORLEVEL 1 (
SET /A i+=1
SET NO_COPYRIGHT_LIST[!i!]=%%x
)
)
)
)

IF DEFINED NO_COPYRIGHT_LIST[1] (
ECHO THE FOLLOWING FILES ARE MISSING THE MAGENTO COPYRIGHT:
ECHO.
ECHO Copyright © Magento, Inc. All rights reserved.
ECHO See COPYING.txt for license details.
ECHO.
FOR /L %%a IN (1,1,%i%) DO (
ECHO !NO_COPYRIGHT_LIST[%%a]!
)
)
14 changes: 6 additions & 8 deletions bin/static-checks.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

@echo off
@echo ===============================PHP CODE SNIFFER REPORT===============================
call vendor\bin\phpcs .\src --standard=.\dev\tests\static\Magento --ignore=src\Magento\FunctionalTestingFramework\Group,src\Magento\FunctionalTestingFramework\AcceptanceTester.php
call vendor\bin\phpcs .\dev\tests\unit --standard=.\dev\tests\static\Magento
call vendor\bin\phpcs .\dev\tests\verification --standard=.\dev\tests\static\Magento --ignore=dev\tests\verification\_generated
call vendor\bin\phpcs --standard=.\dev\tests\static\Magento --ignore=src/Magento/FunctionalTestingFramework/Group,src/Magento/FunctionalTestingFramework/AcceptanceTester.php .\src
call vendor\bin\phpcs --standard=.\dev\tests\static\Magento .\dev\tests\unit
call vendor\bin\phpcs --standard=.\dev\tests\static\Magento --ignore=dev/tests/verification/_generated .\dev\tests\verification

@echo ===============================COPY PASTE DETECTOR REPORT===============================
call vendor\bin\phpcpd .\src

@echo "===============================PHP MESS DETECTOR REPORT===============================
vendor\bin\phpmd .\src text \dev\tests\static\Magento\CodeMessDetector\ruleset.xml --exclude _generated,src\Magento\FunctionalTestingFramework\Group,src\Magento\FunctionalTestingFramework\AcceptanceTester.php
@echo ===============================PHP MESS DETECTOR REPORT===============================
call vendor\bin\phpmd --exclude _generated,src\Magento\FunctionalTestingFramework\Group,src\Magento\FunctionalTestingFramework\AcceptanceTester.php .\src text \dev\tests\static\Magento\CodeMessDetector\ruleset.xml

@echo ===============================MAGENTO COPYRIGHT REPORT===============================
echo msgbox "INFO:Copyright check currently not run as part of .bat implementation" > "%temp%\popup.vbs"
wscript.exe "%temp%\popup.vbs"
::bin\copyright-check
call bin\copyright-check.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Tests\unit\Magento\FunctionalTestFramework\Test\Config\Reader;

use Magento\FunctionalTestingFramework\Config\FileResolver\Module;
use Magento\FunctionalTestingFramework\Config\Reader\Filesystem;
use Magento\FunctionalTestingFramework\Config\ValidationState;
use Magento\FunctionalTestingFramework\Util\Iterator\File;
use PHPUnit\Framework\TestCase;
use AspectMock\Test as AspectMock;
use tests\unit\Util\TestLoggingUtil;

class FilesystemTest extends TestCase
{
/**
* Before test functionality
* @return void
*/
public function setUp()
{
TestLoggingUtil::getInstance()->setMockLoggingUtil();
}

/**
* Test Reading Empty Files
* @throws \Exception
*/
public function testEmptyXmlFile()
{
// create mocked items and read the file
$someFile = $this->setMockFile("somepath.xml", "");
$filesystem = $this->createPseudoFileSystem($someFile);
$filesystem->read();

// validate log statement
TestLoggingUtil::getInstance()->validateMockLogStatement(
"warning",
"XML File is empty.",
["File" => "somepath.xml"]
);
}

/**
* Function used to set mock for File created in test
*
* @param string $fileName
* @param string $content
* @return object
* @throws \Exception
*/
public function setMockFile($fileName, $content)
{
$file = AspectMock::double(
File::class,
[
'current' => "",
'count' => 1,
'getFilename' => $fileName
]
)->make();

//set mocked data property for File
$property = new \ReflectionProperty(File::class, 'data');
$property->setAccessible(true);
$property->setValue($file, [$fileName => $content]);

return $file;
}

/**
* Function used to set mock for filesystem class during test
*
* @param string $fileList
* @return object
* @throws \Exception
*/
public function createPseudoFileSystem($fileList)
{
$filesystem = AspectMock::double(Filesystem::class)->make();

//set resolver to use mocked resolver
$mockFileResolver = AspectMock::double(Module::class, ['get' => $fileList])->make();
$property = new \ReflectionProperty(Filesystem::class, 'fileResolver');
$property->setAccessible(true);
$property->setValue($filesystem, $mockFileResolver);

//set validator to use mocked validator
$mockValidation = AspectMock::double(ValidationState::class, ['isValidationRequired' => false])->make();
$property = new \ReflectionProperty(Filesystem::class, 'validationState');
$property->setAccessible(true);
$property->setValue($filesystem, $mockValidation);

return $filesystem;
}

/**
* After class functionality
* @return void
*/
public static function tearDownAfterClass()
{
TestLoggingUtil::getInstance()->clearMockLoggingUtil();
parent::tearDownAfterClass();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\FunctionalTestingFramework\Config\Reader;

use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;

/**
* Filesystem configuration loader. Loads configuration from XML files, split by scopes.
*/
Expand Down Expand Up @@ -144,6 +147,10 @@ protected function readFiles($fileList)
/** @var \Magento\FunctionalTestingFramework\Config\Dom $configMerger */
$configMerger = null;
foreach ($fileList as $key => $content) {
//check if file is empty and continue to next if it is
if (!$this->verifyFileEmpty($content, $fileList->getFilename())) {
continue;
}
try {
if (!$configMerger) {
$configMerger = $this->createConfigMerger($this->domDocumentClass, $content);
Expand Down Expand Up @@ -192,4 +199,25 @@ protected function createConfigMerger($mergerClass, $initialContents)
}
return $result;
}

/**
* Checks if content is empty and logs warning, returns false if file is empty
*
* @param string $content
* @param string $fileName
* @return bool
*/
protected function verifyFileEmpty($content, $fileName)
{
if (empty($content)) {
if (MftfApplicationConfig::getConfig()->verboseEnabled()) {
LoggingUtil::getInstance()->getLogger(Filesystem::class)->warn(
"XML File is empty.",
["File" => $fileName]
);
}
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function readFiles($fileList)
/** @var \Magento\FunctionalTestingFramework\Test\Config\Dom $configMerger */
$configMerger = null;
foreach ($fileList as $key => $content) {
//check if file is empty and continue to next if it is
if (!parent::verifyFileEmpty($content, $fileList->getFilename())) {
continue;
}
try {
if (!$configMerger) {
$configMerger = $this->createConfigMerger(
Expand Down