Skip to content

Commit b031141

Browse files
committed
MAGETWO-50037: Travis CI Builds Give False Positive Results for Static Tests
- moved get github changes to ce. - changed location of changed files
1 parent 67caf84 commit b031141

File tree

8 files changed

+351
-26
lines changed

8 files changed

+351
-26
lines changed

.travis.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ env:
1111
- TEST_SUITE=integration_part_1
1212
- TEST_SUITE=integration_part_2
1313
- TEST_SUITE=integration_integrity
14-
- TEST_SUITE=static_phpcs
15-
- TEST_SUITE=static_annotation
14+
- TEST_SUITE=static
1615
cache:
1716
apt: true
1817
directories:
@@ -21,9 +20,9 @@ cache:
2120
matrix:
2221
exclude:
2322
- php: 5.6
24-
env: TEST_SUITE=static_phpcs
25-
- php: 5.6
26-
env: TEST_SUITE=static_annotation
23+
env: TEST_SUITE=static
24+
- php: 7.0
25+
env: TEST_SUITE=static
2726
before_install:
2827
- sudo apt-get update -qq
2928
- sudo apt-get install -y -qq postfix
@@ -68,6 +67,4 @@ script:
6867
# Integration integrity tests
6968
- sh -c "if [ '$TEST_SUITE' = 'integration_integrity' ]; then cd dev/tests/integration/; ./../../../vendor/bin/phpunit -c phpunit.xml.dist testsuite/Magento/Test/Integrity; fi"
7069
# Static tests [Code Style]
71-
- sh -c "if [ '$TEST_SUITE' = 'static_phpcs' ]; then cd dev/tests/static; ./../../../vendor/bin/phpunit -c phpunit.xml.dist --filter 'Magento\\\\Test\\\\Php\\\\LiveCodeTest::testCodeStyle'; fi"
72-
# Static tests [Code Style]
73-
- sh -c "if [ '$TEST_SUITE' = 'static_annotation' ]; then cd dev/tests/static; ./../../../vendor/bin/phpunit -c phpunit.xml.dist --filter 'Magento\\\\Test\\\\Php\\\\LiveCodeTest::testAnnotationStandard'; fi"
70+
- sh -c "if [ '$TEST_SUITE' = 'static' ]; then cd dev/tests/static/; php get_github_changes.php --output-file='$TRAVIS_BUILD_DIR/dev/tests/static/testsuite/Magento/Test/_files/changed_files_ce.txt' --base-path='$TRAVIS_BUILD_DIR' --repo='https://github.com/magento/magento2.git'; cd dev/tests/static; ./../../../vendor/bin/phpunit -c phpunit.xml.dist --filter 'Magento\\\\Test\\\\Php\\\\LiveCodeTest'; fi"

dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CodeMessDetector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function run(array $whiteList)
6161
$commandLineArguments = [
6262
'run_file_mock', //emulate script name in console arguments
6363
implode(',', $whiteList),
64-
'xml', //report format
64+
'text', //report format
6565
$this->rulesetFile,
6666
'--reportfile',
6767
$this->reportFile,

dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CodeSniffer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ public function run(array $whiteList)
8888
$settings['files'] = $whiteList;
8989
$settings['standard'] = [$this->rulesetDir];
9090
$settings['extensions'] = $this->extensions;
91-
$settings['reportFile'] = $this->reportFile;
9291
$settings['warningSeverity'] = 0;
93-
$settings['reports']['checkstyle'] = null;
92+
$settings['reports']['full'] = $this->reportFile;
9493

9594
$this->wrapper->setValues($settings);
9695

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
<?php
2+
3+
/**
4+
* Script to get changes between feature branch and the mainline
5+
*
6+
* @category dev
7+
* @package build
8+
* Copyright © 2015 Magento. All rights reserved.
9+
* See COPYING.txt for license details.
10+
*/
11+
12+
// @codingStandardsIgnoreFile
13+
14+
define(
15+
'USAGE',
16+
<<<USAGE
17+
php -f get_github_changes.php --
18+
--output-file="<output_file>"
19+
--base-path="<base_path>"
20+
--repo="<main_repo>"
21+
[--file-formats="<comma_separated_list_of_formats>"]
22+
23+
USAGE
24+
);
25+
26+
$options = getopt('', ['output-file:', 'base-path:', 'repo:', 'file-formats:']);
27+
28+
$requiredOptions = ['output-file', 'base-path', 'repo'];
29+
if (!validateInput($options, $requiredOptions)) {
30+
echo USAGE;
31+
exit(1);
32+
}
33+
34+
$fileFormats = explode(',', isset($options['file-formats']) ? $options['file-formats'] : 'php');
35+
36+
$mainline = 'mainline_' . (string)rand(0, 9999);
37+
$repo = getRepo($options, $mainline);
38+
$changes = retrieveChangesAcrossForks($mainline, $repo);
39+
$changedFiles = getChangedFiles($changes, $fileFormats);
40+
generateChangedFilesList($options['output-file'], $changedFiles);
41+
cleanup($repo, $mainline);
42+
43+
/**
44+
* Generates a file containing changed files
45+
*
46+
* @param string $outputFile
47+
* @param array $changedFiles
48+
* @return void
49+
*/
50+
function generateChangedFilesList($outputFile, $changedFiles)
51+
{
52+
$changedFilesList = fopen($outputFile, 'w');
53+
foreach ($changedFiles as $file) {
54+
fwrite($changedFilesList, $file . PHP_EOL);
55+
}
56+
fclose($changedFilesList);
57+
}
58+
59+
/**
60+
* Gets list of changed files
61+
*
62+
* @param array $changes
63+
* @param array $fileFormats
64+
* @return array
65+
*/
66+
function getChangedFiles($changes, $fileFormats)
67+
{
68+
$files = [];
69+
foreach ($changes as $fileName) {
70+
foreach ($fileFormats as $format) {
71+
$isFileFormat = strpos($fileName, '.' . $format);
72+
if ($isFileFormat) {
73+
$files[] = $fileName;
74+
}
75+
}
76+
}
77+
78+
return $files;
79+
}
80+
81+
/**
82+
* Retrieves changes across forks
83+
*
84+
* @param array $options
85+
* @return array
86+
* @throws Exception
87+
*/
88+
function getRepo($options, $mainline)
89+
{
90+
$repo = new GitRepo($options['base-path']);
91+
$repo->addRemote($mainline, $options['repo']);
92+
$repo->fetch($mainline);
93+
return $repo;
94+
}
95+
96+
/**
97+
* @param $repo
98+
* @return array
99+
*/
100+
function retrieveChangesAcrossForks($mainline, $repo)
101+
{
102+
return $repo->compareChanges($mainline, 'develop');
103+
}
104+
105+
/**
106+
* Deletes temporary "base" repo
107+
*
108+
* @param GitRepo $repo
109+
* @param string $repo
110+
*/
111+
function cleanup($repo, $mainline)
112+
{
113+
$repo->removeRemote($mainline);
114+
}
115+
116+
/**
117+
* Validates input options based on required options
118+
*
119+
* @param array $options
120+
* @param array $requiredOptions
121+
* @return bool
122+
*/
123+
function validateInput(array $options, array $requiredOptions)
124+
{
125+
foreach ($requiredOptions as $requiredOption) {
126+
if (!isset($options[$requiredOption]) || empty($options[$requiredOption])) {
127+
return false;
128+
}
129+
}
130+
return true;
131+
}
132+
133+
134+
class GitRepo
135+
{
136+
/**
137+
* Absolute path to git project
138+
*
139+
* @var string
140+
*/
141+
private $workTree;
142+
143+
/**
144+
* @var array
145+
*/
146+
private $remoteList = [];
147+
148+
/**
149+
* @param string $workTree absolute path to git project
150+
*/
151+
public function __construct($workTree)
152+
{
153+
if (empty($workTree) || !is_dir($workTree)) {
154+
throw new UnexpectedValueException('Working tree should be a valid path to directory');
155+
}
156+
$this->workTree = $workTree;
157+
}
158+
159+
/**
160+
* Adds remote
161+
*
162+
* @param string $alias
163+
* @param string $url
164+
*/
165+
public function addRemote($alias, $url)
166+
{
167+
if (isset($this->remoteList[$alias])) {
168+
return;
169+
}
170+
$this->remoteList[$alias] = $url;
171+
172+
$this->call(sprintf('remote add %s %s', $alias, $url));
173+
}
174+
175+
/**
176+
* Remove remote
177+
*
178+
* @param string $alias
179+
*/
180+
public function removeRemote($alias)
181+
{
182+
if (isset($this->remoteList[$alias])) {
183+
$this->call(sprintf('remote remove %s', $alias));
184+
}
185+
}
186+
187+
/**
188+
* Fetches remote
189+
*
190+
* @param string $remoteAlias
191+
*/
192+
public function fetch($remoteAlias)
193+
{
194+
if (!isset($this->remoteList[$remoteAlias])) {
195+
throw new LogicException('Alias is not defined');
196+
}
197+
198+
$this->call(sprintf('fetch %s', $remoteAlias));
199+
}
200+
201+
/**
202+
* Returns files changes between branch
203+
*
204+
* @param string $remoteAlias
205+
* @param string $remoteBranch
206+
* @return array
207+
*/
208+
public function compareChanges($remoteAlias, $remoteBranch)
209+
{
210+
if (!isset($this->remoteList[$remoteAlias])) {
211+
throw new LogicException('Alias is not defined');
212+
}
213+
214+
$result = $this->call(sprintf('log %s/%s..HEAD --name-status --oneline', $remoteAlias, $remoteBranch));
215+
216+
return is_array($result)
217+
? $this->filterChangedBackFiles(
218+
$this->filterChangedFiles($result),
219+
$remoteAlias,
220+
$remoteBranch
221+
)
222+
: [];
223+
}
224+
225+
/**
226+
* Filters git cli output for changed files
227+
*
228+
* @param array $changes
229+
* @return array
230+
*/
231+
protected function filterChangedFiles(array $changes)
232+
{
233+
$changedFilesMasks = [
234+
'M' => "M\t",
235+
'A' => "A\t"
236+
];
237+
$filteredChanges = [];
238+
foreach ($changes as $fileName) {
239+
foreach ($changedFilesMasks as $mask) {
240+
if (strpos($fileName, $mask) === 0) {
241+
$fileName = str_replace($mask, '', $fileName);
242+
$fileName = trim($fileName);
243+
if (!in_array($fileName, $filteredChanges) && is_file($this->workTree . '/' . $fileName)) {
244+
$filteredChanges[] = $fileName;
245+
}
246+
break;
247+
}
248+
}
249+
}
250+
return $filteredChanges;
251+
}
252+
253+
/**
254+
* Makes a diff of file for specified remote/branch and filters only those have real changes
255+
*
256+
* @param array $changes
257+
* @param string $remoteAlias
258+
* @param string $remoteBranch
259+
* @return array
260+
*/
261+
protected function filterChangedBackFiles(array $changes, $remoteAlias, $remoteBranch)
262+
{
263+
$filteredChanges = [];
264+
foreach ($changes as $fileName) {
265+
$result = $this->call(sprintf(
266+
'diff HEAD %s/%s -- %s', $remoteAlias, $remoteBranch, $this->workTree .'/'. $fileName)
267+
);
268+
if ($result) {
269+
$filteredChanges[] = $fileName;
270+
}
271+
}
272+
273+
return $filteredChanges;
274+
}
275+
276+
/**
277+
* Makes call ro git cli
278+
*
279+
* @param string $command
280+
* @return mixed
281+
*/
282+
private function call($command)
283+
{
284+
$gitCmd = sprintf(
285+
'git --git-dir %s --work-tree %s',
286+
escapeshellarg("{$this->workTree}/.git"),
287+
escapeshellarg($this->workTree)
288+
);
289+
$tmp = sprintf('%s %s', $gitCmd, $command);
290+
exec(escapeshellcmd($tmp), $output);
291+
return $output;
292+
}
293+
}

dev/tests/static/testsuite/Magento/Test/Legacy/ObsoleteCodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected static function _padRow($row, $hasScope)
109109
public function testPhpFiles()
110110
{
111111
$invoker = new AggregateInvoker($this);
112-
$changedFiles = ChangedFiles::getPhpFiles(__DIR__ . '/_files/changed_files*');
112+
$changedFiles = ChangedFiles::getPhpFiles(__DIR__ . '/../_files/changed_files*');
113113
$blacklistFiles = $this->getBlacklistFiles();
114114
foreach ($blacklistFiles as $blacklistFile) {
115115
unset($changedFiles[BP . $blacklistFile]);

dev/tests/static/testsuite/Magento/Test/Legacy/RestrictedCodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected static function _readList($file)
7474
public function testPhpFiles()
7575
{
7676
$invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
77-
$testFiles = \Magento\TestFramework\Utility\ChangedFiles::getPhpFiles(__DIR__ . '/_files/changed_files*');
77+
$testFiles = \Magento\TestFramework\Utility\ChangedFiles::getPhpFiles(__DIR__ . '/../_files/changed_files*');
7878
foreach (self::$_fixtureFiles as $fixtureFile) {
7979
if (array_key_exists(BP . $fixtureFile, $testFiles)) {
8080
unset($testFiles[BP . $fixtureFile]);

0 commit comments

Comments
 (0)