50 lines
2.1 KiB
TypeScript
50 lines
2.1 KiB
TypeScript
|
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
||
|
|
import { tmpdir } from 'node:os'
|
||
|
|
import { join } from 'node:path'
|
||
|
|
import { afterEach, describe, expect, it } from 'vitest'
|
||
|
|
import { load } from 'js-yaml'
|
||
|
|
import { registryNameOf, writeReleaseAgeExclude } from '../src/plugin.ts'
|
||
|
|
|
||
|
|
const dirs: string[] = []
|
||
|
|
function tempDir(): string {
|
||
|
|
const dir = mkdtempSync(join(tmpdir(), 'dsh-cli-plugin-'))
|
||
|
|
dirs.push(dir)
|
||
|
|
return dir
|
||
|
|
}
|
||
|
|
afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }) })
|
||
|
|
|
||
|
|
describe('registryNameOf', () => {
|
||
|
|
it('returns the bare name for a registry specifier', () => {
|
||
|
|
expect(registryNameOf('dsh-theme-plugin')).toBe('dsh-theme-plugin')
|
||
|
|
expect(registryNameOf('dsh-theme-plugin@latest')).toBe('dsh-theme-plugin')
|
||
|
|
expect(registryNameOf('@scope/pkg@1.2.0')).toBe('@scope/pkg')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('returns undefined for non-registry sources', () => {
|
||
|
|
expect(registryNameOf('github:user/repo')).toBeUndefined()
|
||
|
|
expect(registryNameOf('git+https://github.com/user/repo.git')).toBeUndefined()
|
||
|
|
expect(registryNameOf('file:../plugin')).toBeUndefined()
|
||
|
|
expect(registryNameOf('./plugin')).toBeUndefined()
|
||
|
|
expect(registryNameOf('https://example.com/p.tgz')).toBeUndefined()
|
||
|
|
})
|
||
|
|
})
|
||
|
|
|
||
|
|
describe('writeReleaseAgeExclude', () => {
|
||
|
|
it('writes the exclusion into pnpm-workspace.yaml, preserving existing settings', () => {
|
||
|
|
const dir = tempDir()
|
||
|
|
writeFileSync(join(dir, 'pnpm-workspace.yaml'), 'packages:\n - .\nallowBuilds:\n node-pty: true\n')
|
||
|
|
writeReleaseAgeExclude(dir, ['dsh-theme-plugin'])
|
||
|
|
writeReleaseAgeExclude(dir, ['@scope/pkg'])
|
||
|
|
const doc = load(readFileSync(join(dir, 'pnpm-workspace.yaml'), 'utf8')) as Record<string, unknown>
|
||
|
|
expect(doc.minimumReleaseAgeExclude).toEqual(['dsh-theme-plugin', '@scope/pkg'])
|
||
|
|
expect((doc.allowBuilds as Record<string, unknown>)['node-pty']).toBe(true)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('creates the file when absent', () => {
|
||
|
|
const dir = tempDir()
|
||
|
|
writeReleaseAgeExclude(dir, ['x'])
|
||
|
|
const doc = load(readFileSync(join(dir, 'pnpm-workspace.yaml'), 'utf8')) as Record<string, unknown>
|
||
|
|
expect(doc.minimumReleaseAgeExclude).toEqual(['x'])
|
||
|
|
})
|
||
|
|
})
|