fix(tests): support npm pack object output

This commit is contained in:
dajiaohuang
2026-08-24 20:09:12 -04:00
committed by haelyra
parent 0b04c1bfa1
commit 6d42f32ca8
5 changed files with 77 additions and 4 deletions
+21
View File
@@ -0,0 +1,21 @@
function isPackEntry(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
function getNpmPackEntry(output, packageName) {
if (Array.isArray(output)) {
return output.find(isPackEntry);
}
if (!isPackEntry(output)) {
return undefined;
}
if (isPackEntry(output[packageName])) {
return output[packageName];
}
return Object.values(output).find(isPackEntry);
}
module.exports = { getNpmPackEntry };
+46
View File
@@ -0,0 +1,46 @@
const assert = require('assert');
const { getNpmPackEntry } = require('./npm-pack-output');
let passed = 0;
let failed = 0;
function test(name, fn) {
try {
fn();
console.log(`${name}`);
passed += 1;
} catch (error) {
console.log(`${name}`);
console.error(` ${error.message}`);
failed += 1;
}
}
test('reads the npm 11 array response', () => {
const entry = getNpmPackEntry([
{ name: 'ecc-universal', filename: 'ecc-universal-2.2.0.tgz' },
], 'ecc-universal');
assert.strictEqual(entry.filename, 'ecc-universal-2.2.0.tgz');
});
test('reads the npm 12 package-keyed response', () => {
const entry = getNpmPackEntry({
'ecc-universal': {
name: 'ecc-universal',
filename: 'ecc-universal-2.2.0.tgz',
},
}, 'ecc-universal');
assert.strictEqual(entry.filename, 'ecc-universal-2.2.0.tgz');
});
test('returns undefined for empty or malformed responses', () => {
assert.strictEqual(getNpmPackEntry([], 'ecc-universal'), undefined);
assert.strictEqual(getNpmPackEntry({}, 'ecc-universal'), undefined);
assert.strictEqual(getNpmPackEntry(null, 'ecc-universal'), undefined);
});
console.log(`\nPassed: ${passed}`);
console.log(`Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);
+3 -1
View File
@@ -6,6 +6,7 @@ const assert = require("assert")
const fs = require("fs")
const path = require("path")
const { spawnSync } = require("child_process")
const { getNpmPackEntry } = require("../lib/npm-pack-output")
function runTest(name, fn) {
try {
@@ -54,7 +55,8 @@ function main() {
assert.strictEqual(result.status, 0, result.error?.message || result.stderr)
const packOutput = JSON.parse(result.stdout)
const packagedPaths = new Set(packOutput[0]?.files?.map((file) => file.path) ?? [])
const packEntry = getNpmPackEntry(packOutput, packageJson.name)
const packagedPaths = new Set(packEntry?.files?.map((file) => file.path) ?? [])
assert.ok(
packagedPaths.has(".opencode/dist/index.js"),
+4 -2
View File
@@ -11,6 +11,7 @@ const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
const { getNpmPackEntry } = require('../lib/npm-pack-output');
const repoRoot = path.join(__dirname, '..', '..');
const packageJson = JSON.parse(
@@ -123,14 +124,15 @@ function getPackedFixture() {
['pack', '--json', '--ignore-scripts', '--pack-destination', directory]
);
const packOutput = JSON.parse(packResult.stdout);
const filename = packOutput[0]?.filename;
const packEntry = getNpmPackEntry(packOutput, packageJson.name);
const filename = packEntry?.filename;
assert.ok(filename, 'npm pack should report the archive filename');
packedFixture = {
archivePath: path.join(directory, filename),
directory,
publishedPaths: new Set(
packOutput[0]?.files?.map(file => file.path) || []
packEntry?.files?.map(file => file.path) || []
),
};
return packedFixture;
+3 -1
View File
@@ -6,6 +6,7 @@ const assert = require("assert")
const fs = require("fs")
const path = require("path")
const { spawnSync } = require("child_process")
const { getNpmPackEntry } = require("../lib/npm-pack-output")
function runTest(name, fn) {
try {
@@ -149,7 +150,8 @@ function main() {
assert.strictEqual(result.status, 0, result.error?.message || result.stderr)
const packOutput = JSON.parse(result.stdout)
const packagedPaths = new Set(packOutput[0]?.files?.map((file) => file.path) ?? [])
const packEntry = getNpmPackEntry(packOutput, packageJson.name)
const packagedPaths = new Set(packEntry?.files?.map((file) => file.path) ?? [])
for (const requiredPath of [
"scripts/catalog.js",