From 86c1e37b285ee81028a0e5734dbba6f4fd9e896d Mon Sep 17 00:00:00 2001 From: xsbchen Date: Fri, 2 Feb 2024 14:23:07 +0800 Subject: [PATCH] fix: slug string conflict with scope --- .../sources/commands/pack.test.js | 4 ++-- packages/yarnpkg-core/sources/structUtils.ts | 2 +- .../yarnpkg-core/tests/structUtils.test.ts | 23 +++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 packages/yarnpkg-core/tests/structUtils.test.ts diff --git a/packages/acceptance-tests/pkg-tests-specs/sources/commands/pack.test.js b/packages/acceptance-tests/pkg-tests-specs/sources/commands/pack.test.js index 8cefaf95e194..21138f0727ea 100644 --- a/packages/acceptance-tests/pkg-tests-specs/sources/commands/pack.test.js +++ b/packages/acceptance-tests/pkg-tests-specs/sources/commands/pack.test.js @@ -765,7 +765,7 @@ describe(`Commands`, () => { await run(`install`); await run(`pack`, `--out`, `%s.tgz`); - expect(xfs.existsSync(`${path}/@scope-test.tgz`)).toEqual(true); + expect(xfs.existsSync(`${path}/@scope_test.tgz`)).toEqual(true); }), ); @@ -791,7 +791,7 @@ describe(`Commands`, () => { await run(`install`); await run(`pack`, `--out`, `%s-%v.tgz`); - expect(xfs.existsSync(`${path}/@scope-test-0.0.1.tgz`)).toEqual(true); + expect(xfs.existsSync(`${path}/@scope_test-0.0.1.tgz`)).toEqual(true); }), ); diff --git a/packages/yarnpkg-core/sources/structUtils.ts b/packages/yarnpkg-core/sources/structUtils.ts index b47708ebed8a..62316eea9bf1 100644 --- a/packages/yarnpkg-core/sources/structUtils.ts +++ b/packages/yarnpkg-core/sources/structUtils.ts @@ -665,7 +665,7 @@ export function stringifyLocator(locator: Locator) { */ export function slugifyIdent(ident: Ident) { if (ident.scope !== null) { - return `@${ident.scope}-${ident.name}`; + return `@${ident.scope}_${ident.name}`; } else { return ident.name; } diff --git a/packages/yarnpkg-core/tests/structUtils.test.ts b/packages/yarnpkg-core/tests/structUtils.test.ts new file mode 100644 index 000000000000..8b17d647edd6 --- /dev/null +++ b/packages/yarnpkg-core/tests/structUtils.test.ts @@ -0,0 +1,23 @@ +import * as structUtils from '../sources/structUtils'; + +describe(`structUtils`, () => { + describe(`makeIdent`, () => { + it(`should return a unique slug string with scope`, () => { + const slugA = structUtils.slugifyIdent(structUtils.makeIdent(`myscope`, `user-email`)); + expect(slugA).toEqual(`@myscope_user-email`); + const slugB = structUtils.slugifyIdent(structUtils.makeIdent(`myscope-user`, `email`)); + expect(slugB).toEqual(`@myscope-user_email`); + expect(slugA).not.toEqual(slugB); + }); + }); + + describe(`slugifyIdent`, () => { + it(`should return a unique slug string with scope`, () => { + const slugA = structUtils.slugifyIdent(structUtils.makeIdent(`myscope`, `user-email`)); + expect(slugA).toEqual(`@myscope_user-email`); + const slugB = structUtils.slugifyIdent(structUtils.makeIdent(`myscope-user`, `email`)); + expect(slugB).toEqual(`@myscope-user_email`); + expect(slugA).not.toEqual(slugB); + }); + }); +});