| 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 { listBranches } |
| 8 | |
| 9 | describe('listBranches', () |
| 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 | beforeEach(async () |
| 24 | root = mkdtempSync( |
| 25 | gitDir = path. |
| 26 | execFileSync('git', |
| 27 | git('config', |
| 28 | git('config', |
| 29 | }); |
| 30 | |
| 31 | afterEach(() => |
| 32 | |
| 33 | function commit(message |
| 34 | writeFileSync(path.join |
| 35 | git('add', |
| 36 | git('commit', |
| 37 | } |
| 38 | |
| 39 | it('returns nothing for a repository with no commits', |
| 40 | expect(await listBranches |
| 41 | }); |
| 42 | |
| 43 | it('returns short branch names, ordered by name', |
| 44 | commit('first'); |
| 45 | git('branch', |
| 46 | git('branch', |
| 47 | |
| 48 | expect(await listBranches |
| 49 | 'a-branch', |
| 50 | 'feat/contents', |
| 51 | 'main', |
| 52 | ]); |
| 53 | }); |
| 54 | |
| 55 | it('does not mark the checked out branch', |
| 56 | commit('first'); |
| 57 | git('checkout', |
| 58 | |
| 59 | expect(await listBranches |
| 60 | }); |
| 61 | |
| 62 | it('ignores tags and remote-tracking refs', |
| 63 | commit('first'); |
| 64 | git('tag', |
| 65 | git('update-ref', |
| 66 | |
| 67 | expect(await listBranches |
| 68 | }); |
| 69 | |
| 70 | it('names a branch in full when a tag shares its name', |
| 71 | commit('first'); |
| 72 | git('branch', |
| 73 | git('tag', |
| 74 | |
| 75 | expect(await listBranches |
| 76 | }); |
| 77 | }); |