This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
248 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const path = require('path') | ||
const parser = require('./parser') | ||
const namespace = require('@rdfjs/namespace') | ||
const { removeFilePart } = require('./utils') | ||
const iriResolve = require('rdf-loader-code/lib/iriResolve') | ||
const validators = require('./validators') | ||
|
||
const ns = { | ||
schema: namespace('http://schema.org/'), | ||
rdf: namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#'), | ||
p: namespace('https://pipeline.described.at/'), | ||
code: namespace('https://code.described.at/') | ||
} | ||
|
||
async function validate ({ file, checks }) { | ||
const manifestPath = path.resolve(file) | ||
const manifestDir = removeFilePart(manifestPath) | ||
const manifestDirPart = manifestDir.substr(manifestDir.lastIndexOf('/') + 1) | ||
|
||
const manifestGraph = await parser.readGraph(manifestPath, checks) | ||
const operations = manifestGraph.has(ns.rdf.type, ns.p.Operation) | ||
.map(operation => { | ||
const implementedBy = operation.out(ns.code.implementedBy) | ||
const codeLink = implementedBy.out(ns.code.link) | ||
const identifier = codeLink.term | ||
|
||
const { filename, method } = iriResolve(identifier.value) | ||
return { operation: operation.term.value, filename, method } | ||
}) | ||
.map(({ operation, filename, method }) => { | ||
filename = manifestDir + filename.replace(manifestDirPart, '') | ||
method = method || 'default' | ||
return { operation, filename, method } | ||
}) | ||
|
||
await operations.reduce(async (promise, { operation: op, filename, method }) => { | ||
await promise | ||
const fileIssue = await validators.operationIsImportable.validate({ op, filename }) | ||
checks.addGenericCheck(fileIssue) | ||
if (fileIssue.level !== 'error') { | ||
checks.addGenericCheck(await validators.operationIsExported.validate({ op, filename, method })) | ||
} | ||
}, Promise.resolve()) | ||
} | ||
|
||
module.exports = validate |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { template } = require('../utils') | ||
const Issue = require('../issue') | ||
|
||
const operationIsExported = { | ||
ruleId: 15, | ||
ruleDescription: 'Operation defined in manifest has a corresponding export', | ||
messageSuccessTemplate: template`File ${'filename'} exports '${'method'}' for operation ${'op'}`, | ||
messageFailureTemplate: template`File ${'filename'} does not export ${'method'} for operation ${'op'}`, | ||
async validate ({ op, filename, method }) { | ||
let issue | ||
try { | ||
const imported = await import(filename) | ||
|
||
if (typeof imported[method] !== 'function') { | ||
issue = Issue.error({ id: this.ruleId, templateData: { op, filename, method } }) | ||
} | ||
else { | ||
issue = Issue.info({ id: this.ruleId, templateData: { op, filename, method } }) | ||
} | ||
} | ||
catch (err) { | ||
} | ||
|
||
return issue | ||
}, | ||
describeRule () { | ||
return { | ||
ruleId: this.ruleId, | ||
ruleDescription: this.ruleDescription, | ||
messageSuccess: this.messageSuccessTemplate(), | ||
messageFailure: this.messageFailureTemplate() | ||
} | ||
} | ||
} | ||
module.exports = operationIsExported |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { template } = require('../utils') | ||
const Issue = require('../issue') | ||
|
||
const operationIsImportable = { | ||
ruleId: 14, | ||
ruleDescription: 'Operation defined in manifest can be imported', | ||
messageSuccessTemplate: template`${'filename'} can be imported for operation ${'op'}`, | ||
messageFailureTemplate: template`Cannot import ${'filename'} for operation ${'op'}`, | ||
async validate ({ op, filename }) { | ||
let issue = Issue.error({ id: this.ruleId, templateData: { op, filename } }) | ||
|
||
try { | ||
await import(filename) | ||
issue = Issue.info({ id: this.ruleId, templateData: { op, filename } }) | ||
} | ||
catch (err) { } | ||
|
||
return issue | ||
}, | ||
describeRule () { | ||
return { | ||
ruleId: this.ruleId, | ||
ruleDescription: this.ruleDescription, | ||
messageSuccess: this.messageSuccessTemplate(), | ||
messageFailure: this.messageFailureTemplate() | ||
} | ||
} | ||
} | ||
module.exports = operationIsImportable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function defaultexport () { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
@base <http://barnard59.zazuko.com/operations/ftp/> . | ||
@prefix code: <https://code.described.at/> . | ||
@prefix p: <https://pipeline.described.at/> . | ||
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | ||
|
||
<a2> a p:Operation, p:ReadableObjectMode; | ||
rdfs:label "Exported with a named export"; | ||
rdfs:comment "but imported with default import"; | ||
code:implementedBy [ a code:EcmaScript; | ||
code:link <node:fixtures/namedexport.mjs> | ||
]. | ||
|
||
<a4> a p:Operation, p:Writable, p:Readable; | ||
rdfs:label "Exported with a default export"; | ||
rdfs:comment "but imported with a named import"; | ||
code:implementedBy [ a code:EcmaScript; | ||
code:link <node:fixtures/defaultexport.mjs#namedimport> | ||
]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
@base <http://barnard59.zazuko.com/operations/ftp/> . | ||
@prefix code: <https://code.described.at/> . | ||
@prefix p: <https://pipeline.described.at/> . | ||
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | ||
|
||
<a1> a p:Operation, p:ReadableObjectMode; | ||
rdfs:label "Exported with a named export"; | ||
rdfs:comment "and imported with a named import"; | ||
code:implementedBy [ a code:EcmaScript; | ||
code:link <node:fixtures/namedexport.mjs#myexport> | ||
]. | ||
|
||
<a3> a p:Operation, p:Writable, p:Readable; | ||
rdfs:label "Exported with a default export"; | ||
rdfs:comment "and imported with a default import"; | ||
code:implementedBy [ a code:EcmaScript; | ||
code:link <node:fixtures/defaultexport.mjs> | ||
]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function myexport () { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const { describe, it } = require('mocha') | ||
const path = require('path') | ||
const assert = require('assert') | ||
const validateManifest = require('../lib/manifest') | ||
const ChecksCollection = require('../lib/checksCollection.js') | ||
|
||
describe('manifest', () => { | ||
it('finds import errors', async () => { | ||
const checks = new ChecksCollection() | ||
const file = path.join(__dirname, './fixtures/manifest.ttl') | ||
await validateManifest({ file, checks }) | ||
assert.ok(checks.generic.filter((issue) => issue.level === 'error').length === 1) | ||
}) | ||
|
||
it('finds matching imports/exports', async () => { | ||
const checks = new ChecksCollection() | ||
const file = path.join(__dirname, './fixtures/manifest-good.ttl') | ||
await validateManifest({ file, checks }) | ||
assert.ok(checks.generic.filter((issue) => issue.level === 'error').length === 0) | ||
}) | ||
|
||
it('reports faulty imports/exports', async () => { | ||
const checks = new ChecksCollection() | ||
const file = path.join(__dirname, './fixtures/manifest-bad.ttl') | ||
await validateManifest({ file, checks }) | ||
const errors = checks.generic.filter(issue => issue.level === 'error') | ||
assert.ok(errors.length === 2) | ||
const [error1, error2] = errors | ||
assert(error1.message.includes('does not export default')) | ||
assert(error2.message.includes('does not export namedimport')) | ||
}) | ||
}) |