|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +const assert = require('node:assert'); |
| 5 | +const fs = require('node:fs'); |
| 6 | +const os = require('node:os'); |
| 7 | +const path = require('node:path'); |
| 8 | +const { execFileSync } = require('node:child_process'); |
| 9 | +const { createRequire } = require('node:module'); |
| 10 | +const { fileURLToPath } = require('node:url'); |
| 11 | + |
| 12 | +const packageRoot = path.resolve(__dirname, '..'); |
| 13 | +const npmCli = process.env.npm_execpath; |
| 14 | + |
| 15 | +if (!npmCli) { |
| 16 | + throw new Error('npm_execpath is unavailable. Run this validation through npm run test:package.'); |
| 17 | +} |
| 18 | + |
| 19 | +function runNodeScript(script, args, cwd, captureOutput = false) { |
| 20 | + return execFileSync(process.execPath, [script, ...args], { |
| 21 | + cwd, |
| 22 | + encoding: 'utf8', |
| 23 | + stdio: captureOutput ? ['ignore', 'pipe', 'inherit'] : 'inherit', |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +const packOutput = runNodeScript(npmCli, ['pack', '--ignore-scripts', '--json'], packageRoot, true); |
| 28 | +const packResult = JSON.parse(packOutput); |
| 29 | +assert.strictEqual(packResult.length, 1, 'Expected npm pack to produce exactly one package'); |
| 30 | + |
| 31 | +const tarballPath = path.join(packageRoot, packResult[0].filename); |
| 32 | +const testRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'python-environments-api-')); |
| 33 | + |
| 34 | +function canonicalPath(value) { |
| 35 | + return fs.realpathSync.native(path.resolve(value)); |
| 36 | +} |
| 37 | + |
| 38 | +try { |
| 39 | + fs.writeFileSync( |
| 40 | + path.join(testRoot, 'package.json'), |
| 41 | + JSON.stringify({ name: 'python-environments-api-consumer', private: true }), |
| 42 | + ); |
| 43 | + |
| 44 | + runNodeScript( |
| 45 | + npmCli, |
| 46 | + [ |
| 47 | + 'install', |
| 48 | + '--ignore-scripts', |
| 49 | + '--no-package-lock', |
| 50 | + '--no-save', |
| 51 | + tarballPath, |
| 52 | + '@types/node@^22.0.0', |
| 53 | + '@types/vscode@^1.99.0', |
| 54 | + ], |
| 55 | + testRoot, |
| 56 | + ); |
| 57 | + |
| 58 | + const typescriptCli = path.join(packageRoot, 'node_modules', 'typescript', 'bin', 'tsc'); |
| 59 | + const fixtureRoot = path.join(packageRoot, 'test'); |
| 60 | + |
| 61 | + for (const consumer of [ |
| 62 | + { name: 'modern', type: 'module' }, |
| 63 | + { name: 'legacy', type: 'commonjs' }, |
| 64 | + ]) { |
| 65 | + const consumerRoot = path.join(testRoot, consumer.name); |
| 66 | + fs.mkdirSync(consumerRoot); |
| 67 | + fs.copyFileSync(path.join(fixtureRoot, 'consumer.ts'), path.join(consumerRoot, 'consumer.ts')); |
| 68 | + fs.copyFileSync( |
| 69 | + path.join(fixtureRoot, `tsconfig.${consumer.name}.json`), |
| 70 | + path.join(consumerRoot, 'tsconfig.json'), |
| 71 | + ); |
| 72 | + fs.writeFileSync( |
| 73 | + path.join(consumerRoot, 'package.json'), |
| 74 | + JSON.stringify({ private: true, type: consumer.type }), |
| 75 | + ); |
| 76 | + |
| 77 | + runNodeScript(typescriptCli, ['--project', path.join(consumerRoot, 'tsconfig.json')], packageRoot); |
| 78 | + } |
| 79 | + |
| 80 | + const installedPackageRoot = path.join(testRoot, 'node_modules', '@vscode', 'python-environments'); |
| 81 | + const installedPackageJson = JSON.parse(fs.readFileSync(path.join(installedPackageRoot, 'package.json'), 'utf8')); |
| 82 | + assert.strictEqual(installedPackageJson.main, './out/cjs/main.cjs'); |
| 83 | + assert.strictEqual(installedPackageJson.types, './out/cjs/main.d.ts'); |
| 84 | + assert.deepStrictEqual(installedPackageJson.exports, { |
| 85 | + import: { |
| 86 | + types: './out/esm/main.d.ts', |
| 87 | + default: './out/esm/main.mjs', |
| 88 | + }, |
| 89 | + require: { |
| 90 | + types: './out/cjs/main.d.ts', |
| 91 | + default: './out/cjs/main.cjs', |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + for (const target of [ |
| 96 | + installedPackageJson.main, |
| 97 | + installedPackageJson.types, |
| 98 | + installedPackageJson.exports.import.types, |
| 99 | + installedPackageJson.exports.import.default, |
| 100 | + installedPackageJson.exports.require.types, |
| 101 | + installedPackageJson.exports.require.default, |
| 102 | + ]) { |
| 103 | + assert.ok(fs.statSync(path.resolve(installedPackageRoot, target)).isFile(), `${target} must be a file`); |
| 104 | + } |
| 105 | + |
| 106 | + const requireFromConsumer = createRequire(path.join(testRoot, 'legacy', 'consumer.cjs')); |
| 107 | + assert.strictEqual( |
| 108 | + canonicalPath(requireFromConsumer.resolve('@vscode/python-environments')), |
| 109 | + canonicalPath(path.join(installedPackageRoot, installedPackageJson.exports.require.default)), |
| 110 | + 'CommonJS consumers should resolve the packaged CommonJS entry point', |
| 111 | + ); |
| 112 | + |
| 113 | + const esmEntryPoint = execFileSync( |
| 114 | + process.execPath, |
| 115 | + ['--input-type=module', '--eval', "console.log(import.meta.resolve('@vscode/python-environments'))"], |
| 116 | + { |
| 117 | + cwd: path.join(testRoot, 'modern'), |
| 118 | + encoding: 'utf8', |
| 119 | + }, |
| 120 | + ).trim(); |
| 121 | + assert.strictEqual( |
| 122 | + canonicalPath(fileURLToPath(esmEntryPoint)), |
| 123 | + canonicalPath(path.join(installedPackageRoot, installedPackageJson.exports.import.default)), |
| 124 | + 'ES module consumers should resolve the packaged ES module entry point', |
| 125 | + ); |
| 126 | +} finally { |
| 127 | + fs.rmSync(testRoot, { recursive: true, force: true }); |
| 128 | +} |
0 commit comments