Skip to content

Commit 796a196

Browse files
committed
refactor: Remove deprecated API usages and adding more tests
1 parent 6026609 commit 796a196

8 files changed

Lines changed: 258 additions & 9 deletions

File tree

src/commands/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function getFiles(args: string | string[]): string[] {
2323
}
2424

2525
function getFormatFromFileName(filename: string): string {
26-
return path.extname(filename).substr(1).toLowerCase()
26+
return path.extname(filename).substring(1).toLowerCase()
2727
}
2828

2929
function decodeFile(contents: string, format: string): any {

test/compile.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {ExecException} from "child_process"
22
import cli from "./cli"
3-
import assert = require("assert")
4-
import fs = require("fs")
3+
import assert from "assert"
4+
import * as fs from "fs"
55

66
describe("compile", function () {
77
this.timeout(10000)

test/help.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cli from "./cli"
2-
import assert = require("assert")
2+
import assert from "assert"
33

44
describe("help", function () {
55
this.timeout(10000)

test/migrate.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cli from "./cli"
2-
import assert = require("assert")
3-
import fs = require("fs")
4-
import path = require("path")
2+
import assert from "assert"
3+
import * as fs from "fs"
4+
import * as path from "path"
55
import {AnySchemaObject} from "ajv"
66

77
describe("migrate", function () {

test/options.spec.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import assert from "assert"
2+
import {checkOptions, getOptions} from "../src/commands/options"
3+
4+
// checkOptions mutates the schema it receives, so each test must use a fresh object.
5+
function schema(extra: object = {}): any {
6+
return {type: "object", properties: {}, ...extra}
7+
}
8+
9+
describe("options", function () {
10+
describe("checkOptions", () => {
11+
it("should return null when all required params are present", () => {
12+
const result = checkOptions(
13+
schema({properties: {s: {type: "string"}}, required: ["s"]}),
14+
{_: [], s: "test/schema.json"}
15+
)
16+
assert.strictEqual(result, null)
17+
})
18+
19+
it("should return an error string for a missing required param", () => {
20+
const result = checkOptions(
21+
schema({properties: {s: {type: "string"}}, required: ["s"]}),
22+
{_: []}
23+
)
24+
assert(result !== null)
25+
assert(result.includes("required"))
26+
assert(result.includes("-s"))
27+
})
28+
29+
it("should prefix single-char params with single dash", () => {
30+
const result = checkOptions(
31+
schema({properties: {s: {type: "string"}}, required: ["s"]}),
32+
{_: []}
33+
)
34+
assert(result !== null)
35+
assert(result.includes("-s"))
36+
assert(!result.includes("--s"))
37+
})
38+
39+
it("should prefix multi-char params with double dash", () => {
40+
const result = checkOptions(
41+
schema({properties: {schema: {type: "string"}}, required: ["schema"]}),
42+
{_: []}
43+
)
44+
assert(result !== null)
45+
assert(result.includes("--schema"))
46+
})
47+
48+
it("should return an error for an unknown parameter", () => {
49+
const result = checkOptions(schema(), {_: [], unknownParam: "value"})
50+
assert(result !== null)
51+
assert(result.includes("unknown"))
52+
assert(result.includes("--unknownParam"))
53+
})
54+
55+
it("should return an error when too many positional arguments are given", () => {
56+
const result = checkOptions(schema(), {_: ["arg1", "arg2"]})
57+
assert(result !== null)
58+
assert(result.includes("too many arguments"))
59+
})
60+
61+
it("should return null for exactly one positional argument", () => {
62+
const result = checkOptions(schema(), {_: ["one"]})
63+
assert.strictEqual(result, null)
64+
})
65+
66+
it("should accept known ajv options when ajvOptions key is present in schema", () => {
67+
const result = checkOptions(schema({ajvOptions: true}), {_: [], "all-errors": true})
68+
assert.strictEqual(result, null)
69+
})
70+
71+
it("should reject unknown options even with ajvOptions in schema", () => {
72+
const result = checkOptions(schema({ajvOptions: true}), {_: [], "not-an-option": true})
73+
assert(result !== null)
74+
assert(result.includes("unknown"))
75+
})
76+
})
77+
78+
describe("getOptions", () => {
79+
it("should return an empty code object when argv has no ajv options", () => {
80+
const opts = getOptions({_: []})
81+
assert.deepStrictEqual(opts.code, {})
82+
})
83+
84+
it("should not include undefined options in the result", () => {
85+
const opts = getOptions({_: []})
86+
assert(!("allErrors" in opts))
87+
assert(!("strict" in opts))
88+
})
89+
90+
it("should extract allErrors from camelCase argv key", () => {
91+
const opts = getOptions({_: [], allErrors: true})
92+
assert.strictEqual(opts.allErrors, true)
93+
})
94+
95+
it("should extract allErrors from dash-case argv key (--all-errors)", () => {
96+
const opts = getOptions({_: [], "all-errors": true})
97+
assert.strictEqual(opts.allErrors, true)
98+
})
99+
100+
it("should map the data option to $data", () => {
101+
const opts = getOptions({_: [], data: true})
102+
assert.strictEqual(opts.$data, true)
103+
})
104+
105+
it("should put code-es5 into the code sub-object", () => {
106+
const opts = getOptions({_: [], "code-es5": true})
107+
assert.strictEqual(opts.code.es5, true)
108+
})
109+
110+
it("should put code-optimize into the code sub-object", () => {
111+
const opts = getOptions({_: [], "code-optimize": 2})
112+
assert.strictEqual(opts.code.optimize, 2)
113+
})
114+
115+
it("should put code-lines into the code sub-object", () => {
116+
const opts = getOptions({_: [], "code-lines": true})
117+
assert.strictEqual(opts.code.lines, true)
118+
})
119+
120+
it("should handle strict option (boolean)", () => {
121+
const opts = getOptions({_: [], strict: true})
122+
assert.strictEqual(opts.strict, true)
123+
})
124+
125+
it("should handle strict option (string value)", () => {
126+
const opts = getOptions({_: [], strict: "log"})
127+
assert.strictEqual(opts.strict, "log")
128+
})
129+
130+
it("should handle multiple options at once", () => {
131+
const opts = getOptions({_: [], allErrors: true, "code-es5": true, data: true})
132+
assert.strictEqual(opts.allErrors, true)
133+
assert.strictEqual(opts.$data, true)
134+
assert.strictEqual(opts.code.es5, true)
135+
})
136+
})
137+
})

test/test.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cli from "./cli"
2-
import assert = require("assert")
2+
import assert from "assert"
33
import type {DefinedError} from "ajv"
44

55
describe("test", function () {

test/util.spec.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import assert from "assert"
2+
import {getFiles, openFile, logJSON} from "../src/commands/util"
3+
4+
describe("util", function () {
5+
describe("getFiles", () => {
6+
it("should return a single filename as-is (no glob magic)", () => {
7+
const files = getFiles("test/schema.json")
8+
assert.deepStrictEqual(files, ["test/schema.json"])
9+
})
10+
11+
it("should expand a glob pattern to matching files", () => {
12+
const files = getFiles("test/valid_data.js*")
13+
assert(files.length >= 2)
14+
assert(files.every((f) => /valid_data\.js/.test(f)))
15+
})
16+
17+
it("should handle an array of plain filenames", () => {
18+
const files = getFiles(["test/schema.json", "test/valid_data.json"])
19+
assert.deepStrictEqual(files, ["test/schema.json", "test/valid_data.json"])
20+
})
21+
22+
it("should handle an array mixing plain names and globs", () => {
23+
const files = getFiles(["test/schema.json", "test/valid_data.js*"])
24+
assert(files.length >= 3)
25+
assert.strictEqual(files[0], "test/schema.json")
26+
})
27+
28+
it("should return an empty array for a no-match glob", () => {
29+
const files = getFiles("test/no_such_file_xyz_*.json")
30+
assert.deepStrictEqual(files, [])
31+
})
32+
})
33+
34+
describe("openFile", () => {
35+
it("should parse a JSON file", () => {
36+
const data = openFile("test/valid_data.json", "data")
37+
assert(Array.isArray(data))
38+
})
39+
40+
it("should parse a .yml YAML file", () => {
41+
const data = openFile("test/valid_data.yml", "data")
42+
assert(data !== null && typeof data === "object")
43+
})
44+
45+
it("should parse a .yaml YAML file", () => {
46+
const data = openFile("test/valid_data.yaml", "data")
47+
assert(data !== null && typeof data === "object")
48+
})
49+
50+
it("should parse a JSON5 file", () => {
51+
const data = openFile("test/valid_data.json5", "data")
52+
assert(data !== null && typeof data === "object")
53+
})
54+
55+
it("should parse a JSONC file", () => {
56+
const data = openFile("test/valid_data.jsonc", "data")
57+
assert(data !== null && typeof data === "object")
58+
})
59+
60+
it("should parse a JSON schema file", () => {
61+
const data = openFile("test/schema.json", "schema")
62+
assert(typeof data === "object" && data !== null)
63+
assert("type" in data || "$schema" in data || "properties" in data)
64+
})
65+
66+
it("should load file content matching across all supported text formats", () => {
67+
const json = openFile("test/valid_data.json", "data")
68+
const yml = openFile("test/valid_data.yml", "data")
69+
const yaml = openFile("test/valid_data.yaml", "data")
70+
assert.deepStrictEqual(json, yml)
71+
assert.deepStrictEqual(json, yaml)
72+
})
73+
})
74+
75+
describe("logJSON", () => {
76+
const errors = [{keyword: "required", message: "must have required property 'foo'"}]
77+
78+
it('should pretty-print JSON for mode "json"', () => {
79+
const result = logJSON("json", errors)
80+
assert.strictEqual(result, JSON.stringify(errors, null, " "))
81+
})
82+
83+
it('should output compact JSON for mode "line"', () => {
84+
const result = logJSON("line", errors)
85+
assert.strictEqual(result, JSON.stringify(errors))
86+
})
87+
88+
it('should return empty string for mode "no"', () => {
89+
const result = logJSON("no", errors)
90+
assert.strictEqual(result, "")
91+
})
92+
93+
it('should return data unchanged for mode "text" without ajv', () => {
94+
const result = logJSON("text", errors)
95+
assert.strictEqual(result, errors)
96+
})
97+
98+
it('should call ajv.errorsText for mode "text" with ajv instance', () => {
99+
// eslint-disable-next-line @typescript-eslint/no-var-requires
100+
const Ajv = require("ajv")
101+
const ajv = new Ajv()
102+
const result = logJSON("text", errors, ajv)
103+
assert(typeof result === "string")
104+
assert(result.includes("must have required property"))
105+
})
106+
107+
it("should return data unchanged for an unrecognised mode", () => {
108+
const result = logJSON("fancy", errors)
109+
assert.strictEqual(result, errors)
110+
})
111+
})
112+
})

test/validate.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cli from "./cli"
2-
import assert = require("assert")
2+
import assert from "assert"
33
import type {DefinedError} from "ajv"
44

55
describe("validate", function () {

0 commit comments

Comments
 (0)