forked from semabit/release-it-bumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
164 lines (141 loc) · 7.29 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
const fs = require('fs');
const {EOL} = require('os');
const test = require('ava');
const mock = require('mock-fs');
const {factory, runTasks} = require('release-it/test/util');
const Plugin = require('.');
const YAML = require('yamljs');
mock({
'./bower.json': JSON.stringify({version: '1.0.0'}),
'./foo.txt': '2.0.0\n',
'./parameters.yml': YAML.stringify({version: '3.0.0'}),
'./widget.xml': '<?xml version="1.0" encoding="utf-8"?><widget version="4.0.0" xmlns="http://www.w3.org/ns/widgets"></widget>',
'./manifest.json': '{}',
'./manifest.xml': '',
'./indent.yml': YAML.stringify({content: {'v': '3.3.3'}}, null, 10),
'./multi-versions.json': JSON.stringify({release: '0.0.1', 'pre-release': '1.0.0-0'}),
});
const namespace = 'bumper';
const readFile = file =>
fs.readFileSync(file)
.toString()
.trim();
test('should not throw', async t => {
const options = {[namespace]: {}};
const plugin = factory(Plugin, {namespace, options});
await t.notThrowsAsync(runTasks(plugin));
});
test('should return latest version from JSON file', async t => {
const options = {[namespace]: {in: './bower.json'}};
const plugin = factory(Plugin, {namespace, options});
const version = await plugin.getLatestVersion();
t.is(version, '1.0.0');
});
test('should return latest version of path "pre-release" from JSON file', async t => {
const options = {[namespace]: {in: {file: './multi-versions.json', paths: ['pre-release']}}};
const plugin = factory(Plugin, {namespace, options});
const version = await plugin.getLatestVersion();
t.is(version, '1.0.0-0');
});
test('should return latest version of path "release" from JSON file', async t => {
const options = {[namespace]: {in: {file: './multi-versions.json', paths: ['release']}}};
const plugin = factory(Plugin, {namespace, options});
const version = await plugin.getLatestVersion();
t.is(version, '0.0.1');
});
test('should return latest version from plain text file', async t => {
const options = {[namespace]: {in: {file: './foo.txt', type: 'text/plain'}}};
const plugin = factory(Plugin, {namespace, options});
const version = await plugin.getLatestVersion();
t.is(version, '2.0.0');
});
test('should return latest version from YAML file', async t => {
const options = {[namespace]: {in: {file: './parameters.yml', type: 'application/x-yaml'}}};
const plugin = factory(Plugin, {namespace, options});
const version = await plugin.getLatestVersion();
t.is(version, '3.0.0');
});
test('should return latest version from XML file', async t => {
const options = {[namespace]: {in: {file: './widget.xml', type: 'application/xml', path: 'widget.$.version'}}};
const plugin = factory(Plugin, {namespace, options});
const version = await plugin.getLatestVersion();
t.is(version, '4.0.0');
});
test('should write indented JSON file', async t => {
const options = {[namespace]: {out: './manifest.json'}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('1.2.3');
t.is(readFile('./manifest.json'), `{${EOL} "version": "1.2.3"${EOL}}`);
});
test('should write indented YAML file', async t => {
const options = {[namespace]: {out: {file: './manifest.yml', type: 'application/x-yaml', path: 'content.v'}}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('2.4.5');
t.is(readFile('./manifest.yml'), `content:${EOL} v: 2.4.5`);
});
test('should write indented YAML file (10 indent)', async t => {
const options = {[namespace]: {out: {file: './indent.yml', type: 'application/x-yaml', path: 'content.v'}}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('3.3.4');
t.is(readFile('./indent.yml'), `content:${EOL} v: 3.3.4`);
});
test('should write XML file', async t => {
const options = {[namespace]: {out: {file: './manifest.xml', type: 'application/xml', path: 'main.$.version'}}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('6.7.8');
t.is(readFile('./manifest.xml'), `<?xml version="1.0" encoding="utf-8" ?>${EOL}<main version="6.7.8" />`);
});
test('should write indented XML file', async t => {
const options = {[namespace]: {out: {file: './widget.xml', type: 'application/xml', path: 'widget.$.version'}}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('5.9.6');
t.is(readFile('./widget.xml'), `<?xml version="1.0" encoding="utf-8" ?>${EOL}<widget version="5.9.6" xmlns="http://www.w3.org/ns/widgets" />`);
});
test('should write new, indented JSON file', async t => {
const options = {[namespace]: {out: ['./null.json']}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('0.0.0');
t.is(readFile('./null.json'), `{${EOL} "version": "0.0.0"${EOL}}`);
});
test('should write new, indented XML file', async t => {
const options = {[namespace]: {out: [{file: './null.xml', type: 'application/xml', path: 'version'}]}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('0.0.0');
t.is(readFile('./null.xml'), `<?xml version="1.0" encoding="utf-8" ?>${EOL}<version>0.0.0</version>`);
});
test('should write new YAML file', async t => {
const options = {[namespace]: {out: [{file: './null.yml', type: 'application/x-yaml'}]}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('0.0.0');
t.is(readFile('./null.yml'), `version: 0.0.0`);
});
test('should write version at path', async t => {
const options = {[namespace]: {out: {file: './deep.json', path: 'deep.sub.version'}}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('1.2.3');
t.is(readFile('./deep.json'), JSON.stringify({deep: {sub: {version: '1.2.3'}}}, null, ' '));
});
test('should write plain text file', async t => {
const options = {[namespace]: {out: [{file: './VERSION', type: 'text/plain'}]}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('3.2.1');
t.is(readFile('./VERSION'), '3.2.1');
});
test('should write version to all paths', async t => {
const options = {[namespace]: {out: {file: './multi-versions.json', paths: ['release', 'pre-release']}}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('1.1.1');
t.is(readFile('./multi-versions.json'), JSON.stringify({release: '1.1.1', 'pre-release': '1.1.1'}, null, ' '));
});
test('should write version to all paths deep', async t => {
const options = {[namespace]: {out: {file: './deep.multi.json', paths: ['version1.release', 'version2.pre-release']}}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('1.2.3');
t.is(readFile('./deep.multi.json'), JSON.stringify({version1: {release: '1.2.3'}, version2: {'pre-release': '1.2.3'}}, null, ' '));
});
test('should write indented XML file with all paths', async t => {
const options = {[namespace]: {out: [{file: './multi-versions.xml', type: 'application/xml', paths: ['data.release', 'data.pre-release']}]}};
const plugin = factory(Plugin, {namespace, options});
await plugin.bump('0.2.3');
t.is(readFile('./multi-versions.xml'), `<?xml version="1.0" encoding="utf-8" ?>${EOL}<data>${EOL} <release>0.2.3</release>${EOL} <pre-release>0.2.3</pre-release>${EOL}</data>`);
});