-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
497 additions
and
708 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {createFilter} from 'vite'; | ||
import path from 'path'; | ||
import {fileURLToPath} from 'url'; | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
export const rootFolder = path.join(__dirname, '..', '..', '..'); | ||
|
||
export const filter = createFilter( | ||
['core/lib/**/*', 'angular/lib/src/lib/**/*', 'react/lib/**/*', 'svelte/lib/**/*'], | ||
['node_modules', '**/*.spec.ts', '**/__mocks__/**'], | ||
{ | ||
resolve: rootFolder, | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {transform} from '@babel/core'; | ||
import {dirname} from 'path'; | ||
|
||
// TODO: support for svelte files | ||
const supportedExtensionsRegExp = /\.(tsx?|jsx?)$/; | ||
export const canInstrument = (fileName: string) => !fileName.includes('\x00') && supportedExtensionsRegExp.test(fileName); | ||
|
||
export const instrumentFile = (code: string, filename: string) => { | ||
console.log('Instrumenting for coverage: ', filename); | ||
const result = transform(code, { | ||
filename, | ||
plugins: [ | ||
['@babel/plugin-syntax-decorators', {version: 'legacy'}], | ||
[ | ||
'@babel/plugin-syntax-typescript', | ||
{ | ||
isTSX: filename.endsWith('.tsx'), | ||
}, | ||
], | ||
[ | ||
'babel-plugin-istanbul', | ||
{ | ||
cwd: dirname(filename), | ||
exclude: [], | ||
}, | ||
], | ||
], | ||
}); | ||
if (result?.code) { | ||
return result.code; | ||
} | ||
return code; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import fs from 'fs'; | ||
import util from 'util'; | ||
import {normalize} from 'path'; | ||
import {fileURLToPath} from 'url'; | ||
import {instrumentFile, canInstrument} from './instrument'; | ||
import {filter} from './filter'; | ||
import {intercept} from './interceptSwitch'; | ||
|
||
const postProcessReadFile = (filePath: string | URL, code: string | Buffer) => { | ||
if (typeof filePath === 'object') { | ||
filePath = fileURLToPath(filePath); | ||
} | ||
const normalizedFilePath = normalize(filePath); | ||
if (intercept() && canInstrument(normalizedFilePath) && filter(normalizedFilePath)) { | ||
const isBuffer = Buffer.isBuffer(code); | ||
if (isBuffer) { | ||
code = code.toString('utf8'); | ||
} | ||
try { | ||
code = instrumentFile(code as string, normalizedFilePath); | ||
} catch (error) { | ||
console.log(`Error while instrumenting ${normalizedFilePath}:`, error); | ||
throw error; | ||
} | ||
if (isBuffer) { | ||
code = Buffer.from(code, 'utf8'); | ||
} | ||
} | ||
return code; | ||
}; | ||
|
||
// override readFileSync to provide instrumented files: | ||
const trueReadFileSync = fs.readFileSync; | ||
fs.readFileSync = (...args) => { | ||
return postProcessReadFile(args[0] as string | URL, trueReadFileSync(...args)) as any; | ||
}; | ||
|
||
// override fs.promises.readFile to provide instrumented files: | ||
const truePromisesReadFile = fs.promises.readFile; | ||
fs.promises.readFile = async (...args) => { | ||
return postProcessReadFile(args[0] as string | URL, await truePromisesReadFile(...args)) as any; | ||
}; | ||
|
||
// override readFile to provide instrumented files: | ||
const trueReadFile = fs.readFile; | ||
const newReadFile = (...args: Parameters<typeof trueReadFile>) => { | ||
const cbIndex = args.length - 1; | ||
const trueCb = args[cbIndex] as any; | ||
args[cbIndex] = (err: any, result: any) => { | ||
if (err) { | ||
trueCb(err, result); | ||
} else { | ||
trueCb(err, postProcessReadFile(args[0] as string | URL, result)); | ||
} | ||
}; | ||
trueReadFile(...args); | ||
}; | ||
(newReadFile as any)[util.promisify.custom] = fs.promises.readFile; | ||
fs.readFile = newReadFile as any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const key = '__AGNOS_UI_CODE_COVERAGE_SKIP_INTERCEPT__'; | ||
const global = globalThis as any; | ||
|
||
export const intercept = () => (global[key] ?? 0) === 0; | ||
|
||
export const skipIntercept = async <T>(fn: () => T | Promise<T>): Promise<T> => { | ||
try { | ||
const value = global[key] ?? 0; | ||
global[key] = value + 1; | ||
return await fn(); | ||
} finally { | ||
global[key]--; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module 'nyc'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {promises as fs} from 'fs'; | ||
import path from 'path'; | ||
import {v4 as uuidv4} from 'uuid'; | ||
|
||
export default async (baseDir: string, coverage: string) => { | ||
await fs.writeFile(path.join(baseDir, '.nyc_output', `${uuidv4()}.json`), coverage); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import NYC from 'nyc'; | ||
import {promises as fs} from 'fs'; | ||
import path from 'path'; | ||
import {skipIntercept} from './interceptSwitch'; | ||
import {rootFolder} from './filter'; | ||
|
||
export default async (baseDir: string) => { | ||
console.log('Cleaning up coverage folders...'); | ||
const nycDir = path.join(baseDir, '.nyc_output'); | ||
const reportDir = path.join(baseDir, 'coverage'); | ||
await fs.rm(nycDir, {recursive: true, force: true}); | ||
await fs.mkdir(nycDir); | ||
await fs.rm(reportDir, {recursive: true, force: true}); | ||
await fs.mkdir(reportDir); | ||
console.log('Coverage setup ready !'); | ||
const nycInstance = new NYC({ | ||
cwd: rootFolder, | ||
tempDirectory: nycDir, | ||
reportDir, | ||
reporter: ['lcov', 'json', 'text-summary'], | ||
extension: ['.ts', '.tsx', '.svelte'], | ||
}); | ||
return async () => { | ||
console.log('Saving coverage report...'); | ||
const files = await fs.readdir(nycDir); | ||
if (files.length) { | ||
await skipIntercept(() => nycInstance.report()); | ||
console.log('Coverage report saved !'); | ||
} else { | ||
console.log('No coverage computed.'); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type {Plugin} from 'vite'; | ||
import {canInstrument, instrumentFile} from './instrument'; | ||
import {filter} from './filter'; | ||
|
||
export default (): Plugin => { | ||
return { | ||
name: '@agnos-ui/code-coverage', | ||
enforce: 'pre', | ||
transform(code, id, options) { | ||
if (!filter(id) || !canInstrument(id)) return; | ||
return instrumentFile(code, id); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type {AfterSuiteRunMeta, CoverageProvider, CoverageProviderModule, ReportContext, Vitest} from 'vitest'; | ||
import setup from './setup'; | ||
import reportCoverage from './reportCoverage'; | ||
|
||
const customCoverageProviderModule: CoverageProviderModule = { | ||
async getProvider(): Promise<CoverageProvider> { | ||
let ctx: Vitest | undefined; | ||
let finalizeReport: undefined | (() => Promise<void>); | ||
return { | ||
name: '@agnos-ui/code-coverage', | ||
initialize(newCtx: Vitest) { | ||
ctx = newCtx; | ||
}, | ||
resolveOptions() { | ||
return ctx!.config.coverage; | ||
}, | ||
async clean(clean?: boolean) { | ||
finalizeReport = await setup(ctx!.config.root); | ||
}, | ||
async onAfterSuiteRun({coverage}: AfterSuiteRunMeta) { | ||
if (coverage) { | ||
reportCoverage(ctx!.config.root, JSON.stringify(coverage)); | ||
} | ||
}, | ||
async reportCoverage(reportContext?: ReportContext) { | ||
await finalizeReport?.(); | ||
finalizeReport = undefined; | ||
}, | ||
}; | ||
}, | ||
takeCoverage() { | ||
const coverage = (globalThis as any).__coverage__; | ||
(globalThis as any).__coverage__ = {}; | ||
return coverage; | ||
}, | ||
}; | ||
|
||
export default customCoverageProviderModule; |
Oops, something went wrong.