Add tests
This commit is contained in:
rodič
bb6969d9f0
revize
1398d2504a
|
@ -1,4 +1,4 @@
|
|||
name: GitHub Pages
|
||||
name: Test, build, deploy
|
||||
|
||||
on:
|
||||
push:
|
||||
|
@ -19,7 +19,7 @@ jobs:
|
|||
deno-version: v1.x
|
||||
|
||||
- name: Build sources
|
||||
run: make build
|
||||
run: make
|
||||
|
||||
- name: Add custom domain
|
||||
run: "touch dist/CNAME && echo \"spec.utxo.cz\" >> dist/CNAME"
|
||||
|
|
5
Makefile
5
Makefile
|
@ -3,7 +3,10 @@ VERSION = 0.1.0
|
|||
|
||||
.PHONY: all build
|
||||
|
||||
all: build
|
||||
all: test build
|
||||
|
||||
test:
|
||||
deno test --unstable --allow-read utils/test.js
|
||||
|
||||
build:
|
||||
deno run --unstable --allow-read --allow-write utils/build.js
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
- name: Adam Kracík
|
||||
twitter:
|
||||
- name: Adam Studeník
|
||||
twitter: adamstudenik
|
||||
- name: Anett Rohlikova
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
type: object
|
||||
additionalProperties: false
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
pattern: ^[a-z0-9]+$
|
||||
name:
|
||||
type: string
|
||||
shortname:
|
||||
type: string
|
||||
description:
|
||||
type: string
|
||||
dates:
|
||||
type: array
|
||||
minItems: 1
|
||||
items:
|
||||
type: string
|
||||
pattern: "^\\d{4}-\\d{2}-\\d{2}$"
|
||||
place:
|
||||
type: string
|
||||
country:
|
||||
type: string
|
||||
links:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: string
|
||||
format: uri
|
||||
specDef:
|
||||
type: array
|
||||
items:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
properties:
|
||||
type:
|
||||
type: string
|
||||
pattern: "[a-z]+"
|
||||
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
type: array
|
||||
items:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
pattern: "^[a-z0-9-]+$"
|
||||
name:
|
||||
type: string
|
||||
nickname:
|
||||
type: string
|
||||
twitter:
|
||||
type: string
|
||||
pattern: "^[a-zA-Z0-9_]+$"
|
||||
orgs:
|
||||
type: string
|
||||
bio:
|
||||
type: string
|
||||
web:
|
||||
type: object
|
||||
properties:
|
||||
url:
|
||||
type: string
|
||||
format: uri
|
||||
name:
|
||||
type: string
|
||||
lead:
|
||||
type: boolean
|
|
@ -0,0 +1,12 @@
|
|||
type: array
|
||||
items:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
pattern: "^[a-z0-9-]+$"
|
||||
name:
|
||||
type: string
|
||||
examples:
|
||||
type: string
|
|
@ -0,0 +1,42 @@
|
|||
import { assertEquals } from "https://deno.land/std@0.119.0/testing/asserts.ts"
|
||||
import { UTXO } from './utxo.lib.js'
|
||||
|
||||
// initialize ajv JSON Schema validator
|
||||
import Ajv from 'https://esm.sh/ajv@8.8.1'
|
||||
import addFormats from 'https://esm.sh/ajv-formats@2.1.1'
|
||||
const ajv = new Ajv({allErrors: true})
|
||||
addFormats(ajv)
|
||||
|
||||
const utxo = new UTXO({ silent: true })
|
||||
await utxo.init()
|
||||
const schemas = await utxo.schemas()
|
||||
const validators = {}
|
||||
for (const item of schemas) {
|
||||
validators[item.name] = ajv.compile(item.schema)
|
||||
}
|
||||
|
||||
// check entries
|
||||
for (const entryId of utxo.entriesList()) {
|
||||
const entry = utxo.entries[entryId]
|
||||
|
||||
// check index
|
||||
Deno.test(`UTXO.${entryId}: index.yaml`, () => {
|
||||
|
||||
if (!validators.index(entry.index)) {
|
||||
throw validators.index.errors
|
||||
}
|
||||
})
|
||||
|
||||
// check specific specs
|
||||
for (const specId of Object.keys(entry.specs)) {
|
||||
Deno.test(`UTXO.${entryId}: ${specId}`, () => {
|
||||
|
||||
if (!validators[specId]) {
|
||||
return null
|
||||
}
|
||||
if (!validators[specId](entry.specs[specId])) {
|
||||
throw validators[specId].errors
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
import { emptyDir } from "https://deno.land/std@0.119.0/fs/mod.ts"
|
||||
import { copy } from "https://deno.land/std@0.119.0/fs/copy.ts"
|
||||
import { emptyDir } from 'https://deno.land/std@0.119.0/fs/mod.ts'
|
||||
import { copy } from 'https://deno.land/std@0.119.0/fs/copy.ts'
|
||||
import { load } from 'https://deno.land/x/js_yaml_port/js-yaml.js'
|
||||
|
||||
const baseUrl = 'https://spec.utxo.cz'
|
||||
|
@ -14,7 +14,6 @@ const banner = `
|
|||
`
|
||||
|
||||
export class UTXO {
|
||||
|
||||
constructor (options = {}) {
|
||||
this.options = options
|
||||
this.srcDir = this.options.srcDir || './spec'
|
||||
|
@ -30,16 +29,16 @@ export class UTXO {
|
|||
if (!f.name.match(/^\d+$/)) {
|
||||
continue
|
||||
}
|
||||
const specDir = [ this.srcDir, f.name ].join('/')
|
||||
const specDir = [this.srcDir, f.name].join('/')
|
||||
|
||||
const entry = this.entries[f.name] = {}
|
||||
// load index
|
||||
entry.index = await this._yamlLoad([ specDir, 'index.yaml' ].join('/'))
|
||||
entry.index = await this._yamlLoad([specDir, 'index.yaml'].join('/'))
|
||||
|
||||
// load sub-specs
|
||||
entry.specs = {}
|
||||
for (const sp of entry.index.specDef) {
|
||||
entry.specs[sp.type] = await this._yamlLoad([ specDir, `${sp.type}.yaml` ].join('/'))
|
||||
entry.specs[sp.type] = await this._yamlLoad([specDir, `${sp.type}.yaml`].join('/'))
|
||||
}
|
||||
}
|
||||
if (!this.options.silent) {
|
||||
|
@ -47,8 +46,11 @@ export class UTXO {
|
|||
}
|
||||
}
|
||||
|
||||
async build (outputDir) {
|
||||
entriesList () {
|
||||
return Object.keys(this.entries)
|
||||
}
|
||||
|
||||
async build (outputDir) {
|
||||
await emptyDir(outputDir)
|
||||
const entriesIndex = []
|
||||
|
||||
|
@ -57,32 +59,32 @@ export class UTXO {
|
|||
console.log(`UTXO.${entryId}: building specs ..`)
|
||||
}
|
||||
const entry = this.entries[entryId]
|
||||
const entryDir = [ outputDir, entryId ].join('/')
|
||||
const entryDir = [outputDir, entryId].join('/')
|
||||
await emptyDir(entryDir)
|
||||
|
||||
// write sub-specs
|
||||
const specEndpoints = {}
|
||||
for (const specName of Object.keys(entry.specs)) {
|
||||
await this._jsonWrite([ entryDir, `${specName}.json` ], entry.specs[specName])
|
||||
await this._jsonWrite([entryDir, `${specName}.json`], entry.specs[specName])
|
||||
specEndpoints[specName] = `${baseUrl}/${entryId}/${specName}.json`
|
||||
}
|
||||
|
||||
// write index
|
||||
let index = JSON.parse(JSON.stringify(entry.index))
|
||||
const index = JSON.parse(JSON.stringify(entry.index))
|
||||
delete index.specDef
|
||||
index.spec = specEndpoints
|
||||
index.stats = {
|
||||
tracks: entry.specs.tracks.length,
|
||||
speakers: entry.specs.speakers.length,
|
||||
events: entry.specs.events.length,
|
||||
events: entry.specs.events.length
|
||||
}
|
||||
|
||||
await this._jsonWrite([ entryDir, 'index.json' ], index)
|
||||
await this._jsonWrite([entryDir, 'index.json'], index)
|
||||
|
||||
// write bundle
|
||||
let bundle = JSON.parse(JSON.stringify(index))
|
||||
const bundle = JSON.parse(JSON.stringify(index))
|
||||
bundle.spec = entry.specs
|
||||
await this._jsonWrite([ entryDir, 'bundle.json' ], bundle)
|
||||
await this._jsonWrite([entryDir, 'bundle.json'], bundle)
|
||||
|
||||
// copy photos
|
||||
const outputPhotosDir = [entryDir, 'photos'].join('/')
|
||||
|
@ -90,7 +92,7 @@ export class UTXO {
|
|||
console.log(`UTXO.${entryId}: copying photos ..`)
|
||||
console.log(`copying photos to ${outputPhotosDir}`)
|
||||
}
|
||||
await copy([ this.srcDir, entryId, 'photos'].join('/'), outputPhotosDir, { overwrite: true })
|
||||
await copy([this.srcDir, entryId, 'photos'].join('/'), outputPhotosDir, { overwrite: true })
|
||||
|
||||
entriesIndex.push({
|
||||
id: `utxo${entryId}`,
|
||||
|
@ -100,13 +102,26 @@ export class UTXO {
|
|||
}
|
||||
|
||||
// write global index
|
||||
await this._jsonWrite([ outputDir, 'index.json' ], entriesIndex)
|
||||
await this._jsonWrite([outputDir, 'index.json'], entriesIndex)
|
||||
|
||||
if (!this.options.silent) {
|
||||
console.log('\nBuild done')
|
||||
}
|
||||
}
|
||||
|
||||
async schemas () {
|
||||
const schemaDir = './utils/schema'
|
||||
const arr = []
|
||||
for await (const f of Deno.readDir(schemaDir)) {
|
||||
const m = f.name.match(/^(.+)\.yaml$/)
|
||||
if (!m) {
|
||||
continue
|
||||
}
|
||||
arr.push({ name: m[1], schema: await this._yamlLoad([schemaDir, f.name].join('/')) })
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
async _yamlLoad (fn) {
|
||||
return load(await Deno.readTextFile(fn))
|
||||
}
|
||||
|
@ -121,5 +136,4 @@ export class UTXO {
|
|||
}
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Načítá se…
Odkázat v novém úkolu