simplify calling validate_json script

and make it consistent with bugs_helper
parent 1c1c8e79
......@@ -6,15 +6,20 @@ import { load, getFiles } from "../file_systems"
import temp from "temp"
import fs from "fs-extra"
import path from "path"
import {
validDataset,
getDatasets,
getSchemas,
datasetDirectorySchema,
} from "../../src/datasets"
//temp.track()
function parseArgs(argv: string[]): any {
const optionsDefinitions = [
{
description: "pathname to a JSON file or a glob pattern to JSON files",
description: "pathname to the directory containing the dataset",
defaultOption: true,
multiple: true,
name: "data",
type: String,
},
......@@ -26,18 +31,13 @@ function parseArgs(argv: string[]): any {
type: String,
},
{
description:
"pathname to a schema referenced by --schema, relative to https://git.en-root.org/tricoteuses/tricoteuses-assemblee/tree/master/src/schemas",
multiple: true,
name: "reference",
alias: "r",
description: `dataset (possible values must include exactly one of ${getDatasets()})`,
name: "dataset",
type: String,
},
{
description:
"pathname to the schema against which the JSON data must validate, relative to https://git.en-root.org/tricoteuses/tricoteuses-assemblee/tree/master/src/schemas",
description: `schema (possible values are ${getSchemas()})`,
name: "schema",
alias: "s",
type: String,
},
{
......@@ -69,10 +69,21 @@ function parseArgs(argv: string[]): any {
const options = commandLineArgs(optionsDefinitions, {
argv: argv,
})
if (!("help" in options) && !("data" in options)) {
console.error("--data is mandatory")
options.help = true
if (options.dataset) {
if (!validDataset(options.dataset)) {
console.error(`--dataset ${options.dataset}`)
options.help = true
}
}
if (options.schema) {
if (!getSchemas().includes(options.schema)) {
console.error(`--schema ${options.schema}`)
options.help = true
}
}
if ("help" in options) {
const usage = commandLineUsage(sections)
console.warn(usage)
......@@ -124,14 +135,18 @@ class Validate {
const ajv = new Ajv()
const dir = this.getSchemaDir(`schema-${version}`)
let references = []
for (const reference of this.options.reference)
references.push(path.join(dir, reference))
for (const reference of getFiles([
`${dir}/${this.options.schema}/*.json`,
]))
references.push(reference)
for (const file of getFiles(references)) {
if (this.options.verbose) console.log(`--reference ${file}`)
if (this.options.verbose) console.log(`reference: ${file}`)
ajv.addSchema(load(file))
}
const s = path.join(dir, this.options.schema)
if (this.options.verbose) console.log(`--schema ${s}`)
const schema = this.options.schema
const Schema = schema.charAt(0).toUpperCase() + schema.slice(1)
const s = `${dir}/${schema}/${Schema}.json`
if (this.options.verbose) console.log(`schema ${s}`)
this.version2schema[version] = ajv.compile(load(s))
}
return this.version2schema[version]
......@@ -139,7 +154,7 @@ class Validate {
getVersion(content: any): string {
if (content.schemaVersion != undefined) return content.schemaVersion
return this.options.schema.split("/")[0] + "-1.0"
return `${this.options.schema}-1.0`
}
getSchemaURL(version: string): any {
......@@ -170,9 +185,9 @@ class Validate {
}
}
process() {
async process(): Promise<number> {
let status = 0
for (const file of getFiles(this.options.data)) {
for (const file of getFiles([this.options.data])) {
const content = load(file)
status |= this.validate(content, file)
}
......@@ -180,13 +195,39 @@ class Validate {
}
}
function main(argv: any): number {
async function main(argv: any): Promise<number> {
const options = parseArgs(argv)
if (options === null) return 1
const validate = new Validate(options)
return validate.process()
let directorySchema = []
if (options.schema) {
directorySchema = [[options.data, options.schema]]
} else if (options.dataset) {
directorySchema = datasetDirectorySchema(options.dataset)
} else {
console.error("either --schema or --dataset is required")
return 1
}
for (const d of directorySchema) {
options.schema = d[1]
options.data = d[0]
if (options.verbose)
console.log(
`working on files ${options.data} with schema ${options.schema}`,
)
const validate = new Validate(options)
const status = await validate.process()
if (status != 0) return status
}
return 0
}
/* istanbul ignore if */
if (process.argv[1].endsWith("validate_json.ts"))
process.exit(main(process.argv))
main(process.argv)
.then(status => {
process.exit(status)
})
.catch(error => {
console.error(error)
process.exit(1)
})
......@@ -4,6 +4,7 @@ let sinon = require("sinon")
import { assert } from "chai"
import fs from "fs-extra"
import path from "path"
import * as git from "../src/git"
import { load } from "../src/file_systems"
let validate_json = rewire("../src/scripts/validate_json")
......@@ -42,11 +43,8 @@ test("#getSchemaDir", function() {
test("#getValidator", function() {
const Validate = validate_json.__get__("Validate")
const v = new Validate({
reference: ["document/CodeLibelle.json", "document/Indexation.json"],
schema: "document/Document.json",
repository: execSync("git rev-parse --show-toplevel")
.toString()
.trim(),
schema: "document",
repository: git.run(".", "rev-parse --show-toplevel"),
})
const validator = v.getValidator("document-1.0")
......@@ -66,11 +64,8 @@ test("#getValidator", function() {
test("#validate", function() {
const Validate = validate_json.__get__("Validate")
const v = new Validate({
reference: ["document/CodeLibelle.json", "document/Indexation.json"],
schema: "document/Document.json",
repository: execSync("git rev-parse --show-toplevel")
.toString()
.trim(),
schema: "document",
repository: git.run(".", "rev-parse --show-toplevel"),
})
const error = sinon.stub(console, "error")
......@@ -87,9 +82,7 @@ test("#validate", function() {
test("#getVersion", function() {
const Validate = validate_json.__get__("Validate")
const v = new Validate({
schema: "acteur/Acteur.json",
})
const v = new Validate({ schema: "acteur" })
assert.equal(v.getVersion({}), "acteur-1.0")
const version = "document-2.0"
assert.equal(v.getVersion({ schemaVersion: version }), version)
......@@ -97,9 +90,7 @@ test("#getVersion", function() {
test("#getSchemaURL", function() {
const Validate = validate_json.__get__("Validate")
const v = new Validate({
schema: "acteur/Acteur.json",
})
const v = new Validate({ schema: "acteur" })
const version = "document-2.0"
v.options.repository = "http://site/repo.git"
......@@ -115,53 +106,54 @@ test("#getSchemaURL", function() {
assert.equal(v.getSchemaURL(version), `src/schemas`)
})
test("#parseArgs", function() {
const parseArgs = validate_json.__get__("parseArgs")
test("#wrongArgument", async function() {
const main = validate_json.__get__("main")
const log = sinon.stub(console, "log")
const warn = sinon.stub(console, "warn")
let options = parseArgs(["--help"])
assert.equal(options, null)
assert(warn.args[0][0].includes("Validate JSON files"))
warn.reset()
const error = sinon.stub(console, "error")
options = parseArgs([])
assert.equal(options, null)
assert(warn.args[0][0].includes("Validate JSON files"))
assert(error.args[0][0].includes("data is mandatory"))
warn.reset()
error.reset()
options = parseArgs(["--data", "path.json"])
assert.deepEqual(options.data, ["path.json"])
for (const argument of ["schema", "dataset"]) {
const status = await main([
"--verbose",
`--${argument}=INVALID`,
"--data=tests/check/RUANR5L15S2020IDS22089.json",
])
assert.equal(status, 1)
assert.deepEqual(error.args, [[`--${argument} INVALID`]])
error.reset()
}
log.restore()
warn.restore()
error.restore()
})
test("#main", function() {
test("#main", async function() {
const main = validate_json.__get__("main")
const log = sinon.stub(console, "log")
const warn = sinon.stub(console, "warn")
const error = sinon.stub(console, "error")
// good run
let status = main([
let status = await main([
"--verbose",
"--repository",
execSync("git rev-parse --show-toplevel")
.toString()
.trim(),
"--reference=document/CodeLibelle.json",
"--reference=document/Indexation.json",
"--schema=document/Document.json",
git.run(".", "rev-parse --show-toplevel"),
"--schema=document",
"--data=tests/validate_json/RAPPANR5L15B1302.json",
])
assert.deepEqual(warn.args, [])
warn.reset()
assert.deepEqual(error.args, [])
error.reset()
assert.equal(status, 0)
assert.deepEqual(warn.args, [])
warn.reset()
// missing mandatory --data option
assert.equal(main([]), 1)
status = await main([])
assert.equal(status, 1)
assert.deepEqual(error.args, [["either --schema or --dataset is required"]])
log.restore()
warn.restore()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment