|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { createHash } from 'node:crypto'; |
| 3 | +import { mkdtemp, readFile, rm } from 'node:fs/promises'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { installerSource, publishInstaller } from '../scripts/publish-installer.mjs'; |
| 7 | + |
| 8 | +const originalSource = { ...installerSource }; |
| 9 | +const directories: string[] = []; |
| 10 | +const script = '#!/bin/sh\nprintf "installer fixture\\n"\n'; |
| 11 | + |
| 12 | +async function outputDirectory() { |
| 13 | + const directory = await mkdtemp(path.join(tmpdir(), 'documentdb-installer-')); |
| 14 | + directories.push(directory); |
| 15 | + return directory; |
| 16 | +} |
| 17 | + |
| 18 | +afterEach(async () => { |
| 19 | + Object.assign(installerSource, originalSource); |
| 20 | + vi.unstubAllGlobals(); |
| 21 | + await Promise.all(directories.splice(0).map((directory) => rm(directory, { recursive: true }))); |
| 22 | +}); |
| 23 | + |
| 24 | +describe('published installer', () => { |
| 25 | + it('pins a release tag and checksum', () => { |
| 26 | + expect(installerSource.release).toMatch(/^v\d/); |
| 27 | + expect(installerSource.sha256).toMatch(/^[a-f0-9]{64}$/); |
| 28 | + }); |
| 29 | + |
| 30 | + it('publishes the verified bytes without changing the execution barrier', async () => { |
| 31 | + installerSource.release = 'v9.9-0'; |
| 32 | + installerSource.sha256 = createHash('sha256').update(script).digest('hex'); |
| 33 | + const download = vi.fn().mockResolvedValue(new Response(script)); |
| 34 | + vi.stubGlobal('fetch', download); |
| 35 | + const directory = await outputDirectory(); |
| 36 | + await publishInstaller(directory); |
| 37 | + expect(await readFile(path.join(directory, 'install.sh'), 'utf8')).toBe(script); |
| 38 | + expect(download.mock.calls[0][0]).toBe( |
| 39 | + 'https://github.com/documentdb/documentdb/releases/download/v9.9-0/install.sh', |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + it.each(['HTTP error', 'checksum mismatch', 'network error'])('refuses %s without publishing', async (failure) => { |
| 44 | + installerSource.release = 'v9.9-0'; |
| 45 | + installerSource.sha256 = '0'.repeat(64); |
| 46 | + const download = vi.fn(); |
| 47 | + if (failure === 'network error') { |
| 48 | + download.mockRejectedValue(new Error('network error')); |
| 49 | + } else { |
| 50 | + download.mockResolvedValue(new Response(script, { status: failure === 'HTTP error' ? 404 : 200 })); |
| 51 | + } |
| 52 | + vi.stubGlobal('fetch', download); |
| 53 | + const directory = await outputDirectory(); |
| 54 | + await expect(publishInstaller(directory)).rejects.toThrow(); |
| 55 | + await expect(readFile(path.join(directory, 'install.sh'))).rejects.toThrow(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('rejects a moving source reference before downloading', async () => { |
| 59 | + installerSource.release = 'latest'; |
| 60 | + const download = vi.fn(); |
| 61 | + vi.stubGlobal('fetch', download); |
| 62 | + await expect(publishInstaller(await outputDirectory())).rejects.toThrow('release tag'); |
| 63 | + expect(download).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | +}); |
0 commit comments