@hkvstore/taco-cli
Version:
taco-cli is a command-line interface for rapid Apache Cordova development (forked from Microsoft taco-cli)
440 lines (438 loc) • 19.7 kB
JavaScript
// 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/node.d.ts" />
/// <reference path="../../typings/should.d.ts" />
/// <reference path="../../typings/cordovaExtensions.d.ts" />
/// <reference path="../../typings/del.d.ts" />
/// <reference path="../../typings/tacoTestsUtils.d.ts"/>
;
/* 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");
var del = require("del");
var os = require("os");
var path = require("path");
var Q = require("q");
var rimraf = require("rimraf");
var kitHelper = require("../cli/utils/kitHelper");
var TacoUtility = require("taco-utils");
var TacoTestUtility = require("taco-tests-utils");
var tacoTestsUtils = require("taco-tests-utils");
var CommandHelper = require("./utils/commandHelper");
var TestProjectHelper = TacoTestUtility.ProjectHelper;
var utils = TacoUtility.UtilHelper;
var MemoryStream = tacoTestsUtils.MemoryStream;
var platformCommand = CommandHelper.getCommand("platform");
var pluginCommand = CommandHelper.getCommand("plugin");
var createCommand = CommandHelper.getCommand("create");
var testKitId = "5.1.1-Kit";
/* tslint:disable:no-var-requires */
// var require needed to require package json
var cliVersion = require("../package.json").version;
// Expected valued for various scenarios
var userOverridePlatformVersions = {
android: "4.0.1",
ios: "3.8.0"
};
var kitPlatformVersions = {
android: "4.0.2",
ios: "3.8.0"
};
var cliPlatformVersions = {
android: "4.0.0",
ios: "3.8.0"
};
var userOverridePluginVersions = {
"cordova-plugin-camera": "1.0.0",
"cordova-plugin-contacts": "1.0.0"
};
var kitPluginVersions = {
"cordova-plugin-camera": "1.1.0",
"cordova-plugin-contacts": "1.0.0"
};
var kitPlatformOperations = [
{
command: "add android ios",
expectedVersions: kitPlatformVersions,
expectedTelemetryProperties: {
kit: { isPii: false, value: "5.1.1-Kit" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "add" },
target1: { isPii: true, value: "android@4.0.2" },
target2: { isPii: true, value: "ios@3.8.0" }
}
},
{
command: "rm android ios",
expectedVersions: {},
expectedTelemetryProperties: {
kit: { isPii: false, value: "5.1.1-Kit" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "remove" },
target1: { isPii: true, value: "android" },
target2: { isPii: true, value: "ios" }
}
},
{
command: "add android@4.0.1 ios@3.8.0",
expectedVersions: userOverridePlatformVersions,
expectedTelemetryProperties: {
kit: { isPii: false, value: "5.1.1-Kit" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "add" },
target1: { isPii: true, value: "android@4.0.1" },
target2: { isPii: true, value: "ios@3.8.0" }
}
}
];
var cliPlatformOperations = [
{
command: "add android browser",
expectedVersions: cliPlatformVersions,
expectedTelemetryProperties: {
cli: { isPii: false, value: "5.0.0" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "add" },
target1: { isPii: true, value: "android" },
target2: { isPii: true, value: "browser" }
}
},
{
command: "remove android browser",
expectedVersions: {},
expectedTelemetryProperties: {
cli: { isPii: false, value: "5.0.0" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "remove" },
target1: { isPii: true, value: "android" },
target2: { isPii: true, value: "browser" }
}
},
{
command: "add android@4.0.1 browser@4.0.0",
expectedVersions: userOverridePlatformVersions,
expectedTelemetryProperties: {
cli: { isPii: false, value: "5.0.0" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "add" },
target1: { isPii: true, value: "android@4.0.1" },
target2: { isPii: true, value: "browser@4.0.0" }
}
},
{
command: "remove android browser",
expectedVersions: {},
expectedTelemetryProperties: {
cli: { isPii: false, value: "5.0.0" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "remove" },
target1: { isPii: true, value: "android" },
target2: { isPii: true, value: "browser" }
}
}
];
var kitPluginOperations = [
{
command: "add cordova-plugin-camera@1.0.0 cordova-plugin-contacts@1.0.0",
expectedVersions: userOverridePluginVersions,
expectedTelemetryProperties: {
kit: { isPii: false, value: "5.1.1-Kit" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "add" },
target1: { isPii: true, value: "cordova-plugin-camera@1.0.0" },
target2: { isPii: true, value: "cordova-plugin-contacts@1.0.0" }
}
},
{
command: "remove cordova-plugin-camera cordova-plugin-contacts",
expectedVersions: {},
expectedTelemetryProperties: {
kit: { isPii: false, value: "5.1.1-Kit" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "remove" },
target1: { isPii: true, value: "cordova-plugin-camera" },
target2: { isPii: true, value: "cordova-plugin-contacts" }
}
}
];
var cliPluginOperations = [
{
command: "add cordova-plugin-camera@1.0.0 cordova-plugin-contacts@1.0.0",
expectedVersions: userOverridePluginVersions,
expectedTelemetryProperties: {
cli: { isPii: false, value: "5.0.0" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "add" },
target1: { isPii: true, value: "cordova-plugin-camera@1.0.0" },
target2: { isPii: true, value: "cordova-plugin-contacts@1.0.0" }
}
},
{
command: "rm cordova-plugin-camera cordova-plugin-contacts",
expectedVersions: {},
expectedTelemetryProperties: {
cli: { isPii: false, value: "5.0.0" },
cliVersion: { isPii: false, value: cliVersion },
isTacoProject: { isPii: false, value: "true" },
projectType: { isPii: false, value: "JavaScript" },
subCommand: { isPii: false, value: "remove" },
target1: { isPii: true, value: "cordova-plugin-camera" },
target2: { isPii: true, value: "cordova-plugin-contacts" }
}
}
];
describe("taco platform for kit", function () {
var tacoHome = path.join(os.tmpdir(), "taco-cli", "platformPlugin");
var cliProjectDir = "cliProject";
var kitProjectDir = "kitProject";
var originalCwd;
var cordovaVersion = "5.1.1";
function createProject(args, projectDir) {
// Create a dummy test project with no platforms added
utils.createDirectoryIfNecessary(tacoHome);
process.chdir(tacoHome);
return Q.denodeify(del)(projectDir).then(function () {
return createCommand.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) {
// Create a dummy test project with no platforms added
return createProject(["kitProject", "--kit", kit], kitProjectDir);
}
function platformRun(args) {
return platformCommand.run(args);
}
function pluginRun(args) {
return pluginCommand.run(args);
}
function sleep(milliseconds) {
var deferred = Q.defer();
setTimeout(deferred.resolve, milliseconds);
return deferred.promise;
}
;
before(function (mocha) {
originalCwd = process.cwd();
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(tacoHome);
createKitProject("5.1.1-Kit")
.done(function () {
mocha();
});
});
after(function (done) {
process.chdir(originalCwd);
kitHelper.kitPackagePromise = null;
rimraf(tacoHome, function (err) { done(); }); // ignore errors
});
describe("taco platform/plugin operation for a kit project with platform/plugin overrides execute with no errors", function () {
var kitProjectpath;
before(function () {
kitProjectpath = path.join(tacoHome, kitProjectDir);
process.chdir(kitProjectpath);
});
after(function (done) {
process.chdir(tacoHome);
rimraf(kitProjectpath, function (err) { done(); }); // ignore errors
});
kitPlatformOperations.forEach(function (scenario) {
it("taco platform " + scenario.command + " executes with no error", function (done) {
var args = scenario.command.split(" ");
platformRun(args)
.then(function (telemetryParameters) {
// Wait for 5 seconds after the installation to avoid false negatives in version checking
telemetryParameters.should.be.eql(scenario.expectedTelemetryProperties);
return sleep(5);
}).then(function () {
if (args[0] === "add") {
// Check the version of platform after addition
TestProjectHelper.checkPlatformVersions(scenario.expectedVersions, kitProjectpath);
}
}).then(function () {
done();
}, function (err) {
done(err);
});
});
});
kitPluginOperations.forEach(function (scenario) {
it("taco plugin " + scenario.command + " executes with no error", function (done) {
var args = scenario.command.split(" ");
pluginRun(args)
.then(function (telemetryParameters) {
// Wait for 5 seconds after the installation to avoid false negatives in version checking
telemetryParameters.should.be.eql(scenario.expectedTelemetryProperties);
return sleep(5);
}).then(function () {
if (args[0] === "add") {
// Check the version of plugin after addition
TestProjectHelper.checkPluginVersions(scenario.expectedVersions, kitProjectpath);
}
}).then(function () {
done();
}, function (err) {
done(err);
});
});
});
});
describe("taco platform/plugin operation for a CLI project with no platform/plugin overrides execute with no errors", function () {
var cliProjectPath;
before(function (mocha) {
createCliProject("5.0.0")
.then(function () {
cliProjectPath = path.join(tacoHome, cliProjectDir);
process.chdir(cliProjectPath);
mocha();
});
});
after(function (done) {
rimraf(cliProjectPath, function (err) { done(); }); // ignore errors
});
cliPlatformOperations.forEach(function (scenario) {
it("taco platform " + scenario.command + " executes with no error", function (done) {
var args = scenario.command.split(" ");
platformRun(args)
.then(function (telemetryParameters) {
// Wait for 5 seconds after the installation to avoid false negatives in version checking
telemetryParameters.should.be.eql(scenario.expectedTelemetryProperties);
return sleep(5);
}).then(function () {
if (args[0] === "add") {
// Check the version of platform after addition
TestProjectHelper.checkPlatformVersions(scenario.expectedVersions, cliProjectPath);
}
}).then(function () {
done();
}, function (err) {
done(err);
});
});
});
cliPluginOperations.forEach(function (scenario) {
it("taco plugin " + scenario.command + " executes with no error", function (done) {
var args = scenario.command.split(" ");
pluginRun(args)
.then(function (telemetryParameters) {
// Wait for 5 seconds after the installation to avoid false negatives in version checking
telemetryParameters.should.be.eql(scenario.expectedTelemetryProperties);
return sleep(5);
}).then(function () {
if (args[0] === "add") {
// Check the version of plugin after addition
TestProjectHelper.checkPluginVersions(scenario.expectedVersions, cliProjectPath);
}
}).then(function () {
done();
}, function (err) {
done(err);
});
});
});
});
describe("Onboarding experience", function () {
// because of function overloading assigning "(buffer: string, cb?: Function) => boolean" as the type for
// stdoutWrite just doesn't work
var stdoutWrite = process.stdout.write; // We save the original implementation, so we can restore it later
var memoryStdout;
beforeEach(function (done) {
// We create a taco project outside of the test
Q.fcall(createCliProject, "5.0.0").done(function () {
// After the taco project is created, we initialize the console, so we won't get the creation messages in the console
memoryStdout = new MemoryStream; // Each individual test gets a new and empty console
process.stdout.write = memoryStdout.writeAsFunction(); // We'll be printing into an "in-memory" console, so we can test the output
done();
}, function (err) {
done(err);
});
});
after(function () {
// We just need to reset the stdout just once, after all the tests have finished
process.stdout.write = stdoutWrite;
});
function testCommandForArguments(commandRun, platformCommandLineArguments, scenarioArguments, alternativeScenarioArguments, done) {
// Some messages are only printed the first time something is executed. When we run all the tests
// all those messages don't get printed, but if we only run the onboarding tests, they are the first
// tests to run, so they do get printed. We accept both options and we validate we got one of them
commandRun(platformCommandLineArguments).done(function () {
var actual = memoryStdout.contentsAsText();
if (scenarioArguments.every(function (msg) { return actual.indexOf(msg) >= 0; }) || alternativeScenarioArguments.every(function (msg) { return actual.indexOf(msg) >= 0; })) {
done();
}
else {
done(new Error("Bad onboarding for " + platformCommandLineArguments));
}
}, function (err) {
done(err);
});
}
it("prints the onboarding experience when adding a platform", function (done) {
var firstPart = ["CommandPlatformStatusAdding"];
var lastPart = [
"CommandPlatformStatusAdded",
"OnboardingExperienceTitle",
" * HowToUseCommandInstallReqsPlugin",
" * HowToUseCommandAddPlugin",
" * HowToUseCommandSetupRemote",
" * HowToUseCommandBuildPlatform",
" * HowToUseCommandEmulatePlatform",
" * HowToUseCommandRunPlatform",
"",
"HowToUseCommandHelp",
"HowToUseCommandDocs",
""];
testCommandForArguments(platformRun, ["add", "android"], firstPart.concat(lastPart), lastPart, done);
});
it("prints the onboarding experience when adding a plugin", function (done) {
var firstPart = [
"CommandPluginTestedPlatforms",
"CommandPluginStatusAdding"];
var lastPart = [
"CommandPluginWithIdStatusAdded",
"OnboardingExperienceTitle",
" * HowToUseCommandInstallReqsPlugin",
" * HowToUseCommandSetupRemote",
" * HowToUseCommandBuildPlatform",
" * HowToUseCommandEmulatePlatform",
" * HowToUseCommandRunPlatform",
"",
"HowToUseCommandHelp",
"HowToUseCommandDocs",
""];
testCommandForArguments(pluginRun, ["add", "cordova-plugin-camera"], firstPart.concat(lastPart), lastPart, done);
});
});
});
//# sourceMappingURL=platformPlugin.js.map