Skip to content

Commit db50f20

Browse files
committed
MQE-2974
Created covtodb script
1 parent 9b9e4a2 commit db50f20

File tree

4 files changed

+215
-15
lines changed

4 files changed

+215
-15
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "magento/code-coverage-diff-tool",
33
"require": {
4-
"phpunit/phpcov": "8.2.0"
4+
"phpunit/phpcov": "8.2.0",
5+
"sergeytsalkov/meekrodb": "*"
56
}
67
}

composer.lock

Lines changed: 63 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

covToDB.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require_once __DIR__ . '/vendor/autoload.php';
8+
9+
/**
10+
* The main entrypoint. Called at the bottom of this file.
11+
*/
12+
function main($baseCoverageDir, $runID, $testType) {
13+
readCoverageFromFolder($baseCoverageDir, $runID, $testType);
14+
}
15+
16+
/**
17+
* Reads each .cov file in a directory and inserts records into the db define din db.class.php
18+
*
19+
* @param string $coveragePath
20+
* @param string $runID
21+
* @return void
22+
*/
23+
function readCoverageFromFolder($coveragePath, $runID, $testType) {
24+
if (!realpath($coveragePath)) {
25+
printf("No coverage files found in $coveragePath\n");
26+
return;
27+
}
28+
$fileCount = count(scandir($coveragePath));
29+
$currentFile = 0;
30+
printf("Reading coverage...\n");
31+
32+
require_once __DIR__ . DIRECTORY_SEPARATOR . 'db.class.php';
33+
//INPUT RUN IF NOT PRESENT
34+
$run = DB::query("SELECT * FROM CC_RUN WHERE runid=%s", $runID);
35+
if (empty($run)) {
36+
DB::insert("CC_RUN", [
37+
"runid" => $runID
38+
]);
39+
$cachedCCRUNID = DB::insertId();
40+
} else {
41+
$cachedCCRUNID = $run[0]['ccrunid'];
42+
}
43+
unset($run);
44+
45+
//CACHE LOCAL IDS FOR EASY USE
46+
$cachedTests = [];
47+
$tests = DB::query("SELECT * FROM CC_TESTS WHERE ccrunid=%s AND testtype=%s", $cachedCCRUNID, $testType);
48+
foreach ($tests as $test) {
49+
$cachedTests[$test['testname']] = $test['testid'];
50+
}
51+
unset($tests);
52+
$cachedFiles = [];
53+
$files = DB::query("SELECT * FROM CC_FILES WHERE ccrunid=%s", $cachedCCRUNID);
54+
foreach ($files as $file) {
55+
$cachedFiles[$file['filepath']] = $file['fileid'];
56+
}
57+
unset($files);
58+
59+
//ITERATE THROUGH FILES
60+
foreach (scandir($coveragePath) as $file) {
61+
printf("Reading ($currentFile/$fileCount)\r");
62+
$currentFile += 1;
63+
if (pathinfo($file)['extension'] !== 'cov') {
64+
continue;
65+
}
66+
$fileCoverage = readCoverage($coveragePath . DIRECTORY_SEPARATOR . $file);
67+
//TEST NAME INSERT
68+
foreach ($fileCoverage->getTests() as $testname => $content) {
69+
if (isset($cachedTests[$testname])) {
70+
continue;
71+
}
72+
// $existingTest = DB::query(
73+
// "SELECT * FROM CC_TESTS WHERE ccrunid=%s AND testtype=%s AND testname=%s",
74+
// $cachedCCRUNID, $testType, $testname
75+
// );
76+
// if (!empty($existingTest)) {
77+
// continue;
78+
// }
79+
DB::insert("CC_TESTS", [
80+
"testtype" => $testType,
81+
"testname" => $testname,
82+
"ccrunid" => $cachedCCRUNID
83+
]);
84+
$cachedTests[$testname] = DB::insertId();
85+
}
86+
87+
//DO FILE + LINE INSERTS
88+
foreach ($fileCoverage->getData(true)->lineCoverage() as $testFile => $content) {
89+
if (!isset($cachedFiles[$testFile])) {
90+
DB::insert("CC_FILES", [
91+
"filepath" => $testFile,
92+
"ccrunid" => $cachedCCRUNID
93+
]);
94+
$cachedFiles[$testFile] = DB::insertId();
95+
}
96+
// $existingFile = DB::query(
97+
// "SELECT * FROM CC_FILES WHERE ccrunid=%s AND filepath=%s",
98+
// $cachedCCRUNID, $testFile
99+
// );
100+
// if (empty($existingFile)) {
101+
// DB::insert("CC_FILES", [
102+
// "filepath" => $testFile,
103+
// "ccrunid" => $cachedCCRUNID
104+
// ]);
105+
// $cachedFiles[$testFile] = DB::insertId();
106+
// }
107+
foreach ($content as $lineNumber => $tests) {
108+
foreach ($tests as $test) {
109+
$existingLine = DB::query(
110+
"SELECT * FROM CC_LINES WHERE linenumber=%s AND testid=%s AND fileid=%s AND ccrunid=%s",
111+
$lineNumber, $cachedTests[$test], $cachedFiles[$testFile], $cachedCCRUNID
112+
);
113+
if (empty($existingLine)) {
114+
DB::insert("CC_LINES", [
115+
"linenumber" => $lineNumber,
116+
"testid" => $cachedTests[$test],
117+
"fileid" => $cachedFiles[$testFile],
118+
"ccrunid" => $cachedCCRUNID
119+
]);
120+
}
121+
}
122+
}
123+
}
124+
printf("Inserted from file {$file}\n");
125+
}
126+
printf("Inserted from all files in {$coveragePath}\n");
127+
}
128+
129+
/**
130+
* Reads a single .cov file into memory.
131+
*/
132+
function readCoverage($coveragePath) {
133+
if (!is_file($coveragePath)) {
134+
return null;
135+
}
136+
$file = include($coveragePath);
137+
return $file;
138+
}
139+
140+
if (!array_key_exists(3, $argv)) {
141+
printf("This script requires 3 parameters to run:\nInputDirectory RunID TestType");
142+
} else {
143+
main(filter_var($argv[1], FILTER_SANITIZE_STRING),filter_var($argv[2], FILTER_SANITIZE_STRING),filter_var($argv[3], FILTER_SANITIZE_STRING));
144+
}

db.class.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
DB::$host = "localhost";
3+
DB::$port = "3306";
4+
DB::$user = 'root';
5+
DB::$password = '123123q';
6+
DB::$dbName = 'CC_PROTO';

0 commit comments

Comments
 (0)