forked from release-it/bumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
259 lines (231 loc) · 8.69 KB
/
test.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import test from 'bron';
import { readFileSync } from 'fs';
import { EOL } from 'os';
import assert from 'assert';
import mock from 'mock-fs';
import { factory, runTasks } from 'release-it/test/util/index.js';
import Bumper from './index.js';
mock({
'./bower.json': JSON.stringify({ version: '1.0.0' }) + EOL,
'./foo.txt': `1.0.0${EOL}`,
'./foo2.txt': `1.0.0${EOL}`,
'./foo.php': `/* comments${EOL}version: v1.0.0 */ <? echo <p>hello world</p>; ?>${EOL}`,
'./manifest.json': `{}${EOL}`,
'./dryrun.json': JSON.stringify({ version: '1.0.0' }) + EOL,
'./foo.toml': `[tool.test]${EOL}version = "1.0.0"${EOL}`,
'./foo.ini': `path.version=1.0.0${EOL}path.name=fake${EOL}`,
'./VERSION': `v1.0.0${EOL}`,
'./README.md': `Release v1.0.0${EOL}`,
'./foo.yaml': `version: v1.0.0${EOL}`,
'./invalid.toml': `/# -*- some invalid toml -*-${EOL}version = "1.0.0"${EOL}`
});
const namespace = 'bumper';
const nl = value => value.split(/\r\n|\r|\n/g).join(EOL);
const readFile = file => nl(readFileSync(file).toString());
test('should not throw', async () => {
const options = { [namespace]: {} };
const plugin = factory(Bumper, { namespace, options });
await assert.doesNotReject(runTasks(plugin));
});
test('should return latest version from JSON file', async () => {
const options = { [namespace]: { in: './bower.json' } };
const plugin = factory(Bumper, { namespace, options });
const version = await plugin.getLatestVersion();
assert.equal(version, '1.0.0');
});
test('should return latest version from plain text file', async () => {
const options = {
[namespace]: { in: { file: './foo.txt', type: 'text/plain' } }
};
const plugin = factory(Bumper, { namespace, options });
const version = await plugin.getLatestVersion();
assert.equal(version, '1.0.0');
});
test('should return latest version from plain text file (.txt)', async () => {
const options = { [namespace]: { in: { file: './foo.txt' } } };
const plugin = factory(Bumper, { namespace, options });
const version = await plugin.getLatestVersion();
assert.equal(version, '1.0.0');
});
test('should return latest version from YAML file', async () => {
const options = { [namespace]: { in: './foo.yaml' } };
const plugin = factory(Bumper, { namespace, options });
const version = await plugin.getLatestVersion();
assert.equal(version, '1.0.0');
});
test('should write indented JSON file', async () => {
const options = { [namespace]: { out: './manifest.json' } };
const plugin = factory(Bumper, { namespace, options });
await plugin.bump('1.2.3');
assert.equal(readFile('./manifest.json'), `{${EOL} "version": "1.2.3"${EOL}}${EOL}`);
});
test('should write new, indented JSON file', async () => {
const options = { [namespace]: { out: ['./null.json'] } };
const plugin = factory(Bumper, { namespace, options });
await plugin.bump('0.0.0');
assert.equal(readFile('./null.json'), `{${EOL} "version": "0.0.0"${EOL}}${EOL}`);
});
test('should write version at path', async () => {
const options = {
[namespace]: { out: { file: './deep.json', path: 'deep.sub.version' } }
};
const plugin = factory(Bumper, { namespace, options });
await plugin.bump('1.2.3');
assert.equal(
readFile('./deep.json'),
`{${EOL} "deep": {${EOL} "sub": {${EOL} "version": "1.2.3"${EOL} }${EOL} }${EOL}}${EOL}`
);
});
test('should write version at multiple paths', async () => {
const options = {
[namespace]: {
out: {
file: './multi.json',
path: ['version', 'deep.version', 'deep.sub.version']
}
}
};
const plugin = factory(Bumper, { namespace, options });
await plugin.bump('1.2.3');
assert.equal(
readFile('./multi.json'),
`{${EOL} "version": "1.2.3",${EOL} "deep": {${EOL} "version": "1.2.3",${EOL} "sub": {${EOL} "version": "1.2.3"${EOL} }${EOL} }${EOL}}${EOL}`
);
});
test('should write plain version text file', async () => {
const options = {
[namespace]: { out: [{ file: './VERSION-OUT', type: 'text/plain' }] }
};
const plugin = factory(Bumper, { namespace, options });
await plugin.bump('3.2.1');
assert.equal(readFile('./VERSION-OUT'), `3.2.1${EOL}`);
});
test('should write plain version text file (default text type)', async () => {
const options = { [namespace]: { out: [{ file: './VERSION-OUT' }] } };
const plugin = factory(Bumper, { namespace, options });
await plugin.bump('3.2.1');
assert.equal(readFile('./VERSION-OUT'), `3.2.1${EOL}`);
});
test('should write toml file', async () => {
const options = {
[namespace]: {
out: {
file: './foo.toml',
type: 'application/toml',
path: 'tool.test.version'
}
}
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.toml'), `[tool.test]${EOL}version = "1.0.1"${EOL}`);
});
test('should write toml file (.toml)', async () => {
const options = {
[namespace]: { out: { file: './foo.toml', path: 'tool.test.version' } }
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.toml'), `[tool.test]${EOL}version = "1.0.1"${EOL}`);
});
test('should write ini file', async () => {
const options = {
[namespace]: { out: { file: './foo.ini', path: 'path.version' } }
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.ini'), `path.version=1.0.1${EOL}path.name=fake${EOL}`);
});
test('should write plain text file', async () => {
const options = {
[namespace]: {
in: './bower.json',
out: { file: './foo.php', type: 'text/php' }
}
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.php'), `/* comments${EOL}version: v1.0.1 */ <? echo <p>hello world</p>; ?>${EOL}`);
});
test('should write YAML file', async () => {
const options = { [namespace]: { out: { file: './foo.yaml' } } };
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.yaml'), `version: 1.0.1${EOL}`);
});
test('should read/write plain text file', async () => {
const options = {
[namespace]: {
in: { file: './foo.txt', type: 'text/plain' },
out: { file: './foo.txt', type: 'text/plain' }
}
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.txt'), `1.0.1${EOL}`);
});
test('should read/write plain text file (.txt)', async () => {
const options = {
[namespace]: { in: { file: './foo.txt' }, out: { file: './foo.txt' } }
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.txt'), `1.0.1${EOL}`);
});
test('should read one and write multiple files', async () => {
const options = {
[namespace]: { in: { file: './foo.txt' }, out: './foo*.txt' }
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.txt'), `1.0.1${EOL}`);
assert.equal(readFile('./foo2.txt'), `1.0.1${EOL}`);
});
test('should read one and write multiple files and respect prefix', async () => {
const options = {
[namespace]: {
in: { file: 'VERSION', type: 'text/plain' },
out: ['README.md', 'VERSION']
}
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./README.md'), `Release v1.0.1${EOL}`);
assert.equal(readFile('./VERSION'), `v1.0.1${EOL}`);
});
test('should write various file types', async () => {
const options = {
[namespace]: {
out: [{ file: './foo*.txt' }, { file: './(bower|manifest).json' }]
}
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./foo.txt'), `1.0.1${EOL}`);
assert.equal(readFile('./foo2.txt'), `1.0.1${EOL}`);
assert.equal(readFile('./bower.json'), `{${EOL} "version": "1.0.1"${EOL}}${EOL}`);
assert.equal(readFile('./manifest.json'), `{${EOL} "version": "1.0.1"${EOL}}${EOL}`);
});
test('should not write in dry run', async () => {
const options = { [namespace]: { in: './dryrun.json' } };
const plugin = factory(Bumper, {
namespace,
options,
global: { isDryRun: true }
});
await plugin.bump('1.0.1');
assert.equal(readFile('./dryrun.json'), `{"version":"1.0.0"}${EOL}`);
});
test.only('should give precedence to mime type over file extension', async () => {
const options = {
[namespace]: {
out: {
file: './invalid.toml',
type: 'text/plain'
}
}
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./invalid.toml'), `/# -*- some invalid toml -*-${EOL}version = "1.0.1"${EOL}`);
});