diff options
author | Ruy Adorno <ruyadorno@hotmail.com> | 2022-03-24 18:46:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-24 18:46:54 +0300 |
commit | 723a0918a5a9d9f795584f85d04506fafda9ca42 (patch) | |
tree | 37f0f659d7b81abd9b6cb6d2038ce83571e2c2b4 /test | |
parent | 362831c4eba2554b44feec60fdff197d92eac0c1 (diff) |
feat(version): reify on workspace version change (#4588)
Adds a minimalistic reify step that updates the installed tree after a
version change within one of the configured workspaces when using any
of the workspaces config options.
It's also possible to use the `--save` config option in order to
auto update semver ranges of dependencies declarations accross dependent
`package.json` files.
Fixes: https://github.com/npm/cli/issues/3403
Relates to: https://github.com/npm/rfcs/issues/556
Relates to: https://github.com/npm/cli/issues/3757
Relates to: https://github.com/npm/cli/issues/4193
Diffstat (limited to 'test')
-rw-r--r-- | test/lib/commands/version.js | 117 |
1 files changed, 110 insertions, 7 deletions
diff --git a/test/lib/commands/version.js b/test/lib/commands/version.js index 980353897..154f6a6f8 100644 --- a/test/lib/commands/version.js +++ b/test/lib/commands/version.js @@ -1,3 +1,5 @@ +const { readFileSync, statSync } = require('fs') +const { resolve } = require('path') const t = require('tap') const { fake: mockNpm } = require('../../fixtures/mock-npm') const mockGlobals = require('../../fixtures/mock-globals.js') @@ -10,8 +12,13 @@ const config = { 'tag-version-prefix': 'v', json: false, } +const flatOptions = { + workspacesUpdate: true, +} const npm = mockNpm({ config, + flatOptions, + localPrefix: '', prefix: '', version: '1.0.0', output: (...msg) => { @@ -21,14 +28,16 @@ const npm = mockNpm({ }, }) const mocks = { - libnpmversion: noop, + '../../../lib/utils/reify-finish.js': noop, } const Version = t.mock('../../../lib/commands/version.js', mocks) const version = new Version(npm) t.afterEach(() => { + flatOptions.workspacesUpdate = true config.json = false + npm.localPrefix = '' npm.prefix = '' result = [] }) @@ -120,7 +129,7 @@ t.test('empty versions', t => { ...mocks, libnpmversion: (arg, opts) => { t.equal(arg, 'major', 'should forward expected value') - t.same( + t.match( opts, { path: '', @@ -271,7 +280,6 @@ t.test('empty versions', t => { }) t.test('with one arg, all workspaces', async t => { - const libNpmVersionArgs = [] const testDir = t.testdir({ 'package.json': JSON.stringify( { @@ -296,12 +304,54 @@ t.test('empty versions', t => { }, }) const Version = t.mock('../../../lib/commands/version.js', { - ...mocks, - libnpmversion: (arg, opts) => { - libNpmVersionArgs.push([arg, opts]) - return '2.0.0' + '../../../lib/utils/reify-finish.js': noop, + }) + npm.localPrefix = testDir + npm.prefix = testDir + const version = new Version(npm) + + await version.execWorkspaces(['major'], []) + t.same( + result, + ['workspace-a', 'v2.0.0', 'workspace-b', 'v2.0.0'], + 'outputs the new version for only the workspaces prefixed by the tagVersionPrefix' + ) + + t.matchSnapshot(readFileSync(resolve(testDir, 'package-lock.json'), 'utf8')) + }) + + t.test('with one arg, all workspaces, saves package.json', async t => { + const testDir = t.testdir({ + 'package.json': JSON.stringify( + { + name: 'workspaces-test', + version: '1.0.0', + workspaces: ['workspace-a', 'workspace-b'], + dependencies: { + 'workspace-a': '^1.0.0', + 'workspace-b': '^1.0.0', + }, + }, + null, + 2 + ), + 'workspace-a': { + 'package.json': JSON.stringify({ + name: 'workspace-a', + version: '1.0.0', + }), }, + 'workspace-b': { + 'package.json': JSON.stringify({ + name: 'workspace-b', + version: '1.0.0', + }), + }, + }) + const Version = t.mock('../../../lib/commands/version.js', { + '../../../lib/utils/reify-finish.js': noop, }) + config.save = true npm.localPrefix = testDir npm.prefix = testDir const version = new Version(npm) @@ -312,6 +362,8 @@ t.test('empty versions', t => { ['workspace-a', 'v2.0.0', 'workspace-b', 'v2.0.0'], 'outputs the new version for only the workspaces prefixed by the tagVersionPrefix' ) + + t.matchSnapshot(readFileSync(resolve(testDir, 'package-lock.json'), 'utf8')) }) t.test('too many args', async t => { @@ -321,6 +373,57 @@ t.test('empty versions', t => { 'should throw usage instructions error' ) }) + + t.test('no workspaces-update', async t => { + flatOptions.workspacesUpdate = false + + const libNpmVersionArgs = [] + const testDir = t.testdir({ + 'package.json': JSON.stringify( + { + name: 'workspaces-test', + version: '1.0.0', + workspaces: ['workspace-a', 'workspace-b'], + }, + null, + 2 + ), + 'workspace-a': { + 'package.json': JSON.stringify({ + name: 'workspace-a', + version: '1.0.0', + }), + }, + 'workspace-b': { + 'package.json': JSON.stringify({ + name: 'workspace-b', + version: '1.0.0', + }), + }, + }) + const Version = t.mock('../../../lib/commands/version.js', { + ...mocks, + libnpmversion: (arg, opts) => { + libNpmVersionArgs.push([arg, opts]) + return '2.0.0' + }, + }) + npm.localPrefix = testDir + npm.prefix = testDir + const version = new Version(npm) + + await version.execWorkspaces(['major'], []) + t.same( + result, + ['workspace-a', 'v2.0.0', 'workspace-b', 'v2.0.0'], + 'outputs the new version for only the workspaces prefixed by the tagVersionPrefix' + ) + + t.throws( + () => statSync(resolve(testDir, 'package-lock.json')), + 'should not have a lockfile since have not reified' + ) + }) }) t.end() |