Skip to content

Commit 49d4ee0

Browse files
committed
Refactor installPackageForTesting to accept explicit parameters
- Change signature to (sourcePath, packageName, options) - Remove path detection logic (moved to wrapper) - Simplify versionSpec handling - Remove isLocalPath conditionals for cleaner code - Separate concerns: low-level copy vs high-level resolution
1 parent d9e73c6 commit 49d4ee0

1 file changed

Lines changed: 80 additions & 75 deletions

File tree

scripts/utils/package.mjs

Lines changed: 80 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import path from 'node:path'
1010

1111
import constants from '../constants.mjs'
1212
import WIN32 from '../../registry/dist/lib/constants/WIN32.js'
13-
import {
14-
readPackageJson,
15-
resolveOriginalPackageName,
16-
} from '../../registry/dist/lib/packages.js'
13+
import { readPackageJson } from '../../registry/dist/lib/packages.js'
1714
import { pEach } from '../../registry/dist/lib/promises.js'
1815
import { spawn } from '../../registry/dist/lib/spawn.js'
1916
import { cleanTestScript } from '../../test/utils/script-cleaning.mjs'
@@ -286,96 +283,104 @@ async function runCommand(command, args, options = {}) {
286283

287284
/**
288285
* Install a package for testing in a temporary directory.
286+
*
287+
* @param {string} sourcePath - Absolute path to package source directory
288+
* @param {string} packageName - Package name for node_modules installation
289+
* @param {object} options - Installation options
290+
* @param {string} [options.versionSpec] - Version or URL to install (optional, for npm packages)
291+
* @returns {Promise<{installed: boolean, packagePath?: string, reason?: string}>}
289292
*/
290-
async function installPackageForTesting(socketPkgName) {
291-
const origPkgName = resolveOriginalPackageName(socketPkgName)
293+
async function installPackageForTesting(sourcePath, packageName, options = {}) {
294+
const { versionSpec } = options
292295

293-
const overridePath = path.join(constants.npmPackagesPath, socketPkgName)
294-
295-
if (!existsSync(overridePath)) {
296+
if (!existsSync(sourcePath)) {
296297
return {
297298
installed: false,
298-
reason: 'No Socket override found',
299+
reason: `Source path does not exist: ${sourcePath}`,
299300
}
300301
}
301302

302303
try {
303-
// Read the test/npm/package.json to get the version spec.
304-
const testPkgJson = await readPackageJson(constants.testNpmPkgJsonPath, {
305-
normalize: true,
306-
})
307-
const versionSpec = testPkgJson.devDependencies?.[origPkgName]
308-
309-
if (!versionSpec) {
310-
return {
311-
installed: false,
312-
reason: 'Not in devDependencies',
313-
}
314-
}
315-
316304
// Create temp directory for this package.
305+
const sanitizedName = packageName.replace(/[@/]/g, '-')
317306
const tempDir = await fs.mkdtemp(
318-
path.join(os.tmpdir(), `socket-test-${socketPkgName}-`),
307+
path.join(os.tmpdir(), `socket-test-${sanitizedName}-`),
319308
)
320-
const packageTempDir = path.join(tempDir, socketPkgName)
309+
const packageTempDir = path.join(tempDir, sanitizedName)
321310
await fs.mkdir(packageTempDir, { recursive: true })
322311

323-
// Create minimal package.json in temp directory.
324-
await fs.writeFile(
325-
path.join(packageTempDir, 'package.json'),
326-
JSON.stringify(
327-
{
328-
name: 'test-temp',
329-
version: '1.0.0',
330-
private: true,
331-
},
332-
null,
333-
2,
334-
),
335-
)
312+
let installedPath
313+
let originalScripts
314+
let originalDevDependencies
315+
316+
if (versionSpec) {
317+
// Installing from npm registry first, then copying source on top
318+
// Create minimal package.json in temp directory.
319+
await fs.writeFile(
320+
path.join(packageTempDir, 'package.json'),
321+
JSON.stringify(
322+
{
323+
name: 'test-temp',
324+
version: '1.0.0',
325+
private: true,
326+
},
327+
null,
328+
2,
329+
),
330+
)
336331

337-
// Install the package.
338-
const packageSpec = versionSpec.startsWith('https://')
339-
? versionSpec
340-
: `${origPkgName}@${versionSpec}`
332+
// Install the package.
333+
const packageSpec = versionSpec.startsWith('https://')
334+
? versionSpec
335+
: `${packageName}@${versionSpec}`
341336

342-
await runCommand('pnpm', ['add', packageSpec, ...PNPM_NPM_LIKE_FLAGS], {
343-
cwd: packageTempDir,
344-
})
337+
await runCommand('pnpm', ['add', packageSpec, ...PNPM_NPM_LIKE_FLAGS], {
338+
cwd: packageTempDir,
339+
})
345340

346-
// Copy Socket override files on top.
347-
const installedPath = path.join(packageTempDir, 'node_modules', origPkgName)
341+
installedPath = path.join(packageTempDir, 'node_modules', packageName)
348342

349-
// Check if the installed path is a symlink to the override path.
350-
let realInstalledPath
351-
try {
352-
realInstalledPath = await fs.realpath(installedPath)
353-
} catch {
354-
realInstalledPath = path.resolve(installedPath)
355-
}
343+
// Check if the installed path is a symlink to the source path.
344+
let realInstalledPath
345+
try {
346+
realInstalledPath = await fs.realpath(installedPath)
347+
} catch {
348+
realInstalledPath = path.resolve(installedPath)
349+
}
356350

357-
let realOverridePath
358-
try {
359-
realOverridePath = await fs.realpath(overridePath)
360-
} catch {
361-
realOverridePath = path.resolve(overridePath)
362-
}
351+
let realSourcePath
352+
try {
353+
realSourcePath = await fs.realpath(sourcePath)
354+
} catch {
355+
realSourcePath = path.resolve(sourcePath)
356+
}
363357

364-
// Skip if source and destination resolve to the same path.
365-
if (realOverridePath === realInstalledPath) {
366-
return {
367-
installed: false,
368-
reason: 'Package is already a Socket override (symlinked)',
358+
// Skip if source and destination resolve to the same path.
359+
if (realSourcePath === realInstalledPath) {
360+
return {
361+
installed: false,
362+
reason: 'Package is already a Socket override (symlinked)',
363+
}
369364
}
370-
}
371365

372-
// Save original scripts before copying.
373-
const originalPkgJson = await readPackageJson(installedPath, {
374-
normalize: true,
375-
})
376-
const originalScripts = originalPkgJson.scripts
366+
// Save original scripts and devDependencies before copying.
367+
const originalPkgJson = await readPackageJson(installedPath, {
368+
normalize: true,
369+
})
370+
originalScripts = originalPkgJson.scripts
371+
originalDevDependencies = originalPkgJson.devDependencies
372+
} else {
373+
// Just copying local package, no npm install
374+
const scopedPath = packageName.startsWith('@')
375+
? path.join(packageTempDir, 'node_modules', packageName.split('/')[0])
376+
: path.join(packageTempDir, 'node_modules')
377+
378+
await fs.mkdir(scopedPath, { recursive: true })
379+
installedPath = path.join(packageTempDir, 'node_modules', packageName)
380+
}
377381

378-
await fs.cp(overridePath, installedPath, {
382+
// Copy source files to installedPath
383+
await fs.cp(sourcePath, installedPath, {
379384
force: true,
380385
recursive: true,
381386
dereference: true,
@@ -389,9 +394,9 @@ async function installPackageForTesting(socketPkgName) {
389394
const pkgJsonPath = path.join(installedPath, 'package.json')
390395
const pkgJson = JSON.parse(await fs.readFile(pkgJsonPath, 'utf8'))
391396

392-
// Preserve devDependencies from original.
393-
if (originalPkgJson.devDependencies) {
394-
pkgJson.devDependencies = originalPkgJson.devDependencies
397+
// Preserve devDependencies from original (only when we installed from npm).
398+
if (versionSpec && originalDevDependencies) {
399+
pkgJson.devDependencies = originalDevDependencies
395400
}
396401

397402
// Preserve test scripts.

0 commit comments

Comments
 (0)