Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/commands/optimize/cmd-optimize-pnpm-versions.test.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync } from 'node:fs'
import path from 'node:path'

import trash from 'trash'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'

import { JsonContent } from '@socketsecurity/registry/lib/fs'
Expand All @@ -20,20 +21,39 @@ const fixtureBaseDir = path.join(testPath, 'fixtures/commands/optimize')
const pnpm8FixtureDir = path.join(fixtureBaseDir, 'pnpm8')
const pnpm9FixtureDir = path.join(fixtureBaseDir, 'pnpm9')

async function cleanupFixtureInstallArtifacts(
fixtureDir: string,
): Promise<void> {
const staleDirs = [
path.join(fixtureDir, '.cache'),
path.join(fixtureDir, 'node_modules'),
]

try {
await Promise.all(
staleDirs.filter(dir => existsSync(dir)).map(dir => trash(dir)),
)
} catch {
// Ignore cleanup errors.
}
}

describe('socket optimize - pnpm versions', { timeout: 60_000 }, async () => {
const { binCliPath } = constants

describe('pnpm v8', () => {
const pnpm8BinPath = path.join(pnpm8FixtureDir, 'node_modules', '.bin')

beforeEach(async () => {
// Reset fixtures to their committed state (package.json and pnpm-lock.yaml).
// Reset fixtures to their committed state (package.json and pnpm-lock.yaml)
// and remove untracked install artifacts left by prior runs.
try {
await spawn('git', ['checkout', 'HEAD', '--', '.'], {
cwd: pnpm8FixtureDir,
stdio: 'ignore',
})
} catch {}
await cleanupFixtureInstallArtifacts(pnpm8FixtureDir)
// Ensure pnpm v8 is installed in the fixture.
// Skip if pnpm is not available globally (e.g., Windows CI).
try {
Expand All @@ -54,13 +74,14 @@ describe('socket optimize - pnpm versions', { timeout: 60_000 }, async () => {
})

afterEach(async () => {
// Reset fixtures to their committed state after each test.
// Reset tracked fixture files and remove install artifacts.
try {
await spawn('git', ['checkout', 'HEAD', '--', '.'], {
cwd: pnpm8FixtureDir,
stdio: 'ignore',
})
} catch {}
await cleanupFixtureInstallArtifacts(pnpm8FixtureDir)
})

it(
Expand Down Expand Up @@ -161,13 +182,15 @@ describe('socket optimize - pnpm versions', { timeout: 60_000 }, async () => {
const pnpm9BinPath = path.join(pnpm9FixtureDir, 'node_modules', '.bin')

beforeEach(async () => {
// Reset fixtures to their committed state (package.json and pnpm-lock.yaml).
// Reset fixtures to their committed state (package.json and pnpm-lock.yaml)
// and remove untracked install artifacts left by prior runs.
try {
await spawn('git', ['checkout', 'HEAD', '--', '.'], {
cwd: pnpm9FixtureDir,
stdio: 'ignore',
})
} catch {}
await cleanupFixtureInstallArtifacts(pnpm9FixtureDir)
// Ensure pnpm v9 is installed in the fixture.
// Skip if pnpm is not available globally (e.g., Windows CI).
try {
Expand All @@ -188,13 +211,14 @@ describe('socket optimize - pnpm versions', { timeout: 60_000 }, async () => {
})

afterEach(async () => {
// Reset fixtures to their committed state after each test.
// Reset tracked fixture files and remove install artifacts.
try {
await spawn('git', ['checkout', 'HEAD', '--', '.'], {
cwd: pnpm9FixtureDir,
stdio: 'ignore',
})
} catch {}
await cleanupFixtureInstallArtifacts(pnpm9FixtureDir)
})

it(
Expand Down
Loading