bugs: check and test infrastructure, with a simple example

parent 4d642472
import * as git from "./git"
import { load, write, getFiles } from "./file_systems"
export class BasePlugin {
options: any
name: string
constructor(options: any, name: string) {
this.options = options
this.name = name
}
fix(_content: any, _filename: any): any {
return null
}
preCheck(): any {}
check(_content: any, _filename: any): any {
return null
}
postCheck(_analysis: any): any {}
}
export class SchemaBugs {
options: any
plugins: any
constructor(options: any) {
this.options = options
this.get(this.options.schema)
}
get(schema: any): any {
this.plugins = []
for (const file of getFiles([`src/bugs/${schema}-*.ts`])) {
const module = require(`../${file}`)
this.plugins.push(new module["Plugin"](this.options))
}
}
fix(filenames: any): any {
let results: any = {}
for (const plugin of this.plugins) {
for (const filename of filenames) {
const content = load(filename)
const result = plugin.fix(content, filename)
results[plugin.name] = result
if (result == true) write(content, filename)
}
if (this.options.commit) {
git.commit(this.options.commit, `Fix ${plugin.name}`)
}
}
return results
}
preCheck() {
for (const plugin of this.plugins) plugin.preCheck()
}
check(content: any, filename: any): any {
let results: any = {}
for (const plugin of this.plugins)
results[plugin.name] = plugin.check(content, filename)
return results
}
postCheck(analysis: any): any {
for (const plugin of this.plugins) plugin.postCheck(analysis)
}
}
import { BasePlugin } from "../bugs"
function fixActeLegislatif(acteLegislatif: any): any {
const statutConclusion = acteLegislatif.statutConclusion
if (statutConclusion !== undefined) {
if (statutConclusion.famCode === "TSORTFnull") {
delete acteLegislatif.statutConclusion
return true
}
}
let actesLegislatifs = acteLegislatif.actesLegislatifs
if (actesLegislatifs !== undefined) {
for (const acteLegislatif of actesLegislatifs) {
if (fixActeLegislatif(acteLegislatif) == true) return true
}
}
return false
}
export class Plugin extends BasePlugin {
constructor(options: any, _name?: string) {
super(options, "dossier-00001")
}
fix(dossier: any, _filename: any): any {
if (dossier.uid != "DLR5L15N37831") {
return null
}
let actesLegislatifs = dossier.actesLegislatifs
for (const acteLegislatif of actesLegislatifs) {
if (fixActeLegislatif(acteLegislatif) == true) return true
}
return false
}
}
import { load } from "../../src/file_systems"
import { assert } from "chai"
import temp from "temp"
import fs from "fs-extra"
import { SchemaBugs } from "../../src/bugs"
import { BasePlugin } from "../../src/bugs"
import { Plugin as Dossier00001 } from "../../src/bugs/dossier-00001"
suite("bugs")
test("#SchemaBugsFix", function() {
const d = new SchemaBugs({ schema: "dossier" })
const bug = new Dossier00001({})
d.plugins = [bug]
const tmpDir = temp.mkdirSync("clean")
fs.copySync("tests/bugs/dossier-00001", `${tmpDir}`)
const f = `${tmpDir}/fail.json`
// true means a bug was found and fixed
assert.deepEqual(d.fix([f]), { "dossier-00001": true })
// false means the bug is not found, because it was fixed
assert.deepEqual(d.fix([f]), { "dossier-00001": false })
})
test("#SchemaBugs", function() {
const d = new SchemaBugs({ schema: "dossier" })
assert(d.plugins.length > 0)
class Plugin extends BasePlugin {
count: any
constructor(options: any) {
super(options, "test")
this.count = {}
}
fix(_content: any, filename: any): any {
this.count["fix"] = [filename]
return null
}
preCheck(): any {
this.count["preCheck"] = 1
}
check(content: any, filename: any): any {
this.count["check"] = [content, filename]
return null
}
postCheck(analysis: any): any {
this.count["postCheck"] = analysis
}
}
const i = new Plugin({})
d.plugins = [i]
const filename = "tests/bugs/dossier-00001/ok.json"
const content = "content"
d.fix([filename])
d.preCheck()
const report = d.check(content, filename)
const analysis = { filename: report }
d.postCheck(analysis)
assert.deepEqual(i.count["fix"], [filename])
assert.equal(i.count["preCheck"], 1)
assert.deepEqual(i.count["check"], [content, filename])
assert.deepEqual(i.count["postCheck"], analysis)
})
test("#PluginDossier00001", function() {
const bug = new Dossier00001({})
const d = "tests/bugs/dossier-00001"
{
const f = `${d}/fail.json`
const content = load(f)
assert.equal(bug.fix(content, f), true, f)
}
{
const f = `${d}/ok.json`
const content = load(f)
assert.equal(bug.fix(content, f), false, f)
}
})
This diff is collapsed.
This diff is collapsed.
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