-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: set package.json version from release please (#192)
- Loading branch information
1 parent
c0f6c06
commit 6bc355a
Showing
5 changed files
with
221 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
const t = require('tap') | ||
const { setLogger } = require('release-please') // this avoids a release-please cycle when testing | ||
const { Node } = require('release-please/build/src/strategies/node') | ||
const { Version } = require('release-please/build/src/version') | ||
const { TagName } = require('release-please/build/src/util/tag-name') | ||
const NodeWorkspace = require('../../lib/release-please/node-workspace') | ||
const Changelog = require('../../lib/release-please/changelog') | ||
|
||
setLogger({ error () {}, warn () {}, info () {}, debug () {}, trace () {} }) | ||
|
||
const mockNodeWorkspace = async (workspaceNames = ['a']) => { | ||
const names = { '.': 'npm' } | ||
const versions = { '.': new Version(1, 1, 1) } | ||
|
||
for (const ws of workspaceNames) { | ||
names[`workspaces/${ws}`] = `@npmcli/${ws}` | ||
versions[`workspaces/${ws}`] = new Version(2, 2, 2) | ||
} | ||
|
||
const paths = Object.keys(names) | ||
|
||
const github = { | ||
repository: { owner: 'npm', repo: 'cli' }, | ||
getFileContentsOnBranch: (file) => { | ||
const path = file.replace(/\/?package.json$/, '') || '.' | ||
const dependencies = path === '.' ? paths.filter(p => p !== '.').reduce((acc, ws) => { | ||
acc[names[ws]] = `^${versions[ws]}` | ||
return acc | ||
}, {}) : {} | ||
return { | ||
parsedContent: JSON.stringify({ | ||
name: names[path], | ||
version: versions[path].toString(), | ||
dependencies, | ||
}), | ||
} | ||
}, | ||
} | ||
|
||
const workspaces = (fn) => paths.reduce((acc, p) => { | ||
acc[p] = fn(p) | ||
return acc | ||
}, {}) | ||
|
||
return { | ||
workspaces, | ||
github, | ||
versions, | ||
paths, | ||
plugin: new NodeWorkspace(github, 'latest', workspaces(() => ({ releaseType: 'node' }))), | ||
} | ||
} | ||
|
||
const mockPullRequests = async (workspace, updates = workspace.paths) => { | ||
const { workspaces, plugin, github, versions } = workspace | ||
|
||
const strategiesByPath = workspaces((path) => new Node({ | ||
github, | ||
path, | ||
changelogSections: [ | ||
{ type: 'deps', section: 'Dependencies' }, | ||
{ type: 'fix', section: 'Fixes' }, | ||
], | ||
changelogNotes: new Changelog({ github }), | ||
})) | ||
|
||
const commitsByPath = workspaces((path) => updates.includes(path) ? [{ | ||
sha: '123', | ||
message: 'fix: stuff', | ||
files: ['package.json'], | ||
}] : []) | ||
|
||
const releaseByPath = workspaces((p) => ({ | ||
sha: '', | ||
notes: '', | ||
tag: new TagName(versions[p], '', '-', true), | ||
})) | ||
|
||
await plugin.preconfigure(strategiesByPath, commitsByPath, releaseByPath) | ||
|
||
const candidatePullRequests = [] | ||
for (const [path, strategy] of Object.entries(strategiesByPath)) { | ||
const pullRequest = await strategy.buildReleasePullRequest( | ||
commitsByPath[path], | ||
releaseByPath[path] | ||
) | ||
if (pullRequest?.version) { | ||
candidatePullRequests.push({ | ||
path, | ||
pullRequest, | ||
config: { | ||
releaseType: 'node', | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
const result = await plugin.run(candidatePullRequests) | ||
return result[0].pullRequest | ||
} | ||
|
||
t.test('root and ws fixes', async t => { | ||
const workspace = await mockNodeWorkspace() | ||
const pullRequest = await mockPullRequests(workspace) | ||
const pkgs = pullRequest.updates | ||
.filter(u => u.updater.rawContent) | ||
.map(u => JSON.parse(u.updater.rawContent)) | ||
|
||
t.strictSame(pkgs, [ | ||
{ | ||
name: '@npmcli/a', | ||
version: '2.2.3', | ||
dependencies: {}, | ||
}, | ||
{ | ||
name: 'npm', | ||
version: '1.1.2', | ||
dependencies: { '@npmcli/a': '^2.2.3' }, | ||
}, | ||
]) | ||
}) | ||
|
||
t.test('root only', async t => { | ||
const workspace = await mockNodeWorkspace() | ||
const pullRequest = await mockPullRequests(workspace, ['.']) | ||
const pkgs = pullRequest.updates | ||
.filter(u => u.updater.rawContent) | ||
.map(u => JSON.parse(u.updater.rawContent)) | ||
|
||
t.strictSame(pkgs, [ | ||
{ | ||
name: 'npm', | ||
version: '1.1.2', | ||
dependencies: { '@npmcli/a': '^2.2.2' }, | ||
}, | ||
]) | ||
}) | ||
|
||
t.test('ws only', async t => { | ||
const workspace = await mockNodeWorkspace() | ||
const pullRequest = await mockPullRequests(workspace, ['workspaces/a']) | ||
const pkgs = pullRequest.updates | ||
.filter(u => u.updater.rawContent) | ||
.map(u => JSON.parse(u.updater.rawContent)) | ||
|
||
t.strictSame(pkgs, [ | ||
{ | ||
name: '@npmcli/a', | ||
version: '2.2.3', | ||
dependencies: {}, | ||
}, | ||
{ | ||
name: 'npm', | ||
version: '1.1.2', | ||
dependencies: { '@npmcli/a': '^2.2.3' }, | ||
}, | ||
]) | ||
}) | ||
|
||
t.test('orders root to top', async t => { | ||
const ws1 = await mockNodeWorkspace(['a', 'b', 'c', 'd', 'e', 'f']) | ||
const [rootWs1] = ws1.paths.splice(0, 1) | ||
ws1.paths.push(rootWs1) | ||
const pr1 = await mockPullRequests(ws1) | ||
t.equal(pr1.body.releaseData[0].component, 'npm') | ||
|
||
const ws2 = await mockNodeWorkspace(['a', '123', 'bb', 'bbb', 'bbbe', 'aaaa']) | ||
const [rootWs2] = ws2.paths.splice(0, 1) | ||
ws2.paths.splice(4, 0, rootWs2) | ||
const pr2 = await mockPullRequests(ws2) | ||
t.equal(pr2.body.releaseData[0].component, 'npm') | ||
}) | ||
|
||
t.test('stubbed errors', async t => { | ||
const { plugin } = await mockNodeWorkspace() | ||
t.throws(() => plugin.newCandidate()) | ||
t.throws(() => plugin.bumpVersion()) | ||
}) |