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: [ files: [
'node_modules/requirejs/require.js', 'node_modules/requirejs/require.js',
'tests/test-main.js', 'tests/test-main.js',
'tests/fake.*.js',
'tests/assertions.js', 'tests/assertions.js',
'tests/test.*.js', 'tests/test.*.js',
// Only packaged, not included in browser as RequireJS will // Only packaged, not included in browser as RequireJS will
// do the actual loading // do the actual loading
{pattern: 'tests/fake.*.js', included: false},
{pattern: 'core/*.js', included: false}, {pattern: 'core/*.js', included: false},
{pattern: 'core/input/*.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 // PhantomJS can't create Event objects directly, so we need to use this
function make_event(name, props) { function make_event(name, props) {
var evt = document.createEvent('Event'); var evt = document.createEvent('Event');
@ -13,6 +13,8 @@ var FakeWebSocket;
return evt; return evt;
} }
var FakeWebSocket;
FakeWebSocket = function (uri, protocols) { FakeWebSocket = function (uri, protocols) {
this.url = uri; this.url = uri;
this.binaryType = "arraybuffer"; this.binaryType = "arraybuffer";
@ -75,7 +77,7 @@ var FakeWebSocket;
FakeWebSocket.__is_fake = true; FakeWebSocket.__is_fake = true;
FakeWebSocket.replace = function () { function replace () {
if (!WebSocket.__is_fake) { if (!WebSocket.__is_fake) {
var real_version = WebSocket; var real_version = WebSocket;
WebSocket = FakeWebSocket; WebSocket = FakeWebSocket;
@ -83,9 +85,11 @@ var FakeWebSocket;
} }
}; };
FakeWebSocket.restore = function () { function restore () {
if (WebSocket.__is_fake) { if (WebSocket.__is_fake) {
WebSocket = WebSocket.__real_version; 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 temp = require('temp');
var all_modules = {}; temp.track();
// uses the first instance of the string 'requires local modules: ' var template = {
program.tests.forEach(function (testname) { 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>",
var full_path = path.resolve(process.cwd(), testname); script_tag: function(p) { return "<script src='" + p + "'></script>"; },
var content = fs.readFileSync(full_path).toString(); 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 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 fakes_ind = content.indexOf('requires test modules: '); template.header += "\n" + template.script_tag(get_path('node_modules/chai/chai.js'));
if (fakes_ind > -1) { template.header += "\n" + template.script_tag(get_path('node_modules/mocha/mocha.js'));
fakes_ind += 'requires test modules: '.length; template.header += "\n" + template.script_tag(get_path('node_modules/sinon/pkg/sinon.js'));
var fakes_eol = content.indexOf('\n', fakes_ind); template.header += "\n" + template.script_tag(get_path('node_modules/sinon-chai/lib/sinon-chai.js'));
var fakes_modules = content.slice(fakes_ind, fakes_eol).split(/,\s*/); template.header += "\n" + template.script_tag(get_path('node_modules/requirejs/require.js'));
fakes_modules.forEach(function (mod) { template.header += "\n<script>requirejs.config({ baseUrl: \"" + get_path('.') + "\" });</script>";
all_modules[get_path('tests/', mod) + '.js'] = 1; 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) { 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) { template.header = program.autoInject.reduce(function(acc, sn) {
return acc + "\n" + template.script_tag(get_path_cwd(sn)); return acc + "\n" + template.script_tag(get_path_cwd(sn));
}, template.header); }, 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; var use_ansi = false;
if (program.color) use_ansi = true; if (program.color) use_ansi = true;
else if (program.disableColor) use_ansi = false; else if (program.disableColor) use_ansi = false;

View File

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

View File

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