| 1 | import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | import { execFileSync } |
| 3 | import { mkdtempSync, rmSync |
| 4 | import { tmpdir } |
| 5 | import path from 'node:path |
| 6 | import { afterEach, beforeEach |
| 7 | |
| 8 | import { BranchesService } |
| 9 | |
| 10 | describe('BranchesService', () |
| 11 | let service: |
| 12 | let root: |
| 13 | let gitDir: |
| 14 | |
| 15 | function git(... |
| 16 | const env = |
| 17 | delete env.GIT_DIR; |
| 18 | return execFileSync('git |
| 19 | cwd: root, |
| 20 | encoding: 'utf8', |
| 21 | env, |
| 22 | }).trim(); |
| 23 | } |
| 24 | |
| 25 | beforeEach(async () |
| 26 | const module: |
| 27 | providers: [BranchesService], |
| 28 | }).compile(); |
| 29 | service = module. |
| 30 | |
| 31 | root = mkdtempSync( |
| 32 | gitDir = path. |
| 33 | execFileSync('git', |
| 34 | git('config', |
| 35 | git('config', |
| 36 | }); |
| 37 | |
| 38 | afterEach(() => |
| 39 | |
| 40 | function commit(message |
| 41 | writeFileSync(path.join |
| 42 | git('add', |
| 43 | git('commit', |
| 44 | } |
| 45 | |
| 46 | it('returns nothing for a repository with no commits', |
| 47 | expect(await service |
| 48 | }); |
| 49 | |
| 50 | it('returns short branch names, ordered by name', |
| 51 | commit('first'); |
| 52 | git('branch', |
| 53 | git('branch', |
| 54 | |
| 55 | expect(await service |
| 56 | 'a-branch', |
| 57 | 'feat/contents', |
| 58 | 'main', |
| 59 | ]); |
| 60 | }); |
| 61 | |
| 62 | it('does not mark the checked out branch', |
| 63 | commit('first'); |
| 64 | git('checkout', |
| 65 | |
| 66 | expect(await service |
| 67 | }); |
| 68 | |
| 69 | it('ignores tags and remote-tracking refs', |
| 70 | commit('first'); |
| 71 | git('tag', |
| 72 | git('update-ref', |
| 73 | |
| 74 | expect(await service |
| 75 | }); |
| 76 | |
| 77 | it('names a branch in full when a tag shares its name', |
| 78 | commit('first'); |
| 79 | git('branch', |
| 80 | git('tag', |
| 81 | |
| 82 | expect(await service |
| 83 | }); |
| 84 | }); |