|
| 1 | +import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join, sep } from 'node:path'; |
| 4 | +import { rollup } from 'rollup'; |
| 5 | +import { afterEach, beforeEach, expect, test, vi } from 'vitest'; |
| 6 | +import { sentryRollupPlugin } from '../../src/rollup'; |
| 7 | + |
| 8 | +const upload = vi.hoisted(() => vi.fn()); |
| 9 | +const fixtures: string[] = []; |
| 10 | +vi.mock('sentry', () => ({ |
| 11 | + createSentrySDK: () => ({ |
| 12 | + sourcemap: { upload }, |
| 13 | + release: {}, |
| 14 | + }), |
| 15 | +})); |
| 16 | + |
| 17 | +beforeEach(() => { |
| 18 | + upload.mockReset(); |
| 19 | +}); |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + for (const fixture of fixtures.splice(0)) { |
| 23 | + rmSync(fixture, { recursive: true, force: true }); |
| 24 | + } |
| 25 | +}); |
| 26 | + |
| 27 | +test.each([ |
| 28 | + { name: 'ordinary filenames', file: 'sdk.mjs', explicitAssets: false, nativePath: false }, |
| 29 | + { name: 'glob characters in detected paths', file: 'sdk+[...].mjs', explicitAssets: false, nativePath: false }, |
| 30 | + { name: 'explicit asset globs', file: 'sdk+[...].mjs', explicitAssets: true, nativePath: false }, |
| 31 | + { name: 'native path separators', file: 'sdk+[...].mjs', explicitAssets: false, nativePath: true }, |
| 32 | + { name: 'ignored assets', file: 'sdk+[...].mjs', explicitAssets: false, nativePath: false, ignored: true }, |
| 33 | +])('handles $name when uploading Rollup artifacts', async ({ file, explicitAssets, nativePath, ignored }) => { |
| 34 | + const fixture = mkdtempSync(join(tmpdir(), 'sentry-rollup-literal-paths-')); |
| 35 | + fixtures.push(fixture); |
| 36 | + const input = join(fixture, 'input.js'); |
| 37 | + const output = join(fixture, 'dist', '_libs', file); |
| 38 | + writeFileSync(input, 'export function example() { throw new Error("original fixture"); }\n'); |
| 39 | + const uploadedFiles: string[] = []; |
| 40 | + upload.mockImplementation(async (options: { directory: string }) => { |
| 41 | + uploadedFiles.push(...readdirSync(options.directory)); |
| 42 | + }); |
| 43 | + |
| 44 | + const bundle = await rollup({ |
| 45 | + input, |
| 46 | + plugins: sentryRollupPlugin({ |
| 47 | + authToken: 'local-fixture-only', |
| 48 | + org: 'local-fixture', |
| 49 | + project: 'local-fixture', |
| 50 | + telemetry: false, |
| 51 | + silent: true, |
| 52 | + release: { create: false, finalize: false }, |
| 53 | + sourcemaps: { |
| 54 | + assets: explicitAssets ? join(fixture, 'dist', '**', '*.mjs').split(sep).join('/') : undefined, |
| 55 | + ignore: ignored ? join(fixture, 'dist', '**', 'sdk+*.mjs').split(sep).join('/') : undefined, |
| 56 | + }, |
| 57 | + }), |
| 58 | + }); |
| 59 | + try { |
| 60 | + await bundle.write({ file: nativePath ? output : output.replace(/\\/g, '/'), format: 'es', sourcemap: true }); |
| 61 | + } finally { |
| 62 | + await bundle.close(); |
| 63 | + } |
| 64 | + |
| 65 | + expect(existsSync(output)).toBe(true); |
| 66 | + expect(existsSync(`${output}.map`)).toBe(true); |
| 67 | + expect(readFileSync(output, 'utf8')).toContain('sentry-dbid-'); |
| 68 | + expect(uploadedFiles.filter(name => name.endsWith('.js'))).toHaveLength(ignored ? 0 : 1); |
| 69 | + expect(uploadedFiles.filter(name => name.endsWith('.js.map'))).toHaveLength(ignored ? 0 : 1); |
| 70 | +}); |
0 commit comments