MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / inCfgTestFile

Function inCfgTestFile

src/graph/test-code.ts:72–90  ·  view source on GitHub ↗
(filePath: string, readSource: SourceReader, depth = 0)

Source from the content-addressed store, hash-verified

70 * `#[path]` and modules declared inside inline blocks are not followed.
71 */
72export function inCfgTestFile(filePath: string, readSource: SourceReader, depth = 0): boolean {
73 if (depth > 4 || !filePath.endsWith('.rs')) return false;
74 const parts = filePath.split('/');
75 const base = parts.pop()!;
76 if (base === 'lib.rs' || base === 'main.rs') return false;
77 const name = base === 'mod.rs' ? parts.pop() : base.slice(0, -3);
78 // Only a name Rust can declare is a module (`odd(name.rs` never is), and
79 // only such a name is ever written into the declaration's pattern.
80 if (!name || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(name) || parts.length === 0) return false;
81 const dir = parts.join('/');
82 for (const owner of [`${dir}.rs`, `${dir}/mod.rs`, `${dir}/lib.rs`, `${dir}/main.rs`]) {
83 const source = readSource(owner);
84 if (source === undefined) continue;
85 const declared = rustModDeclaration(source, name);
86 if (declared === 'cfg-test') return true;
87 if (declared === 'plain' && inCfgTestFile(owner, readSource, depth + 1)) return true;
88 }
89 return false;
90}
91
92/** How `source` declares the out-of-line module `name` at its top level: under a test `cfg`, plainly, or not at all. */
93function rustModDeclaration(source: string, name: string): 'cfg-test' | 'plain' | undefined {

Callers 3

isTestCodeFunction · 0.85

Calls 3

readSourceFunction · 0.85
rustModDeclarationFunction · 0.85
joinMethod · 0.45

Tested by

no test coverage detected