| 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 { listTree, orderTreeEntries |
| 8 | |
| 9 | describe('listTree', () |
| 10 | let root: |
| 11 | let gitDir: |
| 12 | |
| 13 | beforeEach(() => |
| 14 | root = mkdtempSync( |
| 15 | gitDir = path. |
| 16 | const env = |
| 17 | delete env.GIT_DIR; |
| 18 | const git = |
| 19 | execFileSync('git', |
| 20 | |
| 21 | execFileSync('git', |
| 22 | git('config', |
| 23 | git('config', |
| 24 | mkdirSync(path.join |
| 25 | writeFileSync(path.join |
| 26 | writeFileSync(path.join |
| 27 | git('add', |
| 28 | git('commit', |
| 29 | }); |
| 30 | |
| 31 | afterEach(() => |
| 32 | |
| 33 | it('lists one level of the root with sizes for blobs', |
| 34 | const entries = |
| 35 | |
| 36 | expect(entries).toEqual |
| 37 | expect.objectContaining({ |
| 38 | type: 'tree', |
| 39 | name: 'src', |
| 40 | path: 'src', |
| 41 | size: null, |
| 42 | }), |
| 43 | expect.objectContaining({ |
| 44 | type: 'blob', |
| 45 | name: 'README.md', |
| 46 | path: 'README.md', |
| 47 | size: 6, |
| 48 | mode: '100644', |
| 49 | }), |
| 50 | ]); |
| 51 | }); |
| 52 | |
| 53 | it('strips the prefix from names but keeps full paths', |
| 54 | const entries = |
| 55 | |
| 56 | expect(entries).toEqual |
| 57 | expect.objectContaining({ |
| 58 | ]); |
| 59 | }); |
| 60 | |
| 61 | /** Git reads a leading ":" as pathspec magic, and ":/" means "from the root". Without :(literal) this listing would quietly return the root instead of the directory that was asked for. */ |
| 62 | it('does not let pathspec magic escape the requested directory', |
| 63 | const entries = |
| 64 | expect(entries).toEqual |
| 65 | }); |
| 66 | |
| 67 | it('treats glob magic as a literal name', |
| 68 | const entries = |
| 69 | gitDir, |
| 70 | ref: 'main', |
| 71 | prefix: ':(glob)**/', |
| 72 | }); |
| 73 | expect(entries).toEqual |
| 74 | }); |
| 75 | |
| 76 | it('returns nothing for a path that is a file rather than a directory', |
| 77 | const entries = |
| 78 | gitDir, |
| 79 | ref: 'main', |
| 80 | prefix: 'README.md/', |
| 81 | }); |
| 82 | expect(entries).toEqual |
| 83 | }); |
| 84 | |
| 85 | it('rejects a ref that does not exist', |
| 86 | await expect( |
| 87 | listTree({ gitDir, |
| 88 | ).rejects.toThrow() |
| 89 | }); |
| 90 | |
| 91 | it('does not treat a ref starting with a dash as an option', |
| 92 | // --end-of-options makes this a bad revision rather than a parsed flag |
| 93 | await expect( |
| 94 | listTree({ gitDir, |
| 95 | ).rejects.toThrow( |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | describe('orderTreeEntries', () |
| 100 | const entry = |
| 101 | ({ name, type |
| 102 | |
| 103 | it('puts directories before files, each group alphabetical', |
| 104 | const ordered = |
| 105 | entry('README.md', |
| 106 | entry('zeta', |
| 107 | entry('alpha.txt', |
| 108 | entry('Alpha', |
| 109 | ]); |
| 110 | |
| 111 | // "alpha.txt" before "README.md": alphabetical, not the byte order that would hoist every capitalized name to the front |
| 112 | expect(ordered.map |
| 113 | 'Alpha', |
| 114 | 'zeta', |
| 115 | 'alpha.txt', |
| 116 | 'README.md', |
| 117 | ]); |
| 118 | }); |
| 119 | |
| 120 | it('groups submodules with directories', |
| 121 | const ordered = |
| 122 | entry('a.txt', |
| 123 | entry('vendor', |
| 124 | ]); |
| 125 | |
| 126 | expect(ordered.map |
| 127 | }); |
| 128 | |
| 129 | it('interleaves case instead of blocking uppercase first', |
| 130 | // byte order would give Alpha, Beta.md, alpha.txt |
| 131 | const ordered = |
| 132 | entry('Beta.md', |
| 133 | entry('alpha.txt', |
| 134 | entry('Alpha', |
| 135 | ]); |
| 136 | |
| 137 | expect(ordered.map |
| 138 | 'Alpha', |
| 139 | 'alpha.txt', |
| 140 | 'Beta.md', |
| 141 | ]); |
| 142 | }); |
| 143 | |
| 144 | it('orders embedded numbers naturally', |
| 145 | const ordered = |
| 146 | entry('file10.ts', |
| 147 | entry('file2.ts', |
| 148 | ]); |
| 149 | |
| 150 | expect(ordered.map |
| 151 | }); |
| 152 | |
| 153 | /** Git stores trees as if their names ended in "/", so "src.d" sorts ahead of "src" in the raw listing. Sorting by name puts that right. */ |
| 154 | it('corrects git own prefix ordering for directories', |
| 155 | const ordered = |
| 156 | entry('src.d', |
| 157 | entry('src', |
| 158 | ]); |
| 159 | |
| 160 | expect(ordered.map |
| 161 | }); |
| 162 | }); |