Skip to content

Commit e949bdb

Browse files
author
Volodymyr Kublytskyi
committed
#13434: Strict type static test should be preformed only for added files.
1 parent af7737c commit e949bdb

File tree

4 files changed

+177
-48
lines changed

4 files changed

+177
-48
lines changed

app/code/Magento/ImportExport/Model/Import/SampleFileProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\ImportExport\Model\Import;
79

810
use Magento\Framework\Exception\NoSuchEntityException;

dev/tests/static/get_github_changes.php

Lines changed: 122 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@
4242
$changedFiles = getChangedFiles($changes, $fileExtensions);
4343
generateChangedFilesList($options['output-file'], $changedFiles);
4444
saveChangedFileContent($repo);
45+
46+
$additions = retrieveNewFilesAcrossForks($mainline, $repo, $options['branch']);
47+
$addedFiles = getChangedFiles($additions, $fileExtensions);
48+
$additionsFile = pathinfo($options['output-file']);
49+
$additionsFile = $additionsFile['dirname']
50+
. DIRECTORY_SEPARATOR
51+
. $additionsFile['filename']
52+
. '.added.'
53+
. $additionsFile['extension'];
54+
generateChangedFilesList($additionsFile, $addedFiles);
55+
4556
cleanup($repo, $mainline);
4657

4758
/**
@@ -143,7 +154,18 @@ function getRepo($options, $mainline)
143154
*/
144155
function retrieveChangesAcrossForks($mainline, GitRepo $repo, $branchName)
145156
{
146-
return $repo->compareChanges($mainline, $branchName);
157+
return $repo->compareChanges($mainline, $branchName, GitRepo::CHANGE_TYPE_ALL);
158+
}
159+
160+
/**
161+
* @param string $mainline
162+
* @param GitRepo $repo
163+
* @param string $branchName
164+
* @return array
165+
*/
166+
function retrieveNewFilesAcrossForks($mainline, GitRepo $repo, $branchName)
167+
{
168+
return $repo->compareChanges($mainline, $branchName, GitRepo::CHANGE_TYPE_ADDED);
147169
}
148170

149171
/**
@@ -178,6 +200,10 @@ function validateInput(array $options, array $requiredOptions)
178200
class GitRepo
179201
// @codingStandardsIgnoreEnd
180202
{
203+
const CHANGE_TYPE_ADDED = 1;
204+
const CHANGE_TYPE_MODIFIED = 2;
205+
const CHANGE_TYPE_ALL = 3;
206+
181207
/**
182208
* Absolute path to git project
183209
*
@@ -274,9 +300,10 @@ public function getBranches($source = '--all')
274300
*
275301
* @param string $remoteAlias
276302
* @param string $remoteBranch
303+
* @param int $changesType
277304
* @return array
278305
*/
279-
public function compareChanges($remoteAlias, $remoteBranch)
306+
public function compareChanges($remoteAlias, $remoteBranch, $changesType = self::CHANGE_TYPE_ALL)
280307
{
281308
if (!isset($this->remoteList[$remoteAlias])) {
282309
throw new LogicException('Alias "' . $remoteAlias . '" is not defined');
@@ -288,7 +315,8 @@ public function compareChanges($remoteAlias, $remoteBranch)
288315
? $this->filterChangedFiles(
289316
$result,
290317
$remoteAlias,
291-
$remoteBranch
318+
$remoteBranch,
319+
$changesType
292320
)
293321
: [];
294322
}
@@ -299,50 +327,113 @@ public function compareChanges($remoteAlias, $remoteBranch)
299327
* @param array $changes
300328
* @param string $remoteAlias
301329
* @param string $remoteBranch
330+
* @param int $changesType
302331
* @return array
303332
*/
304-
protected function filterChangedFiles(array $changes, $remoteAlias, $remoteBranch)
305-
{
333+
protected function filterChangedFiles(
334+
array $changes,
335+
$remoteAlias,
336+
$remoteBranch,
337+
$changesType = self::CHANGE_TYPE_ALL
338+
) {
306339
$countScannedFiles = 0;
307-
$changedFilesMasks = [
308-
'M' => "M\t",
309-
'A' => "A\t"
310-
];
340+
$changedFilesMasks = $this->buildChangedFilesMask($changesType);
311341
$filteredChanges = [];
312342
foreach ($changes as $fileName) {
313343
$countScannedFiles++;
314344
if (($countScannedFiles % 5000) == 0) {
315345
echo $countScannedFiles . " files scanned so far\n";
316346
}
317-
foreach ($changedFilesMasks as $mask) {
318-
if (strpos($fileName, $mask) === 0) {
319-
$fileName = str_replace($mask, '', $fileName);
320-
$fileName = trim($fileName);
321-
if (!in_array($fileName, $filteredChanges) && is_file($this->workTree . '/' . $fileName)) {
322-
$result = $this->call(
323-
sprintf(
324-
'diff HEAD %s/%s -- %s',
325-
$remoteAlias,
326-
$remoteBranch,
327-
$this->workTree . '/' . $fileName
328-
)
329-
);
330-
if ($result) {
331-
if (!(isset($this->changedContentFiles[$fileName]))) {
332-
$this->setChangedContentFile($result, $fileName);
333-
}
334-
$filteredChanges[] = $fileName;
335-
}
336-
}
337-
break;
338-
}
347+
348+
$changeTypeMask = $this->detectChangeTypeMask($fileName, $changedFilesMasks);
349+
if (null === $changeTypeMask) {
350+
continue;
339351
}
352+
353+
$fileName = trim(substr($fileName, strlen($changeTypeMask)));
354+
if (in_array($fileName, $filteredChanges)) {
355+
continue;
356+
}
357+
358+
$fileChanges = $this->getFileChangeDetails($fileName, $remoteAlias, $remoteBranch);
359+
if (empty($fileChanges)) {
360+
continue;
361+
}
362+
363+
if (!(isset($this->changedContentFiles[$fileName]))) {
364+
$this->setChangedContentFile($fileChanges, $fileName);
365+
}
366+
$filteredChanges[] = $fileName;
340367
}
341368
echo $countScannedFiles . " files scanned\n";
342369

343370
return $filteredChanges;
344371
}
345372

373+
/**
374+
* Build mask of git diff report
375+
*
376+
* @param int $changesType
377+
* @return array
378+
*/
379+
private function buildChangedFilesMask(int $changesType): array
380+
{
381+
$changedFilesMasks = [];
382+
foreach ([
383+
self::CHANGE_TYPE_ADDED => "A\t",
384+
self::CHANGE_TYPE_MODIFIED => "M\t",
385+
] as $changeType => $changedFilesMask) {
386+
if ($changeType & $changesType) {
387+
$changedFilesMasks[] = $changedFilesMask;
388+
}
389+
}
390+
return $changedFilesMasks;
391+
}
392+
393+
/**
394+
* Find one of the allowed modification mask returned by git diff.
395+
* Example of change record: "A path/to/added_file"
396+
*
397+
* @param string $changeRecord
398+
* @param array $allowedMasks
399+
* @return string|null
400+
*/
401+
private function detectChangeTypeMask(string $changeRecord, array $allowedMasks)
402+
{
403+
foreach ($allowedMasks as $mask) {
404+
if (strpos($changeRecord, $mask) === 0) {
405+
return $mask;
406+
}
407+
}
408+
return null;
409+
}
410+
411+
/**
412+
* Read detailed information about changes in a file
413+
*
414+
* @param string $fileName
415+
* @param string $remoteAlias
416+
* @param string $remoteBranch
417+
* @return array
418+
*/
419+
private function getFileChangeDetails(string $fileName, string $remoteAlias, string $remoteBranch): array
420+
{
421+
if (!is_file($this->workTree . '/' . $fileName)) {
422+
return [];
423+
}
424+
425+
$result = $this->call(
426+
sprintf(
427+
'diff HEAD %s/%s -- %s',
428+
$remoteAlias,
429+
$remoteBranch,
430+
$this->workTree . '/' . $fileName
431+
)
432+
);
433+
434+
return $result;
435+
}
436+
346437
/**
347438
* Set changed content for file.
348439
*

dev/tests/static/testsuite/Magento/Test/Php/LiveCodeTest.php

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,74 @@ public static function getWhitelist(
107107
*/
108108
private static function getChangedFilesList($changedFilesBaseDir)
109109
{
110-
$changedFiles = [];
110+
return self::getFilesFromListFile(
111+
$changedFilesBaseDir,
112+
'changed_files*',
113+
function () {
114+
// if no list files, probably, this is the dev environment
115+
@exec('git diff --name-only', $changedFiles);
116+
@exec('git diff --cached --name-only', $addedFiles);
117+
$changedFiles = array_unique(array_merge($changedFiles, $addedFiles));
118+
return $changedFiles;
119+
}
120+
);
121+
}
122+
123+
/**
124+
* This method loads list of added files.
125+
*
126+
* @param string $changedFilesBaseDir
127+
* @return string[]
128+
*/
129+
private static function getAddedFilesList($changedFilesBaseDir)
130+
{
131+
return self::getFilesFromListFile(
132+
$changedFilesBaseDir,
133+
'changed_files*.added.*',
134+
function () {
135+
// if no list files, probably, this is the dev environment
136+
@exec('git diff --cached --name-only', $addedFiles);
137+
return $addedFiles;
138+
}
139+
);
140+
}
111141

112-
$globFilesListPattern = ($changedFilesBaseDir ?: self::getChangedFilesBaseDir()) . '/_files/changed_files*';
142+
/**
143+
* Read files from generated lists.
144+
*
145+
* @param string $listsBaseDir
146+
* @param string $listFilePattern
147+
* @param callable $noListCallback
148+
* @return string[]
149+
*/
150+
private static function getFilesFromListFile($listsBaseDir, $listFilePattern, $noListCallback)
151+
{
152+
$filesDefinedInList = [];
153+
154+
$globFilesListPattern = ($listsBaseDir ?: self::getChangedFilesBaseDir())
155+
. '/_files/' . $listFilePattern;
113156
$listFiles = glob($globFilesListPattern);
114157
if (count($listFiles)) {
115158
foreach ($listFiles as $listFile) {
116-
$changedFiles = array_merge(
117-
$changedFiles,
159+
$filesDefinedInList = array_merge(
160+
$filesDefinedInList,
118161
file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
119162
);
120163
}
121164
} else {
122-
// if no list files, probably, this is the dev environment
123-
@exec('git diff --name-only', $changedFiles);
165+
$filesDefinedInList = call_user_func($noListCallback);
124166
}
125167

126168
array_walk(
127-
$changedFiles,
169+
$filesDefinedInList,
128170
function (&$file) {
129171
$file = BP . '/' . $file;
130172
}
131173
);
132174

133-
return $changedFiles;
175+
$filesDefinedInList = array_values(array_unique($filesDefinedInList));
176+
177+
return $filesDefinedInList;
134178
}
135179

136180
/**
@@ -279,7 +323,7 @@ public function testCopyPaste()
279323
*/
280324
public function testStrictTypes()
281325
{
282-
$changedFiles = self::getChangedFilesList('');
326+
$changedFiles = self::getAddedFilesList('');
283327

284328
$componentRegistrar = new ComponentRegistrar();
285329
$directoriesToCheck = $componentRegistrar->getPaths(ComponentRegistrar::MODULE);
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
11
# Format: <componentType=module|library|theme|language|*> <componentName> <globPattern> or simply <globPattern>
2-
3-
app/code/Magento/CatalogImportExport/Model/Import/Product.php
4-
app/code/Magento/ImportExport/Controller/Adminhtml/Import/Download.php
5-
app/code/Magento/ImportExport/Model/Import/SampleFileProvider.php
6-
app/code/Magento/CatalogInventory/Model/StockManagement.php
7-
app/code/Magento/CatalogInventory/Observer/ItemsForReindex.php
8-
app/code/Magento/CatalogInventory/Observer/SubtractQuoteInventoryObserver.php
9-
app/code/Magento/Store/Api/Data/WebsiteInterface.php

0 commit comments

Comments
 (0)