From 0fe718e27367ef64358e4e8d0075c094a9c1b843 Mon Sep 17 00:00:00 2001 From: Audrey So Date: Tue, 18 Jul 2017 10:35:28 -0700 Subject: [PATCH 1/2] CB-12361 : added unit-tests for getPlatformDetailsFromDir --- .../getPlatformDetailsFromDir.spec.js | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 spec/cordova/platform/getPlatformDetailsFromDir.spec.js diff --git a/spec/cordova/platform/getPlatformDetailsFromDir.spec.js b/spec/cordova/platform/getPlatformDetailsFromDir.spec.js new file mode 100644 index 000000000..7a95e290e --- /dev/null +++ b/spec/cordova/platform/getPlatformDetailsFromDir.spec.js @@ -0,0 +1,79 @@ +/** + 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 path = require('path'); +var fs = require('fs'); +var Q = require('q'); +var rewire = require('rewire'); +var cordova_util = require('../../../src/cordova/util'); +var platform_getPlatformDetails = rewire('../../../src/cordova/platform/getPlatformDetailsFromDir'); +var events = require('cordova-common').events; +var fail; + +describe('cordova/platform/getPlatformDetailsFromDir', function () { + var package_json_mock; + package_json_mock = jasmine.createSpyObj('package json mock', ['cordova', 'dependencies']); + package_json_mock.name = 'io.cordova.hellocordova'; + package_json_mock.version = '1.0.0'; + + beforeEach(function () { + spyOn(Q, 'reject'); + spyOn(fs, 'existsSync'); + spyOn(cordova_util, 'requireNoCache'); + spyOn(events, 'emit'); + }); + + it('should throw if no config.xml or pkgJson', function (done) { + platform_getPlatformDetails('dir', ['ios']); + expect(Q.reject).toHaveBeenCalledWith(jasmine.stringMatching(/does not seem to contain a valid package.json or a valid Cordova platform/)); + done(); + }); + + it('should throw if no platform is provided', function (done) { + cordova_util.requireNoCache.and.returnValue({}); + platform_getPlatformDetails('dir'); + expect(Q.reject).toHaveBeenCalledWith(jasmine.stringMatching(/does not seem to contain a Cordova platform:/)); + done(); + }); + + it('should return a promise with platform and version', function (done) { + fs.existsSync.and.callFake(function(filePath) { + if(path.basename(filePath) === 'package.json') { + return true; + } else { + return false; + } + }); + cordova_util.requireNoCache.and.returnValue(package_json_mock); + platform_getPlatformDetails('dir', ['cordova-android']) + .then(function(result) { + expect(result.platform).toBe('io.cordova.hellocordova'); + expect(result.version).toBe('1.0.0'); + expect(Q.reject).not.toHaveBeenCalled(); + }).fail(function (err) { + fail('unexpected failure handler invoked!'); + console.error(err); + }).done(done); + }); + + it('should remove the cordova- prefix from the platform name for known platforms', function (done) { + platform_getPlatformDetails.platformFromName('cordova-ios'); + expect(events.emit).toHaveBeenCalledWith('verbose', jasmine.stringMatching(/Removing "cordova-" prefix/)); + expect(platform_getPlatformDetails.platformFromName('cordova-ios')).toBe('ios'); + done(); + }); +}); From 02e734ce97ffbb0f3c2a0e8ad3b469b50451c042 Mon Sep 17 00:00:00 2001 From: Audrey So Date: Thu, 27 Jul 2017 14:56:48 -0700 Subject: [PATCH 2/2] CB-12361 : rebased and revised get platform details test --- spec/cordova/platform/getPlatformDetailsFromDir.spec.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/cordova/platform/getPlatformDetailsFromDir.spec.js b/spec/cordova/platform/getPlatformDetailsFromDir.spec.js index 7a95e290e..0a98b2b93 100644 --- a/spec/cordova/platform/getPlatformDetailsFromDir.spec.js +++ b/spec/cordova/platform/getPlatformDetailsFromDir.spec.js @@ -71,9 +71,8 @@ describe('cordova/platform/getPlatformDetailsFromDir', function () { }); it('should remove the cordova- prefix from the platform name for known platforms', function (done) { - platform_getPlatformDetails.platformFromName('cordova-ios'); - expect(events.emit).toHaveBeenCalledWith('verbose', jasmine.stringMatching(/Removing "cordova-" prefix/)); expect(platform_getPlatformDetails.platformFromName('cordova-ios')).toBe('ios'); + expect(events.emit).toHaveBeenCalledWith('verbose', jasmine.stringMatching(/Removing "cordova-" prefix/)); done(); }); });