Skip to content

Commit 6d02f30

Browse files
dependabot[bot]vrubezhny
authored andcommitted
[Draft] Migrate from istanbul to nyc #3025
Currently not working example of using `nyc`: changed for test coverage in `unit` tests: ``` npm run test:coverage ``` The source instrumenting is done by `npm run test:instrument` (invoked as part of `test:coverage` command) The `istanbul` dependecy is not removed yet due to not changed: - test/coverage.ts - test/ui/cluster-ui-test.ts - test/ui/public-ui-test.js depending on `istanbul` API, which is subject to change after `nyc` finally provides a valuable coverage report. Fixes: #3025 Signed-off-by: Victor Rubezhny <[email protected]>
1 parent 1fd0186 commit 6d02f30

File tree

10 files changed

+1426
-266
lines changed

10 files changed

+1426
-266
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ dist/
1010
public/
1111
test-resources/
1212
.DS_Store
13-
13+
.nyc_output/

build/run-instrument.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE file in the project root for license information.
4+
*-----------------------------------------------------------------------------------------------*/
5+
6+
import { mkdirSync, readFileSync, statSync, writeFileSync } from 'fs';
7+
import * as glob from 'glob';
8+
import { createInstrumenter } from 'istanbul-lib-instrument';
9+
import * as path from 'path';
10+
11+
/* eslint-disable no-console */
12+
console.log('Instrumenting files...');
13+
14+
const instrumenter = createInstrumenter({
15+
coverageVariable: '__coverage__',
16+
embedSource: true, // Embed the source code into the instrumented file
17+
});
18+
19+
// Find the Extension root directory
20+
console.log(`Script File: ${__filename}`);
21+
console.log(`Script Dir: ${__dirname}`);
22+
const extDir = path.resolve(__dirname, __dirname.endsWith('out/build') ? '../../' : '../');
23+
console.log(`Extension Root dir: ${extDir}`);
24+
25+
// Define the source and output directories
26+
const sourceDir = path.resolve(extDir, 'out/src-orig'); // Original source directory
27+
const outputDir = path.resolve(extDir, 'out/src'); // Instrumented files will be saved here
28+
29+
console.log('Source dir: ', sourceDir);
30+
console.log('Output dir: ', outputDir);
31+
32+
// Ensure the output directory exists
33+
mkdirSync(outputDir, { recursive: true });
34+
35+
// Use glob to match all JavaScript files in the source directory
36+
const files = glob.sync(path.join(sourceDir, '**/*'));
37+
// Loop over each file and instrument it
38+
39+
files.forEach((file) => {
40+
process.stdout.write(`Instrumenting: ${file}... `);
41+
42+
// Get the relative path of the file and write the instrumented code to the output directory
43+
const relativePath = path.relative(sourceDir, file);
44+
const outputPath = path.join(outputDir, relativePath);
45+
46+
// Ensure the directory exists
47+
mkdirSync(path.dirname(outputPath), { recursive: true });
48+
49+
const stats = statSync(file);
50+
if (stats.isDirectory()) {
51+
console.log('Directory');
52+
} else if (stats.isFile()) {
53+
const code = readFileSync(file, 'utf8');
54+
const isJsFile = /\.js$/i.test(file);
55+
const instrumentedCode = isJsFile ? instrumenter.instrumentSync(code, file) : code;
56+
57+
58+
// Write the instrumented file
59+
writeFileSync(outputPath, instrumentedCode, 'utf8');
60+
console.log(isJsFile ? 'Done' : 'Skipped');
61+
}
62+
});
63+
64+
console.log('Files have been instrumented.');

build/run-tests.ts

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import * as etest from '@vscode/test-electron';
77
import * as path from 'path';
8+
import * as configData from '../package.json';
89

910
/**
1011
* Run mocha tests from project's tests folder.
@@ -13,25 +14,93 @@ import * as path from 'path';
1314
*/
1415
async function main(): Promise<void> {
1516
const [, , tests, extension = ''] = process.argv;
16-
const extensionRootPath = path.resolve(__dirname, '../../');
17+
const extensionRootPath = path.resolve(__dirname, __dirname.endsWith('out/build') ? '../../' : '../');
1718
const extensionDevelopmentPath = path.resolve(extensionRootPath, extension);
1819
const extensionTestsPath = path.resolve(extensionRootPath, 'out', 'test', tests);
1920
const integrationWorkspacePath = path.resolve(extensionRootPath, 'test', 'fixtures', 'components', 'components.code-workspace');
2021
const unitTestWorkspacePath = path.resolve(extensionRootPath, 'test', 'fixtures', 'components', 'empty.code-workspace');
21-
try {
22-
await etest.runTests({
23-
extensionDevelopmentPath,
24-
extensionTestsPath,
25-
launchArgs: [
26-
tests === 'integration' ? integrationWorkspacePath : unitTestWorkspacePath,
27-
'--disable-workspace-trust',
28-
],
29-
});
30-
} catch (err) {
31-
// eslint-disable-next-line no-console
32-
console.error(`Failed to run tests: ${err}`);
33-
process.exit(1);
22+
23+
/* eslint-disable no-console */
24+
console.info(`${__filename}: Started`);
25+
26+
console.info(`${path.basename(__filename)}: __dirname: ${__dirname}`);
27+
console.info(`${path.basename(__filename)}: extensionRootPath: ${extensionRootPath}`);
28+
console.info(`${path.basename(__filename)}: extensionDevelopmentPath: ${extensionDevelopmentPath}`);
29+
console.info(`${path.basename(__filename)}: extensionTestsPath: ${extensionTestsPath}`);
30+
console.info(`${path.basename(__filename)}: integrationWorkspacePath: ${integrationWorkspacePath}`);
31+
console.info(`${path.basename(__filename)}: unitTestWorkspacePath: ${unitTestWorkspacePath}`);
32+
33+
console.info(`${path.basename(__filename)}: NYC config vales:`);
34+
console.log('NYC include paths:', path.resolve('../src/**/*'));
35+
console.log('NYC report-dir:', process.env.NYC_REPORT_DIR || 'coverage (default)');
36+
console.log('NYC temp-dir:', process.env.NYC_TEMP_DIR || '.nyc_output (default)');
37+
38+
39+
/* eslint-disable no-console */
40+
if (process.env.COVERAGE) {
41+
console.log(`Running nyc as part of the ${tests} tests execution...`)
42+
// const { instrument } = require('istanbul-lib-instrument');
43+
// const __instrumenter = new Instrumenter();
44+
45+
const nyc = require('nyc');
46+
// const nycConfig = {
47+
// cwd: path.resolve(__dirname),
48+
// require: ['ts-node/register'],
49+
// extension: ['.ts'],
50+
// reporter: ['lcov', 'text-summary'],
51+
// };
52+
const nycConfig = configData.nyc;
53+
const nycInstance = new nyc(nycConfig);
54+
55+
nycInstance.wrap();
56+
57+
// // // Now run your tests with the Electron runner
58+
// // void (async () => {
59+
// // try {
60+
// // const result = await etest.runTests({
61+
// // extensionDevelopmentPath,
62+
// // extensionTestsPath,
63+
// // launchArgs: [
64+
// // tests === 'integration' ? integrationWorkspacePath : unitTestWorkspacePath,
65+
// // '--disable-workspace-trust',
66+
// // ],
67+
// // });
68+
69+
// // console.error(`Run tests spawn result: ${result}`);
70+
// // process.exit(result);
71+
// // } catch (err) {
72+
// // console.error(`Failed to run tests: ${err}`);
73+
// // process.exit(1);
74+
// // }
75+
// // })();
76+
} else {
77+
console.log(`Running the ${tests} tests...`)
3478
}
79+
try {
80+
const result = await etest.runTests({
81+
extensionDevelopmentPath,
82+
extensionTestsPath,
83+
launchArgs: [
84+
tests === 'integration' ? integrationWorkspacePath : unitTestWorkspacePath,
85+
'--disable-workspace-trust',
86+
],
87+
});
88+
89+
// eslint-disable-next-line no-console
90+
console.error(`Run tests spawn result: ${result}`);
91+
process.exit(result);
92+
} catch (err) {
93+
// eslint-disable-next-line no-console
94+
console.error(`Failed to run tests: ${err}`);
95+
process.exit(1);
96+
}
97+
// }
3598
}
3699

37-
void main();
100+
// void main();
101+
102+
main().catch((err) => {
103+
// eslint-disable-next-line no-console
104+
console.error('Failed to run tests:', err);
105+
process.exit(1);
106+
});

eslint.config.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ export default [
143143
'vars': 'local',
144144
'args': 'none', // Check args after they're used
145145
'caughtErrors': 'none', // Disable checking for caught errors
146+
'varsIgnorePattern': '^_',
147+
"argsIgnorePattern": "^_",
146148
}
147149
],
148150
'@typescript-eslint/no-explicit-any': 'warn',
@@ -173,9 +175,10 @@ export default [
173175
'out',
174176
'.vscode-test',
175177
'.yarn',
178+
'.*',
176179
'images',
177180
'doc/images',
178-
'__coverage__',
181+
'.nyc_output',
179182
'coverage',
180183
'public/dist',
181184
'*.min.js',

0 commit comments

Comments
 (0)