forked from cookpete/auto-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
238 lines (202 loc) · 7.24 KB
/
run.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const { describe, it, beforeEach, afterEach } = require('mocha')
const { expect } = require('chai')
const { join } = require('path')
const { readFile } = require('../src/utils')
const remotes = require('./data/remotes')
const releases = require('./data/releases')
const { tags, commitsMap } = require('./data/commits-map')
const commitsNoRemote = require('./data/commits-no-remote')
const {
run,
__get__,
__Rewire__: mock,
__ResetDependency__: unmock
} = require('../src/run')
const getOptions = __get__('getOptions')
describe('getOptions', () => {
it('parses commit limit correctly', async () => {
const options = await getOptions(['', '', '--commit-limit', '10'])
expect(options.commitLimit).to.equal(10)
})
it('parses false commit limit correctly', async () => {
const options = await getOptions(['', '', '--commit-limit', 'false'])
expect(options.commitLimit).to.equal(false)
})
it('parses --issue-url correctly when given --issue-url', async () => {
const options = await getOptions(['', '', '--issue-url', 'https://test.issue.local/issues/{id}'])
expect(options.issueUrl).to.equal('https://test.issue.local/issues/{id}')
})
it('parses -i correctly when given -i', async () => {
const options = await getOptions(['', '', '-i', 'https://test.issue.local/issues/{id}'])
expect(options.issueUrl).to.equal('https://test.issue.local/issues/{id}')
})
})
describe('run', () => {
beforeEach(() => {
mock('fileExists', () => false)
mock('readJson', () => null)
mock('fetchRemote', () => remotes.github)
mock('fetchTags', () => Promise.resolve(tags))
mock('parseReleases', () => Promise.resolve(releases))
mock('writeFile', () => {})
mock('log', () => {})
})
afterEach(() => {
unmock('fileExists')
unmock('readJson')
unmock('fetchRemote')
unmock('fetchTags')
unmock('parseReleases')
unmock('writeFile')
unmock('log')
})
it('generates a changelog', async () => {
const expected = await readFile(join(__dirname, 'data', 'template-compact.md'))
mock('writeFile', (output, log) => {
expect(output).to.equal('CHANGELOG.md')
expect(log).to.equal(expected)
})
return run(['', ''])
})
it.skip('generates a changelog with no remote', async () => {
const expected = await readFile(join(__dirname, 'data', 'template-compact-no-remote.md'))
mock('fetchRemote', () => remotes.null)
mock('fetchCommits', () => commitsNoRemote)
mock('writeFile', (output, log) => {
expect(output).to.equal('CHANGELOG.md')
expect(log).to.equal(expected)
})
return run(['', ''])
})
it('uses options from package.json', async () => {
const expected = await readFile(join(__dirname, 'data', 'template-keepachangelog.md'))
mock('fileExists', () => true)
mock('readJson', () => ({
'auto-changelog': {
template: 'keepachangelog'
}
}))
mock('writeFile', (output, log) => {
expect(output).to.equal('CHANGELOG.md')
expect(log).to.equal(expected)
})
return run(['', ''])
})
it.skip('uses version from package.json', async () => {
mock('fileExists', () => true)
mock('readJson', () => ({
version: '2.0.0'
}))
mock('writeFile', (output, log) => {
expect(log).to.include('v2.0.0')
})
return run(['', '', '--package'])
})
it.skip('uses version from custom package file', async () => {
mock('fileExists', () => true)
mock('readJson', file => {
if (file === 'test.json') {
return { version: '2.0.0' }
}
return {}
})
mock('writeFile', (output, log) => {
expect(log).to.include('v2.0.0')
})
return run(['', '', '--package', 'test.json'])
})
it.skip('uses version from package.json with no prefix', async () => {
mock('fileExists', () => true)
mock('readJson', () => ({
version: '2.0.0'
}))
mock('fetchTags', () => Promise.resolve(tags.map(tag => tag.replace('v', ''))))
mock('writeFile', (output, log) => {
expect(log).to.include('2.0.0')
expect(log).to.not.include('v2.0.0')
})
return run(['', '', '--package'])
})
it('command line options override options from package.json', async () => {
mock('fileExists', path => path === '.auto-changelog')
mock('readJson', () => ({
'auto-changelog': {
output: 'should-not-be-this.md'
}
}))
mock('writeFile', (output, log) => {
expect(output).to.equal('should-be-this.md')
})
return run(['', '', '--output', 'should-be-this.md'])
})
it('uses options from .auto-changelog', async () => {
const expected = await readFile(join(__dirname, 'data', 'template-keepachangelog.md'))
mock('fileExists', path => path === '.auto-changelog')
mock('readJson', path => {
return path === '.auto-changelog' ? { template: 'keepachangelog' } : null
})
mock('writeFile', (output, log) => {
expect(log).to.equal(expected)
})
return run(['', ''])
})
it('command line options override options from .auto-changelog', async () => {
mock('fileExists', path => path === '.auto-changelog')
mock('readJson', (path) => {
return path === '.auto-changelog' ? { output: 'should-not-be-this.md' } : null
})
mock('writeFile', (output, log) => {
expect(output).to.equal('should-be-this.md')
})
return run(['', '', '--output', 'should-be-this.md'])
})
it.skip('supports unreleased option', () => {
mock('writeFile', (output, log) => {
expect(log).to.include('Unreleased')
expect(log).to.include('https://github.com/user/repo/compare/v1.0.0...HEAD')
})
return run(['', '', '--unreleased'])
})
it.skip('supports breakingPattern option', () => {
const addBreakingFlag = commit => {
if (/Some breaking change/.test(commit.message)) {
return { ...commit, breaking: true }
}
return commit
}
mock('fetchCommits', diff => Promise.resolve(commitsMap[diff].map(addBreakingFlag)))
mock('writeFile', (output, log) => {
expect(log).to.include('**Breaking change:** Some breaking change')
})
// No need to actually pass in the option here as we amend the commits
return run(['', '', '--commit-limit', '0'])
})
it.skip('supports releaseSummary option', () => {
mock('writeFile', (output, log) => {
expect(log).to.include('This is my major release description.\n\n- And a bullet point')
})
return run(['', '', '--release-summary'])
})
it('does not error when using latest version option', () => {
return run(['', '', '--latest-version', 'v3.0.0'])
})
// For some reason is preventing the fetchTags test from running…?`
it.skip('does not error when using stdout option', () => {
return run(['', '', '--stdout'])
})
it('throws an error when no package found', done => {
run(['', '', '--package'])
.then(() => done('Should throw an error'))
.catch(() => done())
})
it('throws an error when no custom package found', done => {
run(['', '', '--package', 'does-not-exist.json'])
.then(() => done('Should throw an error'))
.catch(() => done())
})
it('throws an error when no template found', done => {
run(['', '', '--template', 'not-found'])
.then(() => done('Should throw an error'))
.catch(() => done())
})
})