|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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.io/license |
| 7 | + * |
| 8 | + */ |
| 9 | +import { schema } from '@angular-devkit/core'; |
| 10 | +import { readFileSync } from 'fs'; |
| 11 | +import { join } from 'path'; |
| 12 | +import { of } from 'rxjs'; |
| 13 | +import { CommandJsonPathException, parseJsonSchemaToCommandDescription } from './json-schema'; |
| 14 | + |
| 15 | +describe('parseJsonSchemaToCommandDescription', () => { |
| 16 | + let registry: schema.CoreSchemaRegistry; |
| 17 | + const baseSchemaJson = { |
| 18 | + '$schema': 'http://json-schema.org/schema', |
| 19 | + '$id': 'ng-cli://commands/version.json', |
| 20 | + 'description': 'Outputs Angular CLI version.', |
| 21 | + '$longDescription': 'not a file ref', |
| 22 | + |
| 23 | + '$aliases': ['v'], |
| 24 | + '$scope': 'all', |
| 25 | + '$impl': './version-impl#VersionCommand', |
| 26 | + |
| 27 | + 'type': 'object', |
| 28 | + 'allOf': [ |
| 29 | + { '$ref': './definitions.json#/definitions/base' }, |
| 30 | + ], |
| 31 | + }; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + registry = new schema.CoreSchemaRegistry([]); |
| 35 | + registry.registerUriHandler((uri: string) => { |
| 36 | + if (uri.startsWith('ng-cli://')) { |
| 37 | + const content = readFileSync( |
| 38 | + join(__dirname, '..', uri.substr('ng-cli://'.length)), 'utf-8'); |
| 39 | + |
| 40 | + return of(JSON.parse(content)); |
| 41 | + } else { |
| 42 | + return null; |
| 43 | + } |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it(`should throw on invalid $longDescription path`, async () => { |
| 48 | + const name = 'version'; |
| 49 | + const schemaPath = join(__dirname, './bad-sample.json'); |
| 50 | + const schemaJson = { ...baseSchemaJson, $longDescription: 'not a file ref' }; |
| 51 | + try { |
| 52 | + await parseJsonSchemaToCommandDescription(name, schemaPath, registry, schemaJson); |
| 53 | + } catch (error) { |
| 54 | + const refPath = join(__dirname, schemaJson.$longDescription); |
| 55 | + expect(error).toEqual(new CommandJsonPathException(refPath, name)); |
| 56 | + |
| 57 | + return; |
| 58 | + } |
| 59 | + expect(true).toBe(false, 'function should have thrown'); |
| 60 | + }); |
| 61 | + |
| 62 | + it(`should throw on invalid $usageNotes path`, async () => { |
| 63 | + const name = 'version'; |
| 64 | + const schemaPath = join(__dirname, './bad-sample.json'); |
| 65 | + const schemaJson = { ...baseSchemaJson, $usageNotes: 'not a file ref' }; |
| 66 | + try { |
| 67 | + await parseJsonSchemaToCommandDescription(name, schemaPath, registry, schemaJson); |
| 68 | + } catch (error) { |
| 69 | + const refPath = join(__dirname, schemaJson.$usageNotes); |
| 70 | + expect(error).toEqual(new CommandJsonPathException(refPath, name)); |
| 71 | + |
| 72 | + return; |
| 73 | + } |
| 74 | + expect(true).toBe(false, 'function should have thrown'); |
| 75 | + }); |
| 76 | +}); |
0 commit comments