Skip to content

Commit 17ddf57

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-60201' into troll_pr
2 parents 38360b5 + ea16e9a commit 17ddf57

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ function (&$file) {
6060
*/
6161
public static function getChangedContent($fileName)
6262
{
63+
$data = [];
6364
$extension = self::getFileExtension($fileName);
6465
$fileName = ltrim(str_replace(BP, '', $fileName), DIRECTORY_SEPARATOR);
65-
$changedContent = file_get_contents(BP . sprintf(self::CHANGED_FILES_CONTENT_FILE, $extension));
66-
$data = json_decode($changedContent, true);
66+
$changedFilesContentFile = BP . sprintf(self::CHANGED_FILES_CONTENT_FILE, $extension);
67+
if (file_exists($changedFilesContentFile)) {
68+
$changedContent = file_get_contents($changedFilesContentFile);
69+
$data = json_decode($changedContent, true);
70+
}
6771

6872
return isset($data[$fileName]) ? $data[$fileName] : '';
6973
}

dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,64 @@ public function detect($filePath, $functions)
3636
$result = [];
3737
$regexp = $this->composeRegexp($functions);
3838

39-
if ($regexp) {
40-
$fileContent = \Magento\TestFramework\Utility\ChangedFiles::getChangedContent($filePath);
41-
$matches = preg_grep($regexp, explode("\n", $fileContent));
42-
$file = file($filePath);
43-
if (!empty($matches)) {
44-
foreach ($matches as $line) {
45-
$actualFunctions = [];
46-
foreach ($functions as $function) {
47-
if (false !== strpos($line, $function)) {
48-
$actualFunctions[] = $function;
49-
}
39+
if (!$regexp) {
40+
return $result;
41+
}
42+
43+
$fileContent = \Magento\TestFramework\Utility\ChangedFiles::getChangedContent($filePath);
44+
$file = file($filePath);
45+
46+
return $fileContent
47+
? $this->grepChangedContent($file, $regexp, $functions, $fileContent)
48+
: $this->grepFile($file, $regexp);
49+
}
50+
51+
/**
52+
* Grep only changed content.
53+
*
54+
* @param array $file
55+
* @param string $regexp
56+
* @param string[] $functions
57+
* @param string $fileContent
58+
* @return array
59+
*/
60+
public function grepChangedContent(array $file, $regexp, $functions, $fileContent)
61+
{
62+
$result = [];
63+
$matches = preg_grep($regexp, explode("\n", $fileContent));
64+
if (!empty($matches)) {
65+
foreach ($matches as $line) {
66+
$actualFunctions = [];
67+
foreach ($functions as $function) {
68+
if (false !== strpos($line, $function)) {
69+
$actualFunctions[] = $function;
5070
}
51-
$result[array_search($line . "\n", $file) + 1] = $actualFunctions;
5271
}
72+
$result[array_search($line . "\n", $file) + 1] = $actualFunctions;
5373
}
5474
}
75+
76+
return $result;
77+
}
78+
79+
/**
80+
* Grep File.
81+
*
82+
* @param array $file
83+
* @param string $regexp
84+
* @return array
85+
*/
86+
public function grepFile(array $file, $regexp)
87+
{
88+
$result = [];
89+
array_unshift($file, '');
90+
$lines = preg_grep($regexp, $file);
91+
foreach ($lines as $lineNumber => $line) {
92+
if (preg_match_all($regexp, $line, $matches)) {
93+
$result[$lineNumber] = $matches[1];
94+
}
95+
}
96+
5597
return $result;
5698
}
5799

dev/tests/static/get_github_changes.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ function saveChangedFileContent(GitRepo $repo)
5656
{
5757
$changedFilesContentFileName = BP . Magento\TestFramework\Utility\ChangedFiles::CHANGED_FILES_CONTENT_FILE;
5858
foreach ($repo->getChangedContentFiles() as $key => $changedContentFile) {
59-
file_put_contents(sprintf($changedFilesContentFileName, $key), json_encode($changedContentFile), FILE_APPEND);
59+
$filePath = sprintf($changedFilesContentFileName, $key);
60+
$oldContent = file_exists($filePath) ? file_get_contents($filePath) : '{}';
61+
$oldData = json_decode($oldContent, true);
62+
$data = array_merge($oldData, $changedContentFile);
63+
file_put_contents($filePath, json_encode($data));
6064
}
6165
}
6266

0 commit comments

Comments
 (0)