| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { mkdirSync, mkdtempSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { afterEach, beforeEach |
| 6 | |
| 7 | import { walkCommits, type |
| 8 | |
| 9 | describe('walkCommits', () |
| 10 | let root: |
| 11 | let gitDir: |
| 12 | |
| 13 | function git(... |
| 14 | const env = |
| 15 | delete env.GIT_DIR; |
| 16 | return execFileSync('git |
| 17 | cwd: root, |
| 18 | encoding: 'utf8', |
| 19 | env, |
| 20 | }).trim(); |
| 21 | } |
| 22 | |
| 23 | function commit(file |
| 24 | mkdirSync(path.dirname |
| 25 | writeFileSync(path.join |
| 26 | git('add', |
| 27 | git('commit', |
| 28 | return git('rev-parse |
| 29 | } |
| 30 | |
| 31 | async function collect |
| 32 | const records: |
| 33 | for await (const |
| 34 | records.push(record |
| 35 | } |
| 36 | return records; |
| 37 | } |
| 38 | |
| 39 | beforeEach(() => |
| 40 | root = mkdtempSync( |
| 41 | gitDir = path. |
| 42 | execFileSync('git', |
| 43 | git('config', |
| 44 | git('config', |
| 45 | }); |
| 46 | |
| 47 | afterEach(() => |
| 48 | |
| 49 | it('yields commits oldest first with the paths each one touched', |
| 50 | commit('README.md', |
| 51 | commit('src/deep/x.ts', |
| 52 | |
| 53 | const records = |
| 54 | |
| 55 | expect(records.map |
| 56 | expect(records[0] |
| 57 | expect(records[1] |
| 58 | }); |
| 59 | |
| 60 | it('reports the committer timestamp', |
| 61 | commit('README.md', |
| 62 | const [record] |
| 63 | |
| 64 | const seconds = |
| 65 | expect(record.committedAt |
| 66 | expect(record.sha |
| 67 | }); |
| 68 | |
| 69 | it('walks only the commits a range adds', |
| 70 | const base = |
| 71 | commit('src/y.ts', |
| 72 | |
| 73 | const records = |
| 74 | expect(records.map |
| 75 | }); |
| 76 | |
| 77 | it('attributes a merged file to the commit that wrote it, not the merge', |
| 78 | commit('README.md', |
| 79 | git('checkout', |
| 80 | commit('src/z.ts', |
| 81 | git('checkout', |
| 82 | git('merge', |
| 83 | |
| 84 | const records = |
| 85 | const touchedZ = |
| 86 | |
| 87 | expect(touchedZ.map |
| 88 | // the merge is still yielded, it just claims no paths of its own |
| 89 | expect(records.at |
| 90 | }); |
| 91 | |
| 92 | it('keeps subjects intact and survives paths that need quoting', |
| 93 | commit( |
| 94 | 'a file "with quotes".txt', |
| 95 | 'a', |
| 96 | 'subject: with punctuation, and more', |
| 97 | ); |
| 98 | |
| 99 | const [record] |
| 100 | expect(record.subject |
| 101 | expect(record.paths |
| 102 | }); |
| 103 | |
| 104 | it('restricts the walk to a pathspec', |
| 105 | commit('README.md', |
| 106 | commit('src/y.ts', |
| 107 | |
| 108 | const records = |
| 109 | expect(records.map |
| 110 | }); |
| 111 | |
| 112 | it('yields nothing for an empty range', |
| 113 | commit('README.md', |
| 114 | expect(await collect |
| 115 | }); |
| 116 | }); |