|
| 1 | +const fs = require('fs'); |
| 2 | +const { execSync } = require('child_process'); |
| 3 | +const path = require('path'); |
| 4 | +const os = require('os'); |
| 5 | + |
| 6 | +// Package managers to test |
| 7 | +const PACKAGE_MANAGERS = ['npm', 'pnpm']; |
| 8 | + |
| 9 | +// Well-known dependencies to install |
| 10 | +const TEST_DEPENDENCIES = ['lodash']; |
| 11 | + |
| 12 | +const results = { passed: 0, failed: 0, tests: [] }; |
| 13 | + |
| 14 | +function test(name, fn) { |
| 15 | + try { |
| 16 | + const result = fn(); |
| 17 | + results.tests.push({ name, status: result ? 'PASS' : 'FAIL', error: null }); |
| 18 | + result ? results.passed++ : results.failed++; |
| 19 | + } catch (e) { |
| 20 | + results.tests.push({ name, status: 'ERROR', error: e.message }); |
| 21 | + results.failed++; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function exec(cmd, options = {}) { |
| 26 | + try { |
| 27 | + const output = execSync(cmd, { |
| 28 | + encoding: 'utf8', |
| 29 | + timeout: 120000, // 2 minutes |
| 30 | + ...options |
| 31 | + }); |
| 32 | + return { success: true, output }; |
| 33 | + } catch (e) { |
| 34 | + return { success: false, error: e.message, output: e.stdout || '' }; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function createTempDir(prefix) { |
| 39 | + return fs.mkdtempSync(path.join(os.tmpdir(), prefix)); |
| 40 | +} |
| 41 | + |
| 42 | +function cleanup(dir) { |
| 43 | + try { |
| 44 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 45 | + } catch (e) { |
| 46 | + // Ignore cleanup errors |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function testPackageManager(pm) { |
| 51 | + const testDir = createTempDir(`pmg-e2e-${pm}-`); |
| 52 | + console.log(`\n Test directory: ${testDir}`); |
| 53 | + |
| 54 | + try { |
| 55 | + // Initialize project |
| 56 | + test(`${pm}: Initialize project`, () => { |
| 57 | + const initCmd = pm === 'npm' ? 'npm init -y' : 'pnpm init'; |
| 58 | + const result = exec(initCmd, { cwd: testDir }); |
| 59 | + if (!result.success) { |
| 60 | + console.log(` ❌ FAIL: ${result.error}`); |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + const packageJsonExists = fs.existsSync(path.join(testDir, 'package.json')); |
| 65 | + if (packageJsonExists) { |
| 66 | + console.log(` ✅ PASS: Project initialized`); |
| 67 | + return true; |
| 68 | + } |
| 69 | + console.log(` ❌ FAIL: package.json not created`); |
| 70 | + return false; |
| 71 | + }); |
| 72 | + |
| 73 | + // Add dependencies |
| 74 | + const depsStr = TEST_DEPENDENCIES.join(', '); |
| 75 | + test(`${pm}: Add dependencies (${depsStr})`, () => { |
| 76 | + const depsList = TEST_DEPENDENCIES.join(' '); |
| 77 | + const addCmd = pm === 'npm' |
| 78 | + ? `npm install ${depsList}` |
| 79 | + : `pnpm add ${depsList}`; |
| 80 | + const result = exec(addCmd, { cwd: testDir }); |
| 81 | + if (!result.success) { |
| 82 | + console.log(` ❌ FAIL: ${result.error}`); |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + // Verify dependencies were added to package.json |
| 87 | + const packageJson = JSON.parse(fs.readFileSync(path.join(testDir, 'package.json'), 'utf8')); |
| 88 | + const missingDeps = TEST_DEPENDENCIES.filter( |
| 89 | + dep => !packageJson.dependencies || !packageJson.dependencies[dep] |
| 90 | + ); |
| 91 | + if (missingDeps.length === 0) { |
| 92 | + console.log(` ✅ PASS: Dependencies added`); |
| 93 | + return true; |
| 94 | + } |
| 95 | + console.log(` ❌ FAIL: Missing dependencies in package.json: ${missingDeps.join(', ')}`); |
| 96 | + return false; |
| 97 | + }); |
| 98 | + |
| 99 | + // Verify node_modules exists |
| 100 | + test(`${pm}: Verify node_modules created`, () => { |
| 101 | + const nodeModulesExists = fs.existsSync(path.join(testDir, 'node_modules')); |
| 102 | + if (!nodeModulesExists) { |
| 103 | + console.log(` ❌ FAIL: node_modules missing`); |
| 104 | + return false; |
| 105 | + } |
| 106 | + const missingDeps = TEST_DEPENDENCIES.filter( |
| 107 | + dep => !fs.existsSync(path.join(testDir, 'node_modules', dep)) |
| 108 | + ); |
| 109 | + if (missingDeps.length === 0) { |
| 110 | + console.log(` ✅ PASS: node_modules and dependencies exist`); |
| 111 | + return true; |
| 112 | + } |
| 113 | + console.log(` ❌ FAIL: Missing in node_modules: ${missingDeps.join(', ')}`); |
| 114 | + return false; |
| 115 | + }); |
| 116 | + |
| 117 | + // Clean install (remove node_modules and reinstall) |
| 118 | + test(`${pm}: Clean install`, () => { |
| 119 | + // Remove node_modules |
| 120 | + const nodeModulesPath = path.join(testDir, 'node_modules'); |
| 121 | + fs.rmSync(nodeModulesPath, { recursive: true, force: true }); |
| 122 | + |
| 123 | + // Reinstall |
| 124 | + const installCmd = pm === 'npm' ? 'npm install' : 'pnpm install'; |
| 125 | + const result = exec(installCmd, { cwd: testDir }); |
| 126 | + if (!result.success) { |
| 127 | + console.log(` ❌ FAIL: ${result.error}`); |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + // Verify node_modules recreated with all dependencies |
| 132 | + const missingDeps = TEST_DEPENDENCIES.filter( |
| 133 | + dep => !fs.existsSync(path.join(testDir, 'node_modules', dep)) |
| 134 | + ); |
| 135 | + if (missingDeps.length === 0) { |
| 136 | + console.log(` ✅ PASS: Clean install successful`); |
| 137 | + return true; |
| 138 | + } |
| 139 | + console.log(` ❌ FAIL: Dependencies not reinstalled: ${missingDeps.join(', ')}`); |
| 140 | + return false; |
| 141 | + }); |
| 142 | + |
| 143 | + } finally { |
| 144 | + cleanup(testDir); |
| 145 | + console.log(` Cleaned up: ${testDir}`); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// Main |
| 150 | +console.log('=== PMG Package Manager E2E Tests ===\n'); |
| 151 | +console.log('This script tests basic npm/pnpm flows to ensure sandbox compatibility.\n'); |
| 152 | + |
| 153 | +for (const pm of PACKAGE_MANAGERS) { |
| 154 | + // Check if package manager is available |
| 155 | + const checkResult = exec(`which ${pm}`); |
| 156 | + if (!checkResult.success) { |
| 157 | + console.log(`--- Skipping ${pm} (not installed) ---`); |
| 158 | + continue; |
| 159 | + } |
| 160 | + |
| 161 | + console.log(`--- Testing ${pm.toUpperCase()} ---`); |
| 162 | + testPackageManager(pm); |
| 163 | +} |
| 164 | + |
| 165 | +// Summary |
| 166 | +console.log('\n=== SUMMARY ==='); |
| 167 | +console.log(`Passed: ${results.passed}/${results.tests.length}`); |
| 168 | +console.log(`Failed: ${results.failed}/${results.tests.length}`); |
| 169 | + |
| 170 | +if (results.failed > 0) { |
| 171 | + console.log('\nFailed tests:'); |
| 172 | + results.tests.filter(t => t.status !== 'PASS').forEach(t => { |
| 173 | + console.log(` - ${t.name}: ${t.status} ${t.error || ''}`); |
| 174 | + }); |
| 175 | + process.exit(1); |
| 176 | +} |
| 177 | + |
| 178 | +console.log('\nAll tests passed!'); |
0 commit comments