Skip to content

Commit 9b79bfe

Browse files
authored
fix(plugin-npm): normalize registry (#5582)
**What's the problem this PR addresses?** While working on #5581 I noticed that we're not normalizing the registry passed to the `npmHttpUtils` methods. There are tests that are supposed to catch this but the mock wasn't reset before each test so they didn't. **How did you fix it?** Normalize the registry and update the tests to use `wrapNetworkRequest`. **Checklist** - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). - [x] I have set the packages that need to be released for my changes to be effective. - [x] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent 506ded5 commit 9b79bfe

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

Diff for: .yarn/versions/4a8968a1.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/plugin-npm": patch
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-essentials"
10+
- "@yarnpkg/plugin-init"
11+
- "@yarnpkg/plugin-interactive-tools"
12+
- "@yarnpkg/plugin-nm"
13+
- "@yarnpkg/plugin-npm-cli"
14+
- "@yarnpkg/plugin-pack"
15+
- "@yarnpkg/plugin-patch"
16+
- "@yarnpkg/plugin-pnp"
17+
- "@yarnpkg/plugin-pnpm"
18+
- "@yarnpkg/plugin-stage"
19+
- "@yarnpkg/plugin-typescript"
20+
- "@yarnpkg/plugin-version"
21+
- "@yarnpkg/plugin-workspace-tools"
22+
- "@yarnpkg/builder"
23+
- "@yarnpkg/core"
24+
- "@yarnpkg/doctor"

Diff for: packages/plugin-npm/sources/npmHttpUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ function normalizeRegistry(configuration: Configuration, {ident, registry}: Part
353353
if (typeof registry !== `string`)
354354
throw new Error(`Assertion failed: The registry should be a string`);
355355

356-
return registry;
356+
return npmConfigUtils.normalizeRegistry(registry);
357357
}
358358

359359
async function getAuthenticationHeader(registry: string, {authType = AuthType.CONFIGURATION, configuration, ident}: {authType?: AuthType, configuration: Configuration, ident: RegistryOptions['ident']}) {

Diff for: packages/plugin-npm/tests/npmHttpUtils.test.js renamed to packages/plugin-npm/tests/npmHttpUtils.test.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
import {httpUtils} from '@yarnpkg/core';
21
import {npmHttpUtils} from '@yarnpkg/plugin-npm';
32

43
import {makeConfiguration} from './_makeConfiguration';
54

6-
jest.mock(`@yarnpkg/core`, () => ({
7-
...jest.requireActual(`@yarnpkg/core`),
8-
httpUtils: {
9-
...jest.requireActual(`@yarnpkg/core`).httpUtils,
10-
get: jest.fn(() => Promise.resolve()),
11-
},
12-
}));
13-
145
describe(`npmHttpUtils.get`, () => {
156
for (const registry of [`https://example.org`, `https://example.org/`, `https://example.org/foo`, `https://example.org/foo/`]) {
167
for (const path of [`/bar`]) {
@@ -19,12 +10,18 @@ describe(`npmHttpUtils.get`, () => {
1910
it(`should craft the final path correctly (${registry} + ${path} = ${expected})`, async () => {
2011
const configuration = await makeConfiguration();
2112

13+
let actualTarget: string | undefined;
2214
await npmHttpUtils.get(path, {
2315
configuration,
2416
registry,
17+
async wrapNetworkRequest(executor, extra) {
18+
actualTarget = extra.target.toString();
19+
20+
return () => Promise.resolve({body: {}, headers: {}, statusCode: 200});
21+
},
2522
});
2623

27-
expect(httpUtils.get).toHaveBeenCalledWith(expected, expect.anything());
24+
expect(actualTarget).toEqual(expected);
2825
});
2926
}
3027
}

0 commit comments

Comments
 (0)