mirror of
https://github.com/stablyai/orca.git
synced 2026-11-13 17:37:40 +00:00
* test: remove report inventories and dependency policy replays * test: remove lint rule inventories and release mock replays --------- Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
144 lines
5.3 KiB
JavaScript
144 lines
5.3 KiB
JavaScript
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import path from 'node:path'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { runProcessSync } from '@orca/process-host'
|
|
import { collectBaseLineBlocks, collectAddedLineRanges } from './check-changed-code-quality.mjs'
|
|
|
|
describe('changed-code quality line matching', () => {
|
|
it.each(['edit', 'rename'])('treats bracketed filenames literally after %s', (change) => {
|
|
const repo = mkdtempSync(path.join(tmpdir(), 'changed-code-quality-literal-'))
|
|
try {
|
|
const hooks = `core.hooksPath=${path.join(repo, '.git', 'no-hooks')}`
|
|
const git = (...args) => {
|
|
const result = runProcessSync({
|
|
program: 'git',
|
|
args: ['-c', 'commit.gpgsign=false', '-c', hooks, ...args],
|
|
cwd: repo
|
|
})
|
|
expect(result.code, result.stderr).toBe(0)
|
|
return result.stdout.trim()
|
|
}
|
|
git('init', '--quiet')
|
|
const file = '[cd].ts'
|
|
const source = change === 'rename' ? '[ab].ts' : file
|
|
const content = 'export const first = 1\n\nexport const legacy = 3\n'
|
|
writeFileSync(path.join(repo, source), content)
|
|
writeFileSync(path.join(repo, 'c.ts'), content)
|
|
git('add', '.')
|
|
git('-c', 'user.name=t', '-c', 'user.email=t@example.invalid', 'commit', '-qm', 'base')
|
|
const base = git('rev-parse', 'HEAD')
|
|
if (change === 'rename') {
|
|
git('mv', source, file)
|
|
}
|
|
writeFileSync(path.join(repo, file), content.replace('first = 1', 'first = 2'))
|
|
writeFileSync(path.join(repo, 'c.ts'), content.replace('legacy = 3', 'legacy = 4'))
|
|
|
|
const { rangesByFile } = collectAddedLineRanges(repo, base)
|
|
expect(rangesByFile.get(file)).toEqual([{ start: 1, end: 1 }])
|
|
expect(rangesByFile.get('c.ts')).toEqual([{ start: 3, end: 3 }])
|
|
} finally {
|
|
rmSync(repo, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('moved-code exemption', () => {
|
|
it('reads the base text of a file that was renamed away', () => {
|
|
const repo = mkdtempSync(path.join(tmpdir(), 'changed-code-quality-rename-'))
|
|
try {
|
|
const git = (...args) => {
|
|
const result = runProcessSync({
|
|
program: 'git',
|
|
args: ['-c', 'commit.gpgsign=false', ...args],
|
|
cwd: repo
|
|
})
|
|
expect(result.code, result.stderr).toBe(0)
|
|
return result.stdout.trim()
|
|
}
|
|
git('init', '--quiet')
|
|
writeFileSync(path.join(repo, 'old.ts'), 'const moved = value as Thing\n')
|
|
git('add', 'old.ts')
|
|
git('-c', 'user.name=t', '-c', 'user.email=t@example.invalid', 'commit', '-qm', 'base')
|
|
const base = git('rev-parse', 'HEAD')
|
|
git('mv', 'old.ts', 'new.ts')
|
|
|
|
expect(collectBaseLineBlocks(repo, base)).toEqual([['const moved = value as Thing']])
|
|
} finally {
|
|
rmSync(repo, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('keeps source boundaries across batches, missing files, and multibyte contents', () => {
|
|
const repo = mkdtempSync(path.join(tmpdir(), 'changed-code-quality-batch-'))
|
|
try {
|
|
const git = (...args) => {
|
|
const result = runProcessSync({ program: 'git', args, cwd: repo })
|
|
expect(result.code, result.stderr).toBe(0)
|
|
return result.stdout.trim()
|
|
}
|
|
git('init', '--quiet')
|
|
const files = Array.from({ length: 40 }, (_, index) => `file ${index} é.ts`)
|
|
const sources = files.map((_, index) =>
|
|
index === 0 ? '' : ` const café${index} = '🐳'\r\n\r\n// missing\nlastLine()`
|
|
)
|
|
files.forEach((file, index) => writeFileSync(path.join(repo, file), sources[index]))
|
|
git('add', '--all')
|
|
git(
|
|
'-c',
|
|
'commit.gpgsign=false',
|
|
'-c',
|
|
'user.name=t',
|
|
'-c',
|
|
'user.email=t@example.invalid',
|
|
'commit',
|
|
'-qm',
|
|
'base'
|
|
)
|
|
const base = git('rev-parse', 'HEAD')
|
|
const expected = sources.map((_, index) =>
|
|
index === 0 ? [] : [`const café${index} = '🐳'`, '// missing', 'lastLine()']
|
|
)
|
|
expect(
|
|
collectBaseLineBlocks(repo, base, ['missing.ts', ...files, 'also-missing.ts'])
|
|
).toEqual(expected)
|
|
expect(collectBaseLineBlocks(repo, base, [])).toEqual([])
|
|
expect(collectBaseLineBlocks(repo, base, ['missing.ts'])).toEqual([])
|
|
expect(collectBaseLineBlocks(path.join(repo, 'absent'), base, files)).toEqual([])
|
|
} finally {
|
|
rmSync(repo, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it.skipIf(process.platform === 'win32')('reads filenames containing newlines', () => {
|
|
const repo = mkdtempSync(path.join(tmpdir(), 'changed-code-quality-newline-'))
|
|
try {
|
|
const git = (...args) => {
|
|
const result = runProcessSync({ program: 'git', args, cwd: repo })
|
|
expect(result.code, result.stderr).toBe(0)
|
|
return result.stdout.trim()
|
|
}
|
|
git('init', '--quiet')
|
|
const file = 'line\nbreak.ts'
|
|
writeFileSync(path.join(repo, file), 'const kept = true\n')
|
|
git('add', '--all')
|
|
git(
|
|
'-c',
|
|
'commit.gpgsign=false',
|
|
'-c',
|
|
'user.name=t',
|
|
'-c',
|
|
'user.email=t@example.invalid',
|
|
'commit',
|
|
'-qm',
|
|
'base'
|
|
)
|
|
expect(collectBaseLineBlocks(repo, git('rev-parse', 'HEAD'), [file])).toEqual([
|
|
['const kept = true']
|
|
])
|
|
} finally {
|
|
rmSync(repo, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|