Remove magic dependency system in tests

The tests now load what they need using RequireJS rather than some
magic based on comments.
This commit is contained in:
Pierre Ossman 2017-01-18 17:12:28 +01:00
parent 6bc9cadaef
commit 73a6b8c9a2
6 changed files with 51 additions and 81 deletions

View File

@ -110,11 +110,11 @@ module.exports = function(config) {
files: [
'node_modules/requirejs/require.js',
'tests/test-main.js',
'tests/fake.*.js',
'tests/assertions.js',
'tests/test.*.js',
// Only packaged, not included in browser as RequireJS will
// do the actual loading
{pattern: 'tests/fake.*.js', included: false},
{pattern: 'core/*.js', included: false},
{pattern: 'core/input/*.js', included: false},
],

View File

@ -1,6 +1,6 @@
var FakeWebSocket;
"use strict";
(function () {
define(function () {
// PhantomJS can't create Event objects directly, so we need to use this
function make_event(name, props) {
var evt = document.createEvent('Event');
@ -13,6 +13,8 @@ var FakeWebSocket;
return evt;
}
var FakeWebSocket;
FakeWebSocket = function (uri, protocols) {
this.url = uri;
this.binaryType = "arraybuffer";
@ -75,7 +77,7 @@ var FakeWebSocket;
FakeWebSocket.__is_fake = true;
FakeWebSocket.replace = function () {
function replace () {
if (!WebSocket.__is_fake) {
var real_version = WebSocket;
WebSocket = FakeWebSocket;
@ -83,9 +85,11 @@ var FakeWebSocket;
}
};
FakeWebSocket.restore = function () {
function restore () {
if (WebSocket.__is_fake) {
WebSocket = WebSocket.__real_version;
}
};
})();
return { replace: replace, restore: restore };
});

View File

@ -54,79 +54,43 @@ var get_path_cwd = function (/* arguments */) {
}
};
if (all_js && !program.autoInject) {
var all_modules = {};
var temp = require('temp');
temp.track();
// uses the first instance of the string 'requires local modules: '
program.tests.forEach(function (testname) {
var full_path = path.resolve(process.cwd(), testname);
var content = fs.readFileSync(full_path).toString();
var ind = content.indexOf('requires local modules: ');
if (ind > -1) {
ind += 'requires local modules: '.length;
var eol = content.indexOf('\n', ind);
var modules = content.slice(ind, eol).split(/,\s*/);
modules.forEach(function (mod) {
all_modules[get_path('core/', mod) + '.js'] = 1;
});
}
var template = {
header: "<html>\n<head>\n<meta charset='utf-8' />\n<link rel='stylesheet' href='" + get_path('node_modules/mocha/mocha.css') + "'/>\n</head>\n<body><div id='mocha'></div>",
script_tag: function(p) { return "<script src='" + p + "'></script>"; },
footer: "<script>\nmocha.checkLeaks();\nmocha.globals(['navigator', 'create', 'ClientUtils', '__utils__', 'requestAnimationFrame', 'WebSocket']);\nmocha.run(function () { window.__mocha_done = true; });\n</script>\n</body>\n</html>"
};
var fakes_ind = content.indexOf('requires test modules: ');
if (fakes_ind > -1) {
fakes_ind += 'requires test modules: '.length;
var fakes_eol = content.indexOf('\n', fakes_ind);
var fakes_modules = content.slice(fakes_ind, fakes_eol).split(/,\s*/);
fakes_modules.forEach(function (mod) {
all_modules[get_path('tests/', mod) + '.js'] = 1;
});
}
});
template.header += "\n" + template.script_tag(get_path('node_modules/chai/chai.js'));
template.header += "\n" + template.script_tag(get_path('node_modules/mocha/mocha.js'));
template.header += "\n" + template.script_tag(get_path('node_modules/sinon/pkg/sinon.js'));
template.header += "\n" + template.script_tag(get_path('node_modules/sinon-chai/lib/sinon-chai.js'));
template.header += "\n" + template.script_tag(get_path('node_modules/requirejs/require.js'));
template.header += "\n<script>requirejs.config({ baseUrl: \"" + get_path('.') + "\" });</script>";
template.header += "\n<script>mocha.setup('bdd');</script>";
program.autoInject = Object.keys(all_modules);
}
template.header += "\n" + template.script_tag(get_path('tests/assertions.js'));
if (program.autoInject) {
var temp = require('temp');
temp.track();
var template = {
header: "<html>\n<head>\n<meta charset='utf-8' />\n<link rel='stylesheet' href='" + get_path('node_modules/mocha/mocha.css') + "'/>\n</head>\n<body><div id='mocha'></div>",
script_tag: function(p) { return "<script src='" + p + "'></script>"; },
footer: "<script>\nmocha.checkLeaks();\nmocha.globals(['navigator', 'create', 'ClientUtils', '__utils__', 'requestAnimationFrame', 'WebSocket']);\nmocha.run(function () { window.__mocha_done = true; });\n</script>\n</body>\n</html>"
};
template.header += "\n" + template.script_tag(get_path('node_modules/chai/chai.js'));
template.header += "\n" + template.script_tag(get_path('node_modules/mocha/mocha.js'));
template.header += "\n" + template.script_tag(get_path('node_modules/sinon/pkg/sinon.js'));
template.header += "\n" + template.script_tag(get_path('node_modules/sinon-chai/lib/sinon-chai.js'));
template.header += "\n" + template.script_tag(get_path('node_modules/requirejs/require.js'));
template.header += "\n<script>requirejs.config({ baseUrl: \"" + get_path('.') + "\" });</script>";
template.header += "\n<script>mocha.setup('bdd');</script>";
template.header = program.autoInject.reduce(function(acc, sn) {
return acc + "\n" + template.script_tag(get_path_cwd(sn));
}, template.header);
file_paths = program.tests.map(function(jsn, ind) {
var templ = template.header;
templ += "\n";
templ += template.script_tag(get_path_cwd(jsn));
templ += template.footer;
var tempfile = temp.openSync({ prefix: 'novnc-zombie-inject-', suffix: '-file_num-'+ind+'.html' });
fs.writeSync(tempfile.fd, templ);
fs.closeSync(tempfile.fd);
return tempfile.path;
});
}
else {
file_paths = program.tests.map(function(fn) {
return path.resolve(process.cwd(), fn);
});
}
file_paths = program.tests.map(function(jsn, ind) {
var templ = template.header;
templ += "\n";
templ += template.script_tag(get_path_cwd(jsn));
templ += template.footer;
var tempfile = temp.openSync({ prefix: 'novnc-zombie-inject-', suffix: '-file_num-'+ind+'.html' });
fs.writeSync(tempfile.fd, templ);
fs.closeSync(tempfile.fd);
return tempfile.path;
});
var use_ansi = false;
if (program.color) use_ansi = true;
else if (program.disableColor) use_ansi = false;

View File

@ -1,4 +1,3 @@
// requires test modules: assertions
/* jshint expr: true */
var expect = chai.expect;

View File

@ -1,4 +1,4 @@
// requires test modules: fake.websocket, assertions
// requires test modules: assertions
/* jshint expr: true */
var assert = chai.assert;
var expect = chai.expect;
@ -36,20 +36,23 @@ var push32 = function (arr, num) {
describe('Remote Frame Buffer Protocol Client', function() {
"use strict";
before(FakeWebSocket.replace);
after(FakeWebSocket.restore);
var Websock;
var Websock, FakeWebSocket;
before(function (done) {
requirejs(["core/rfb", "core/websock"],
function (r, w) {
requirejs(["core/rfb", "core/websock", "tests/fake.websocket"],
function (r, w, f) {
RFB = r.RFB;
Websock = w.Websock;
FakeWebSocket = f;
FakeWebSocket.replace();
done();
});
});
after(function () {
FakeWebSocket.restore();
});
before(function () {
this.clock = sinon.useFakeTimers();
// Use a single set of buffers instead of reallocating to

View File

@ -1,4 +1,3 @@
// requires test modules: fake.websocket, assertions
/* jshint expr: true */
var assert = chai.assert;
var expect = chai.expect;
@ -6,12 +5,13 @@ var expect = chai.expect;
describe('Websock', function() {
"use strict";
var Websock;
var Websock, FakeWebSocket;
before(function (done) {
requirejs(["core/websock"],
function (w) {
requirejs(["core/websock", "tests/fake.websocket"],
function (w, f) {
Websock = w.Websock;
FakeWebSocket = f;
done();
});
});