|
1 | | -/** @fileoverview Shared test utilities for npm package testing. */ |
| 1 | +/** |
| 2 | + * @fileoverview Shared test utilities for npm package testing. |
| 3 | + * Provides helpers for setting up isolated test environments for both |
| 4 | + * socket-registry packages and local development packages. |
| 5 | + */ |
2 | 6 |
|
| 7 | +import { existsSync } from 'node:fs' |
3 | 8 | import path from 'node:path' |
| 9 | +import { fileURLToPath } from 'node:url' |
4 | 10 |
|
| 11 | +import constants from '../../scripts/constants.mjs' |
5 | 12 | import { installPackageForTesting } from '../../scripts/utils/package.mjs' |
| 13 | +import { |
| 14 | + readPackageJson, |
| 15 | + resolveOriginalPackageName, |
| 16 | +} from '../../registry/dist/lib/packages.js' |
| 17 | + |
| 18 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
6 | 19 |
|
7 | 20 | /** |
8 | | - * Creates a test setup function for npm package tests. |
9 | | - * @param {string} sockRegPkgName - Socket registry package name. |
10 | | - * @returns {Promise<{pkgPath: string}>} |
| 21 | + * Isolates a package in a temporary test environment. |
| 22 | + * |
| 23 | + * Supports two modes: |
| 24 | + * 1. Socket registry packages: Pass package name (e.g., '@socketregistry/packageurl-js') |
| 25 | + * 2. Local development packages: Pass relative or absolute path (e.g., '../../socket-packageurl-js' or '.') |
| 26 | + * |
| 27 | + * The helper creates an isolated test environment by: |
| 28 | + * - Installing the package in a temporary directory |
| 29 | + * - Copying package files to node_modules |
| 30 | + * - Installing dependencies |
| 31 | + * - Preserving test scripts (for registry packages) |
| 32 | + * |
| 33 | + * @param {string} packageOrPath - Package name or local path to package directory. |
| 34 | + * @param {object} [options] - Optional configuration. |
| 35 | + * @param {string[]} [options.entryPoints] - Array of entry point filenames to load. |
| 36 | + * @returns {Promise<{pkgPath: string, modules?: any[]}>} |
| 37 | + * |
| 38 | + * @example |
| 39 | + * // Test a socket-registry package |
| 40 | + * const { pkgPath } = await isolatePackage('@socketregistry/packageurl-js') |
| 41 | + * |
| 42 | + * @example |
| 43 | + * // Test a local development package |
| 44 | + * const { pkgPath } = await isolatePackage('../../socket-packageurl-js') |
| 45 | + * |
| 46 | + * @example |
| 47 | + * // Load multiple entry points |
| 48 | + * const { pkgPath, modules } = await isolatePackage('../../socket-packageurl-js', { |
| 49 | + * entryPoints: ['package-url.js', 'url-converter.js'] |
| 50 | + * }) |
11 | 51 | */ |
12 | | -async function setupPackageTest(sockRegPkgName) { |
13 | | - const result = await installPackageForTesting(sockRegPkgName) |
| 52 | +async function isolatePackage(packageOrPath, options = {}) { |
| 53 | + const { entryPoints } = options |
| 54 | + |
| 55 | + // Determine if this is a path or package name |
| 56 | + const isPath = packageOrPath.startsWith('.') || path.isAbsolute(packageOrPath) |
| 57 | + |
| 58 | + let sourcePath |
| 59 | + let packageName |
| 60 | + let versionSpec |
| 61 | + |
| 62 | + if (isPath) { |
| 63 | + // Local development package |
| 64 | + sourcePath = path.resolve(__dirname, '..', '..', packageOrPath) |
| 65 | + |
| 66 | + // Read package.json to get the name |
| 67 | + const pkgJson = await readPackageJson(sourcePath, { normalize: true }) |
| 68 | + packageName = pkgJson.name |
| 69 | + } else { |
| 70 | + // Socket registry package |
| 71 | + const socketPkgName = packageOrPath |
| 72 | + sourcePath = path.join(constants.npmPackagesPath, socketPkgName) |
| 73 | + |
| 74 | + if (!existsSync(sourcePath)) { |
| 75 | + throw new Error(`No Socket override found for ${socketPkgName}`) |
| 76 | + } |
| 77 | + |
| 78 | + // Resolve to original npm package name |
| 79 | + packageName = resolveOriginalPackageName(socketPkgName) |
| 80 | + |
| 81 | + // Get version spec from test/npm/package.json |
| 82 | + const testPkgJson = await readPackageJson(constants.testNpmPkgJsonPath, { |
| 83 | + normalize: true, |
| 84 | + }) |
| 85 | + versionSpec = testPkgJson.devDependencies?.[packageName] |
| 86 | + |
| 87 | + if (!versionSpec) { |
| 88 | + throw new Error(`${packageName} not in devDependencies`) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + const result = await installPackageForTesting(sourcePath, packageName, { |
| 93 | + versionSpec, |
| 94 | + }) |
| 95 | + |
14 | 96 | if (!result.installed) { |
15 | 97 | throw new Error(`Failed to install package: ${result.reason}`) |
16 | 98 | } |
17 | | - return { |
18 | | - pkgPath: result.packagePath, |
19 | | - } |
20 | | -} |
21 | 99 |
|
22 | | -/** |
23 | | - * Common test helper for packages that require multiple entry points. |
24 | | - * @param {string} sockRegPkgName - Socket registry package name. |
25 | | - * @param {string[]} entryPoints - Array of entry point filenames. |
26 | | - * @returns {Promise<{pkgPath: string, modules: any[]}>} |
27 | | - */ |
28 | | -async function setupMultiEntryTest(sockRegPkgName, entryPoints) { |
29 | | - const { pkgPath } = await setupPackageTest(sockRegPkgName) |
30 | | - const modules = entryPoints.map(entryPoint => |
31 | | - require(path.join(pkgPath, entryPoint)), |
32 | | - ) |
33 | | - return { |
34 | | - pkgPath, |
35 | | - modules, |
| 100 | + const pkgPath = result.packagePath |
| 101 | + |
| 102 | + if (entryPoints && entryPoints.length > 0) { |
| 103 | + const modules = entryPoints.map(entryPoint => |
| 104 | + require(path.join(pkgPath, entryPoint)), |
| 105 | + ) |
| 106 | + return { pkgPath, modules } |
36 | 107 | } |
| 108 | + |
| 109 | + return { pkgPath } |
37 | 110 | } |
38 | 111 |
|
39 | | -export { setupMultiEntryTest, setupPackageTest } |
| 112 | +export { isolatePackage } |
0 commit comments