You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@annotator.apache.org by ra...@apache.org on 2018/10/15 02:30:54 UTC

[incubator-annotator] 01/02: Remove uses of require from validation tests

This is an automated email from the ASF dual-hosted git repository.

randall pushed a commit to branch validation-test-tweaks
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git

commit 60dfe112a4aa6f5232eeebe04ab9aa146830049d
Author: Randall Leeds <ra...@apache.org>
AuthorDate: Sun Oct 14 16:57:10 2018 -0700

    Remove uses of require from validation tests
    
    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        |  1 +
 test/data-model.mjs | 47 ++++++++++++++++++++++++++++-------------------
 yarn.lock           |  6 ++++++
 3 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/package.json b/package.json
index 2e633ed..5d33f76 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/test/data-model.mjs b/test/data-model.mjs
index 5d70280..16e4bed 100644
--- a/test/data-model.mjs
+++ b/test/data-model.mjs
@@ -13,22 +13,15 @@
  * 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());
diff --git a/yarn.lock b/yarn.lock
index 849d2f1..499bdfb 100644
--- 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"