Skip to content

Commit 9952c02

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-60201' into troll_pr
2 parents d3b981f + 05c2f2f commit 9952c02

File tree

3 files changed

+110
-8
lines changed

3 files changed

+110
-8
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © 2016 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\TestFramework\Utility;
78

89
use Magento\Framework\App\Utility\Files;
@@ -15,6 +16,11 @@
1516
*/
1617
class ChangedFiles
1718
{
19+
/**
20+
* File path with changed files content.
21+
*/
22+
const CHANGED_FILES_CONTENT_FILE = '/dev/tests/static/testsuite/Magento/Test/_files/changed_%s_files_content.json';
23+
1824
/**
1925
* Returns array of PHP-files, that use or declare Magento application classes and Magento libs
2026
*
@@ -45,4 +51,32 @@ function (&$file) {
4551

4652
return $phpFiles;
4753
}
54+
55+
/**
56+
* Get changed content.
57+
*
58+
* @param string $fileName
59+
* @return string
60+
*/
61+
public static function getChangedContent($fileName)
62+
{
63+
$extension = self::getFileExtension($fileName);
64+
$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);
67+
68+
return isset($data[$fileName]) ? $data[$fileName] : '';
69+
}
70+
71+
/**
72+
* Get file extension.
73+
*
74+
* @param string $fileName
75+
* @return string
76+
*/
77+
public static function getFileExtension($fileName)
78+
{
79+
$fileInfo = pathinfo($fileName);
80+
return isset($fileInfo['extension']) ? $fileInfo['extension'] : 'unknown';
81+
}
4882
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ public function detect($filePath, $functions)
3535
{
3636
$result = [];
3737
$regexp = $this->composeRegexp($functions);
38+
3839
if ($regexp) {
40+
$fileContent = \Magento\TestFramework\Utility\ChangedFiles::getChangedContent($filePath);
41+
$matches = preg_grep($regexp, explode("\n", $fileContent));
3942
$file = file($filePath);
40-
array_unshift($file, '');
41-
$lines = preg_grep(
42-
$regexp,
43-
$file
44-
);
45-
foreach ($lines as $lineNumber => $line) {
46-
if (preg_match_all($regexp, $line, $matches)) {
47-
$result[$lineNumber] = $matches[1];
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+
}
50+
}
51+
$result[array_search($line . "\n", $file) + 1] = $actualFunctions;
4852
}
4953
}
5054
}

dev/tests/static/get_github_changes.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,32 @@
3434

3535
$fileExtensions = explode(',', isset($options['file-extensions']) ? $options['file-extensions'] : 'php');
3636

37+
include_once __DIR__ . '/framework/autoload.php';
38+
3739
$mainline = 'mainline_' . (string)rand(0, 9999);
3840
$repo = getRepo($options, $mainline);
3941
$branches = $repo->getBranches('--remotes');
4042
generateBranchesList($options['output-file'], $branches, $options['branch']);
4143
$changes = retrieveChangesAcrossForks($mainline, $repo, $options['branch']);
4244
$changedFiles = getChangedFiles($changes, $fileExtensions);
4345
generateChangedFilesList($options['output-file'], $changedFiles);
46+
saveChangedFileContent($repo);
4447
cleanup($repo, $mainline);
4548

49+
/**
50+
* Save changed file content.
51+
*
52+
* @param GitRepo $repo
53+
* @return void
54+
*/
55+
function saveChangedFileContent(GitRepo $repo)
56+
{
57+
$changedFilesContentFileName = BP . Magento\TestFramework\Utility\ChangedFiles::CHANGED_FILES_CONTENT_FILE;
58+
foreach ($repo->getChangedContentFiles() as $key => $changedContentFile) {
59+
file_put_contents(sprintf($changedFilesContentFileName, $key), json_encode($changedContentFile), FILE_APPEND);
60+
}
61+
}
62+
4663
/**
4764
* Generates a file containing changed files
4865
*
@@ -170,6 +187,18 @@ class GitRepo
170187
*/
171188
private $remoteList = [];
172189

190+
/**
191+
* Array of changed content files.
192+
*
193+
* Example:
194+
* 'extension' =>
195+
* 'path_to_file/filename' => 'Content that was edited',
196+
* 'path_to_file/filename2' => 'Content that was edited',
197+
*
198+
* @var array
199+
*/
200+
private $changedContentFiles = [];
201+
173202
/**
174203
* @param string $workTree absolute path to git project
175204
*/
@@ -285,6 +314,9 @@ protected function filterChangedFiles(array $changes, $remoteAlias, $remoteBranc
285314
'diff HEAD %s/%s -- %s', $remoteAlias, $remoteBranch, $this->workTree . '/' . $fileName)
286315
);
287316
if ($result) {
317+
if (!(isset($this->changedContentFiles[$fileName]))) {
318+
$this->setChangedContentFile($result, $fileName);
319+
}
288320
$filteredChanges[] = $fileName;
289321
}
290322
}
@@ -295,6 +327,38 @@ protected function filterChangedFiles(array $changes, $remoteAlias, $remoteBranc
295327
return $filteredChanges;
296328
}
297329

330+
/**
331+
* Set changed content for file.
332+
*
333+
* @param array $content
334+
* @param string $fileName
335+
* @return void
336+
*/
337+
private function setChangedContentFile(array $content, $fileName)
338+
{
339+
$changedContent = '';
340+
$extension = Magento\TestFramework\Utility\ChangedFiles::getFileExtension($fileName);
341+
342+
foreach ($content as $item) {
343+
if (strpos($item, '---') !== 0 && strpos($item, '-') === 0 && $line = ltrim($item, '-')) {
344+
$changedContent .= $line . "\n";
345+
}
346+
}
347+
if ($changedContent !== '') {
348+
$this->changedContentFiles[$extension][$fileName] = $changedContent;
349+
}
350+
}
351+
352+
/**
353+
* Get changed content files collection.
354+
*
355+
* @return array
356+
*/
357+
public function getChangedContentFiles()
358+
{
359+
return $this->changedContentFiles;
360+
}
361+
298362
/**
299363
* Makes call ro git cli
300364
*

0 commit comments

Comments
 (0)