Skip to content

Commit 41b64cd

Browse files
committed
AC-9497: Static test to notify developers when required GraphQL API changes are not included in a PR
1 parent 435c2f2 commit 41b64cd

File tree

2 files changed

+116
-75
lines changed

2 files changed

+116
-75
lines changed

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

Lines changed: 30 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -60,93 +60,48 @@ public function testCodeStyle(): void
6060

6161
/**
6262
* Test if there is corresponding GraphQL module change for each magento core modules
63+
*
64+
* @return void
65+
* @throws \Magento\Framework\Exception\LocalizedException
6366
*/
6467
public function testCorrespondingGraphQlChangeExists(): void
6568
{
66-
$changedModules = self::getChangedCoreModules();
69+
$changedModules = PHPCodeTest::getChangedCoreModules(self::$changeCheckDir);
6770

6871
// Check if for each module change, a graphQL module change happened
69-
foreach ($changedModules as $changedModule) {
72+
foreach ($changedModules as $module => $fileStat) {
73+
74+
if (str_ends_with($module, 'GraphQl')) {
75+
continue;
76+
}
7077

71-
// get the git diff status of the module files
72-
$fileStat = self::getGitDiff(self::$changeCheckDir . '/' . $changedModule);
73-
$fileChanged = $fileStat['insertions'] >= 5 || $fileStat['deletions'] >= 5;
78+
$fileChanged = $fileStat['filesChanged'] ||
79+
$fileStat['insertions'] ||
80+
$fileStat['deletions'] ||
81+
$fileStat['paramsChanged'];
7482

7583
// check if there is a reasonable change happened in the module
7684
if ($fileChanged) {
77-
// get the git diff status of the graphQL module files
78-
$graphQlFileStat = self::getGitDiff(self::$changeCheckDir . '/' . $changedModule . 'GraphQl');
79-
80-
// assert if there is change in graphql module
81-
$this->assertTrue(
82-
$graphQlFileStat['insertions'] >= 1 || $graphQlFileStat['deletions'] >= 1,
83-
$changedModule. "'s corresponding GraphQl module change is missing"
85+
$this->assertArrayHasKey(
86+
$module . 'GraphQl',
87+
$changedModules,
88+
$module . "'s corresponding GraphQl module change is missing"
8489
);
85-
}
86-
}
87-
}
88-
89-
/**
90-
* returns a multi array with the list of core and graphql modules names
91-
*
92-
* @return array
93-
* @throws \Magento\Framework\Exception\LocalizedException
94-
*/
95-
private static function getChangedCoreModules(): array
96-
{
97-
$whitelistFiles = PHPCodeTest::getWhitelist(['php', 'graphqls'], '', '', '/_files/whitelist/graphql.txt');
98-
99-
$changedModules = [];
100-
foreach ($whitelistFiles as $whitelistFile) {
101-
$fileName = substr($whitelistFile, strlen(self::$changeCheckDir));
102-
$changedModule = explode('/', $fileName);
10390

104-
$isGraphQlModule = str_ends_with($changedModule[1], 'GraphQl');
105-
$isGraphQlModuleExists = file_exists(self::$changeCheckDir . '/' . $changedModule[1] . 'GraphQl');
106-
107-
if (!$isGraphQlModule && $isGraphQlModuleExists &&
108-
(
109-
in_array($changedModule[2], ["Controller", "Model", "Block"]) ||
110-
(($changedModule[2] == "Ui") && in_array($changedModule[3], ["Component", "DataProvider"]))
111-
)
112-
) {
113-
$changedModules[] = $changedModule[1];
91+
if(isset($changedModules[$module . 'GraphQl'])) {
92+
93+
// assert if there is change in graphql module
94+
$this->assertTrue(
95+
(
96+
$changedModules[$module . 'GraphQl']['filesChanged'] ||
97+
$changedModules[$module . 'GraphQl']['insertions'] ||
98+
$changedModules[$module . 'GraphQl']['deletions'] ||
99+
$changedModules[$module . 'GraphQl']['paramsChanged']
100+
),
101+
$module . "'s corresponding GraphQl module change is missing"
102+
);
103+
}
114104
}
115105
}
116-
117-
return array_unique($changedModules);
118-
}
119-
120-
/**
121-
* @param string $directory
122-
* @return array
123-
* @throws \Magento\Framework\Exception\LocalizedException
124-
*/
125-
private static function getGitDiff($directory = ''): array
126-
{
127-
$shell = new \Magento\Framework\Shell(
128-
new \Magento\Framework\Shell\CommandRenderer()
129-
);
130-
131-
$fileStat = explode(
132-
PHP_EOL,
133-
$shell->execute('git diff --stat ' . $directory)
134-
);
135-
136-
$insertions = 0;
137-
$deletions = 0;
138-
$fileChanges = 0;
139-
if (array_key_exists(3, $fileStat)) {
140-
list($fileChanges, $insertions, $deletions) = explode(",", $fileStat[3]);
141-
$fileChanges = intval($fileChanges);
142-
$insertions = intval($insertions);
143-
$deletions = intval($deletions);
144-
}
145-
146-
return [
147-
'fileChanges' => $fileChanges,
148-
'insertions' => $insertions,
149-
'deletions' => $deletions
150-
];
151106
}
152107
}

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,92 @@ private static function getChangedFilesBaseDir()
6767
return __DIR__ . '/..';
6868
}
6969

70+
/**
71+
* returns a multi array with the list of modules with corresponding changes as
72+
* no. of files changed, insertions and deletions
73+
*
74+
* @param string $changeCheckDir
75+
* @return array
76+
* @throws \Magento\Framework\Exception\LocalizedException
77+
*/
78+
public static function getChangedCoreModules(string $changeCheckDir = ''): array
79+
{
80+
$whitelistFiles = self::getWhitelist(['php', 'graphqls'], '', '', '/_files/whitelist/graphql.txt');
81+
82+
$affectedModules = [];
83+
foreach ($whitelistFiles as $whitelistFile) {
84+
$fileName = substr($whitelistFile, strlen($changeCheckDir));
85+
$changedModule = explode('/', $fileName);
86+
87+
$isGraphQlModule = str_ends_with($changedModule[1], 'GraphQl');
88+
$isGraphQlModuleExists = file_exists($changeCheckDir . '/' . $changedModule[1] . 'GraphQl');
89+
90+
if ((!$isGraphQlModule && $isGraphQlModuleExists &&
91+
(
92+
in_array($changedModule[2], ["Controller", "Model", "Block"]) ||
93+
(($changedModule[2] == "Ui") && in_array($changedModule[3], ["Component", "DataProvider"]))
94+
)
95+
) || ($isGraphQlModule)) {
96+
$gitDiffUnifiedStat = self::getGitDiffUnifiedStat($whitelistFile);
97+
if (isset($affectedModules[$changedModule[1]])) {
98+
$affectedModules[$changedModule[1]]['filesChanged'] += $gitDiffUnifiedStat['filesChanged'];
99+
$affectedModules[$changedModule[1]]['insertions'] += $gitDiffUnifiedStat['insertions'];
100+
$affectedModules[$changedModule[1]]['deletions'] += $gitDiffUnifiedStat['deletions'];
101+
$affectedModules[$changedModule[1]]['paramsChanged'] += $gitDiffUnifiedStat['paramsChanged'];
102+
} else {
103+
$affectedModules[$changedModule[1]] = $gitDiffUnifiedStat;
104+
}
105+
}
106+
}
107+
return $affectedModules;
108+
}
109+
110+
/**
111+
* Returns the git stats of the file like
112+
* insertions, deletions and param change if any
113+
*
114+
* @param string $filename
115+
* @return array
116+
* @throws \Magento\Framework\Exception\LocalizedException
117+
*/
118+
private static function getGitDiffUnifiedStat(string $filename = ''): array
119+
{
120+
$shell = new \Magento\Framework\Shell(
121+
new \Magento\Framework\Shell\CommandRenderer()
122+
);
123+
124+
$paramChange = explode(
125+
PHP_EOL,
126+
$shell->execute('git diff --stat --unified=0 ' . $filename)
127+
);
128+
129+
$fileStatus = array_values(array_filter($paramChange, function($value) {
130+
return strpos($value, '(+)') || strpos($value, '(-)');
131+
}, ARRAY_FILTER_USE_BOTH));
132+
$paramChange = array_filter($paramChange, function($value) {
133+
return ((str_starts_with(trim($value), '+')) ||
134+
(str_starts_with(trim($value), '-'))) &&
135+
(strpos($value, '@param'));
136+
}, ARRAY_FILTER_USE_BOTH);
137+
138+
$insertions = 0;
139+
$deletions = 0;
140+
$filesChanged = 0;
141+
if ($fileStatus) {
142+
$fileChanges = explode(",", $fileStatus[0]);
143+
$filesChanged = (isset($fileChanges[0])) ? intval($fileChanges[0]) : 0;
144+
$insertions = (isset($fileChanges[1])) ? intval($fileChanges[1]) : 0;
145+
$deletions = (isset($fileChanges[2])) ? intval($fileChanges[2]) : 0;
146+
}
147+
148+
return [
149+
'filesChanged' => $filesChanged,
150+
'insertions' => $insertions,
151+
'deletions' => $deletions,
152+
'paramsChanged' => sizeof($paramChange)
153+
];
154+
}
155+
70156
/**
71157
* Returns whitelist based on blacklist and git changed files
72158
*

0 commit comments

Comments
 (0)