|
| 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 | +}) |
0 commit comments