UNPKG

@hkvstore/taco-cli

Version:

taco-cli is a command-line interface for rapid Apache Cordova development (forked from Microsoft taco-cli)

293 lines (291 loc) 13.1 kB
// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for details. /// <reference path="../../typings/mocha.d.ts"/> /// <reference path="../../typings/should.d.ts"/> /// <reference path="../../typings/tacoUtils.d.ts"/> /// <reference path="../../typings/tacoKits.d.ts"/> "use strict"; /* tslint:disable:no-var-requires */ // var require needed for should module to work correctly // Note not import: We don't want to refer to shouldModule, but we need the require to occur since it modifies the prototype of Object. var shouldModule = require("should"); /* tslint:enable:no-var-requires */ var del = require("del"); var fs = require("fs"); var os = require("os"); var path = require("path"); var tacoKits = require("taco-kits"); var Q = require("q"); var rimraf = require("rimraf"); var KitMod = require("../cli/kit"); var kitHelper = require("../cli/utils/kitHelper"); var TacoErrorCodes = require("../cli/tacoErrorCodes"); var TacoUtility = require("taco-utils"); var TacoTestUtility = require("taco-tests-utils"); var utils = TacoUtility.UtilHelper; var TacoKitsErrorCodes = tacoKits.TacoErrorCode; var CommandHelper = require("./utils/commandHelper"); var TestProjectHelper = TacoTestUtility.ProjectHelper; function kitRun(args) { if (args === void 0) { args = []; } var kit = CommandHelper.getCommand("kit"); return kit.run(args); } var previous; var runFolder = path.resolve(os.tmpdir(), "taco_cli_kit"); var tacoHome = path.join(runFolder, "taco_home"); var cliProjectDir = "cliProject"; var kitProjectDir = "kitProject"; var tempJson = path.resolve(runFolder, "temp.json"); var originalCwd; var expectedCliTacoJsonKeyValues1 = { "cordova-cli": "4.3.1" }; var expectedCliTacoJsonKeyValues2 = { "cordova-cli": "5.1.1" }; var expectedCliTacoJsonKeyValues3 = { "cordova-cli": "5.4.0" }; var expectedKitTacoJsonKeyValues = { kit: "5.1.1-Kit", "cordova-cli": "5.1.1" }; var expectedKitPlatformVersion = { android: "4.0.2" }; var expectedKitPluginVersion = { "cordova-plugin-camera": "1.1.0" }; function createProject(args, projectDir) { var create = CommandHelper.getCommand("create"); // Create a dummy test project with no platforms added utils.createDirectoryIfNecessary(tacoHome); process.chdir(tacoHome); return Q.denodeify(del)(projectDir).then(function () { return create.run(args); }).then(function () { var projectPath = path.join(tacoHome, projectDir); process.chdir(projectPath); }); } function createCliProject(cli) { return createProject(["cliProject", "--cordova", cli], cliProjectDir); } function createKitProject(kit) { return createProject(["kitProject", "--kit", kit], kitProjectDir); } function platformRun(args) { var platform = CommandHelper.getCommand("platform"); return platform.run(args); } function pluginRun(args) { var plugin = CommandHelper.getCommand("plugin"); return plugin.run(args); } function addPlatformToProject(platfromName, projectPath) { process.chdir(projectPath); return platformRun(["add", platfromName]); } function addTestPluginsToProject(projectPath) { process.chdir(projectPath); return pluginRun(["add", "cordova-plugin-camera"]) .then(function () { // cordova.raw.plugin("add", [...]) returns before the plugin is actually added. // So, a delay is required here to ensure that the plugins are indeed added return sleep(100); }).then(function () { var medicPluginTestsPath = path.resolve(".", "plugins", "cordova-plugin-camera", "tests"); var pluginCommandArgs = ["add", medicPluginTestsPath]; return pluginRun(pluginCommandArgs); }); } function getMockYesOrNoHandler(errorHandler, onClose, desiredResponse) { return { question: function (question, callback) { switch (question) { case "CommandKitSelectProjectUpdatePrompt": callback(desiredResponse); break; default: errorHandler(new Error("Unexpected query!")); } }, close: onClose }; } function sleep(milliseconds) { return Q.delay(milliseconds); } function runKitCommandSuccessCaseAndVerifyTacoJson(args, tacoJsonPath, tacoJsonKeyValues) { return kitRun(args) .then(function (telemetryParameters) { fs.existsSync(tacoJsonPath).should.be.true; var tacoJson = JSON.parse(fs.readFileSync(tacoJsonPath, { encoding: "utf-8" })); tacoJsonKeyValues.should.be.eql(tacoJson); return telemetryParameters; }); } function runKitCommandFailureCaseAndVerifyTacoJson(args, tacoJsonPath, tacoJsonKeyValues, expectedErrorCode) { return kitRun(args) .then(function () { throw new Error("Scenario succeeded when it should have failed"); }, function (err) { err.errorCode.should.equal(expectedErrorCode); // Also make sure that the project's taco.json // file exists and has the expected values fs.existsSync(tacoJsonPath).should.be.true; var tacoJson = JSON.parse(fs.readFileSync(tacoJsonPath, { encoding: "utf-8" })); tacoJsonKeyValues.should.be.eql(tacoJson); }); } describe("Kit command: ", function () { before(function () { originalCwd = process.cwd(); previous = process.env["TACO_UNIT_TEST"]; process.env["TACO_UNIT_TEST"] = true; // Use a dummy home location so we don't trash any real configurations process.env["TACO_HOME"] = tacoHome; // Force KitHelper to fetch the package fresh kitHelper.kitPackagePromise = null; rimraf.sync(runFolder); }); after(function () { process.env["TACO_UNIT_TEST"] = previous; process.chdir(originalCwd); kitHelper.kitPackagePromise = null; rimraf.sync(runFolder); // ignore errors }); describe("Kit list command : ", function () { it("'taco kit' should not throw any error", function () { return kitRun() .then(function (telemetryParameters) { var expected = { subCommand: { isPii: false, value: "list" } }; telemetryParameters.should.be.eql(expected); }); }); it("'taco kit list' should not throw any error", function () { return kitRun(["list"]) .then(function (telemetryParameters) { var expected = { subCommand: { isPii: false, value: "list" } }; telemetryParameters.should.be.eql(expected); }); }); it("'taco kit list --kit {kit-ID}' should not throw any error", function () { return kitRun(["list", "--kit", "5.1.1-Kit"]) .then(function (telemetryParameters) { var expected = { subCommand: { isPii: false, value: "list" }, "options.kit": { isPii: false, value: "5.1.1-Kit" } }; telemetryParameters.should.be.eql(expected); }); }); it("'taco kit list --json {path}' should generate the JSON", function () { return kitRun(["list", "--json", tempJson]) .then(function (telemetryParameters) { fs.existsSync(tempJson).should.be.true; var expected = { subCommand: { isPii: false, value: "list" }, "options.json": { isPii: true, value: tempJson } }; telemetryParameters.should.be.eql(expected); }); }); it("'taco kit list should work for a project UTF16 taco.json", function () { var kitProjectpath = path.join(tacoHome, kitProjectDir); var tacoJsonPath = path.resolve(kitProjectpath, "taco.json"); // Create a kit project, modify taco.json encoding and run taco kit list return createKitProject("5.1.1-Kit") .then(function () { // change the encoding to UTF 16 var tacoJsonContents = fs.readFileSync(tacoJsonPath).toString(); fs.writeFileSync(tacoJsonPath, new Buffer(tacoJsonContents, "utf16le")); return kitRun(["list"]); }) .then(function (telemetryParameters) { var expected = { subCommand: { isPii: false, value: "list" } }; telemetryParameters.should.be.eql(expected); }); }); }); describe("Kit project to a cli project: ", function () { var kitProjectpath = path.join(tacoHome, kitProjectDir); var tacoJsonPath = path.resolve(kitProjectpath, "taco.json"); before(function (done) { createKitProject("5.1.1-Kit") .then(function () { return addPlatformToProject("android", kitProjectpath); }) .then(function () { done(); }, function (err) { done(err); }); }); after(function (done) { process.chdir(tacoHome); rimraf(kitProjectpath, function (err) { done(); }); // ignore errors }); it("'taco kit select --cordova {Invalid-CLI-VERSION}' should execute with expected errors", function (done) { runKitCommandFailureCaseAndVerifyTacoJson(["select", "--cordova", "InvalidCordovaCliVersion"], tacoJsonPath, expectedKitTacoJsonKeyValues, TacoErrorCodes.ErrorInvalidVersion) .done(function () { return done(); }, done); }); it("'taco kit select --cordova {CLI-VERSION}' and a negative response to update query should fail with a warning", function (done) { KitMod.yesOrNoHandler = getMockYesOrNoHandler(done, utils.emptyMethod, "PromptResponseNo"); runKitCommandFailureCaseAndVerifyTacoJson(["select", "--cordova", "4.3.1"], tacoJsonPath, expectedKitTacoJsonKeyValues, 0 /* Command should fail with a warning */) .done(function () { return done(); }, done); }); it("'taco kit select --cordova {CLI-VERSION}' on a project with a platform added, should execute with no errors", function (done) { KitMod.yesOrNoHandler = getMockYesOrNoHandler(done, utils.emptyMethod, "PromptResponseYes"); runKitCommandSuccessCaseAndVerifyTacoJson(["select", "--cordova", "5.4.0"], tacoJsonPath, expectedCliTacoJsonKeyValues3) .then(function (telemetryParameters) { var expected = { subCommand: { isPii: false, value: "select" }, "options.cordova": { isPii: false, value: "5.4.0" } }; telemetryParameters.should.be.eql(expected); }) .done(function () { return done(); }, done); }); }); describe("CLI project to a Kit project: ", function () { var cliProjectpath = path.join(tacoHome, cliProjectDir); var tacoJsonPath = path.resolve(cliProjectpath, "taco.json"); before(function (done) { createCliProject("5.1.1") .then(function () { return addPlatformToProject("android", cliProjectpath); }) .then(function () { done(); }, function (err) { done(err); }); }); after(function (done) { process.chdir(tacoHome); rimraf(cliProjectpath, function (err) { done(); }); // ignore errors }); it("'taco kit select --kit {Invalid-kit-ID}' should execute with expected error", function (done) { runKitCommandFailureCaseAndVerifyTacoJson(["select", "--kit", "InvalidKit"], tacoJsonPath, expectedCliTacoJsonKeyValues2, TacoKitsErrorCodes.TacoKitsExceptionInvalidKit) .done(function () { return done(); }, done); }); it("'taco kit select --kit {kit-ID}' followed by a positive response to platform/plugin update query should should execute with no errors", function (done) { KitMod.yesOrNoHandler = getMockYesOrNoHandler(done, utils.emptyMethod, "PromptResponseYes"); return addTestPluginsToProject(cliProjectpath) .then(function () { return runKitCommandSuccessCaseAndVerifyTacoJson(["select", "--kit", "5.1.1-Kit"], tacoJsonPath, expectedKitTacoJsonKeyValues); }) .then(function (telemetryParameters) { var expected = { subCommand: { isPii: false, value: "select" }, "options.kit": { isPii: false, value: "5.1.1-Kit" } }; telemetryParameters.should.be.eql(expected); TestProjectHelper.checkPlatformVersions(expectedKitPlatformVersion, cliProjectpath); TestProjectHelper.checkPluginVersions(expectedKitPluginVersion, cliProjectpath); }).done(function () { return done(); }, done); }); }); }); //# sourceMappingURL=kit.js.map