86 lines
2.9 KiB
JavaScript
86 lines
2.9 KiB
JavaScript
// scripts/ci/lib/test-files.mjs
|
|
// 테스트 파일 판정·계수 공용 헬퍼 — test-metrics / check-test-quality 가 같은 규칙을 공유한다.
|
|
// 규칙을 늘릴 때는 이 파일 한 곳만 고치고, 두 스크립트의 --self-test 를 함께 확인한다.
|
|
|
|
import { execFileSync } from 'node:child_process'
|
|
import { existsSync, readFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
|
|
/** test:hygiene 과 동일한 패턴 (의도적 동일 — 게이트 3개가 같은 파일을 보게 한다). */
|
|
export const TEST_FILE =
|
|
/(?:^|\/)(?:tests?|__tests__|e2e)\/.*\.(?:test|spec)\.[cm]?[jt]sx?$|\.(?:test|spec)\.[cm]?[jt]sx?$/
|
|
export const EXCLUDED_PATH =
|
|
/(?:^|\/)(?:node_modules|dist|build|out|coverage|playwright-report|test-results|scratch)(?:\/|$)/
|
|
|
|
/** 약한 단정 — "값이 있다"만 확인하고 결함을 잡지 못하는 형태. */
|
|
export const WEAK_ASSERTION =
|
|
/\bexpect\s*\([^)]*\)\s*\.\s*(?:not\s*\.\s*)?(?:toBeDefined|toBeTruthy|toBeFalsy|toBeNull)\s*\(/g
|
|
|
|
/** 테스트 안의 직접 시계/난수 호출 (주입·고정 줄은 제외). */
|
|
export const AMBIENT_TIME = /\b(?:Date\.now|Math\.random|performance\.now)\s*\(\s*\)/g
|
|
export const AMBIENT_ALLOW =
|
|
/(?:setSystemTime|useFakeTimers|advanceTimersByTime|advanceTimers|withMockedNow|randomSeed|mkdtemp|vi\.spyOn\()/
|
|
|
|
export function gitLsFiles(cwd = process.cwd()) {
|
|
const raw = execFileSync(
|
|
'git',
|
|
['ls-files', '--cached', '--others', '--exclude-standard'],
|
|
{ cwd, maxBuffer: 1 << 28, encoding: 'utf8' },
|
|
)
|
|
return [
|
|
...new Set(
|
|
raw
|
|
.split('\n')
|
|
.map((line) => line.trim().replaceAll('\\', '/'))
|
|
.filter((rel) => Boolean(rel) && existsSync(join(cwd, rel))),
|
|
),
|
|
]
|
|
}
|
|
|
|
export function isTestFile(path) {
|
|
if (EXCLUDED_PATH.test(path)) return false
|
|
if (path.endsWith('.cs')) return path.startsWith('apps/api-server.Tests/') && /Tests?\.cs$/.test(path)
|
|
if (/\.(?:mjs|js)$/.test(path) && path.includes('__tests__')) return true
|
|
return TEST_FILE.test(path)
|
|
}
|
|
|
|
export function countMatches(text, re) {
|
|
re.lastIndex = 0
|
|
let count = 0
|
|
while (re.exec(text)) count += 1
|
|
return count
|
|
}
|
|
|
|
/** 줄 단위로 세어 주입/고정 줄은 제외한다. */
|
|
export function countAmbient(text) {
|
|
let count = 0
|
|
for (const line of text.split('\n')) {
|
|
if (AMBIENT_ALLOW.test(line)) continue
|
|
AMBIENT_TIME.lastIndex = 0
|
|
while (AMBIENT_TIME.exec(line)) count += 1
|
|
}
|
|
return count
|
|
}
|
|
|
|
export function readRepoFile(path, cwd = process.cwd()) {
|
|
return readFileSync(join(cwd, path), 'utf8')
|
|
}
|
|
|
|
/** test 파일 경로 → { path, weak, ambient } */
|
|
export function scanQuality(cwd = process.cwd()) {
|
|
const rows = []
|
|
for (const path of gitLsFiles(cwd).filter(isTestFile)) {
|
|
let text
|
|
try {
|
|
text = readRepoFile(path, cwd)
|
|
} catch {
|
|
continue
|
|
}
|
|
rows.push({
|
|
path,
|
|
weak: countMatches(text, WEAK_ASSERTION),
|
|
ambient: countAmbient(text),
|
|
})
|
|
}
|
|
return rows
|
|
}
|