Skip to content

Instantly share code, notes, and snippets.

@adityamukho
Last active March 29, 2019 08:41
Show Gist options
  • Save adityamukho/d1a042bb808d871d7d4ef0f266191867 to your computer and use it in GitHub Desktop.
Save adityamukho/d1a042bb808d871d7d4ef0f266191867 to your computer and use it in GitHub Desktop.
Selectively run ArangoDB Foxx tests by specifying file patterns and `grep` expressions. (Tested with ArangoDB 3.4)
//Include in your service's scripts folder. Add a reference to it in manifests.json under the "scripts" object.
'use strict';
const manager = require('@arangodb/foxx/manager');
const { get } = require('lodash');
const fs = require('fs');
const Minimatch = require('minimatch').Minimatch;
const isWindows = require('internal').platform.substr(0, 3) === 'win';
const mocha = require('@arangodb/mocha');
const { files, reporter = 'default', grep } = get(module.context.argv, [0], {});
const service = manager.lookupService(module.context.mount);
const testFiles = findTestFiles(service, files);
//Adapted from @arangodb/foxx/mocha.js
const result = mocha.run((file, context) => service.run(file, { context: context }), testFiles, reporter, grep);
if (reporter === 'xunit' && Array.isArray(result) && result[1]) {
result[1].name = service.mount;
}
module.exports = result;
//Adapted from @arangodb/foxx/mocha.js
function isNotPattern(pattern) {
return pattern.indexOf('*') === -1;
}
//Adapted from @arangodb/foxx/mocha.js
function findTestFiles(service, files) {
const patterns = files || service.manifest.tests || [];
if (patterns.every(isNotPattern)) {
return patterns.slice();
}
const basePath = fs.join(service.root, service.path);
const paths = fs.listTree(basePath);
const matchers = patterns.map((pattern) => {
if (pattern.charAt(0) === '/') {
pattern = pattern.slice(1);
}
else if (pattern.charAt(0) === '.' && pattern.charAt(1) === '/') {
pattern = pattern.slice(2);
}
return new Minimatch(pattern);
});
return paths.filter(
(path) => path && matchers.some((pattern) => pattern.match(
isWindows ? path.replace(/\\/g, '/') : path
)) && fs.isFile(fs.join(basePath, path))
);
}

With Foxx-CLI

$ foxx run [options] <mount> <scriptName> [args]

Where args is an optional JSON string representation of the following object:

{
  "files": ["An array of file patterns specified the same way as in the manifest 'tests' array"], //Optional
  "reporter": "A String specifying the reporter to use [stream | xunit | suite | tap | default]", //Optional
  "grep": "A regex pattern to match against the suite or the test title, running only those that match" //Optional
}

Examples

The following examples omit the server/database connection paramters for brevity. The mount point must be substituted. The scriptName is assumed to be runTests.

  1. With a files pattern and the tap reporter (no grep filters):
$ foxx run <mount> runTests '{"files": ["test/unit/lib/operations/commit/*"], "reporter": "tap"}'
  1. With just a grep pattern (reporter defaults to default, no file filters):
$ foxx run <mount> runTests '{"grep": "getTransientOrCreateLatestSnapshot"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment