From 8bee49c5f8424a32e5f3afdc7785849c8c597eb6 Mon Sep 17 00:00:00 2001 From: "David I. Lehn" Date: Fri, 15 Sep 2023 02:36:37 -0400 Subject: [PATCH] Add EARL Turtle test result mode. --- CHANGELOG.md | 1 + README.md | 14 +++++--- test/EarlReport.js | 80 +++++++++++++++++++++++++++++++++++++++++++--- test/test.js | 10 ++++-- 4 files changed, 93 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56f8d44..5048aaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ is recommeded to use this paramter instead of `maxDeepIterations` directly. - **BREAKING**: Check output `format` parameter. Must be omitted, falsey, or "application/n-quads". +- Add EARL Turtle test result mode. ### Changed - **BREAKING**: Change algorithm name from "URDNA2015" to "RDFC-1.0" to match diff --git a/README.md b/README.md index 73443ce..ad9bb78 100644 --- a/README.md +++ b/README.md @@ -189,21 +189,27 @@ directory you can use the following: npm run fetch-test-suite -Node.js tests can be run with a simple command: +Node.js tests: npm test +Browser tests via Karma: + + npm run test-karma + If you installed the test suites elsewhere, or wish to run other tests, use the `TEST_DIR` environment var: TEST_DIR="/tmp/tests" npm test -To generate earl reports: +To generate EARL reports: - # generate the earl report for node.js + # generate a JSON-LD EARL report with Node.js EARL=earl-node.jsonld npm test -Browser testing with karma is done indirectly through [jsonld.js][]. + # generate a Turtle EARL report with Node.js + # used for official reports + EARL=js-rdf-canonize-earl.ttl npm test Benchmark --------- diff --git a/test/EarlReport.js b/test/EarlReport.js index e5ebb32..f3cd2c8 100644 --- a/test/EarlReport.js +++ b/test/EarlReport.js @@ -71,6 +71,8 @@ class EarlReport { this.now = new Date(); this.now.setMilliseconds(0); this.env = options.env; + this.format = options.format; + this.version = options.version; // test environment this._environment = null; /* eslint-disable quote-props */ @@ -104,7 +106,8 @@ class EarlReport { 'doap:homepage': 'https://github.com/digitalbazaar/rdf-canonize', 'doap:license': 'https://github.com/digitalbazaar/rdf-canonize/blob/master/LICENSE', - 'doap:description': 'A JSON-LD processor for JavaScript', + 'doap:description': + 'A RDF Dataset Canonicalization processor for JavaScript', 'doap:programming-language': 'JavaScript', 'dc:creator': 'https://digitalbazaar.com/', 'doap:developer': { @@ -117,7 +120,7 @@ class EarlReport { 'foaf:homepage': 'https://digitalbazaar.com/' }, 'doap:release': { - 'doap:revision': '', + 'doap:revision': `${this.version}`, 'doap:created': today }, 'subjectOf': [] @@ -154,11 +157,78 @@ class EarlReport { } report() { - return this._report; + if(this.format === 'json') { + return JSON.stringify(this._report, null, 2); + } else if(this.format === 'ttl') { + return this.asTurtle(this._report); + } else { + throw new Error(`Unknown report format: "${this.format}".`); + } } - reportJson() { - return JSON.stringify(this._report, null, 2); + asTurtle() { + console.log(this._report); + console.log(this._report.subjectOf[0]); + const r = this._report; + let report = ''; + // add prefixes + report += `\ +@prefix rdf: . +@prefix rdfs: . +@prefix dc: . +@prefix foaf: . +@prefix doap: . +@prefix earl: . +@prefix ex: . +@prefix xsd: . + +`; + + // add test subject + report += `\ +<> foaf:primaryTopic <${r['@id']}> ; + dc:issued "${r['doap:release']['doap:created']}"^^xsd:dateTime ; + foaf:maker <${r['doap:developer']['@id']}> . + +<${r['@id']}> a doap:Project, earl:TestSubject, earl:Software ; + doap:name "${r['doap:name']}" ; + doap:release [ + doap:name "${r['doap:name']}@${r['doap:release']['doap:revision']}"; + doap:revision "${r['doap:release']['doap:revision']}" ; + doap:created "${r['doap:release']['doap:created']}" ; + ] ; + doap:developer <${r['doap:developer']['@id']}> ; + doap:homepage <${r['doap:homepage']}> ; + doap:description "${r['doap:description']}"@en ; + doap:programming-language "${r['doap:programming-language']}" . + +`; + + // add software developer + report += `\ +<${r['doap:developer']['@id']}> a foaf:Organization, earl:Assertor; + foaf:name "${r['doap:developer']['foaf:name']}"; + foaf:homepage <${r['doap:developer']['foaf:homepage']}>; + +`; + + // add assertions + for(const a of r.subjectOf) { + report += `\ +[ a earl:Assertion; + earl:assertedBy <${r['doap:developer']['@id']}>; + earl:subject <${r['@id']}>; + earl:test ; + earl:result [ + a earl:TestResult; + earl:outcome ${a['earl:result']['earl:outcome']}; + dc:date "${a['earl:result']['dc:date']}"^^xsd:dateTime]; + earl:mode ${a['earl:mode']} ] . + +`; + } + + return report; } // setup @context and environment to handle benchmark data diff --git a/test/test.js b/test/test.js index 1f16476..18c976e 100644 --- a/test/test.js +++ b/test/test.js @@ -9,7 +9,8 @@ * Set dirs, manifests, or js to run: * TESTS="r1 r2 ..." * Output an EARL report: - * EARL=filename + * EARL=filename (default: none) + * JSON or Turtle type determined by extension. * Test environment details for EARL report: * This is useful for benchmark comparison. * By default no details are added for privacy reasons. @@ -235,7 +236,10 @@ if(options.env.TEST_ENV) { // create earl report if(options.earl && options.earl.filename) { options.earl.report = new EarlReport({ - env: testEnv + env: testEnv, + format: options.earl.filename.endsWith('.ttl') ? 'ttl' : 'json', + // FIXME: enable easier override + version: options.testEnvDefaults.version }); if(benchmarkOptions.enabled) { options.earl.report.setupForBenchmarks({testEnv}); @@ -259,7 +263,7 @@ if(options.earl.report) { const _it = result.hadOnly ? it.only : it; _it('should print the earl report', function() { return options.writeFile( - options.earl.filename, options.earl.report.reportJson()); + options.earl.filename, options.earl.report.report()); }); }); }