| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { afterEach, beforeEach |
| 6 | |
| 7 | import { fileHunks, listDiffFiles |
| 8 | |
| 9 | describe('listDiffFiles', () |
| 10 | let root: |
| 11 | let gitDir: |
| 12 | let from: |
| 13 | let to: |
| 14 | |
| 15 | beforeEach(() => |
| 16 | root = mkdtempSync( |
| 17 | gitDir = path. |
| 18 | const env = |
| 19 | delete env.GIT_DIR; |
| 20 | const git = |
| 21 | execFileSync('git', |
| 22 | |
| 23 | execFileSync('git', |
| 24 | git('config', |
| 25 | git('config', |
| 26 | |
| 27 | writeFileSync(path.join |
| 28 | writeFileSync(path.join |
| 29 | git('add', |
| 30 | git('commit', |
| 31 | from = git( |
| 32 | |
| 33 | writeFileSync(path.join |
| 34 | rmSync(path.join |
| 35 | writeFileSync(path.join |
| 36 | writeFileSync(path.join |
| 37 | git('add', |
| 38 | git('commit', |
| 39 | to = git( |
| 40 | }); |
| 41 | |
| 42 | afterEach(() => |
| 43 | |
| 44 | it('reports a status and line counts per path', |
| 45 | const files = |
| 46 | const byPath = |
| 47 | |
| 48 | expect(files).toHaveLength |
| 49 | expect(byPath['keep.txt |
| 50 | status: 'M', |
| 51 | additions: 1, |
| 52 | deletions: 0, |
| 53 | binary: false, |
| 54 | }); |
| 55 | expect(byPath['added.txt |
| 56 | expect(byPath['gone.txt |
| 57 | }); |
| 58 | |
| 59 | it('marks a binary file instead of inventing line counts', |
| 60 | const files = |
| 61 | expect(files.find |
| 62 | status: 'A', |
| 63 | additions: 0, |
| 64 | deletions: 0, |
| 65 | binary: true, |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | it('has no merge base for unrelated histories', |
| 70 | const orphan = |
| 71 | 'git', |
| 72 | ['commit-tree', `${to |
| 73 | { |
| 74 | cwd: root, |
| 75 | encoding: 'utf8', |
| 76 | env: { |
| 77 | ...process.env, |
| 78 | GIT_DIR: gitDir, |
| 79 | GIT_AUTHOR_NAME: 'T', |
| 80 | GIT_AUTHOR_EMAIL: 't@t', |
| 81 | GIT_COMMITTER_NAME: 'T', |
| 82 | GIT_COMMITTER_EMAIL: 't@t', |
| 83 | }, |
| 84 | }, |
| 85 | ).trim(); |
| 86 | |
| 87 | expect(await mergeBase |
| 88 | expect(await mergeBase |
| 89 | }); |
| 90 | |
| 91 | it('reads a file’s hunks with both line numbers on every line', |
| 92 | const [hunk] |
| 93 | expect(hunk).toEqual |
| 94 | deletions: [1, 2] |
| 95 | additions: [1, 3] |
| 96 | lines: [ |
| 97 | { kind: ' ', |
| 98 | { kind: ' ', |
| 99 | { kind: '+', |
| 100 | ], |
| 101 | }); |
| 102 | expect(await fileHunks |
| 103 | }); |
| 104 | }); |
| 105 | |
| 106 | describe('parseHunks', () |
| 107 | it('counts a removed line that reads like a file header as part of the hunk', |
| 108 | const [hunk] |
| 109 | '--- a/x\n+++ b/x\n@@ -1,2 +1 @@\n--- not a header\n |
| 110 | ); |
| 111 | expect(hunk.lines |
| 112 | { kind: '-', |
| 113 | { kind: ' ', |
| 114 | ]); |
| 115 | expect(hunk.additions |
| 116 | }); |
| 117 | }); |