-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathnode-workspace.js
178 lines (155 loc) · 4.88 KB
/
node-workspace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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())
})