Skip to content

Commit

Permalink
feat: API coverage tool
Browse files Browse the repository at this point in the history
  • Loading branch information
decahedron1 committed Aug 31, 2024
1 parent 3072279 commit e31720d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
},
"rust-analyzer.cachePriming.enable": true,
"rust-analyzer.diagnostics.experimental.enable": true,
"rust-analyzer.showUnlinkedFileNotification": false
"rust-analyzer.showUnlinkedFileNotification": false,
"deno.enablePaths": [
"tools/api-coverage.ts"
]
}
54 changes: 54 additions & 0 deletions tools/api-coverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { dirname, join } from 'jsr:@std/[email protected]';
import { walk } from 'jsr:@std/[email protected]';

const PROJECT_ROOT = dirname(import.meta.dirname!);
const DECODER = new TextDecoder('utf-8');
const SYMBOL_DEF_REGEX = /pub\s+([A-Za-z_][A-Za-z0-9_]+):/;
const SYMBOL_USAGE_REGEX = /ortsys!\[\s*(?:unsafe\s+)?([A-Za-z_][A-Za-z0-9_]+)/gm;

const IGNORED_SYMBOLS = new Set<string>([
'CreateEnv', // we will always create an env with a custom logger for integration w/ tracing
]);

const sysSymbols = new Set<string>();
const sysFile = await Deno.readFile(join(PROJECT_ROOT, 'ort-sys', 'src', 'lib.rs'));
let isInOrtApi = false;
for (const line of DECODER.decode(sysFile).split('\n')) {
if (line === 'pub struct OrtApi {') {
isInOrtApi = true;
continue;
}

if (isInOrtApi) {
if (line === '}') {
isInOrtApi = false;
continue;
}

const trimmedLine = line.trimStart();
if (SYMBOL_DEF_REGEX.test(trimmedLine)) {
const [ _, symbol ] = trimmedLine.match(SYMBOL_DEF_REGEX)!;
sysSymbols.add(symbol);
}
}
}

const usedSymbols = new Set<string>();
for await (const sourceFile of walk(join(PROJECT_ROOT, 'src'))) {
if (sourceFile.isDirectory) {
continue;
}

const contents = DECODER.decode(await Deno.readFile(sourceFile.path));
for (const [ _, symbol ] of contents.matchAll(SYMBOL_USAGE_REGEX)) {
usedSymbols.add(symbol);
}
}

const unusedSymbols = sysSymbols
.difference(usedSymbols)
.difference(IGNORED_SYMBOLS);
for (const symbol of unusedSymbols) {
console.log(`%c\t${symbol}`, 'color: red');
}
console.log(`%cCoverage: ${usedSymbols.size}/${sysSymbols.size} (${((usedSymbols.size / sysSymbols.size) * 100).toFixed(2)}%)`, 'color: green; font-weight: bold');

0 comments on commit e31720d

Please sign in to comment.