|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import { TestFixture } from '../test-utils'; |
| 5 | + |
| 6 | +describe('pgpm ls', () => { |
| 7 | + let fixture: TestFixture; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + fixture = new TestFixture(); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + fixture.cleanup(); |
| 15 | + }); |
| 16 | + |
| 17 | + const createWorkspace = (modules: Array<{ |
| 18 | + name: string; |
| 19 | + version: string; |
| 20 | + requires?: string[]; |
| 21 | + }> = []) => { |
| 22 | + const workspaceDir = path.join(fixture.tempDir, 'workspace'); |
| 23 | + fs.mkdirSync(path.join(workspaceDir, 'packages'), { recursive: true }); |
| 24 | + fs.writeFileSync( |
| 25 | + path.join(workspaceDir, 'pgpm.json'), |
| 26 | + JSON.stringify({ packages: ['packages/*'] }, null, 2) |
| 27 | + ); |
| 28 | + |
| 29 | + for (const module of modules) { |
| 30 | + const moduleDir = path.join(workspaceDir, 'packages', module.name); |
| 31 | + fs.mkdirSync(moduleDir, { recursive: true }); |
| 32 | + fs.writeFileSync( |
| 33 | + path.join(moduleDir, 'pgpm.plan'), |
| 34 | + '%syntax-version=1.0.0\n' |
| 35 | + ); |
| 36 | + fs.writeFileSync( |
| 37 | + path.join(moduleDir, `${module.name}.control`), |
| 38 | + [ |
| 39 | + `# ${module.name} extension`, |
| 40 | + `default_version = '${module.version}'`, |
| 41 | + module.requires && module.requires.length > 0 |
| 42 | + ? `requires = '${module.requires.join(',')}'` |
| 43 | + : '' |
| 44 | + ].filter(Boolean).join('\n') + '\n' |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + return workspaceDir; |
| 49 | + }; |
| 50 | + |
| 51 | + const runLs = async (argv: Record<string, unknown>) => { |
| 52 | + const output: string[] = []; |
| 53 | + const write = jest.spyOn(process.stdout, 'write').mockImplementation((chunk: any) => { |
| 54 | + output.push(String(chunk)); |
| 55 | + return true; |
| 56 | + }); |
| 57 | + try { |
| 58 | + await fixture.runCmd(argv); |
| 59 | + } finally { |
| 60 | + write.mockRestore(); |
| 61 | + } |
| 62 | + return output.join(''); |
| 63 | + }; |
| 64 | + |
| 65 | + it('prints module objects as JSON sorted by name', async () => { |
| 66 | + const workspaceDir = createWorkspace([ |
| 67 | + { name: 'b', version: '2.0.0', requires: ['plpgsql'] }, |
| 68 | + { name: 'a', version: '1.0.0' } |
| 69 | + ]); |
| 70 | + |
| 71 | + const output = await runLs({ |
| 72 | + _: ['ls'], |
| 73 | + cwd: workspaceDir, |
| 74 | + json: true |
| 75 | + }); |
| 76 | + |
| 77 | + expect(output).toBe(`[ |
| 78 | + { |
| 79 | + "name": "a", |
| 80 | + "version": "1.0.0", |
| 81 | + "path": "packages/a", |
| 82 | + "requires": [] |
| 83 | + }, |
| 84 | + { |
| 85 | + "name": "b", |
| 86 | + "version": "2.0.0", |
| 87 | + "path": "packages/b", |
| 88 | + "requires": [ |
| 89 | + "plpgsql" |
| 90 | + ] |
| 91 | + } |
| 92 | +] |
| 93 | +`); |
| 94 | + }); |
| 95 | + |
| 96 | + it('prints sorted names as a single-line JSON array', async () => { |
| 97 | + const workspaceDir = createWorkspace([ |
| 98 | + { name: 'b', version: '2.0.0' }, |
| 99 | + { name: 'a', version: '1.0.0' } |
| 100 | + ]); |
| 101 | + |
| 102 | + const output = await runLs({ |
| 103 | + _: ['ls'], |
| 104 | + cwd: workspaceDir, |
| 105 | + names: true, |
| 106 | + json: true |
| 107 | + }); |
| 108 | + |
| 109 | + expect(output).toBe('["a","b"]'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('prints workspace-relative paths as a single-line JSON array', async () => { |
| 113 | + const workspaceDir = createWorkspace([ |
| 114 | + { name: 'b', version: '2.0.0' }, |
| 115 | + { name: 'a', version: '1.0.0' } |
| 116 | + ]); |
| 117 | + |
| 118 | + const output = await runLs({ |
| 119 | + _: ['ls'], |
| 120 | + cwd: workspaceDir, |
| 121 | + paths: true, |
| 122 | + json: true |
| 123 | + }); |
| 124 | + |
| 125 | + expect(output).toBe('["packages/a","packages/b"]'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('prints human-readable module listings', async () => { |
| 129 | + const workspaceDir = createWorkspace([ |
| 130 | + { name: 'b', version: '2.0.0' }, |
| 131 | + { name: 'a', version: '1.0.0' } |
| 132 | + ]); |
| 133 | + |
| 134 | + const output = await runLs({ |
| 135 | + _: ['ls'], |
| 136 | + cwd: workspaceDir |
| 137 | + }); |
| 138 | + |
| 139 | + expect(output).toContain('a'); |
| 140 | + expect(output).toContain('b'); |
| 141 | + expect(output).toContain('1.0.0'); |
| 142 | + expect(output).toContain('packages/a'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('prints an empty JSON array for an empty workspace', async () => { |
| 146 | + const workspaceDir = createWorkspace(); |
| 147 | + |
| 148 | + const output = await runLs({ |
| 149 | + _: ['ls'], |
| 150 | + cwd: workspaceDir, |
| 151 | + names: true, |
| 152 | + json: true |
| 153 | + }); |
| 154 | + |
| 155 | + expect(output).toBe('[]'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('fails outside a workspace', async () => { |
| 159 | + const exit = jest.spyOn(process, 'exit').mockImplementation((code?: string | number) => { |
| 160 | + throw new Error(`process.exit(${code})`); |
| 161 | + }); |
| 162 | + const error = jest.spyOn(console, 'error').mockImplementation(() => undefined); |
| 163 | + |
| 164 | + await expect(fixture.runCmd({ |
| 165 | + _: ['ls'], |
| 166 | + cwd: fixture.tempDir, |
| 167 | + json: true |
| 168 | + })).rejects.toThrow('process.exit(1)'); |
| 169 | + |
| 170 | + expect(error).toHaveBeenCalledWith( |
| 171 | + expect.stringContaining('Not inside a pgpm workspace') |
| 172 | + ); |
| 173 | + |
| 174 | + error.mockRestore(); |
| 175 | + exit.mockRestore(); |
| 176 | + }); |
| 177 | +}); |
0 commit comments