This repository has been archived by the owner on Jun 5, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
snap-packager.test.js
231 lines (188 loc) · 5.19 KB
/
snap-packager.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
const {spawn} = require('child_process');
const path = require('path');
const EventEmitter = require('events');
const test = require('ava');
const sinon = require('sinon');
const yaml = require('js-yaml');
const fse = require('fs-extra');
const packager = require('electron-packager');
const SnapPackager = require('./snap-packager.js');
const SnapValues = require('./snap-values.js');
const makeOptions = {
appName: 'Nimble Notes v3!',
forgeConfig: {
packagerConfig: {
icon: './test/fixtures/icon'
}
},
packageJSON: {
version: '2.0.3',
description: 'Simple note taking',
license: 'MIT'
},
targetArch: 'x64',
dir: './test/artifacts/out/Nimble Notes v3!-linux-x64',
makeDir: './test/artifacts/make',
targetPlatform: 'linux'
};
const makerOptions = {
confinement: 'strict',
grade: 'stable',
stagePackages: ['default', 'scrot'],
plugs: ['media-hub', 'default'],
layout: {
'/usr/lib/x86_64-linux-gnu/imlib2': {
bind: '$SNAP/meta/gui/nimble-notes-v3.png'
}
}
};
const dependencies = {
process,
fse,
spawn
};
const testArtifactsPath = './test/artifacts';
test.beforeEach(() => {
if (fse.existsSync(testArtifactsPath)) {
fse.rmdirSync(testArtifactsPath, {recursive: true});
}
});
test.afterEach(() => {
if (fse.existsSync(testArtifactsPath)) {
fse.rmdirSync(testArtifactsPath, {recursive: true});
}
});
test('generation of desktop file', t => {
const pkg = new SnapPackager({
makeOptions,
makerOptions,
dependencies
});
t.is(pkg.generateDesktopFile(), `[Desktop Entry]
Name=Nimble Notes v3!
Exec=nimble-notes-v3
Icon=\${SNAP}/meta/gui/nimble-notes-v3.png
Type=Application
Encoding=UTF-8
`);
});
test('generation of snapcraft.yaml', t => {
const pkg = new SnapPackager({
makeOptions,
makerOptions,
dependencies
});
const snapYaml = yaml.load(pkg.generateSnapcraftYAML());
t.like(snapYaml, {
name: 'nimble-notes-v3',
title: 'Nimble Notes v3!',
version: '2.0.3',
icon: './snap/gui/nimble-notes-v3.png',
summary: 'Simple note taking',
description: 'Simple note taking',
license: 'MIT',
confinement: 'strict',
grade: 'stable',
apps: {
'nimble-notes-v3': {
command: 'nimble-notes-v3/nimble-notes-v3 --no-sandbox',
plugs: [...makerOptions.plugs.filter(item => item !== 'default'),
...SnapValues.defaultPlugs]
}
},
parts: {
app: {
source: './app',
'override-build': 'cp -rv . $SNAPCRAFT_PART_INSTALL/nimble-notes-v3',
'stage-packages': [...makerOptions.stagePackages.filter(item => item !== 'default'),
...SnapValues.defaultStagePackages]
}
},
layout: {
'/usr/lib/x86_64-linux-gnu/imlib2': {
bind: '$SNAP/meta/gui/nimble-notes-v3.png'
}
}
});
t.true(snapYaml.apps['SNAP-TEMPLATE'] === undefined);
});
test('creation of snap files will remove destination path if it exists', t => {
const fseStub = sinon.stub(fse);
const pkg = new SnapPackager({
makeOptions,
makerOptions,
dependencies: {
...dependencies,
fse: fseStub
}
});
fseStub.existsSync.returns(true);
fseStub.readFileSync.callThrough();
pkg.createSnapcraftFiles();
t.true(fseStub.rmdirSync.calledOnce);
});
test('will throw error if snapcraft finishes with non-zero exit code', t => {
class SpawnEmitter extends EventEmitter {}
const spawnEmitter = new SpawnEmitter();
spawnEmitter.stdout = new SpawnEmitter();
const spawnStub = sinon.stub().returns(spawnEmitter);
const pkg = new SnapPackager({
makeOptions,
makerOptions,
dependencies: {
spawn: spawnStub
}
});
const snapPromise = pkg.createSnapPackage().catch(error => {
t.is(error.message, 'Snapcraft exited with a non-zero status code of: 120');
});
spawnEmitter.emit('close', 120);
return snapPromise;
});
test('will throw error if snapcraft process errors', t => {
class SpawnEmitter extends EventEmitter {}
const spawnEmitter = new SpawnEmitter();
spawnEmitter.stdout = new SpawnEmitter();
const spawnStub = sinon.stub().returns(spawnEmitter);
const pkg = new SnapPackager({
makeOptions,
makerOptions,
dependencies: {
spawn: spawnStub
}
});
const snapPromise = pkg.createSnapPackage().catch(error => {
t.is(error.message, 'Snapcraft process error');
});
spawnEmitter.emit('error', new Error('Snapcraft process error'));
return snapPromise;
});
if (!process.env.FAST_TESTS) {
test.serial('creation of snap package', async t => {
await packager({
dir: './test/fixtures/app',
out: './test/artifacts/out',
name: 'Nimble Notes v3!',
platform: 'linux',
overwrite: true,
quiet: true
});
const pkg = new SnapPackager({
makeOptions,
makerOptions,
dependencies
});
pkg.createSnapcraftFiles();
const destDir = path.join(makeOptions.makeDir, 'snapcraft');
t.is(fse.readFileSync(path.join(destDir, 'snap', 'snapcraft.yaml'), 'utf8'),
pkg.generateSnapcraftYAML());
t.is(fse.readFileSync(path.join(destDir, 'snap', 'gui', 'nimble-notes-v3.desktop'), 'utf8'),
pkg.generateDesktopFile());
t.true(fse.existsSync(path.join(destDir, 'snap', 'gui', 'nimble-notes-v3.png')));
t.true(fse.existsSync(path.join(destDir, 'app')));
const snapfilePath = await pkg.createSnapPackage();
t.is(snapfilePath, path.join(
makeOptions.makeDir, 'snapcraft', 'nimble-notes-v3-2.0.3.snap'));
t.true(fse.existsSync(snapfilePath));
});
}