Skip to content

Commit 4deaa7e

Browse files
committed
Validate template turtle files
1 parent e9a5739 commit 4deaa7e

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ install:
2323
script:
2424
# Test the code
2525
- npm run standard
26+
- npm run validate
2627
- npm run nyc
2728
# Test global install of the package
2829
- npm pack .

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,23 @@
112112
"nock": "^9.0.14",
113113
"node-mocks-http": "^1.7.0",
114114
"nyc": "^13.0.1",
115+
"randombytes": "^2.0.1",
115116
"sinon": "^2.1.0",
116117
"sinon-chai": "^2.8.0",
117118
"snyk": "^1.88.2",
118119
"standard": "^8.6.0",
119120
"supertest": "^3.0.0",
120-
"whatwg-url": "^6.1.0",
121-
"randombytes": "^2.0.1"
121+
"turtle-validator": "^1.0.2",
122+
"whatwg-url": "^6.1.0"
122123
},
123124
"main": "index.js",
124125
"scripts": {
125126
"solid": "node ./bin/solid",
126127
"standard": "standard '{bin,examples,lib,test}/**/*.js'",
128+
"validate": "node ./test/validate-turtle.js",
127129
"nyc": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 nyc --reporter=text-summary mocha",
128130
"mocha": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 mocha",
129-
"test": "npm run standard && npm run nyc",
131+
"test": "npm run standard && npm run validate && npm run nyc",
130132
"clean": "rimraf config/templates config/views"
131133
},
132134
"nyc": {

test/validate-turtle.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
const fs = require('fs')
3+
const Handlebars = require('handlebars')
4+
const path = require('path')
5+
const validate = require('turtle-validator/lib/validator')
6+
7+
const regex = new RegExp('\\.(acl|ttl)$', 'i')
8+
const substitutions = {
9+
webId: 'http://example.com/#me',
10+
email: 'test@example.com',
11+
name: 'Test test'
12+
}
13+
14+
const files = recursiveFiles(path.join(__dirname, '../default-templates/'))
15+
16+
for (const file of files) {
17+
const data = fs.readFileSync(file, 'utf8')
18+
const template = Handlebars.compile(data)
19+
validate(template(substitutions), feedback => {
20+
if (feedback.errors.length > 0) {
21+
throw new Error(`Validation error in ${file}: ${feedback.errors[0]}`)
22+
}
23+
})
24+
}
25+
26+
function recursiveFiles (dir) {
27+
const content = fs.readdirSync(dir)
28+
return [].concat(...content.map(file => {
29+
const fullPath = path.join(dir, file)
30+
const stat = fs.statSync(fullPath)
31+
if (stat.isDirectory()) {
32+
return recursiveFiles(fullPath)
33+
} else if (regex.test(file)) {
34+
return [fullPath]
35+
}
36+
return []
37+
}))
38+
}

0 commit comments

Comments
 (0)