Skip to content

Commit

Permalink
lookup: fix handling of npm-package-arg result
Browse files Browse the repository at this point in the history
Convert `fetchSpec` to "latest" when it's "*" to accomodate for
the breaking change that happened in version 10 of the library.
Make lookup tests closer to reality by using the package arg parser
there and constructing fake module metadata that look more like
what the npm API returns.

Refs: #628
Refs: https://github.com/npm/npm-package-arg/releases/tag/v10.0.0
  • Loading branch information
targos committed Jun 5, 2024
1 parent 66069f3 commit 324a4a3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 77 deletions.
17 changes: 2 additions & 15 deletions lib/citgm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { createHash } from 'crypto';
import { EventEmitter } from 'events';

import npa from 'npm-package-arg';
import BufferList from 'bl';
import which from 'which';

Expand All @@ -15,6 +13,7 @@ import {
} from './package-manager/index.js';
import * as tempDirectory from './temp-directory.js';
import { unpack } from './unpack.js';
import { parsePackageArg } from './utils.js';

export const windows = process.platform === 'win32';

Expand Down Expand Up @@ -53,18 +52,6 @@ function init(context) {
);
}

function sha1(mod) {
const shasum = createHash('sha1');
shasum.update(mod);
return shasum.digest('hex');
}

function extractDetail(mod) {
const detail = npa(mod); // Will throw if mod is invalid
detail.name = detail.name || `noname-${sha1(mod)}`;
return detail;
}

/**
* The Tester will emit specific events once it is run:
* - end = emitted when the test is complete
Expand All @@ -75,7 +62,7 @@ function extractDetail(mod) {
export class Tester extends EventEmitter {
constructor(mod, options) {
super();
this.module = extractDetail(mod);
this.module = parsePackageArg(mod);
this.options = options;
this.testOutput = new BufferList();
this.testError = new BufferList();
Expand Down
18 changes: 18 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createHash } from 'node:crypto';
import fs from 'node:fs/promises';

import npa from 'npm-package-arg';

/**
* Remove directory recursively with retries for Windows.
* @param path
Expand All @@ -12,3 +15,18 @@ export async function removeDirectory(path) {
retryDelay: 50
});
}

function sha1(mod) {
const shasum = createHash('sha1');
shasum.update(mod);
return shasum.digest('hex');
}

export function parsePackageArg(mod) {
const detail = npa(mod); // Will throw if mod is invalid
detail.name = detail.name || `noname-${sha1(mod)}`;
if (detail.fetchSpec === '*') {
detail.fetchSpec = 'latest';
}
return detail;
}
129 changes: 67 additions & 62 deletions test/test-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,35 @@ import { readFileSync } from 'fs';
import tap from 'tap';

import { getLookupTable, makeUrl, lookup } from '../lib/lookup.js';
import { parsePackageArg } from '../lib/utils.js';

const { test } = tap;

function createFakeMeta(config) {
const { name, version = '1.0.0', repository, gitHead } = config;
if (!name) {
throw new Error('name is required');
}
const now = new Date().toISOString();
return {
_id: `${name}@${version}`,
name,
description: 'Some module',
'dist-tags': {
latest: version
},
versions: [version],
time: {
created: now,
modified: now,
[version]: now
},
repository,
version,
gitHead
};
}

test('lookup: makeUrl', (t) => {
t.plan(5);
const repo = 'https://github.com/nodejs/citgm';
Expand Down Expand Up @@ -103,19 +129,18 @@ test('lookup: module not in table', (t) => {
t.plan(1);
const context = {
lookup: null,
module: {
name: 'omg-i-pass',
raw: null
},
meta: {},
module: parsePackageArg('omg-i-pass'),
meta: createFakeMeta({ name: 'omg-i-pass' }),
options: {},
emit: function () {}
};

const rawBefore = context.module.raw;
lookup(context);
t.notOk(
t.equal(
context.module.raw,
'raw should remain falsey if module is not in lookup'
rawBefore,
'raw should remain unchanged if module is not in lookup'
);
t.end();
});
Expand All @@ -124,16 +149,14 @@ test('lookup: module not in table with gitHead', (t) => {
t.plan(1);
const context = {
lookup: null,
module: {
module: parsePackageArg('omg-i-pass'),
meta: createFakeMeta({
name: 'omg-i-pass',
raw: null
},
meta: {
repository: {
url: 'https://github.com/nodejs/omg-i-pass'
},
gitHead: 'abc123'
},
}),
options: {},
emit: function () {}
};
Expand All @@ -151,23 +174,21 @@ test('lookup: module in table', (t) => {
t.plan(1);
const context = {
lookup: null,
module: {
module: parsePackageArg('lodash'),
meta: createFakeMeta({
name: 'lodash',
raw: null
},
meta: {
repository: {
url: 'https://github.com/lodash/lodash'
}
},
}),
options: {},
emit: function () {}
};

lookup(context);
t.equal(
context.module.raw,
'https://github.com/lodash/lodash/archive/HEAD.tar.gz',
'https://github.com/lodash/lodash/archive/1.0.0.tar.gz',
'raw should be truthy if the module was in the list'
);
t.end();
Expand All @@ -177,16 +198,14 @@ test('lookup: module in table with gitHead', (t) => {
t.plan(1);
const context = {
lookup: null,
module: {
module: parsePackageArg('lodash'),
meta: createFakeMeta({
name: 'lodash',
raw: null
},
meta: {
repository: {
url: 'https://github.com/lodash/lodash'
},
gitHead: 'abc123'
},
}),
options: {},
emit: function () {}
};
Expand All @@ -203,16 +222,13 @@ test('lookup: module in table with gitHead', (t) => {
test('lookup: module in table with scripts', (t) => {
t.plan(1);
const context = {
module: {
module: parsePackageArg('omg-i-pass-with-scripts'),
meta: createFakeMeta({
name: 'omg-i-pass-with-scripts',
raw: null
},
meta: {
repository: {
url: 'git+https://github.com/nodejs/citgm'
},
version: '1.0.0'
},
}
}),
options: {
lookup: 'test/fixtures/custom-lookup-scripts.json'
},
Expand All @@ -232,17 +248,14 @@ test('lookup: module in table with useGitClone', (t) => {
t.plan(2);
const context = {
lookup: null,
module: {
fetchSpec: 'latest',
module: parsePackageArg('lodash'),
meta: createFakeMeta({
name: 'lodash',
raw: null
},
meta: {
'dist-tags': { latest: '1.2.3' },
version: '1.2.3',
repository: {
url: 'https://github.com/lodash/lodash'
}
},
}),
options: {
lookup: 'test/fixtures/custom-lookup-useGitClone.json'
},
Expand Down Expand Up @@ -278,14 +291,11 @@ test('lookup: no table', (t) => {
test('lookup: replace with no repo', (t) => {
t.plan(1);
const context = {
module: {
module: parsePackageArg('omg-i-pass'),
meta: createFakeMeta({
name: 'omg-i-pass',
raw: null
},
meta: {
repository: undefined,
version: '1.2.3'
},
}),
options: {
lookup: 'test/fixtures/custom-lookup-no-repo.json'
},
Expand All @@ -304,15 +314,14 @@ test('lookup: not found in lookup.json with --sha', (t) => {
t.plan(1);
const context = {
lookup: null,
module: {
name: 'test'
},
meta: {
gitHead: 'metaGitHead',
module: parsePackageArg('test'),
meta: createFakeMeta({
name: 'test',
repository: {
url: 'https://github.com/test-org/test-repo'
}
},
},
gitHead: 'metaGitHead'
}),
options: {
sha: 'customsha'
},
Expand All @@ -331,15 +340,13 @@ test('lookup: --fail-flaky', (t) => {
t.plan(1);
const context = {
lookup: null,
module: {
module: parsePackageArg('lodash'),
meta: createFakeMeta({
name: 'lodash',
raw: null
},
meta: {
repository: {
url: 'https://github.com/lodash/lodash'
}
},
}),
options: {
failFlaky: true
},
Expand Down Expand Up @@ -372,14 +379,12 @@ test('lookup: ensure lookup works', (t) => {
test('lookup: lookup with install', (t) => {
t.plan(1);
const context = {
module: {
module: parsePackageArg('omg-i-pass-with-install-param'),
meta: createFakeMeta({
name: 'omg-i-pass-with-install-param',
raw: null
},
meta: {
repository: '/dev/null',
version: '0.1.1'
},
version: '0.1.1',
repository: '/dev/null'
}),
options: {
lookup: 'test/fixtures/custom-lookup-install.json'
},
Expand Down

0 comments on commit 324a4a3

Please sign in to comment.