forked from conventional-changelog/standard-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-files.spec.js
174 lines (150 loc) · 5.13 KB
/
config-files.spec.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
/* global describe it beforeEach afterEach */
'use strict'
const shell = require('shelljs')
const fs = require('fs')
const { Readable } = require('stream')
const mockery = require('mockery')
const stdMocks = require('std-mocks')
require('chai').should()
function exec () {
const cli = require('../command')
const opt = cli.parse('standard-version')
opt.skip = { commit: true, tag: true }
return require('../index')(opt)
}
/**
* Mock external conventional-changelog modules
*
* Mocks should be unregistered in test cleanup by calling unmock()
*
* bump?: 'major' | 'minor' | 'patch' | Error | (opt, cb) => { cb(err) | cb(null, { releaseType }) }
* changelog?: string | Error | Array<string | Error | (opt) => string | null>
* tags?: string[] | Error
*/
function mock ({ bump, changelog, tags } = {}) {
mockery.enable({ warnOnUnregistered: false, useCleanCache: true })
mockery.registerMock('conventional-recommended-bump', function (opt, cb) {
if (typeof bump === 'function') bump(opt, cb)
else if (bump instanceof Error) cb(bump)
else cb(null, bump ? { releaseType: bump } : {})
})
if (!Array.isArray(changelog)) changelog = [changelog]
mockery.registerMock(
'conventional-changelog',
(opt) =>
new Readable({
read (_size) {
const next = changelog.shift()
if (next instanceof Error) {
this.destroy(next)
} else if (typeof next === 'function') {
this.push(next(opt))
} else {
this.push(next ? Buffer.from(next, 'utf8') : null)
}
}
})
)
mockery.registerMock('git-semver-tags', function (cb) {
if (tags instanceof Error) cb(tags)
else cb(null, tags | [])
})
stdMocks.use()
return () => stdMocks.flush()
}
describe('config files', () => {
beforeEach(function () {
shell.rm('-rf', 'tmp')
shell.config.silent = true
shell.mkdir('tmp')
shell.cd('tmp')
fs.writeFileSync(
'package.json',
JSON.stringify({ version: '1.0.0' }),
'utf-8'
)
})
afterEach(function () {
shell.cd('../')
shell.rm('-rf', 'tmp')
mockery.deregisterAll()
mockery.disable()
stdMocks.restore()
// push out prints from the Mocha reporter
const { stdout } = stdMocks.flush()
for (const str of stdout) {
if (str.startsWith(' ')) process.stdout.write(str)
}
})
it('reads config from package.json', async function () {
const issueUrlFormat = 'https://standard-version.company.net/browse/{{id}}'
mock({
bump: 'minor',
changelog: ({ preset }) => preset.issueUrlFormat
})
const pkg = {
version: '1.0.0',
repository: { url: 'git+https://[email protected]/office/app.git' },
'standard-version': { issueUrlFormat }
}
fs.writeFileSync('package.json', JSON.stringify(pkg), 'utf-8')
await exec()
const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.include(issueUrlFormat)
})
it('reads config from .versionrc', async function () {
const issueUrlFormat = 'http://www.foo.com/{{id}}'
const changelog = ({ preset }) => preset.issueUrlFormat
mock({ bump: 'minor', changelog })
fs.writeFileSync('.versionrc', JSON.stringify({ issueUrlFormat }), 'utf-8')
await exec()
const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.include(issueUrlFormat)
})
it('reads config from .versionrc.json', async function () {
const issueUrlFormat = 'http://www.foo.com/{{id}}'
const changelog = ({ preset }) => preset.issueUrlFormat
mock({ bump: 'minor', changelog })
fs.writeFileSync(
'.versionrc.json',
JSON.stringify({ issueUrlFormat }),
'utf-8'
)
await exec()
const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.include(issueUrlFormat)
})
it('evaluates a config-function from .versionrc.js', async function () {
const issueUrlFormat = 'http://www.foo.com/{{id}}'
const src = `module.exports = function() { return ${JSON.stringify({
issueUrlFormat
})} }`
const changelog = ({ preset }) => preset.issueUrlFormat
mock({ bump: 'minor', changelog })
fs.writeFileSync('.versionrc.js', src, 'utf-8')
await exec()
const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.include(issueUrlFormat)
})
it('evaluates a config-object from .versionrc.js', async function () {
const issueUrlFormat = 'http://www.foo.com/{{id}}'
const src = `module.exports = ${JSON.stringify({ issueUrlFormat })}`
const changelog = ({ preset }) => preset.issueUrlFormat
mock({ bump: 'minor', changelog })
fs.writeFileSync('.versionrc.js', src, 'utf-8')
await exec()
const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.include(issueUrlFormat)
})
it('throws an error when a non-object is returned from .versionrc.js', async function () {
mock({ bump: 'minor' })
fs.writeFileSync('.versionrc.js', 'module.exports = 3', 'utf-8')
try {
await exec()
/* istanbul ignore next */
throw new Error('Unexpected success')
} catch (error) {
error.message.should.match(/Invalid configuration/)
}
})
})