This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
phantoms test for demo & run engine.io tests
- Loading branch information
Showing
5 changed files
with
353 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
|
||
const PATH = require("path"); | ||
const FS = require("fs"); | ||
const SPAWN = require("child_process").spawn; | ||
|
||
|
||
function main(callback) { | ||
|
||
console.log("Running `./demo`:"); | ||
|
||
require("./demo").main(function(err) { | ||
if (err) return callback(err); | ||
|
||
return runEngineIoTests(callback); | ||
}); | ||
|
||
|
||
// @see http://stackoverflow.com/questions/1187518/javascript-array-difference | ||
function diff(a, b) { | ||
return a.filter(function(i) {return !(b.indexOf(i) > -1);}); | ||
}; | ||
|
||
function runEngineIoTests(callback) { | ||
|
||
// TODO: Use `sm` for this: `sm test <packagePath>` | ||
function ensureDevDependenciesInstalled(packagePath, callback) { | ||
|
||
var keys = Object.keys(JSON.parse(FS.readFileSync(PATH.join(packagePath, "package.json"))).devDependencies); | ||
var dirs = FS.readdirSync(PATH.join(packagePath, "node_modules")); | ||
|
||
// See if dev dependencies are missing. | ||
if (diff(keys, dirs).length > 0) { | ||
var proc = SPAWN("npm", [ | ||
"install" | ||
], { | ||
cwd: packagePath | ||
}); | ||
|
||
proc.on("error", function(err) { | ||
callback(err); | ||
}); | ||
|
||
proc.stdout.on("data", function(data) { | ||
process.stdout.write(data.toString()); | ||
}); | ||
proc.stderr.on("data", function(data) { | ||
process.stderr.write(data.toString()); | ||
}); | ||
proc.on("exit", function(code) { | ||
if (code !== 0) { | ||
callback(new Error("Did not get `status === 0`!")); | ||
return; | ||
} | ||
callback(null); | ||
}); | ||
} else { | ||
callback(null); | ||
} | ||
} | ||
|
||
// TODO: Use `sm` for this: `sm test <packagePath>` | ||
function testPackage(packagePath, callback) { | ||
|
||
var proc = SPAWN("npm", [ | ||
"test" | ||
], { | ||
cwd: packagePath | ||
}); | ||
|
||
proc.on("error", function(err) { | ||
callback(err); | ||
}); | ||
|
||
proc.stdout.on("data", function(data) { | ||
process.stdout.write(data.toString()); | ||
}); | ||
proc.stderr.on("data", function(data) { | ||
process.stderr.write(data.toString()); | ||
}); | ||
proc.on("exit", function(code) { | ||
if (code !== 0) { | ||
callback(new Error("Did not get `status === 0`!")); | ||
return; | ||
} | ||
callback(null); | ||
}); | ||
} | ||
|
||
|
||
console.log("Running `engine.io` and `engine.io-client` tests:") | ||
|
||
var path = PATH.join(__dirname, "../node_modules/engine.io"); | ||
|
||
ensureDevDependenciesInstalled(path, function(err) { | ||
if (err) return callback(err); | ||
|
||
testPackage(path, function(err) { | ||
if (err) return callback(err); | ||
|
||
path = PATH.join(__dirname, "../demo/node_modules/engine.io-client"); | ||
|
||
ensureDevDependenciesInstalled(path, function(err) { | ||
if (err) return callback(err); | ||
|
||
testPackage(path, function(err) { | ||
if (err) return callback(err); | ||
|
||
callback(null); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
} | ||
|
||
|
||
if (require.main === module) { | ||
main(function(err) { | ||
if (err) { | ||
console.error(err.stack); | ||
process.exit(1); | ||
} | ||
console.log("OK"); | ||
process.exit(0); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
//#!/usr/bin/env phantomjs | ||
|
||
var SYSTEM = require("system"); | ||
var WEBPAGE = require("webpage"); | ||
var FS = require("fs"); | ||
|
||
|
||
function main(callback) { | ||
|
||
function error(err) { | ||
callback(err); | ||
} | ||
|
||
var messages = []; | ||
var steps = []; | ||
var page = WEBPAGE.create(); | ||
|
||
page.onLoadStarted = function () { | ||
console.log("Start loading ..."); | ||
}; | ||
|
||
page.onConsoleMessage = function (msg) { | ||
if (/^:error:/.test(msg)) { | ||
error(msg.substring(7)); | ||
return; | ||
} else | ||
if (/^:/.test(msg)) { | ||
steps[msg.substring(1)](); | ||
return; | ||
} | ||
|
||
messages.push(msg); | ||
console.log("[phantomjs]", msg); | ||
}; | ||
|
||
page.onLoadFinished = function (status) { | ||
console.log("Loading finished. Running tests ..."); | ||
|
||
try { | ||
|
||
// Check some basic things. | ||
if (status !== "success") { | ||
return error("Page did not load with 'success'!"); | ||
} | ||
|
||
steps["start"] = function() { | ||
page.evaluate(function () { | ||
console.log(":wait-a-bit"); | ||
}); | ||
} | ||
|
||
steps["wait-a-bit"] = function() { | ||
setTimeout(function() { | ||
|
||
if (messages[0] !== "Connecting") { | ||
return error('`message[0] !== "Connecting"'); | ||
} | ||
if (messages[1] !== "Connected") { | ||
return error('`message[0] !== "Connected"'); | ||
} | ||
if (messages[2].indexOf("Relaying message") !== 0) { | ||
return error('`message[0].indexOf("Relaying message") !== 0"'); | ||
} | ||
if (messages[3] !== "Send ping: 1") { | ||
return error('`message[0] !== "Send ping: 1"'); | ||
} | ||
if (messages[4] !== "Received pong: 1") { | ||
return error('`message[0] !== "Received pong: 1"'); | ||
} | ||
if (messages[5] !== "Send ping: 2") { | ||
return error('`message[0] !== "Send ping: 2"'); | ||
} | ||
if (messages[6] !== "Received pong: 2") { | ||
return error('`message[0] !== "Received pong: 2"'); | ||
} | ||
|
||
steps["done"](); | ||
|
||
}, 3000); | ||
} | ||
|
||
steps["done"] = function() { | ||
callback(null); | ||
} | ||
|
||
steps["start"](); | ||
|
||
} catch(err) { | ||
callback(err); | ||
} | ||
}; | ||
|
||
page.open("http://localhost:" + SYSTEM.args[1] + "/"); | ||
} | ||
|
||
main(function(err) { | ||
if (err) { | ||
console.error((typeof err === "object" && err.stack)?err.stack:err); | ||
console.error("ERROR"); | ||
phantom.exit(1); | ||
} | ||
phantom.exit(0); | ||
}); |
Oops, something went wrong.