// oxlint-disable vitest/no-hooks -- the temporary workspace roots are cleaned up once per test file import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import path from 'node:path' import { afterEach, describe, expect, it, vi } from 'vitest' import { collectPublishablePackages, expandWorkspacePattern, getVersionChannel, MixedVersionError, readWorkspacePatterns, resolveDistTag, runDistTag, } from '../distTag.js' const ROOT = path.resolve(import.meta.dirname, '../..') const STABLE = '2.0.0' const PRERELEASE = '2.0.0-rc.142' const roots: string[] = [] const createWorkspace = async ( files: Record, ): Promise => { const root = await mkdtemp(path.join(tmpdir(), 'dist-tag-')) roots.push(root) await Promise.all( Object.entries(files).map(async ([file, content]) => { const filePath = path.join(root, file) await mkdir(path.dirname(filePath), { recursive: true }) await writeFile(filePath, content, 'utf8') }), ) return root } const workspaceFile = (patterns: string[]): string => [ 'packages:', ...patterns.map((pattern) => ` - ${pattern}`), '', 'catalog:', ' vue: ^3.5.46', '', ].join('\n') const packageManifest = ( name: string, version?: string, isPrivate = false, ): string => JSON.stringify( { name, ...(version === undefined ? {} : { version }), ...(isPrivate ? { private: true } : {}), }, null, 2, ) // Capture everything the resolver writes, so tests can assert on the messages. const captureOutput = (): (() => string) => { const log = vi.spyOn(console, 'log').mockImplementation(() => {}) const error = vi.spyOn(console, 'error').mockImplementation(() => {}) return () => [...log.mock.calls, ...error.mock.calls].flat().join('\n') } const lastLine = (output: string): string | undefined => output.trim().split('\n').at(-1) describe('distTag', () => { afterEach(async () => { vi.restoreAllMocks() await Promise.all( roots.splice(0).map((root) => rm(root, { force: true, recursive: true })), ) }) describe('readWorkspacePatterns()', () => { it('reads the packages sequence and ignores the rest of the file', async () => { const root = await createWorkspace({ 'pnpm-workspace.yaml': [ '# Workspace packages', 'packages:', ' - docs/', " - 'plugins/*/*'", ' - "themes/*"', '', 'catalog:', ' vue: ^3.5.46', '', ].join('\n'), }) await expect(readWorkspacePatterns(root)).resolves.toStrictEqual([ 'docs/', 'plugins/*/*', 'themes/*', ]) }) it('skips negated patterns', async () => { const root = await createWorkspace({ 'pnpm-workspace.yaml': workspaceFile([ 'packages/*', '!packages/ignored', ]), }) await expect(readWorkspacePatterns(root)).resolves.toStrictEqual([ 'packages/*', ]) }) }) describe('expandWorkspacePattern()', () => { it('expands nested wildcards to the matching directories', async () => { const root = await createWorkspace({ 'plugins/category/plugin-a/package.json': '{}', 'plugins/category/plugin-b/package.json': '{}', 'plugins/plugin-c/package.json': '{}', 'themes/theme-a/package.json': '{}', }) await expect( expandWorkspacePattern(root, 'plugins/*/*'), ).resolves.toStrictEqual([ path.join(root, 'plugins/category/plugin-a'), path.join(root, 'plugins/category/plugin-b'), ]) await expect( expandWorkspacePattern(root, 'themes/*'), ).resolves.toStrictEqual([path.join(root, 'themes/theme-a')]) }) it('expands every nested directory with a double wildcard', async () => { const root = await createWorkspace({ 'packages/a/package.json': '{}', 'packages/b/nested/package.json': '{}', }) await expect( expandWorkspacePattern(root, 'packages/**'), ).resolves.toStrictEqual([ path.join(root, 'packages/a'), path.join(root, 'packages/b'), path.join(root, 'packages/b/nested'), ]) }) it('ignores directories that are missing from the workspace', async () => { const root = await createWorkspace({ 'pnpm-workspace.yaml': workspaceFile(['packages/*']), }) await expect( expandWorkspacePattern(root, 'missing/*'), ).resolves.toStrictEqual([]) }) }) describe('collectPublishablePackages()', () => { it('discovers packages from the workspace patterns', async () => { const root = await createWorkspace({ 'pnpm-workspace.yaml': workspaceFile([ 'docs/', 'plugins/*/*', 'themes/*', 'tools/*', ]), 'docs/package.json': packageManifest('ecosystem-docs', undefined, true), 'other/package.json': packageManifest('outside-package', STABLE), 'plugins/features/plugin-a/package.json': packageManifest( 'plugin-a', STABLE, ), 'plugins/plugin-flat/package.json': packageManifest( 'plugin-flat', STABLE, ), 'themes/theme-a/package.json': packageManifest('theme-a', PRERELEASE), 'tools/tool-a/package.json': packageManifest('tool-a', STABLE), }) // `plugins/*/*` matches the nested package only, and `other/` is not part // of the workspace, so neither of them is published. await expect(collectPublishablePackages(root)).resolves.toStrictEqual([ { name: 'plugin-a', version: STABLE }, { name: 'theme-a', version: PRERELEASE }, { name: 'tool-a', version: STABLE }, ]) }) it('skips private packages, including the ones without a version', async () => { const root = await createWorkspace({ 'pnpm-workspace.yaml': workspaceFile(['packages/*']), 'packages/docs/package.json': packageManifest( 'docs-site', undefined, true, ), 'packages/e2e/package.json': packageManifest( 'e2e-tests', '1.0.0', true, ), 'packages/plugin-a/package.json': packageManifest('plugin-a', STABLE), }) await expect(collectPublishablePackages(root)).resolves.toStrictEqual([ { name: 'plugin-a', version: STABLE }, ]) }) it('fails when a publishable package has no version', async () => { const root = await createWorkspace({ 'pnpm-workspace.yaml': workspaceFile(['packages/*']), 'packages/plugin-a/package.json': packageManifest('plugin-a'), }) await expect(collectPublishablePackages(root)).rejects.toThrow( /plugin-a has no name or version/u, ) }) it('discovers the packages of this repository without the private ones', async () => { const names = (await collectPublishablePackages(ROOT)).map( ({ name }) => name, ) expect(names).toContain('@vuepress/plugin-shiki') expect(names).toContain('@vuepress/theme-default') expect(names.length).toBeGreaterThan(60) expect(names).not.toContain('@vuepress/ecosystem-docs') expect(names).not.toContain('@vuepress/ecosystem-e2e') }) }) describe('getVersionChannel()', () => { it.each([ ['2.0.0', 'stable'], ['1.2.3', 'stable'], ['2.0.0+build.5', 'stable'], ['2.0.0-rc.142', 'prerelease'], ['2.0.0-beta.1', 'prerelease'], ['2.0.0-0', 'prerelease'], ['2.0.0-alpha.beta.1+build.5', 'prerelease'], ])('classifies %s as %s', (version, channel) => { expect(getVersionChannel(version)).toBe(channel) }) it('rejects a version that is not semver', () => { expect(() => getVersionChannel('2.0')).toThrow( '"2.0" is not a supported semver version.', ) }) }) describe('resolveDistTag()', () => { it('resolves latest when every package is stable', () => { expect( resolveDistTag([ { name: 'plugin-a', version: '2.0.0' }, { name: 'plugin-b', version: '1.4.2' }, ]), ).toStrictEqual({ channel: 'stable', packages: [ { name: 'plugin-a', version: '2.0.0' }, { name: 'plugin-b', version: '1.4.2' }, ], tag: 'latest', }) }) it('resolves next when every package is a prerelease', () => { expect( resolveDistTag([ { name: 'plugin-a', version: '2.0.0-rc.142' }, { name: 'plugin-b', version: '2.0.0-rc.138' }, ]).tag, ).toBe('next') }) it('fails on a mixed set and names the offending packages', () => { const packages = [ { name: 'plugin-a', version: '2.0.0' }, { name: 'plugin-b', version: '2.0.0-rc.1' }, ] expect(() => resolveDistTag(packages)).toThrow(MixedVersionError) expect(() => resolveDistTag(packages)).toThrow( 'Cannot publish a mixed set of versions under a single dist-tag.', ) expect(() => resolveDistTag(packages)).toThrow('stable: plugin-a@2.0.0') expect(() => resolveDistTag(packages)).toThrow( 'prerelease: plugin-b@2.0.0-rc.1', ) }) it('fails when no package was found', () => { expect(() => resolveDistTag([])).toThrow( 'No publishable workspace package was found.', ) }) }) describe('runDistTag()', () => { it('prints latest last when every package is stable', async () => { const root = await createWorkspace({ 'pnpm-workspace.yaml': workspaceFile(['packages/*']), 'packages/plugin-a/package.json': packageManifest('plugin-a', STABLE), 'packages/plugin-b/package.json': packageManifest('plugin-b', STABLE), }) const output = captureOutput() await expect(runDistTag(root)).resolves.toBe(0) expect(lastLine(output())).toBe('latest') expect(output()).toContain('Found 2 publishable workspace package(s)') expect(output()).toContain('all stable') }) it('prints next last when every package is a prerelease', async () => { const root = await createWorkspace({ 'pnpm-workspace.yaml': workspaceFile(['packages/*']), 'packages/plugin-a/package.json': packageManifest( 'plugin-a', '2.0.0-rc.142', ), 'packages/plugin-b/package.json': packageManifest( 'plugin-b', '2.0.0-beta.1', ), }) const output = captureOutput() await expect(runDistTag(root)).resolves.toBe(0) expect(lastLine(output())).toBe('next') expect(output()).toContain('all prerelease') }) it('exits non-zero on a mixed set without printing a tag', async () => { const root = await createWorkspace({ 'pnpm-workspace.yaml': workspaceFile(['packages/*']), 'packages/plugin-a/package.json': packageManifest('plugin-a', STABLE), 'packages/plugin-b/package.json': packageManifest( 'plugin-b', PRERELEASE, ), }) const output = captureOutput() await expect(runDistTag(root)).resolves.toBe(1) const text = output() expect(text).toContain('plugin-a@2.0.0') expect(text).toContain(`plugin-b@${PRERELEASE}`) // The workflow captures the last line as the tag, so a failure must never // leave a tag behind. expect(lastLine(text)).not.toBe('latest') expect(lastLine(text)).not.toBe('next') }) it('exits non-zero when the workspace patterns are missing', async () => { const root = await createWorkspace({ 'pnpm-workspace.yaml': 'catalog:\n vue: ^3.5.46\n', }) const output = captureOutput() await expect(runDistTag(root)).resolves.toBe(1) expect(output()).toContain('No package pattern is declared') }) }) })