-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgenerate.js
191 lines (166 loc) · 5.36 KB
/
generate.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
'use strict';
const pkg = require('./fixtures/my-cool-api');
const pkgWithWhitelist = require('./fixtures/my-cool-api-with-whitelist');
const sandbox = require('sinon').createSandbox();
const fs = require('fs');
const assert = require('assert');
const generate = require('../lib/generate');
const archiver = require('../lib/archiver');
describe('generate', () => {
beforeEach(() =>{
sandbox.stub(fs, 'writeFileSync');
sandbox.stub(fs, 'mkdirSync');
sandbox.stub(archiver, 'compress').returns(Promise.resolve());
});
afterEach(() =>{
sandbox.restore();
});
it('creates the service file', async () => {
await generate('/path/to/project', pkg, null, null);
sandbox.assert.calledWith(
fs.writeFileSync,
'/path/to/project/my-cool-api.service',
sandbox.match('SyslogIdentifier=my-cool-api')
);
});
it('creates directory for SPECS', async () => {
await generate('/path/to/project', pkg, null, null);
sandbox.assert.calledWith(fs.mkdirSync, '/path/to/project/SPECS');
});
it('fails if it cannot create directory for SPECS', async () => {
fs.mkdirSync.withArgs('/path/to/project/SPECS').throws();
try {
await generate('/path/to/project', pkg, null, null);
} catch (e) {
return assert.ok(e);
}
assert.fail();
});
it('creates the spec file', async () => {
await generate('/path/to/project', pkg, null, null);
sandbox.assert.calledWith(
fs.writeFileSync,
'/path/to/project/SPECS/my-cool-api.spec',
sandbox.match('%define name my-cool-api')
);
});
it('creates the sources archive', async () => {
await generate('/path/to/project', pkg, null, null);
sandbox.assert.calledWith(
archiver.compress,
'/path/to/project',
'/path/to/project/SOURCES/my-cool-api.tar.gz',
{
files: undefined,
main: 'index.js',
service: 'my-cool-api.service'
}
);
});
it('creates directory for SOURCES', async () => {
await generate('/path/to/project', pkg, null, null);
sandbox.assert.calledWith(fs.mkdirSync, '/path/to/project/SOURCES');
});
it('fails if it cannot create directory for SOURCES', async () => {
fs.mkdirSync.withArgs('/path/to/project/SOURCES').throws();
try {
await generate('/path/to/project', pkg, null, null);
} catch (e) {
return assert.ok(e);
}
assert.fail();
});
it('creates the spec file with the correct release number', async () => {
await generate('/path/to/project', pkg, 7, null);
sandbox.assert.calledWith(
fs.writeFileSync,
'/path/to/project/SPECS/my-cool-api.spec',
sandbox.match('%define release 7')
);
});
it('creates the spec file with a custom name if specified', async () => {
await generate('/path/to/project', pkg, 1, 'penguin');
sandbox.assert.calledWith(
fs.writeFileSync,
'/path/to/project/SPECS/penguin.spec',
sandbox.match('%define name penguin')
);
});
it('creates the sources archive with a custom name if specified', async () => {
await generate('/path/to/project', pkg, null, 'penguin');
sandbox.assert.calledWith(
archiver.compress,
'/path/to/project',
'/path/to/project/SOURCES/penguin.tar.gz',
{
files: undefined,
main: 'index.js',
service: 'penguin.service'
}
);
});
it('passes files and main values from the package.json to archiver', async () => {
await generate('/path/to/project', pkgWithWhitelist, null, null);
sandbox.assert.calledWith(
archiver.compress,
'/path/to/project',
'/path/to/project/SOURCES/my-cool-api.tar.gz',
{
main: 'server.js',
files: [
'lib',
'routes',
'index.js'
],
service: 'my-cool-api.service'
}
);
});
it('creates the service file with a custom name if specified and "files" defined', async () => {
await generate('/path/to/project', pkgWithWhitelist, null, 'penguin');
sandbox.assert.calledWith(
archiver.compress,
'/path/to/project',
'/path/to/project/SOURCES/penguin.tar.gz',
{
main: 'server.js',
files: [
'lib',
'routes',
'index.js'
],
service: 'penguin.service'
}
);
});
it('creates the service file with a custom name if specified', async () => {
await generate('/path/to/project', pkg, 1, 'penguin');
sandbox.assert.calledWith(
fs.writeFileSync,
'/path/to/project/penguin.service',
sandbox.match('SyslogIdentifier=penguin')
);
});
it('creates the sources archive with a custom name if specified', async () => {
await generate('/path/to/project', pkg, null, 'penguin');
sandbox.assert.calledWith(
archiver.compress,
'/path/to/project',
'/path/to/project/SOURCES/penguin.tar.gz'
);
});
it('returns an array of files created', async () => {
const filesExpected = ['SPECS/my-cool-api.spec', 'SOURCES/my-cool-api.tar.gz', 'my-cool-api.service'];
const files = await generate('/path/to/project', pkg, null, null);
assert.deepEqual(files, filesExpected);
});
it('fails if archiver fails', async () => {
archiver.compress.returns(Promise.reject(new Error('Thanks Jim :-)')));
try {
await generate('/path/to/project', pkg, null, null);
} catch (e) {
return assert.ok(e);
}
assert.fail();
});
});