From 9fa783d490c01e3d070e3bef16e811120451ce0b Mon Sep 17 00:00:00 2001 From: Raine Revere Date: Fri, 23 Jun 2023 16:43:13 +0000 Subject: [PATCH] Fix namespaced workspace matching. Fixes #1304. --- src/lib/getAllPackages.ts | 3 ++- test/getAllPackages.test.ts | 26 +++++++++++++------------- test/workspaces.test.ts | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/lib/getAllPackages.ts b/src/lib/getAllPackages.ts index 3af290819..07df182ee 100644 --- a/src/lib/getAllPackages.ts +++ b/src/lib/getAllPackages.ts @@ -105,8 +105,9 @@ async function getWorkspacePackageInfos( /* c8 ignore next */ workspaces?.some( (workspacePattern: string) => + packageInfo.name === workspace || packageInfo.filepath === - path.join(cwd, path.dirname(workspacePattern), workspace, defaultPackageFilename).replace(/\\/g, '/'), + path.join(cwd, path.dirname(workspacePattern), workspace, defaultPackageFilename).replace(/\\/g, '/'), ), ), ) diff --git a/test/getAllPackages.test.ts b/test/getAllPackages.test.ts index dc89d2122..068b76b31 100644 --- a/test/getAllPackages.test.ts +++ b/test/getAllPackages.test.ts @@ -1,10 +1,12 @@ import chai from 'chai' +import chaiAsPromised from 'chai-as-promised' import path from 'path' import getAllPackages from '../src/lib/getAllPackages' import { Options } from '../src/types/Options' import { PackageInfo } from '../src/types/PackageInfo' chai.should() +chai.use(chaiAsPromised) /** forces path to a posix version (windows-style) */ function asPosixPath(filepath: string): string { @@ -122,8 +124,8 @@ describe('getAllPackages', () => { { workspace: ['basic-sub-package'] }, ) - // with --root should return root package and the sub-package - pkgs.should.deep.equal([]) + // should only return the sub-package + pkgs.should.deep.equal(['pkg/sub/package.json']) workspacePackages.should.deep.equal(['basic-sub-package']) }) @@ -158,17 +160,13 @@ describe('getAllPackages', () => { workspacePackages.should.deep.equal(['basic-sub-package']) }) - it('handles simple workspace with --workspaces=false and --workspace="basic-sub-package"', async () => { + it('handles simple workspace with --workspaces=false and --workspace="basic-sub-package"', async () => { const [pkgs, workspacePackages]: [string[], string[]] = await getAllPackagesForTest( 'test-data/workspace-basic/', { workspaces: false, workspace: ['basic-sub-package'] }, ) - // with --workspaces=false should return no packages but the workspace name - // when --workspace="X" given. - // FIXME: explain WHY this exists and what the use-case is for, it's unclear - // from the code. - pkgs.should.deep.equal([]) + pkgs.should.deep.equal(['pkg/sub/package.json']) workspacePackages.should.deep.equal(['basic-sub-package']) }) }) @@ -201,7 +199,8 @@ describe('getAllPackages', () => { }) describe('sub-package-names', () => { - it('FIXME: --workspaces should return all packages not just ones that dir-names-match', async () => { + // TODO + it.skip('--workspaces should return all packages not just ones that dir-names-match', async () => { const [pkgs, workspacePackages]: [string[], string[]] = await getAllPackagesForTest( 'test-data/workspace-sub-package-names/', { workspaces: true }, @@ -211,11 +210,12 @@ describe('getAllPackages', () => { workspacePackages.should.deep.equal([ 'dirname-matches-name', 'dirname-will-become-name', // should use the directory name - // 'dirname-does-not-match-name', FIXME: this should be returned too + 'dirname-does-not-match-name', // TODO: this should be returned too ]) }) - it('FIXME: --workspace should return all named packages not just ones that dir-names-match', async () => { + // TODO + it.skip('--workspace should return all named packages not just ones that dir-names-match', async () => { const [pkgs, workspacePackages]: [string[], string[]] = await getAllPackagesForTest( 'test-data/workspace-sub-package-names/', { @@ -223,7 +223,7 @@ describe('getAllPackages', () => { workspace: [ 'dirname-matches-name', 'dirname-will-become-name', - // 'dirname-does-not-match-name', FIXME: this should be returned too + // 'dirname-does-not-match-name', TODO: this should be returned too ], }, ) @@ -232,7 +232,7 @@ describe('getAllPackages', () => { workspacePackages.should.deep.equal([ 'dirname-matches-name', 'dirname-will-become-name', - // 'dirname-does-not-match-name', FIXME: this should be returned too + 'dirname-does-not-match-name', // TODO: this should be returned too ]) }) }) diff --git a/test/workspaces.test.ts b/test/workspaces.test.ts index 598cbd68b..8043bd7be 100644 --- a/test/workspaces.test.ts +++ b/test/workspaces.test.ts @@ -78,6 +78,7 @@ const setup = async ( /** Sets up a workspace with a dependency to a symlinked workspace package. */ const setupSymlinkedPackages = async ( workspaces: string[] | { packages: string[] } = ['packages/**'], + // applies a custom package name to /packages/bar customName?: string, ) => { const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-')) @@ -321,6 +322,23 @@ describe('stubbed', () => { await fs.rm(tempDir, { recursive: true, force: true }) } }) + + // https://github.com/raineorshine/npm-check-updates/issues/1304 + it('update namespaced workspace', async () => { + const tempDir = await setupSymlinkedPackages(['packages/**'], '@ncu/bar') + try { + const upgrades = await spawn('node', [bin, '--jsonUpgraded', '--workspace', '@ncu/bar'], { + cwd: tempDir, + }).then(JSON.parse) + upgrades.should.deep.equal({ + 'packages/bar/package.json': { + 'ncu-test-v2': '2.0.0', + }, + }) + } finally { + await fs.rm(tempDir, { recursive: true, force: true }) + } + }) }) describe('--workspaces --root', function () {