|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +import { handler as writeBuildInfo } from 'hadron-build/commands/info'; |
| 6 | + |
| 7 | +import { type PackageKind } from './packages'; |
| 8 | +import { type SmokeTestsContext } from './context'; |
| 9 | +import { pick } from 'lodash'; |
| 10 | + |
| 11 | +function assertObjectHasKeys( |
| 12 | + obj: unknown, |
| 13 | + name: string, |
| 14 | + keys: readonly string[] |
| 15 | +) { |
| 16 | + assert( |
| 17 | + typeof obj === 'object' && obj !== null, |
| 18 | + 'Expected buildInfo to be an object' |
| 19 | + ); |
| 20 | + |
| 21 | + for (const key of keys) { |
| 22 | + assert(key in obj, `Expected '${name}' to have '${key}'`); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// subsets of the hadron-build info result |
| 27 | + |
| 28 | +export const commonKeys = ['productName'] as const; |
| 29 | +export type CommonBuildInfo = Record<typeof commonKeys[number], string>; |
| 30 | + |
| 31 | +export function assertCommonBuildInfo( |
| 32 | + buildInfo: unknown |
| 33 | +): asserts buildInfo is CommonBuildInfo { |
| 34 | + assertObjectHasKeys(buildInfo, 'buildInfo', commonKeys); |
| 35 | +} |
| 36 | + |
| 37 | +export const windowsFilenameKeys = [ |
| 38 | + 'windows_setup_filename', |
| 39 | + 'windows_msi_filename', |
| 40 | + 'windows_zip_filename', |
| 41 | + 'windows_nupkg_full_filename', |
| 42 | +] as const; |
| 43 | +export type WindowsBuildInfo = CommonBuildInfo & |
| 44 | + Record<typeof windowsFilenameKeys[number], string>; |
| 45 | + |
| 46 | +export function assertBuildInfoIsWindows( |
| 47 | + buildInfo: unknown |
| 48 | +): asserts buildInfo is WindowsBuildInfo { |
| 49 | + assertObjectHasKeys(buildInfo, 'buildInfo', commonKeys); |
| 50 | + assertObjectHasKeys(buildInfo, 'buildInfo', windowsFilenameKeys); |
| 51 | +} |
| 52 | + |
| 53 | +export const osxFilenameKeys = [ |
| 54 | + 'osx_dmg_filename', |
| 55 | + 'osx_zip_filename', |
| 56 | +] as const; |
| 57 | +export type OSXBuildInfo = CommonBuildInfo & |
| 58 | + Record<typeof osxFilenameKeys[number], string>; |
| 59 | + |
| 60 | +export function assertBuildInfoIsOSX( |
| 61 | + buildInfo: unknown |
| 62 | +): asserts buildInfo is OSXBuildInfo { |
| 63 | + assertObjectHasKeys(buildInfo, 'buildInfo', commonKeys); |
| 64 | + assertObjectHasKeys(buildInfo, 'buildInfo', osxFilenameKeys); |
| 65 | +} |
| 66 | + |
| 67 | +export const ubuntuFilenameKeys = [ |
| 68 | + 'linux_deb_filename', |
| 69 | + 'linux_tar_filename', |
| 70 | +] as const; |
| 71 | +export type UbuntuBuildInfo = CommonBuildInfo & |
| 72 | + Record<typeof ubuntuFilenameKeys[number], string>; |
| 73 | + |
| 74 | +export function assertBuildInfoIsUbuntu( |
| 75 | + buildInfo: unknown |
| 76 | +): asserts buildInfo is UbuntuBuildInfo { |
| 77 | + assertObjectHasKeys(buildInfo, 'buildInfo', commonKeys); |
| 78 | + assertObjectHasKeys(buildInfo, 'buildInfo', ubuntuFilenameKeys); |
| 79 | +} |
| 80 | + |
| 81 | +const rhelFilenameKeys = ['linux_rpm_filename', 'rhel_tar_filename'] as const; |
| 82 | +export type RHELBuildInfo = CommonBuildInfo & |
| 83 | + Record<typeof rhelFilenameKeys[number], string>; |
| 84 | + |
| 85 | +export function assertBuildInfoIsRHEL( |
| 86 | + buildInfo: unknown |
| 87 | +): asserts buildInfo is RHELBuildInfo { |
| 88 | + assertObjectHasKeys(buildInfo, 'buildInfo', commonKeys); |
| 89 | + assertObjectHasKeys(buildInfo, 'buildInfo', rhelFilenameKeys); |
| 90 | +} |
| 91 | + |
| 92 | +export type PackageDetails = { |
| 93 | + kind: PackageKind; |
| 94 | + filename: string; |
| 95 | +} & ( |
| 96 | + | { |
| 97 | + kind: 'windows_setup' | 'windows_msi' | 'windows_zip'; |
| 98 | + buildInfo: WindowsBuildInfo; |
| 99 | + } |
| 100 | + | { |
| 101 | + kind: 'osx_dmg' | 'osx_zip'; |
| 102 | + buildInfo: OSXBuildInfo; |
| 103 | + } |
| 104 | + | { |
| 105 | + kind: 'linux_deb' | 'linux_tar'; |
| 106 | + buildInfo: UbuntuBuildInfo; |
| 107 | + } |
| 108 | + | { |
| 109 | + kind: 'linux_rpm' | 'rhel_tar'; |
| 110 | + buildInfo: RHELBuildInfo; |
| 111 | + } |
| 112 | +); |
| 113 | + |
| 114 | +/** |
| 115 | + * Extracts the filename of the packaged app from the build info, specific to a kind of package. |
| 116 | + */ |
| 117 | +export function getPackageDetails( |
| 118 | + kind: PackageKind, |
| 119 | + buildInfo: unknown |
| 120 | +): PackageDetails { |
| 121 | + if ( |
| 122 | + kind === 'windows_setup' || |
| 123 | + kind === 'windows_msi' || |
| 124 | + kind === 'windows_zip' |
| 125 | + ) { |
| 126 | + assertBuildInfoIsWindows(buildInfo); |
| 127 | + return { kind, buildInfo, filename: buildInfo[`${kind}_filename`] }; |
| 128 | + } else if (kind === 'osx_dmg' || kind === 'osx_zip') { |
| 129 | + assertBuildInfoIsOSX(buildInfo); |
| 130 | + return { kind, buildInfo, filename: buildInfo[`${kind}_filename`] }; |
| 131 | + } else if (kind === 'linux_deb' || kind === 'linux_tar') { |
| 132 | + assertBuildInfoIsUbuntu(buildInfo); |
| 133 | + return { kind, buildInfo, filename: buildInfo[`${kind}_filename`] }; |
| 134 | + } else if (kind === 'linux_rpm' || kind === 'rhel_tar') { |
| 135 | + assertBuildInfoIsRHEL(buildInfo); |
| 136 | + return { kind, buildInfo, filename: buildInfo[`${kind}_filename`] }; |
| 137 | + } else { |
| 138 | + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 139 | + throw new Error(`Unsupported package kind: ${kind}`); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +function readJson<T extends object>(...segments: string[]): T { |
| 144 | + const result = JSON.parse( |
| 145 | + fs.readFileSync(path.join(...segments), 'utf8') |
| 146 | + ) as unknown; |
| 147 | + assert(typeof result === 'object' && result !== null, 'Expected an object'); |
| 148 | + return result as T; |
| 149 | +} |
| 150 | + |
| 151 | +export function readPackageDetails( |
| 152 | + kind: PackageKind, |
| 153 | + filePath: string |
| 154 | +): PackageDetails { |
| 155 | + const result = readJson(filePath); |
| 156 | + return getPackageDetails(kind, result); |
| 157 | +} |
| 158 | + |
| 159 | +export function writeAndReadPackageDetails( |
| 160 | + context: SmokeTestsContext |
| 161 | +): PackageDetails { |
| 162 | + const compassDir = path.resolve(__dirname, '../../../compass'); |
| 163 | + const infoArgs = { |
| 164 | + format: 'json', |
| 165 | + dir: compassDir, |
| 166 | + platform: context.platform, |
| 167 | + arch: context.arch, |
| 168 | + out: path.resolve(context.sandboxPath, 'target.json'), |
| 169 | + }; |
| 170 | + console.log({ infoArgs }); |
| 171 | + |
| 172 | + // These are known environment variables that will affect the way |
| 173 | + // writeBuildInfo works. Log them as a reminder and for our own sanity |
| 174 | + console.log( |
| 175 | + 'info env vars', |
| 176 | + pick(process.env, [ |
| 177 | + 'HADRON_DISTRIBUTION', |
| 178 | + 'HADRON_APP_VERSION', |
| 179 | + 'HADRON_PRODUCT', |
| 180 | + 'HADRON_PRODUCT_NAME', |
| 181 | + 'HADRON_READONLY', |
| 182 | + 'HADRON_ISOLATED', |
| 183 | + 'DEV_VERSION_IDENTIFIER', |
| 184 | + 'IS_RHEL', |
| 185 | + ]) |
| 186 | + ); |
| 187 | + writeBuildInfo(infoArgs); |
| 188 | + return readPackageDetails(context.package, infoArgs.out); |
| 189 | +} |
0 commit comments