diff --git a/test/any_grpc.js b/test/any_grpc.js new file mode 100644 index 00000000..43db28af --- /dev/null +++ b/test/any_grpc.js @@ -0,0 +1,29 @@ +module.exports = {}; + +function assignExportsFromGlobal(globalField, exportsField) { + if (global[globalField] === 'js') { + module.exports[exportsField] = require('../packages/grpc-js'); + } else if (global[globalField] === 'native') { + module.exports[exportsField] = require('../packages/grpc-native'); + } else { + throw new Error([ + `Invalid value for global.${globalField}: ${global.globalField}.`, + 'If running from the command line, please --require a fixture first.' + ].join(' ')); + } +} + +// Set 'server' and 'client' fields on this module's exports. +// These don't refer to the portions of the gRPC interface that are +// relevant to an application behaving as a server or a client respectively. +// Instead, they refer to the entire gRPC module as it's visible to the +// application. +// In other words, a test that simulates a gRPC client should treat +// require('any-grpc').client as the value of require('grpc'), and would simply +// not be expected to use server components. +assignExportsFromGlobal('_server_implementation', 'server'); +assignExportsFromGlobal('_client_implementation', 'client'); +// Increase clarity when there's no distinction between client/server +if (module.exports.client === module.exports.server) { + module.exports.all = module.exports.client; +} diff --git a/test/api/async_test.js b/test/api/async_test.js index e899da56..c66f3782 100644 --- a/test/api/async_test.js +++ b/test/api/async_test.js @@ -20,7 +20,7 @@ var assert = require('assert'); -var grpc = require('grpc'); +var grpc = require('../any_grpc').all; var math = grpc.load( __dirname + '/../../packages/grpc-native-core/deps/grpc/src/proto/math/math.proto').math; diff --git a/test/api/credentials_test.js b/test/api/credentials_test.js index 96879994..3bf3d5f5 100644 --- a/test/api/credentials_test.js +++ b/test/api/credentials_test.js @@ -22,7 +22,7 @@ var assert = require('assert'); var fs = require('fs'); var path = require('path'); -var grpc = require('grpc'); +var grpc = require('../any_grpc').all; /** * This is used for testing functions with multiple asynchronous calls that diff --git a/test/api/math_client_test.js b/test/api/math_client_test.js index 09265abf..1f4f25e6 100644 --- a/test/api/math_client_test.js +++ b/test/api/math_client_test.js @@ -20,7 +20,7 @@ var assert = require('assert'); -var grpc = require('grpc'); +var grpc = require('../any_grpc').all; var math = require('./math/math_pb'); var MathClient = require('./math/math_grpc_pb').MathClient; diff --git a/test/api/metadata_test.js b/test/api/metadata_test.js index 5e24f7ac..7187ee73 100644 --- a/test/api/metadata_test.js +++ b/test/api/metadata_test.js @@ -18,7 +18,7 @@ 'use strict'; -var Metadata = require('grpc').Metadata; +var Metadata = require('../any_grpc').all.Metadata; var assert = require('assert'); diff --git a/test/api/surface_test.js b/test/api/surface_test.js index b2072fdd..d54ff7b4 100644 --- a/test/api/surface_test.js +++ b/test/api/surface_test.js @@ -21,7 +21,7 @@ var assert = require('assert'); var _ = require('lodash'); -var grpc = require('grpc'); +var grpc = require('../any_grpc').all; var MathClient = grpc.load( __dirname + '/../../packages/grpc-native-core/deps/grpc/src/proto/math/math.proto').math.Math; diff --git a/test/fixtures/js_js.js b/test/fixtures/js_js.js new file mode 100644 index 00000000..e0ca3c58 --- /dev/null +++ b/test/fixtures/js_js.js @@ -0,0 +1,2 @@ +global._server_implementation = 'native'; +global._client_implementation = 'js'; \ No newline at end of file diff --git a/test/fixtures/js_native.js b/test/fixtures/js_native.js new file mode 100644 index 00000000..5088df96 --- /dev/null +++ b/test/fixtures/js_native.js @@ -0,0 +1,2 @@ +global._server_implementation = 'js'; +global._client_implementation = 'native'; \ No newline at end of file diff --git a/test/fixtures/native_js.js b/test/fixtures/native_js.js new file mode 100644 index 00000000..5088df96 --- /dev/null +++ b/test/fixtures/native_js.js @@ -0,0 +1,2 @@ +global._server_implementation = 'js'; +global._client_implementation = 'native'; \ No newline at end of file diff --git a/test/fixtures/native_native.js b/test/fixtures/native_native.js new file mode 100644 index 00000000..5b005e48 --- /dev/null +++ b/test/fixtures/native_native.js @@ -0,0 +1,2 @@ +global._server_implementation = 'native'; +global._client_implementation = 'native'; \ No newline at end of file diff --git a/test/gulpfile.js b/test/gulpfile.js index d3da5c0a..c020ff76 100644 --- a/test/gulpfile.js +++ b/test/gulpfile.js @@ -22,6 +22,7 @@ const execa = require('execa'); const path = require('path'); const del = require('del'); const linkSync = require('../util').linkSync; +const merge = require('merge2'); // gulp-help monkeypatches tasks to have an additional description parameter const gulp = help(_gulp); @@ -49,5 +50,19 @@ gulp.task('internal.test.link.add', 'Link local copies of dependencies', () => { }); gulp.task('internal.test.test', 'Run API-level tests', () => { - return gulp.src(`${apiTestDir}/*.js`).pipe(mocha({reporter: 'mocha-jenkins-reporter'})); + // run mocha tests matching a glob with a pre-required fixture, + // returning the associated gulp stream + const runTestsWithFixture = (glob, fixture) => gulp + .src(glob) + .pipe(mocha({ + reporter: 'mocha-jenkins-reporter', + require: `${testDir}/fixtures/${fixture}.js` + })); + const interopTest = `${testDir}/interop/interop_test.js`; + const tasks = [].concat( + ['native_native'/*, 'js_js'*/] + .map((fixture) => runTestsWithFixture(`${apiTestDir}/*.js`, fixture)), + ['native_native', 'native_js'/*, 'js_native', 'js_js'*/] + .map((fixture) => runTestsWithFixture(interopTest, fixture))) + return merge(tasks); }); diff --git a/test/interop/interop_client.js b/test/interop/interop_client.js index 5fad82bf..f9122d35 100644 --- a/test/interop/interop_client.js +++ b/test/interop/interop_client.js @@ -20,10 +20,9 @@ var fs = require('fs'); var path = require('path'); -// TODO(murgatroid99): use multiple grpc implementations -var grpc = require('grpc'); +var grpc = require('../any_grpc').client; var testProto = grpc.load({ - root: __dirname + '/../../packages/grpc-native-core/deps/grpc', + root: __dirname + '/../../../packages/grpc-native-core/deps/grpc', file: 'src/proto/grpc/testing/test.proto'}).grpc.testing; var GoogleAuth = require('google-auth-library'); diff --git a/test/api/interop_sanity_test.js b/test/interop/interop_sanity_test.js similarity index 96% rename from test/api/interop_sanity_test.js rename to test/interop/interop_sanity_test.js index b3f65a03..055a5ab7 100644 --- a/test/api/interop_sanity_test.js +++ b/test/interop/interop_sanity_test.js @@ -18,8 +18,8 @@ 'use strict'; -var interop_server = require('../interop/interop_server.js'); -var interop_client = require('../interop/interop_client.js'); +var interop_server = require('./interop_server.js'); +var interop_client = require('./interop_client.js'); var server; diff --git a/test/interop/interop_server.js b/test/interop/interop_server.js index 65bb088d..58ccacd7 100644 --- a/test/interop/interop_server.js +++ b/test/interop/interop_server.js @@ -22,10 +22,9 @@ var fs = require('fs'); var path = require('path'); var _ = require('lodash'); var AsyncDelayQueue = require('./async_delay_queue'); -// TODO(murgatroid99): use multiple grpc implementations -var grpc = require('grpc'); +var grpc = require('../any_grpc').server; var testProto = grpc.load({ - root: __dirname + '/../../packages/grpc-native-core/deps/grpc', + root: __dirname + '/../../../packages/grpc-native-core/deps/grpc', file: 'src/proto/grpc/testing/test.proto'}).grpc.testing; var ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial';