Skip to content

Commit

Permalink
Fix namespaced workspace matching. Fixes #1304.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Jun 23, 2023
1 parent 5e46ec0 commit 9fa783d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/lib/getAllPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/'),
),
),
)
Expand Down
26 changes: 13 additions & 13 deletions test/getAllPackages.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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'])
})

Expand Down Expand Up @@ -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'])
})
})
Expand Down Expand Up @@ -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 },
Expand All @@ -211,19 +210,20 @@ 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/',
{
workspaces: false,
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
],
},
)
Expand All @@ -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
])
})
})
Expand Down
18 changes: 18 additions & 0 deletions test/workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-'))
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 9fa783d

Please sign in to comment.