|
| 1 | +import path from 'node:path' |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +const mocks = vi.hoisted(() => ({ |
| 5 | + npm: vi.fn< |
| 6 | + ( |
| 7 | + command: string, |
| 8 | + args: string[], |
| 9 | + options: unknown, |
| 10 | + ) => Promise<{ stdout: string; stderr: string }> |
| 11 | + >(), |
| 12 | + checkFailures: new Map<string, string>(), |
| 13 | + publishFailures: new Map<string, string>(), |
| 14 | + existing: new Set<string>(), |
| 15 | + missingArchives: new Set<string>(), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('node:util', () => ({ promisify: () => mocks.npm })) |
| 19 | +vi.mock('node:fs', () => ({ |
| 20 | + existsSync: (file: string) => |
| 21 | + !mocks.missingArchives.has( |
| 22 | + file.replaceAll('\\', '/').split('/').at(-2) ?? '', |
| 23 | + ), |
| 24 | + readFileSync: (file: string) => { |
| 25 | + const name = file.replaceAll('\\', '/').split('/').at(-2) |
| 26 | + return JSON.stringify({ |
| 27 | + name: `@purestack/${name}`, |
| 28 | + version: '1.0.0', |
| 29 | + exports: { '.': './dist/index.js' }, |
| 30 | + }) |
| 31 | + }, |
| 32 | +})) |
| 33 | +vi.mock('./workspaces', () => ({ |
| 34 | + workspaceManifestPaths: () => |
| 35 | + ['first', 'second', 'third'].map((name) => `packages/${name}/package.json`), |
| 36 | +})) |
| 37 | + |
| 38 | +import { publishNewPackages } from './publish-new-packages' |
| 39 | + |
| 40 | +function npmError(message: string) { |
| 41 | + return Object.assign(new Error('npm failed'), { stdout: '', stderr: message }) |
| 42 | +} |
| 43 | + |
| 44 | +function publishAttempts() { |
| 45 | + return mocks.npm.mock.calls |
| 46 | + .map(([, args]) => args[args.indexOf('publish') + 1]) |
| 47 | + .filter((arg) => arg?.endsWith('package.tgz')) |
| 48 | + .map((file) => path.basename(path.dirname(file))) |
| 49 | +} |
| 50 | + |
| 51 | +beforeEach(() => { |
| 52 | + mocks.npm.mockReset() |
| 53 | + mocks.checkFailures.clear() |
| 54 | + mocks.publishFailures.clear() |
| 55 | + mocks.existing.clear() |
| 56 | + mocks.missingArchives.clear() |
| 57 | + vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 58 | + vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 59 | + mocks.npm.mockImplementation(async (_command, args) => { |
| 60 | + const view = args.indexOf('view') |
| 61 | + if (view !== -1) { |
| 62 | + const name = args[view + 1] |
| 63 | + const failure = mocks.checkFailures.get(name) |
| 64 | + if (failure) throw npmError(failure) |
| 65 | + if (!mocks.existing.has(name)) throw npmError('npm error code E404') |
| 66 | + } |
| 67 | + const publish = args.indexOf('publish') |
| 68 | + if (publish !== -1) { |
| 69 | + const name = path.basename(path.dirname(args[publish + 1])) |
| 70 | + const failure = mocks.publishFailures.get(name) |
| 71 | + if (failure) throw npmError(failure) |
| 72 | + } |
| 73 | + return { stdout: '"1.0.0"', stderr: '' } |
| 74 | + }) |
| 75 | +}) |
| 76 | + |
| 77 | +afterEach(() => vi.restoreAllMocks()) |
| 78 | + |
| 79 | +describe('publishing missing packages', () => { |
| 80 | + it('continues publishing after an error and reports the failed package last', async () => { |
| 81 | + mocks.publishFailures.set('first', 'npm error E403: permission denied') |
| 82 | + |
| 83 | + expect(await publishNewPackages(['--publish'])).toBe(1) |
| 84 | + |
| 85 | + expect(publishAttempts()).toEqual(['first', 'second', 'third']) |
| 86 | + expect(console.log).toHaveBeenLastCalledWith('Published 2 new package(s).') |
| 87 | + expect(console.error).toHaveBeenCalledWith('\nFailed 1 package(s):') |
| 88 | + expect(console.error).toHaveBeenLastCalledWith( |
| 89 | + '\n- @purestack/first@1.0.0 (publish)\nnpm error E403: permission denied', |
| 90 | + ) |
| 91 | + }) |
| 92 | + |
| 93 | + it('collects multiple publish failures without stopping later packages', async () => { |
| 94 | + mocks.publishFailures.set('first', 'Authentication failed') |
| 95 | + mocks.publishFailures.set('second', 'Upload failed') |
| 96 | + |
| 97 | + expect(await publishNewPackages(['--publish'])).toBe(1) |
| 98 | + |
| 99 | + expect(publishAttempts()).toEqual(['first', 'second', 'third']) |
| 100 | + expect(console.log).toHaveBeenLastCalledWith('Published 1 new package(s).') |
| 101 | + expect(console.error).toHaveBeenCalledWith('\nFailed 2 package(s):') |
| 102 | + expect(console.error).toHaveBeenCalledWith( |
| 103 | + '\n- @purestack/first@1.0.0 (publish)\nAuthentication failed', |
| 104 | + ) |
| 105 | + expect(console.error).toHaveBeenLastCalledWith( |
| 106 | + '\n- @purestack/second@1.0.0 (publish)\nUpload failed', |
| 107 | + ) |
| 108 | + }) |
| 109 | + |
| 110 | + it('also continues after registry-check errors and missing archives', async () => { |
| 111 | + mocks.checkFailures.set('@purestack/first', 'Registry unavailable') |
| 112 | + mocks.missingArchives.add('second') |
| 113 | + |
| 114 | + expect(await publishNewPackages(['--publish'])).toBe(1) |
| 115 | + |
| 116 | + expect(publishAttempts()).toEqual(['third']) |
| 117 | + expect(console.log).toHaveBeenLastCalledWith('Published 1 new package(s).') |
| 118 | + expect(console.error).toHaveBeenCalledWith('\nFailed 2 package(s):') |
| 119 | + expect(console.error).toHaveBeenCalledWith( |
| 120 | + '\n- @purestack/first@1.0.0 (registry check)\nRegistry unavailable', |
| 121 | + ) |
| 122 | + expect(console.error).toHaveBeenLastCalledWith( |
| 123 | + expect.stringContaining('@purestack/second@1.0.0 (publish)'), |
| 124 | + ) |
| 125 | + }) |
| 126 | + |
| 127 | + it('reports dry-run check failures without attempting to publish', async () => { |
| 128 | + mocks.checkFailures.set('@purestack/first', 'Registry unavailable') |
| 129 | + |
| 130 | + expect(await publishNewPackages([])).toBe(1) |
| 131 | + |
| 132 | + expect(publishAttempts()).toEqual([]) |
| 133 | + expect(console.log).toHaveBeenLastCalledWith( |
| 134 | + 'Dry run complete. 2 package version(s) would be published.', |
| 135 | + ) |
| 136 | + expect(console.error).toHaveBeenLastCalledWith( |
| 137 | + '\n- @purestack/first@1.0.0 (registry check)\nRegistry unavailable', |
| 138 | + ) |
| 139 | + }) |
| 140 | + |
| 141 | + it('keeps already-published versions as successful skips', async () => { |
| 142 | + mocks.existing.add('@purestack/second') |
| 143 | + mocks.existing.add('@purestack/second@1.0.0') |
| 144 | + mocks.publishFailures.set( |
| 145 | + 'first', |
| 146 | + 'cannot publish over the previously published versions', |
| 147 | + ) |
| 148 | + |
| 149 | + expect(await publishNewPackages(['--publish'])).toBe(0) |
| 150 | + |
| 151 | + expect(publishAttempts()).toEqual(['first', 'third']) |
| 152 | + expect(console.log).toHaveBeenLastCalledWith('Published 1 new package(s).') |
| 153 | + expect(console.error).not.toHaveBeenCalled() |
| 154 | + }) |
| 155 | +}) |
0 commit comments