(filePath: string, readSource: SourceReader, depth = 0)
| 40 | * `#[path]` and modules declared inside inline blocks are not followed. |
| 41 | */ |
| 42 | export function inCfgTestFile(filePath: string, readSource: SourceReader, depth = 0): boolean { |
| 43 | if (depth > 4 || !filePath.endsWith('.rs')) return false; |
| 44 | const parts = filePath.split('/'); |
| 45 | const base = parts.pop()!; |
| 46 | if (base === 'lib.rs' || base === 'main.rs') return false; |
| 47 | const name = base === 'mod.rs' ? parts.pop() : base.slice(0, -3); |
| 48 | // Only a name Rust can declare is a module (`odd(name.rs` never is), and |
| 49 | // only such a name is ever written into the declaration's pattern. |
| 50 | if (!name || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(name) || parts.length === 0) return false; |
| 51 | const dir = parts.join('/'); |
| 52 | for (const owner of [`${dir}.rs`, `${dir}/mod.rs`, `${dir}/lib.rs`, `${dir}/main.rs`]) { |
| 53 | const source = readSource(owner); |
| 54 | if (source === undefined) continue; |
| 55 | const declared = rustModDeclaration(source, name); |
| 56 | if (declared === 'cfg-test') return true; |
| 57 | if (declared === 'plain' && inCfgTestFile(owner, readSource, depth + 1)) return true; |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | /** How `source` declares the out-of-line module `name` at its top level: under a test `cfg`, plainly, or not at all. */ |
| 63 | function rustModDeclaration(source: string, name: string): 'cfg-test' | 'plain' | undefined { |
no test coverage detected