@hkvstore/taco-cli
Version:
taco-cli is a command-line interface for rapid Apache Cordova development (forked from Microsoft taco-cli)
217 lines (215 loc) • 8.62 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" />
;
/* 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 http = require("http");
var os = require("os");
var path = require("path");
var Q = require("q");
var querystring = require("querystring");
var rimraf = require("rimraf");
var buildAndRunTelemetry = require("./buildAndRunTelemetry");
var kitHelper = require("../cli/utils/kitHelper");
var ServerMock = require("./utils/serverMock");
var RemoteMock = require("./utils/remoteMock");
var TacoUtility = require("taco-utils");
var BuildInfo = TacoUtility.BuildInfo;
var Command = buildAndRunTelemetry.Command;
var utils = TacoUtility.UtilHelper;
var CommandHelper = require("./utils/commandHelper");
var create = CommandHelper.getCommand("create");
describe("taco run", function () {
var _this = this;
var testHttpServer;
var tacoHome = path.join(os.tmpdir(), "taco-cli", "run");
var originalCwd;
var vcordova = "4.0.0";
function createCleanProject() {
// Create a dummy test project with no platforms added
utils.createDirectoryIfNecessary(tacoHome);
process.chdir(tacoHome);
return Q.denodeify(del)("example").then(function () {
var args = ["example", "--cordova", vcordova];
return create.run(args);
})
.then(function () {
process.chdir(path.join(tacoHome, "example"));
});
}
before(function (mocha) {
originalCwd = process.cwd();
// Set up mocked out resources
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;
// Create a mocked out remote server so we can specify how it reacts
testHttpServer = http.createServer();
var port = 3000;
testHttpServer.listen(port);
// Configure a dummy platform "test" to use the mocked out remote server in insecure mode
RemoteMock.saveConfig("test", { host: "localhost", port: 3000, secure: false, mountPoint: "cordova" }).done(function () {
mocha();
}, function (err) {
mocha(err);
});
});
after(function (done) {
process.chdir(originalCwd);
kitHelper.kitPackagePromise = null;
testHttpServer.close();
rimraf(tacoHome, function (err) { done(); }); // ignore errors
});
beforeEach(function (mocha) {
Q.fcall(createCleanProject).done(function () {
mocha();
}, function (err) {
mocha(err);
});
});
afterEach(function (mocha) {
process.chdir(tacoHome);
del("example", mocha);
});
var runRun = function (args) {
var run = CommandHelper.getCommand("run");
return run.run(args);
};
it("should make the correct sequence of calls for 'taco run --remote test --device'", function (mocha) {
var runArguments = ["--remote", "test", "--device", "--nobuild"];
var configuration = "debug";
var buildNumber = 12343;
var buildInfo = {
buildNumber: buildNumber,
status: BuildInfo.COMPLETE,
buildLang: "en"
};
var buildInfoPath = path.resolve(".", "remote", "test", configuration);
utils.createDirectoryIfNecessary(buildInfoPath);
fs.writeFileSync(path.join(buildInfoPath, "buildInfo.json"), JSON.stringify(buildInfo));
// Mock out the server on the other side
var sequence = [
{
expectedUrl: "/cordova/build/" + buildNumber,
head: {
"Content-Type": "application/json"
},
statusCode: 200,
response: JSON.stringify(new BuildInfo({
status: BuildInfo.COMPLETE,
buildNumber: buildNumber
}))
},
{
expectedUrl: "/cordova/build/" + buildNumber + "/deploy",
head: {
"Content-Type": "application/json"
},
statusCode: 200,
response: JSON.stringify(new BuildInfo({
status: BuildInfo.INSTALLED,
buildNumber: buildNumber
})),
waitForPayload: false
},
{
expectedUrl: "/cordova/build/" + buildNumber + "/run",
head: {
"Content-Type": "application/json"
},
statusCode: 200,
response: JSON.stringify(new BuildInfo({
status: BuildInfo.RUNNING,
buildNumber: buildNumber
})),
waitForPayload: false
}
];
var serverFunction = ServerMock.generateServerFunction(mocha, sequence);
testHttpServer.on("request", serverFunction);
Q(runArguments).then(runRun).finally(function () {
testHttpServer.removeListener("request", serverFunction);
}).done(function () {
mocha();
}, function (err) {
mocha(err);
});
});
it("should make the correct sequence of calls for 'taco run --remote test --emulator", function (mocha) {
var target = "iphone 5";
var runArguments = ["--remote", "test", "--emulator", "--target", target, "--nobuild"];
var configuration = "debug";
var buildNumber = 12344;
var buildInfo = {
buildNumber: buildNumber,
status: BuildInfo.COMPLETE,
buildLang: "en"
};
var buildInfoPath = path.resolve(".", "remote", "test", configuration);
utils.createDirectoryIfNecessary(buildInfoPath);
fs.writeFileSync(path.join(buildInfoPath, "buildInfo.json"), JSON.stringify(buildInfo));
// Mock out the server on the other side
var sequence = [
{
expectedUrl: "/cordova/build/" + buildNumber,
head: {
"Content-Type": "application/json"
},
statusCode: 200,
response: JSON.stringify(new BuildInfo({
status: BuildInfo.COMPLETE,
buildNumber: buildNumber
}))
},
{
expectedUrl: "/cordova/build/" + buildNumber + "/emulate?" + querystring.stringify({ target: target }),
head: {
"Content-Type": "application/json"
},
statusCode: 200,
response: JSON.stringify(new BuildInfo({
status: BuildInfo.EMULATED,
buildNumber: buildNumber
})),
waitForPayload: false
}
];
var serverFunction = ServerMock.generateServerFunction(mocha, sequence);
testHttpServer.on("request", serverFunction);
Q(runArguments).then(runRun).finally(function () {
testHttpServer.removeListener("request", serverFunction);
}).done(function () {
mocha();
}, function (err) {
mocha(err);
});
});
it("should error out if there is no buildInfo.json and we require no build", function (mocha) {
Q(["--remote", "--nobuild", "test"]).then(runRun).done(function () {
mocha(new Error("Run should have failed!"));
}, function (err) {
if (err.message !== "NoRemoteBuildIdFound") {
mocha(err);
}
else {
mocha();
}
});
});
describe("telemetry", function () {
buildAndRunTelemetry.createBuildAndRunTelemetryTests.call(_this, runRun, Command.Run);
});
});
//# sourceMappingURL=run.js.map