Remove uses of require from validation tests
authorRandall Leeds <randall@apache.org>
Sun, 14 Oct 2018 23:57:10 +0000 (16:57 -0700)
committerRandall Leeds <randall@apache.org>
Sun, 14 Oct 2018 23:57:10 +0000 (16:57 -0700)
Strict .mjs does not have access to `require`. Due to a bug in `esm`, it
worked up until now. However, removing it is necessary to unblock
upgrading `esm`.

At the same time, address the fact that it is not ideal to assume that
the node modules directory is relative to the current working directory.
Instead, rely on `resolve` to do a recursive search.

package.json
test/data-model.mjs
yarn.lock

index 2e633ed..5d33f76 100644 (file)
@@ -55,6 +55,7 @@
     "node-fetch": "^2.2.0",
     "nyc": "^11.6.0",
     "prettier": "^1.11.1",
+    "resolve": "^1.8.1",
     "rimraf": "^2.6.2",
     "web-annotation-tests": "https://github.com/w3c/web-annotation-tests",
     "webpack": "^4.2.0",
index 5d70280..16e4bed 100644 (file)
  * the License.
  */
 
-/* global require, process, describe, before, it, assert */
+/* global process, describe, before, it, assert */
 
 import fs from 'fs';
 import URL from 'url';
-import fetch from 'node-fetch';
 
 import Ajv from 'ajv';
-const ajv = new Ajv({ schemaId: 'auto' });
-ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
-
-// add defintion files to available schemas
-const defs = fs.readdirSync('node_modules/web-annotation-tests/definitions/');
-defs.forEach(def => {
-  if (def.substr(-4) === 'json')
-    ajv.addSchema(require('web-annotation-tests/definitions/' + def));
-});
+import META_SCHEMA from 'ajv/lib/refs/json-schema-draft-04.json';
+import fetch from 'node-fetch';
+import resolve from 'resolve';
 
 // file or URL location
 let url = '';
@@ -46,12 +39,28 @@ process.argv.forEach((val, index) => {
   }
 });
 
-// load the annotationMusts test list
-const musts = JSON.parse(
-  fs.readFileSync(
-    'node_modules/web-annotation-tests/annotations/annotationMusts.test'
-  )
-);
+function readSchema(schemaPath, base = 'web-annotation-tests/') {
+  const resolverOptions = { extensions: ['.json', '.test'] };
+  const resolvedPath = resolve.sync(`${base}${schemaPath}`, resolverOptions);
+  const schemaUnparsed = fs.readFileSync(resolvedPath);
+  return JSON.parse(schemaUnparsed);
+}
+
+const DEFINITIONS = [
+  'annotations',
+  'bodyTarget',
+  'choiceSet',
+  'collections',
+  'id',
+  'otherProperties',
+  'specificResource',
+].map(name => readSchema(`definitions/${name}`));
+
+const MUSTS = readSchema('annotations/annotationMusts');
+
+const ajv = new Ajv({ schemaId: 'auto' });
+ajv.addMetaSchema(META_SCHEMA);
+DEFINITIONS.forEach(schema => ajv.addSchema(schema));
 
 describe('Test JSON against Schemas', () => {
   let data = '';
@@ -75,8 +84,8 @@ describe('Test JSON against Schemas', () => {
     }
   });
 
-  musts.assertions.forEach(path => {
-    const schema = require('web-annotation-tests/' + path);
+  MUSTS.assertions.forEach(schemaPath => {
+    const schema = readSchema(schemaPath);
     it(schema.title, () => {
       let valid = ajv.validate(schema, data);
       assert.isOk(valid, ajv.errorsText());
index 849d2f1..499bdfb 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
@@ -5864,6 +5864,12 @@ resolve@^1.3.3, resolve@^1.4.0:
   dependencies:
     path-parse "^1.0.5"
 
+resolve@^1.8.1:
+  version "1.8.1"
+  resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.8.1.tgz#82f1ec19a423ac1fbd080b0bab06ba36e84a7a26"
+  dependencies:
+    path-parse "^1.0.5"
+
 restore-cursor@^1.0.1:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-1.0.1.tgz#34661f46886327fed2991479152252df92daa541"