Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 4B06A200CB6 for ; Thu, 29 Jun 2017 21:40:19 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 49C94160BC6; Thu, 29 Jun 2017 19:40:19 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 69B39160BED for ; Thu, 29 Jun 2017 21:40:18 +0200 (CEST) Received: (qmail 47753 invoked by uid 500); 29 Jun 2017 19:40:17 -0000 Mailing-List: contact dev-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list dev@cordova.apache.org Received: (qmail 47296 invoked by uid 99); 29 Jun 2017 19:40:17 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Jun 2017 19:40:17 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 36DB6E943C; Thu, 29 Jun 2017 19:40:17 +0000 (UTC) From: stevengill To: dev@cordova.apache.org Reply-To: dev@cordova.apache.org References: In-Reply-To: Subject: [GitHub] cordova-lib pull request #573: CB-12361 : updated addHelper tests Content-Type: text/plain Message-Id: <20170629194017.36DB6E943C@git1-us-west.apache.org> Date: Thu, 29 Jun 2017 19:40:17 +0000 (UTC) archived-at: Thu, 29 Jun 2017 19:40:19 -0000 Github user stevengill commented on a diff in the pull request: https://github.com/apache/cordova-lib/pull/573#discussion_r124868182 --- Diff: spec/cordova/platform/addHelper.spec.js --- @@ -16,34 +16,459 @@ */ /* eslint-env jasmine */ +var path = require('path'); +var fs = require('fs'); +var Q = require('q'); +var shell = require('shelljs'); +var events = require('cordova-common').events; +var rewire = require('rewire'); +var platform_addHelper = rewire('../../../src/cordova/platform/addHelper'); +var platform_module = require('../../../src/cordova/platform'); +var platform_metadata = require('../../../src/cordova/platform_metadata'); +var cordova_util = require('../../../src/cordova/util'); +var cordova_config = require('../../../src/cordova/config'); +var plugman = require('../../../src/plugman/plugman'); +var fetch_metadata = require('../../../src/plugman/util/metadata'); +var lazy_load = require('../../../src/cordova/lazy_load'); +// require module here +// spy on it and return +var cordova = require('../../../src/cordova/cordova'); +var prepare = require('../../../src/cordova/prepare'); +var gitclone = require('../../../src/gitclone'); +var fail; + describe('cordova/platform/addHelper', function () { + var projectRoot = '/some/path'; + // These _mock and _revert_mock objects use rewire as the modules these mocks replace + // during testing all return functions, which we cannot spy on using jasmine. + // Thus, we replace these modules inside the scope of addHelper.js using rewire, and shim + // in these _mock test dummies. The test dummies themselves are constructed using + // jasmine.createSpy inside the first beforeEach. + var cfg_parser_mock = function () {}; + var cfg_parser_revert_mock; + var hooks_mock; + var platform_api_mock; + var fetch_mock; + var fetch_revert_mock; + var prepare_mock; + var prepare_revert_mock; + var fake_platform = { + 'platform': 'atari' + }; + beforeEach(function () { + hooks_mock = jasmine.createSpyObj('hooksRunner mock', ['fire']); + hooks_mock.fire.and.returnValue(Q()); + cfg_parser_mock.prototype = jasmine.createSpyObj('config parser mock', ['write', 'removeEngine', 'addEngine', 'getHookScripts']); + cfg_parser_revert_mock = platform_addHelper.__set__('ConfigParser', cfg_parser_mock); + fetch_mock = jasmine.createSpy('fetch mock').and.returnValue(Q()); + fetch_revert_mock = platform_addHelper.__set__('fetch', fetch_mock); + prepare_mock = jasmine.createSpy('prepare mock').and.returnValue(Q()); + prepare_mock.preparePlatforms = jasmine.createSpy('preparePlatforms mock').and.returnValue(Q()); + prepare_revert_mock = platform_addHelper.__set__('prepare', prepare_mock); + spyOn(shell, 'mkdir'); + spyOn(fs, 'existsSync').and.returnValue(false); + spyOn(fs, 'writeFileSync'); + spyOn(cordova_util, 'projectConfig').and.returnValue(path.join(projectRoot, 'config.xml')); + spyOn(cordova_util, 'isDirectory').and.returnValue(false); + spyOn(cordova_util, 'fixRelativePath').and.callFake(function (input) { return input; }); + spyOn(cordova_util, 'isUrl').and.returnValue(false); + spyOn(cordova_util, 'hostSupports').and.returnValue(true); + spyOn(cordova_util, 'removePlatformPluginsJson'); + spyOn(cordova_config, 'read').and.returnValue({}); + // Fake platform details we will use for our mocks, returned by either + // getPlatfromDetailsFromDir (in the local-directory case), or + // downloadPlatform (in every other case) + spyOn(platform_module, 'getPlatformDetailsFromDir').and.returnValue(Q(fake_platform)); + spyOn(platform_addHelper, 'downloadPlatform').and.returnValue(Q(fake_platform)); + spyOn(platform_addHelper, 'getVersionFromConfigFile').and.returnValue(false); + spyOn(platform_addHelper, 'installPluginsForNewPlatform').and.returnValue(Q()); + platform_api_mock = jasmine.createSpyObj('platform api mock', ['createPlatform', 'updatePlatform']); + platform_api_mock.createPlatform.and.returnValue(Q()); + platform_api_mock.updatePlatform.and.returnValue(Q()); + spyOn(cordova_util, 'getPlatformApiFunction').and.returnValue(platform_api_mock); + spyOn(platform_metadata, 'save'); + }); + afterEach(function () { + cfg_parser_revert_mock(); + fetch_revert_mock(); + prepare_revert_mock(); + }); describe('error/warning conditions', function () { - it('should require specifying at least one platform'); - it('should warn if host OS does not support the specified platform'); + it('should require specifying at least one platform', function (done) { + platform_addHelper('add', hooks_mock).then(function () { + fail('addHelper success handler unexpectedly invoked'); + }).fail(function (e) { + expect(e.message).toContain('No platform specified.'); + }).done(done); + }); + + it('should log if host OS does not support the specified platform', function () { + cordova_util.hostSupports.and.returnValue(false); + spyOn(events, 'emit'); + platform_addHelper('add', hooks_mock, projectRoot, ['atari']); + expect(events.emit.calls.mostRecent().args[1]).toContain('can not be built on this OS'); --- End diff -- @filmaj suggested we avoid the `.calls` api and instead use `toHaveBeenCalledWith()`. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org For additional commands, e-mail: dev-help@cordova.apache.org