Skip to content

Commit 911b5b0

Browse files
committed
Increase build, check req, and versions code coverage
1 parent be6798b commit 911b5b0

File tree

3 files changed

+429
-0
lines changed

3 files changed

+429
-0
lines changed

tests/spec/unit/build.spec.js

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,229 @@ describe('build', function () {
340340
done();
341341
});
342342
});
343+
344+
describe('help method', () => {
345+
it('should log a bunch of options', () => {
346+
const logSpy = jasmine.createSpy();
347+
const procStub = { exit: _ => null, cwd: _ => '', argv: ['', ''] };
348+
build.__set__({ console: { log: logSpy }, process: procStub });
349+
350+
build.help();
351+
expect(logSpy).toHaveBeenCalledWith(jasmine.stringMatching(/^Usage:/));
352+
});
353+
});
354+
355+
describe('run method', () => {
356+
let rejectSpy;
357+
358+
beforeEach(() => {
359+
rejectSpy = jasmine.createSpy('reject');
360+
361+
build.__set__('Q', {
362+
reject: rejectSpy
363+
});
364+
});
365+
366+
it('should not accept debug and release options together', () => {
367+
build.run({
368+
debug: true,
369+
release: true
370+
});
371+
372+
expect(rejectSpy).toHaveBeenCalledWith('Cannot specify "debug" and "release" options together.');
373+
});
374+
375+
it('should not accept device and emulator options together', () => {
376+
build.run({
377+
device: true,
378+
emulator: true
379+
});
380+
381+
expect(rejectSpy).toHaveBeenCalledWith('Cannot specify "device" and "emulator" options together.');
382+
});
383+
384+
it('should reject when build config file missing', () => {
385+
const existsSyncSpy = jasmine.createSpy('existsSync').and.returnValue(false);
386+
build.__set__('fs', {
387+
existsSync: existsSyncSpy
388+
});
389+
390+
build.run({
391+
buildConfig: './some/config/path'
392+
});
393+
394+
expect(rejectSpy).toHaveBeenCalledWith(jasmine.stringMatching(/^Build config file does not exist:/));
395+
});
396+
});
397+
398+
describe('getDefaultSimulatorTarget method', () => {
399+
it('should find iPhone X as the default simulator target.', (done) => {
400+
const mockedEmulators = [{
401+
name: 'iPhone 5s',
402+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-5s',
403+
simIdentifier: 'iPhone-5s'
404+
},
405+
{
406+
name: 'iPhone 6',
407+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-6',
408+
simIdentifier: 'iPhone-6'
409+
},
410+
{
411+
name: 'iPhone 6 Plus',
412+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus',
413+
simIdentifier: 'iPhone-6-Plus'
414+
},
415+
{
416+
name: 'iPhone 6s',
417+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-6s',
418+
simIdentifier: 'iPhone-6s'
419+
},
420+
{
421+
name: 'iPhone 6s Plus',
422+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-6s-Plus',
423+
simIdentifier: 'iPhone-6s-Plus'
424+
},
425+
{
426+
name: 'iPhone 7',
427+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-7',
428+
simIdentifier: 'iPhone-7'
429+
},
430+
{
431+
name: 'iPhone 7 Plus',
432+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-7-Plus',
433+
simIdentifier: 'iPhone-7-Plus'
434+
},
435+
{
436+
name: 'iPhone 8',
437+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-8',
438+
simIdentifier: 'iPhone-8'
439+
},
440+
{
441+
name: 'iPhone 8 Plus',
442+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-8-Plus',
443+
simIdentifier: 'iPhone-8-Plus'
444+
},
445+
{
446+
name: 'iPhone SE',
447+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-SE',
448+
simIdentifier: 'iPhone-SE'
449+
},
450+
{
451+
name: 'iPhone X',
452+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-X',
453+
simIdentifier: 'iPhone-X'
454+
},
455+
{
456+
name: 'iPad Air',
457+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPad-Air',
458+
simIdentifier: 'iPad-Air'
459+
},
460+
{
461+
name: 'iPad Air 2',
462+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPad-Air-2',
463+
simIdentifier: 'iPad-Air-2'
464+
},
465+
{
466+
name: 'iPad (5th generation)',
467+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPad--5th-generation-',
468+
simIdentifier: 'iPad--5th-generation-'
469+
}];
470+
471+
// This method will require a module that supports the run method.
472+
build.__set__('require', () => {
473+
return {
474+
run: () => {
475+
return new Promise((resolve, reject) => {
476+
resolve(mockedEmulators);
477+
});
478+
}
479+
};
480+
});
481+
482+
const getDefaultSimulatorTarget = build.__get__('getDefaultSimulatorTarget');
483+
const exec = getDefaultSimulatorTarget();
484+
485+
const expected = {
486+
name: 'iPhone X',
487+
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-X',
488+
simIdentifier: 'iPhone-X'
489+
};
490+
491+
exec.then((actual) => {
492+
expect(actual).toEqual(expected);
493+
done();
494+
});
495+
});
496+
});
497+
498+
describe('findXCodeProjectIn method', () => {
499+
let findXCodeProjectIn;
500+
let shellLsSpy;
501+
let rejectSpy;
502+
let resolveSpy;
503+
let emitSpy;
504+
const fakePath = '/path/foobar';
505+
506+
beforeEach(() => {
507+
findXCodeProjectIn = build.__get__('findXCodeProjectIn');
508+
509+
// Shell Spy
510+
shellLsSpy = jasmine.createSpy('shellLsSpy');
511+
build.__set__('shell', {
512+
ls: shellLsSpy
513+
});
514+
515+
// Q Spy
516+
rejectSpy = jasmine.createSpy('rejectSpy');
517+
resolveSpy = jasmine.createSpy('resolveSpy');
518+
build.__set__('Q', {
519+
reject: rejectSpy,
520+
resolve: resolveSpy
521+
});
522+
523+
// Events spy
524+
emitSpy = jasmine.createSpy('emitSpy');
525+
build.__set__('events', {
526+
emit: emitSpy
527+
});
528+
});
529+
530+
it('should find not find Xcode project', () => {
531+
shellLsSpy.and.returnValue(['README.md']);
532+
533+
findXCodeProjectIn(fakePath);
534+
535+
expect(rejectSpy).toHaveBeenCalledWith('No Xcode project found in ' + fakePath);
536+
});
537+
538+
it('should emit finding multiple Xcode projects', () => {
539+
shellLsSpy.and.returnValue(['Test1.xcodeproj', 'Test2.xcodeproj']);
540+
541+
findXCodeProjectIn(fakePath);
542+
543+
// Emit
544+
let actualEmit = emitSpy.calls.argsFor(0)[1];
545+
expect(emitSpy).toHaveBeenCalled();
546+
expect(actualEmit).toContain('Found multiple .xcodeproj directories in');
547+
548+
// Resolve
549+
let actualResolve = resolveSpy.calls.argsFor(0)[0];
550+
expect(resolveSpy).toHaveBeenCalled();
551+
expect(actualResolve).toContain('Test1');
552+
});
553+
554+
it('should detect and return only one projects', () => {
555+
shellLsSpy.and.returnValue(['Test1.xcodeproj']);
556+
557+
findXCodeProjectIn(fakePath);
558+
559+
// Emit
560+
expect(emitSpy).not.toHaveBeenCalled();
561+
562+
// Resolve
563+
let actualResolve = resolveSpy.calls.argsFor(0)[0];
564+
expect(resolveSpy).toHaveBeenCalled();
565+
expect(actualResolve).toContain('Test1');
566+
});
567+
});
343568
});
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
var rewire = require('rewire');
21+
var checkReqs = rewire('../../../../bin/templates/scripts/cordova/lib/check_reqs');
22+
23+
describe('check_reqs', function () {
24+
describe('checkTool method', () => {
25+
const originalVersion = checkReqs.__get__('versions');
26+
let shellWhichSpy;
27+
let rejectSpy;
28+
let resolveSpy;
29+
let getToolVersionSpy;
30+
31+
beforeEach(() => {
32+
// Shell Spy
33+
shellWhichSpy = jasmine.createSpy('shellWhichSpy');
34+
checkReqs.__set__('shell', {
35+
which: shellWhichSpy
36+
});
37+
38+
// Q Spy
39+
rejectSpy = jasmine.createSpy('rejectSpy');
40+
resolveSpy = jasmine.createSpy('resolveSpy');
41+
checkReqs.__set__('Q', {
42+
reject: rejectSpy,
43+
resolve: resolveSpy
44+
});
45+
46+
// Versions Spy
47+
getToolVersionSpy = jasmine.createSpy('rejectSpy');
48+
});
49+
50+
it('should not have found tool.', () => {
51+
shellWhichSpy.and.returnValue(false);
52+
const checkTool = checkReqs.__get__('checkTool');
53+
54+
checkTool('node', '1.0.0');
55+
56+
expect(rejectSpy).toHaveBeenCalledWith(jasmine.stringMatching(/^node was not found./));
57+
});
58+
59+
it('should throw error because version is not following semver-notated.', (done) => {
60+
shellWhichSpy.and.returnValue('/bin/node');
61+
const checkTool = checkReqs.__get__('checkTool');
62+
63+
checkReqs.__set__('versions', {
64+
get_tool_version: getToolVersionSpy.and.returnValue(new Promise((resolve) => {
65+
return resolve('1.0.0');
66+
}).catch((error) => { console.log(error); })),
67+
compareVersions: originalVersion.compareVersions
68+
});
69+
70+
checkTool('node', 'v1.0.0').catch((error) => {
71+
expect(error).toEqual('Version should contain only numbers and dots');
72+
done();
73+
});
74+
});
75+
76+
it('should resolve passing back tool version.', (done) => {
77+
shellWhichSpy.and.returnValue('/bin/node');
78+
const checkTool = checkReqs.__get__('checkTool');
79+
80+
checkReqs.__set__('versions', {
81+
get_tool_version: getToolVersionSpy.and.returnValue(new Promise((resolve) => {
82+
return resolve('1.0.0');
83+
})),
84+
compareVersions: originalVersion.compareVersions
85+
});
86+
87+
checkTool('node', '1.0.0').then(() => {
88+
let actual = resolveSpy.calls.argsFor(0)[0];
89+
expect(actual).toEqual({version: '1.0.0'});
90+
done();
91+
});
92+
});
93+
94+
it('should reject because tool does not meet minimum requirement.', (done) => {
95+
shellWhichSpy.and.returnValue('/bin/node');
96+
const checkTool = checkReqs.__get__('checkTool');
97+
98+
checkReqs.__set__('versions', {
99+
get_tool_version: getToolVersionSpy.and.returnValue(new Promise((resolve) => {
100+
return resolve('1.0.0');
101+
})),
102+
compareVersions: originalVersion.compareVersions
103+
});
104+
105+
checkTool('node', '1.0.1').then(() => {
106+
let actual = rejectSpy.calls.argsFor(0)[0];
107+
expect(actual).toContain('version 1.0.1 or greater');
108+
expect(actual).toContain('you have version 1.0.0');
109+
done();
110+
});
111+
});
112+
});
113+
});

0 commit comments

Comments
 (0)