forked from apache/cordova-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.spec.js
More file actions
257 lines (242 loc) · 11.4 KB
/
Copy pathplatform.spec.js
File metadata and controls
257 lines (242 loc) · 11.4 KB
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
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
var cordova = require('../cordova'),
path = require('path'),
shell = require('shelljs'),
plugman = require('plugman'),
fs = require('fs'),
util = require('../src/util'),
config = require('../src/config'),
hooker = require('../src/hooker'),
lazy_load = require('../src/lazy_load'),
platform = require('../src/platform'),
platforms = require('../platforms');
var cwd = process.cwd();
var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www'; });
var project_dir = path.join('some', 'path');
describe('platform command', function() {
var is_cordova, list_platforms, fire, config_parser, find_plugins, config_read, load, load_custom, rm, mkdir, existsSync, supports, pkg, name, exec, prep_spy, plugman_install, parsers = {};
beforeEach(function() {
supported_platforms.forEach(function(p) {
parsers[p] = spyOn(platforms[p], 'parser').andReturn({
staging_dir:function(){}
});
});
is_cordova = spyOn(util, 'isCordova').andReturn(project_dir);
fire = spyOn(hooker.prototype, 'fire').andCallFake(function(e, opts, cb) {
if (cb === undefined) cb = opts;
cb(false);
});
name = jasmine.createSpy('config name').andReturn('magical mystery tour');
pkg = jasmine.createSpy('config packageName').andReturn('ca.filmaj.id');
config_parser = spyOn(util, 'config_parser').andReturn({
packageName:pkg,
name:name
});
find_plugins = spyOn(util, 'findPlugins').andReturn([]);
list_platforms = spyOn(util, 'listPlatforms').andReturn(supported_platforms);
config_read = spyOn(config, 'read').andReturn({});
load = spyOn(lazy_load, 'based_on_config').andCallFake(function(root, platform, cb) {
cb();
});
load_custom = spyOn(lazy_load, 'custom').andCallFake(function(url, id, p, v, cb) {
cb();
});
rm = spyOn(shell, 'rm');
mkdir = spyOn(shell, 'mkdir');
existsSync = spyOn(fs, 'existsSync').andReturn(false);
supports = spyOn(platform, 'supports').andCallFake(function(project_root, name, cb) {
cb();
});
exec = spyOn(shell, 'exec').andCallFake(function(cmd, opts, cb) {
cb(0, '');
});
prep_spy = spyOn(cordova, 'prepare').andCallFake(function(t, cb) {
cb();
});
plugman_install = spyOn(plugman, 'install');
});
describe('failure', function() {
it('should not run outside of a Cordova-based project by calling util.isCordova', function() {
is_cordova.andReturn(false);
expect(function() {
cordova.platform();
expect(is_cordova).toHaveBeenCalled();
}).toThrow('Current working directory is not a Cordova-based project.');
});
it('should report back an error if used with `add` and no platform is specified', function() {
expect(function() {
cordova.platform('add');
}).toThrow('You need to qualify `add` or `remove` with one or more platforms!');
});
it('should report back an error if used with `rm` and no platform is specified', function() {
expect(function() {
cordova.platform('rm');
}).toThrow('You need to qualify `add` or `remove` with one or more platforms!');
});
});
describe('success', function() {
it('should run inside a Cordova-based project by calling util.isCordova', function() {
cordova.platform();
expect(is_cordova).toHaveBeenCalled();
});
describe('`ls`', function() {
afterEach(function() {
cordova.removeAllListeners('results');
});
it('should list out no platforms for a fresh project', function(done) {
list_platforms.andReturn([]);
cordova.on('results', function(res) {
expect(res).toEqual('No platforms added. Use `cordova platform add <platform>`.');
done();
});
cordova.platform('list');
});
it('should list out added platforms in a project', function(done) {
cordova.on('results', function(res) {
expect(res.length).toEqual(5);
done();
});
cordova.platform('list');
});
});
describe('`add`', function() {
it('should shell out to specified platform\'s bin/create, using the version that is specified in platforms manifest', function() {
cordova.platform('add', 'android');
expect(exec.mostRecentCall.args[0]).toMatch(/lib.android.cordova.\d.\d.\d[\d\w]*.bin.create/gi);
expect(exec.mostRecentCall.args[0]).toContain(project_dir);
cordova.platform('add', 'wp8');
expect(exec.mostRecentCall.args[0]).toMatch(/lib.wp8.cordova.\d.\d.\d[\d\w]*.wp8.bin.create/gi);
expect(exec.mostRecentCall.args[0]).toContain(project_dir);
});
it('should call into lazy_load.custom if there is a user-specified configruation for consuming custom libraries', function() {
load.andCallThrough();
config_read.andReturn({
lib:{
'wp8':{
uri:'haha',
id:'phonegap',
version:'bleeding edge'
}
}
});
cordova.platform('add', 'wp8');
expect(load_custom).toHaveBeenCalledWith('haha', 'phonegap', 'wp8', 'bleeding edge', jasmine.any(Function));
expect(exec.mostRecentCall.args[0]).toMatch(/lib.wp8.phonegap.bleeding edge.wp8.bin.create/gi);
expect(exec.mostRecentCall.args[0]).toContain(project_dir);
});
});
describe('`remove`',function() {
it('should remove a supported and added platform', function() {
cordova.platform('remove', 'android');
expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'platforms', 'android'));
expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'merges', 'android'));
});
it('should be able to remove multiple platforms', function() {
cordova.platform('remove', ['android', 'blackberry10']);
expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'platforms', 'android'));
expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'merges', 'android'));
expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'platforms', 'blackberry10'));
expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'merges', 'blackberry10'));
});
});
});
describe('hooks', function() {
describe('list (ls) hooks', function() {
it('should fire before hooks through the hooker module', function() {
cordova.platform();
expect(fire).toHaveBeenCalledWith('before_platform_ls', jasmine.any(Function));
});
it('should fire after hooks through the hooker module', function() {
cordova.platform();
expect(fire).toHaveBeenCalledWith('after_platform_ls', jasmine.any(Function));
});
});
describe('remove (rm) hooks', function() {
it('should fire before hooks through the hooker module', function() {
cordova.platform('rm', 'android');
expect(fire).toHaveBeenCalledWith('before_platform_rm', {platforms:['android']}, jasmine.any(Function));
});
it('should fire after hooks through the hooker module', function() {
cordova.platform('rm', 'android');
expect(fire).toHaveBeenCalledWith('after_platform_rm', {platforms:['android']}, jasmine.any(Function));
});
});
describe('add hooks', function() {
it('should fire before and after hooks through the hooker module', function() {
cordova.platform('add', 'android');
expect(fire).toHaveBeenCalledWith('before_platform_add', {platforms:['android']}, jasmine.any(Function));
expect(fire).toHaveBeenCalledWith('after_platform_add', {platforms:['android']}, jasmine.any(Function));
});
});
});
});
describe('platform.supports(name, callback)', function() {
var supports = {};
beforeEach(function() {
supported_platforms.forEach(function(p) {
supports[p] = spyOn(platforms[p].parser, 'check_requirements').andCallFake(function(project, cb) { cb(); });
});
});
it('should require a platform name', function() {
expect(function() {
cordova.platform.supports(project_dir, undefined, function(e){});
}).toThrow();
});
it('should require a callback function', function() {
expect(function() {
cordova.platform.supports(project_dir, 'android', undefined);
}).toThrow();
});
describe('when platform is unknown', function() {
it('should trigger callback with false', function(done) {
cordova.platform.supports(project_dir, 'windows-3.1', function(e) {
expect(e).toEqual(jasmine.any(Error));
done();
});
});
});
describe('when platform is supported', function() {
it('should trigger callback without error', function(done) {
cordova.platform.supports(project_dir, 'android', function(e) {
expect(e).toBeNull();
done();
});
});
});
describe('when platform is unsupported', function() {
it('should trigger callback with error', function(done) {
supported_platforms.forEach(function(p) {
supports[p].andCallFake(function(project, cb) { cb(new Error('no sdk')); });
});
cordova.platform.supports(project_dir, 'android', function(e) {
expect(e).toEqual(jasmine.any(Error));
done();
});
});
});
});
describe('platform parsers', function() {
it('should be exposed on the platform module', function() {
for (var platform in platforms) {
expect(cordova.platform[platform]).toBeDefined();
for (var prop in platforms[platform]) {
expect(cordova.platform[platform][prop]).toBeDefined();
}
}
});
});