|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { createRequire } from 'node:module'; |
| 5 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +const require = createRequire(import.meta.url); |
| 8 | +const { |
| 9 | + createSha512Base64, |
| 10 | + updateLatestYmlForReferencedInstaller, |
| 11 | + updateLatestYmlForSignedInstaller |
| 12 | +} = require('../../scripts/sign-win-artifacts.cjs') as { |
| 13 | + createSha512Base64(filePath: string): string; |
| 14 | + updateLatestYmlForReferencedInstaller(latestPath: string): void; |
| 15 | + updateLatestYmlForSignedInstaller(installerPath: string): void; |
| 16 | +}; |
| 17 | + |
| 18 | +const tempDirs: string[] = []; |
| 19 | + |
| 20 | +afterEach(() => { |
| 21 | + for (const tempDir of tempDirs.splice(0)) { |
| 22 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 23 | + } |
| 24 | +}); |
| 25 | + |
| 26 | +describe('sign-win-artifacts updater metadata', () => { |
| 27 | + it('updates latest.yml checksums after installer bytes change', () => { |
| 28 | + const distDir = createTempDir(); |
| 29 | + const installerPath = path.join(distDir, 'Switchify-PC-Setup-0.1.1-x64.exe'); |
| 30 | + const latestPath = path.join(distDir, 'latest.yml'); |
| 31 | + fs.writeFileSync(installerPath, 'unsigned installer bytes'); |
| 32 | + fs.writeFileSync( |
| 33 | + latestPath, |
| 34 | + [ |
| 35 | + 'version: 0.1.1', |
| 36 | + 'files:', |
| 37 | + ' - url: Switchify-PC-Setup-0.1.1-x64.exe', |
| 38 | + ' sha512: old-file-checksum', |
| 39 | + 'path: Switchify-PC-Setup-0.1.1-x64.exe', |
| 40 | + 'sha512: old-path-checksum' |
| 41 | + ].join('\n') |
| 42 | + ); |
| 43 | + |
| 44 | + fs.writeFileSync(installerPath, 'signed installer bytes'); |
| 45 | + updateLatestYmlForSignedInstaller(installerPath); |
| 46 | + |
| 47 | + const sha512 = createSha512Base64(installerPath); |
| 48 | + const latest = fs.readFileSync(latestPath, 'utf8'); |
| 49 | + expect(latest.match(/^sha512: .+$/gm)).toEqual([`sha512: ${sha512}`]); |
| 50 | + expect(latest.match(/^\s+sha512: .+$/gm)).toEqual([` sha512: ${sha512}`]); |
| 51 | + }); |
| 52 | + |
| 53 | + it('ignores latest.yml when it does not reference the signed installer', () => { |
| 54 | + const distDir = createTempDir(); |
| 55 | + const installerPath = path.join(distDir, 'Switchify-PC-Setup-0.1.1-x64.exe'); |
| 56 | + fs.writeFileSync(installerPath, 'signed installer bytes'); |
| 57 | + fs.writeFileSync( |
| 58 | + path.join(distDir, 'latest.yml'), |
| 59 | + [ |
| 60 | + 'version: 0.1.1', |
| 61 | + 'files:', |
| 62 | + ' - url: Other-Setup.exe', |
| 63 | + ' sha512: old-file-checksum', |
| 64 | + 'path: Other-Setup.exe', |
| 65 | + 'sha512: old-path-checksum' |
| 66 | + ].join('\n') |
| 67 | + ); |
| 68 | + |
| 69 | + expect(() => updateLatestYmlForSignedInstaller(installerPath)).not.toThrow(); |
| 70 | + expect(fs.readFileSync(path.join(distDir, 'latest.yml'), 'utf8')).toContain('old-file-checksum'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('updates latest.yml when the installer name is URL-encoded', () => { |
| 74 | + const distDir = createTempDir(); |
| 75 | + const installerPath = path.join(distDir, 'Switchify PC Setup 0.1.1 x64.exe'); |
| 76 | + const latestPath = path.join(distDir, 'latest.yml'); |
| 77 | + fs.writeFileSync(installerPath, 'signed installer bytes'); |
| 78 | + fs.writeFileSync( |
| 79 | + latestPath, |
| 80 | + [ |
| 81 | + 'version: 0.1.1', |
| 82 | + 'files:', |
| 83 | + ' - url: Switchify%20PC%20Setup%200.1.1%20x64.exe', |
| 84 | + ' sha512: old-file-checksum', |
| 85 | + 'path: Switchify%20PC%20Setup%200.1.1%20x64.exe', |
| 86 | + 'sha512: old-path-checksum' |
| 87 | + ].join('\n') |
| 88 | + ); |
| 89 | + |
| 90 | + updateLatestYmlForSignedInstaller(installerPath); |
| 91 | + |
| 92 | + const sha512 = createSha512Base64(installerPath); |
| 93 | + const latest = fs.readFileSync(latestPath, 'utf8'); |
| 94 | + expect(latest.match(/^sha512: .+$/gm)).toEqual([`sha512: ${sha512}`]); |
| 95 | + expect(latest.match(/^\s+sha512: .+$/gm)).toEqual([` sha512: ${sha512}`]); |
| 96 | + }); |
| 97 | + |
| 98 | + it('does nothing when latest.yml is missing', () => { |
| 99 | + const distDir = createTempDir(); |
| 100 | + const installerPath = path.join(distDir, 'Switchify-PC-Setup-0.1.1-x64.exe'); |
| 101 | + fs.writeFileSync(installerPath, 'signed installer bytes'); |
| 102 | + |
| 103 | + expect(() => updateLatestYmlForSignedInstaller(installerPath)).not.toThrow(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('updates the installer referenced by latest.yml', () => { |
| 107 | + const distDir = createTempDir(); |
| 108 | + const installerPath = path.join(distDir, 'Switchify-PC-Setup-0.1.1-x64.exe'); |
| 109 | + const latestPath = path.join(distDir, 'latest.yml'); |
| 110 | + fs.writeFileSync(installerPath, 'signed installer bytes'); |
| 111 | + fs.writeFileSync( |
| 112 | + latestPath, |
| 113 | + [ |
| 114 | + 'version: 0.1.1', |
| 115 | + 'files:', |
| 116 | + ' - url: Switchify-PC-Setup-0.1.1-x64.exe', |
| 117 | + ' sha512: old-file-checksum', |
| 118 | + 'path: Switchify-PC-Setup-0.1.1-x64.exe', |
| 119 | + 'sha512: old-path-checksum' |
| 120 | + ].join('\n') |
| 121 | + ); |
| 122 | + |
| 123 | + updateLatestYmlForReferencedInstaller(latestPath); |
| 124 | + |
| 125 | + const sha512 = createSha512Base64(installerPath); |
| 126 | + const latest = fs.readFileSync(latestPath, 'utf8'); |
| 127 | + expect(latest.match(/^sha512: .+$/gm)).toEqual([`sha512: ${sha512}`]); |
| 128 | + expect(latest.match(/^\s+sha512: .+$/gm)).toEqual([` sha512: ${sha512}`]); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +function createTempDir(): string { |
| 133 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'switchify-sign-test-')); |
| 134 | + tempDirs.push(tempDir); |
| 135 | + return tempDir; |
| 136 | +} |
0 commit comments