-
Notifications
You must be signed in to change notification settings - Fork 132
MQE-1676: Static check for unused arguments in action group. #555
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0331f7e
MQE-1676: Add a static-check that ensures action groups do not have u…
soumyau 97f7a37
Merge remote-tracking branch 'origin/develop' into MQE-1676
soumyau 4a4fc38
MQE-1676: Add a static-check that ensures action groups do not have u…
soumyau 58f0da6
MQE-1676: Add a static-check that ensures action groups do not have u…
soumyau cc3601a
MQE-1676: Add a static-check that ensures action groups do not have u…
soumyau 4a306c2
MQE-1676: Add a static-check that ensures action groups do not have u…
soumyau f4b2d95
MQE-1676: Add a static-check that ensures action groups do not have u…
soumyau 4f24696
Merge branch 'develop' into MQE-1676
soumyau d9bea3b
Merge remote-tracking branch 'origin/develop' into MQE-1676
soumyau 731a770
MQE-1676: Add a static-check that ensures action groups do not have u…
soumyau 089b88d
MQE-1676: Add a static-check that ensures action groups do not have u…
soumyau 28fe757
MQE-1676: Add a static-check that ensures action groups do not have u…
soumyau 1c7c6ce
MQE-1676: Add a static-check that ensures action groups do not have u…
soumyau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
src/Magento/FunctionalTestingFramework/StaticCheck/ActionGroupArgumentsCheck.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\FunctionalTestingFramework\StaticCheck; | ||
|
||
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; | ||
use Magento\FunctionalTestingFramework\Exceptions\XmlException; | ||
use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler; | ||
use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Magento\FunctionalTestingFramework\Util\ModuleResolver; | ||
use Symfony\Component\Finder\Finder; | ||
use Exception; | ||
|
||
/** | ||
* Class ActionGroupArgumentsCheck | ||
* @package Magento\FunctionalTestingFramework\StaticCheck | ||
*/ | ||
class ActionGroupArgumentsCheck implements StaticCheckInterface | ||
{ | ||
|
||
const ACTIONGROUP_XML_REGEX_PATTERN = '/<actionGroup\sname=(?: (?!<\/actionGroup>).)*/mxs'; | ||
const ACTIONGROUP_ARGUMENT_REGEX_PATTERN = '/<argument[^\/>]*name="([^"\']*)/mxs'; | ||
const ACTIONGROUP_NAME_REGEX_PATTERN = '/<actionGroup name=["\']([^\'"]*)/'; | ||
|
||
const ERROR_LOG_FILENAME = 'mftf-arguments-checks'; | ||
const ERROR_LOG_MESSAGE = 'MFTF Action Group Unused Arguments Check'; | ||
|
||
/** | ||
* Array containing all errors found after running the execute() function. | ||
* @var array | ||
*/ | ||
private $errors = []; | ||
|
||
/** | ||
* String representing the output summary found after running the execute() function. | ||
* @var string | ||
*/ | ||
private $output; | ||
|
||
/** | ||
* Checks unused arguments in action groups and prints out error to file. | ||
* | ||
* @param InputInterface $input | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function execute(InputInterface $input) | ||
{ | ||
MftfApplicationConfig::create( | ||
true, | ||
MftfApplicationConfig::UNIT_TEST_PHASE, | ||
false, | ||
MftfApplicationConfig::LEVEL_NONE, | ||
true | ||
); | ||
|
||
$allModules = ModuleResolver::getInstance()->getModulesPath(); | ||
|
||
$actionGroupXmlFiles = StaticCheckHelper::buildFileList( | ||
$allModules, | ||
DIRECTORY_SEPARATOR . 'ActionGroup' . DIRECTORY_SEPARATOR | ||
); | ||
|
||
$this->errors = $this->findErrorsInFileSet($actionGroupXmlFiles); | ||
|
||
$this->output = StaticCheckHelper::printErrorsToFile( | ||
$this->errors, | ||
self::ERROR_LOG_FILENAME, | ||
self::ERROR_LOG_MESSAGE | ||
); | ||
} | ||
|
||
/** | ||
* Return array containing all errors found after running the execute() function. | ||
* @return array | ||
*/ | ||
public function getErrors() | ||
{ | ||
return $this->errors; | ||
} | ||
|
||
/** | ||
* Return string of a short human readable result of the check. For example: "No unused arguments found." | ||
* @return string | ||
*/ | ||
public function getOutput() | ||
{ | ||
return $this->output; | ||
} | ||
|
||
/** | ||
* Finds all unused arguments in given set of actionGroup files | ||
* @param Finder $files | ||
* @return array $testErrors | ||
*/ | ||
private function findErrorsInFileSet($files) | ||
{ | ||
$actionGroupErrors = []; | ||
foreach ($files as $filePath) { | ||
$contents = file_get_contents($filePath); | ||
preg_match_all(self::ACTIONGROUP_XML_REGEX_PATTERN, $contents, $actionGroups); | ||
$actionGroupToArguments = $this->buildUnusedArgumentList($actionGroups[0]); | ||
jilu1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$actionGroupErrors += $this->setErrorOutput($actionGroupToArguments, $filePath); | ||
} | ||
return $actionGroupErrors; | ||
} | ||
|
||
/** | ||
* Builds array of action group => unused arguments | ||
* @param array $actionGroups | ||
* @return array $actionGroupToArguments | ||
*/ | ||
private function buildUnusedArgumentList($actionGroups) | ||
{ | ||
$actionGroupToArguments = []; | ||
|
||
foreach ($actionGroups as $actionGroupXml) { | ||
preg_match(self::ACTIONGROUP_NAME_REGEX_PATTERN, $actionGroupXml, $actionGroupName); | ||
$unusedArguments = $this->findUnusedArguments($actionGroupXml); | ||
if (!empty($unusedArguments)) { | ||
$actionGroupToArguments[$actionGroupName[1]] = $unusedArguments; | ||
jilu1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
return $actionGroupToArguments; | ||
} | ||
|
||
/** | ||
* Returns unused arguments in an action group | ||
* @param string $actionGroupXml | ||
* @return array | ||
*/ | ||
private function findUnusedArguments($actionGroupXml) | ||
{ | ||
$unusedArguments = []; | ||
|
||
preg_match_all(self::ACTIONGROUP_ARGUMENT_REGEX_PATTERN, $actionGroupXml, $arguments); | ||
preg_match(self::ACTIONGROUP_NAME_REGEX_PATTERN, $actionGroupXml, $actionGroupName); | ||
try { | ||
$actionGroup = ActionGroupObjectHandler::getInstance()->getObject($actionGroupName[1]); | ||
} catch (XmlException $e) { | ||
} | ||
foreach ($arguments[1] as $argument) { | ||
//pattern to match all argument references | ||
$patterns = [ | ||
'(\{{2}' . $argument . '(\.[a-zA-Z0-9_\[\]\(\).,\'\/ ]+)?}{2})', | ||
'([(,\s\'$$]' . $argument . '(\.[a-zA-Z0-9_$\[\]]+)?[),\s\'])' | ||
]; | ||
// matches entity references | ||
if (preg_match($patterns[0], $actionGroupXml)) { | ||
continue; | ||
} | ||
//matches parametrized references | ||
if (preg_match($patterns[1], $actionGroupXml)) { | ||
continue; | ||
} | ||
//for extending action groups, exclude arguments that are also defined in parent action group | ||
if ($this->isParentActionGroupArgument($argument, $actionGroup)) { | ||
continue; | ||
} | ||
$unusedArguments[] = $argument; | ||
} | ||
return $unusedArguments; | ||
} | ||
|
||
/** | ||
* Checks if the argument is also defined in the parent for extending action groups. | ||
* @param string $argument | ||
* @param ActionGroupObject $actionGroup | ||
* @return boolean | ||
*/ | ||
private function isParentActionGroupArgument($argument, $actionGroup) | ||
{ | ||
$parentActionGroupName = $actionGroup->getParentName(); | ||
if ($parentActionGroupName !== null) { | ||
$parentActionGroup = ActionGroupObjectHandler::getInstance()->getObject($parentActionGroupName); | ||
$parentArguments = $parentActionGroup->getArguments(); | ||
foreach ($parentArguments as $parentArgument) { | ||
if ($argument === $parentArgument->getName()) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Builds and returns error output for violating references | ||
* | ||
* @param array $actionGroupToArguments | ||
* @param string $path | ||
* @return mixed | ||
*/ | ||
private function setErrorOutput($actionGroupToArguments, $path) | ||
{ | ||
$actionGroupErrors = []; | ||
if (!empty($actionGroupToArguments)) { | ||
// Build error output | ||
$errorOutput = "\nFile \"{$path->getRealPath()}\""; | ||
$errorOutput .= "\ncontains action group(s) with unused arguments.\n\t\t"; | ||
foreach ($actionGroupToArguments as $actionGroup => $arguments) { | ||
$errorOutput .= "\n\t {$actionGroup} has unused argument(s): " . implode(", ", $arguments); | ||
} | ||
$actionGroupErrors[$path->getRealPath()][] = $errorOutput; | ||
} | ||
return $actionGroupErrors; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Magento/FunctionalTestingFramework/StaticCheck/StaticCheckHelper.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\FunctionalTestingFramework\StaticCheck; | ||
|
||
use Symfony\Component\Finder\Finder; | ||
|
||
class StaticCheckHelper | ||
{ | ||
/** | ||
* Prints out given errors to file, and returns summary result string | ||
* @param array $errors | ||
* @param string $filename | ||
* @param string $message | ||
* @return string | ||
*/ | ||
public static function printErrorsToFile($errors, $filename, $message) | ||
{ | ||
if (empty($errors)) { | ||
return $message . ": No errors found."; | ||
} | ||
|
||
$outputPath = getcwd() . DIRECTORY_SEPARATOR . $filename . ".txt"; | ||
$fileResource = fopen($outputPath, 'w'); | ||
|
||
soumyau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
foreach ($errors as $test => $error) { | ||
fwrite($fileResource, $error[0] . PHP_EOL); | ||
} | ||
|
||
fclose($fileResource); | ||
$errorCount = count($errors); | ||
$output = $message . ": Errors found across {$errorCount} file(s). Error details output to {$outputPath}"; | ||
|
||
return $output; | ||
} | ||
|
||
/** | ||
* Builds list of all XML files in given modulePaths + path given | ||
* @param array $modulePaths | ||
* @param string $path | ||
* @return Finder | ||
*/ | ||
public static function buildFileList($modulePaths, $path) | ||
{ | ||
$finder = new Finder(); | ||
foreach ($modulePaths as $modulePath) { | ||
if (!realpath($modulePath . $path)) { | ||
continue; | ||
} | ||
$finder->files()->in($modulePath . $path)->name("*.xml"); | ||
} | ||
return $finder->files(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.