|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import yargs from 'yargs'; |
| 10 | +import { jsonHelpUsage, schemaOptionsToJsonHelpOptions, setJsonHelpBuilders } from './json-help'; |
| 11 | +import { Option } from './json-schema'; |
| 12 | + |
| 13 | +describe('json-help utilities', () => { |
| 14 | + describe('schemaOptionsToJsonHelpOptions', () => { |
| 15 | + it('normalizes and sorts schema options and filters out hidden options', () => { |
| 16 | + const options: Option[] = [ |
| 17 | + { |
| 18 | + name: 'watch', |
| 19 | + type: 'boolean', |
| 20 | + default: false, |
| 21 | + description: 'Run build when files change.', |
| 22 | + alias: [], |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'noProgress', |
| 26 | + type: 'boolean', |
| 27 | + description: 'Disable progress.', |
| 28 | + alias: ['p'], |
| 29 | + }, |
| 30 | + { |
| 31 | + name: 'hiddenOption', |
| 32 | + type: 'string', |
| 33 | + hidden: true, |
| 34 | + description: 'Should be filtered out.', |
| 35 | + alias: [], |
| 36 | + }, |
| 37 | + { |
| 38 | + name: 'compilationMode', |
| 39 | + type: 'string', |
| 40 | + choices: ['full', 'partial'], |
| 41 | + description: 'Compilation mode.', |
| 42 | + alias: [], |
| 43 | + }, |
| 44 | + { |
| 45 | + name: 'noPrefixString', |
| 46 | + type: 'string', |
| 47 | + alias: 'n', |
| 48 | + deprecated: 'Use another option.', |
| 49 | + description: 'Non-boolean option starting with no-.', |
| 50 | + }, |
| 51 | + ]; |
| 52 | + |
| 53 | + expect(schemaOptionsToJsonHelpOptions(options)).toEqual([ |
| 54 | + { |
| 55 | + name: 'compilation-mode', |
| 56 | + type: 'string', |
| 57 | + deprecated: undefined, |
| 58 | + aliases: undefined, |
| 59 | + default: undefined, |
| 60 | + enum: ['full', 'partial'], |
| 61 | + description: 'Compilation mode.', |
| 62 | + positional: undefined, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'no-prefix-string', |
| 66 | + type: 'string', |
| 67 | + deprecated: 'Use another option.', |
| 68 | + aliases: ['n'], |
| 69 | + default: undefined, |
| 70 | + enum: undefined, |
| 71 | + description: 'Non-boolean option starting with no-.', |
| 72 | + positional: undefined, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: 'progress', |
| 76 | + type: 'boolean', |
| 77 | + deprecated: undefined, |
| 78 | + aliases: ['p'], |
| 79 | + default: undefined, |
| 80 | + enum: undefined, |
| 81 | + description: 'Disable progress.', |
| 82 | + positional: undefined, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'watch', |
| 86 | + type: 'boolean', |
| 87 | + deprecated: undefined, |
| 88 | + aliases: undefined, |
| 89 | + default: false, |
| 90 | + enum: undefined, |
| 91 | + description: 'Run build when files change.', |
| 92 | + positional: undefined, |
| 93 | + }, |
| 94 | + ]); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('setJsonHelpBuilders and jsonHelpUsage', () => { |
| 99 | + it('includes builders in the jsonHelpUsage output when configured', () => { |
| 100 | + const localYargs = yargs(['build', '--help']) |
| 101 | + .scriptName('ng') |
| 102 | + .usage('$0 build [project]', 'Build command') |
| 103 | + .option('configuration', { |
| 104 | + type: 'string', |
| 105 | + alias: 'c', |
| 106 | + describe: 'Builder configuration', |
| 107 | + }); |
| 108 | + |
| 109 | + setJsonHelpBuilders(localYargs, [ |
| 110 | + { |
| 111 | + name: 'Application', |
| 112 | + options: [ |
| 113 | + { |
| 114 | + name: 'aot', |
| 115 | + type: 'boolean', |
| 116 | + default: true, |
| 117 | + description: 'Build using Ahead of Time compilation.', |
| 118 | + }, |
| 119 | + ], |
| 120 | + }, |
| 121 | + { |
| 122 | + name: 'Library', |
| 123 | + options: [ |
| 124 | + { |
| 125 | + name: 'compilation-mode', |
| 126 | + type: 'string', |
| 127 | + enum: ['full', 'partial'], |
| 128 | + description: 'Angular compilation mode.', |
| 129 | + }, |
| 130 | + ], |
| 131 | + }, |
| 132 | + ]); |
| 133 | + |
| 134 | + const parsed = JSON.parse(jsonHelpUsage(localYargs)); |
| 135 | + expect(parsed.builders).toEqual([ |
| 136 | + { |
| 137 | + name: 'Application', |
| 138 | + options: [ |
| 139 | + { |
| 140 | + name: 'aot', |
| 141 | + type: 'boolean', |
| 142 | + default: true, |
| 143 | + description: 'Build using Ahead of Time compilation.', |
| 144 | + }, |
| 145 | + ], |
| 146 | + }, |
| 147 | + { |
| 148 | + name: 'Library', |
| 149 | + options: [ |
| 150 | + { |
| 151 | + name: 'compilation-mode', |
| 152 | + type: 'string', |
| 153 | + enum: ['full', 'partial'], |
| 154 | + description: 'Angular compilation mode.', |
| 155 | + }, |
| 156 | + ], |
| 157 | + }, |
| 158 | + ]); |
| 159 | + }); |
| 160 | + |
| 161 | + it('omits builders when setJsonHelpBuilders is not called', () => { |
| 162 | + const localYargs = yargs(['version', '--help']) |
| 163 | + .scriptName('ng') |
| 164 | + .usage('$0 version', 'Version command'); |
| 165 | + |
| 166 | + const parsed = JSON.parse(jsonHelpUsage(localYargs)); |
| 167 | + expect(parsed.builders).toBeUndefined(); |
| 168 | + }); |
| 169 | + }); |
| 170 | +}); |
0 commit comments