From 73a6b8c9a28e08121f0f4f0ac4acf69e7fce499d Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Wed, 18 Jan 2017 17:12:28 +0100 Subject: [PATCH] Remove magic dependency system in tests The tests now load what they need using RequireJS rather than some magic based on comments. --- karma.conf.js | 2 +- tests/fake.websocket.js | 14 +++--- tests/run_from_console.js | 90 ++++++++++++--------------------------- tests/test.display.js | 1 - tests/test.rfb.js | 17 +++++--- tests/test.websock.js | 8 ++-- 6 files changed, 51 insertions(+), 81 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 5b01221b..bc9177f6 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -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}, ], diff --git a/tests/fake.websocket.js b/tests/fake.websocket.js index 21012059..cdb16591 100644 --- a/tests/fake.websocket.js +++ b/tests/fake.websocket.js @@ -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 }; +}); diff --git a/tests/run_from_console.js b/tests/run_from_console.js index fa332c1e..220fa55a 100755 --- a/tests/run_from_console.js +++ b/tests/run_from_console.js @@ -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: "\n\n\n\n\n
", + script_tag: function(p) { return ""; }, + footer: "\n\n" +}; - 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"; +template.header += "\n"; - 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: "\n\n\n\n\n
", - script_tag: function(p) { return ""; }, - footer: "\n\n" - }; - - 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"; - template.header += "\n"; - - 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; diff --git a/tests/test.display.js b/tests/test.display.js index b3c78d04..51f25d5d 100644 --- a/tests/test.display.js +++ b/tests/test.display.js @@ -1,4 +1,3 @@ -// requires test modules: assertions /* jshint expr: true */ var expect = chai.expect; diff --git a/tests/test.rfb.js b/tests/test.rfb.js index a1a1008a..714a2dfa 100644 --- a/tests/test.rfb.js +++ b/tests/test.rfb.js @@ -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 diff --git a/tests/test.websock.js b/tests/test.websock.js index 5899eb49..af9f9456 100644 --- a/tests/test.websock.js +++ b/tests/test.websock.js @@ -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(); }); });