Loading files
25f7f2a5fThis file is pinned to the selected release. Download the original to open it in your design tool.
import { mkdtempSync, readFileSync, rmSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
import { afterEach, describe, expect, it } from 'vitest'
import { canary, isLocalDevBuild, semverGt, shouldAutoUpdate, stage } from './selfUpdate.js'
let dirs: string[] = []
function tempDir(): string {
const dir = mkdtempSync(join(tmpdir(), 'machine-adapter-self-update-'))
dirs.push(dir)
return dir
}
describe('selfUpdate packaging', () => {
afterEach(() => {
for (const dir of dirs) rmSync(dir, { recursive: true, force: true })
dirs = []
})
it('canary runs installed cli.js as ESM', () => {
const dir = tempDir()
const cli = Buffer.from('#!/usr/bin/env node\nimport { createRequire } from "module";\nconsole.log(createRequire(import.meta.url) ? "1.2.3" : "nope")\n')
expect(canary(cli, dir)).toBe(true)
})
it('stages the module package metadata next to cli.js', () => {
const dir = tempDir()
stage(dir, Buffer.from('console.log("cli")\n'), Buffer.from('console.log("notify")\n'))
expect(JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'))).toEqual({ type: 'module' })
})
})
describe('shouldAutoUpdate', () => {
it('recognises the labels install-cli.sh and version.ts produce', () => {
expect(isLocalDevBuild('0.1.56-dev.a1b2c3d')).toBe(true)
expect(isLocalDevBuild('0.1.56-dev.a1b2c3d.dirty')).toBe(true)
expect(isLocalDevBuild('0.0.0-dev')).toBe(true)
expect(isLocalDevBuild('0.1.56')).toBe(false)
expect(isLocalDevBuild('0.1.56-rc.1')).toBe(false)
expect(isLocalDevBuild('')).toBe(false)
})
it('never overwrites a local build, however far ahead the release is', () => {
// The regression this exists for: a build labelled with the core it was made level with, then
// silently replaced mid-session by the very next release.
expect(semverGt('0.1.57', '0.1.56-dev.a1b2c3d')).toBe(true)
expect(shouldAutoUpdate('0.1.57', '0.1.56-dev.a1b2c3d')).toBe(false)
expect(shouldAutoUpdate('9.9.9', '0.1.56-dev.a1b2c3d.dirty')).toBe(false)
})
it('leaves a released install on the release train', () => {
expect(shouldAutoUpdate('0.1.57', '0.1.56')).toBe(true)
expect(shouldAutoUpdate('0.1.56', '0.1.56')).toBe(false)
expect(shouldAutoUpdate('0.1.55', '0.1.56')).toBe(false)
})
})