From 8784e29a13333074e45e7a39f9118fc9b9a7e1b6 Mon Sep 17 00:00:00 2001 From: dblock Date: Thu, 6 Jun 2024 09:12:57 -0400 Subject: [PATCH] Dynamically create an invalid YAML file in tests to avoid tripping the linter. Signed-off-by: dblock --- tools/tests/linter/NamespacesFolder.test.ts | 124 +++++++++++------- .../invalid_files/invalid_yaml.yaml | 3 - 2 files changed, 74 insertions(+), 53 deletions(-) delete mode 100644 tools/tests/linter/fixtures/folder_validators/namespaces/invalid_files/invalid_yaml.yaml diff --git a/tools/tests/linter/NamespacesFolder.test.ts b/tools/tests/linter/NamespacesFolder.test.ts index 677aa5996..246cd4f7f 100644 --- a/tools/tests/linter/NamespacesFolder.test.ts +++ b/tools/tests/linter/NamespacesFolder.test.ts @@ -7,56 +7,80 @@ * compatible open source license. */ +import path from 'path' +import os from 'os' +import fs from 'fs' import NamespacesFolder from 'linter/components/NamespacesFolder' -test('validate() - When there invalid files', () => { - const validator = new NamespacesFolder('./tools/tests/linter/fixtures/folder_validators/namespaces/invalid_files') - expect(validator.validate()).toEqual([ - { - file: 'invalid_files/indices.txt', - location: 'File Extension', - message: "Invalid file extension. Only '.yaml' files are allowed." - }, - { - file: 'invalid_files/invalid_spec.yaml', - location: 'Operation: GET /{index}/_doc/{id}', - message: 'Missing description property.' - }, - { - file: 'invalid_files/invalid_spec.yaml', - location: 'Operation: GET /{index}/_doc/{id}', - message: "Every parameter must be a reference object to '#/components/parameters/{x-operation-group}::{path|query}.{parameter_name}'." - }, - { - file: 'invalid_files/invalid_spec.yaml', - location: 'Operation: GET /{index}/_doc/{id}', - message: 'Path parameters must match the parameters in the path: {id}, {index}.' - }, - { - file: 'invalid_files/invalid_spec.yaml', - location: 'Operation: GET /{index}/_doc/{id}', - message: "The 200 response must be a reference object to '#/components/responses/invalid_spec.fetch@200'." - }, - { - file: 'invalid_files/invalid_yaml.yaml', - location: 'File Content', - message: 'Unable to read or parse YAML.' - } - ]) -}) +describe('validate()', () => { + test('when there invalid files', () => { + const validator = new NamespacesFolder('./tools/tests/linter/fixtures/folder_validators/namespaces/invalid_files') + expect(validator.validate()).toEqual([ + { + file: 'invalid_files/indices.txt', + location: 'File Extension', + message: "Invalid file extension. Only '.yaml' files are allowed." + }, + { + file: 'invalid_files/invalid_spec.yaml', + location: 'Operation: GET /{index}/_doc/{id}', + message: 'Missing description property.' + }, + { + file: 'invalid_files/invalid_spec.yaml', + location: 'Operation: GET /{index}/_doc/{id}', + message: "Every parameter must be a reference object to '#/components/parameters/{x-operation-group}::{path|query}.{parameter_name}'." + }, + { + file: 'invalid_files/invalid_spec.yaml', + location: 'Operation: GET /{index}/_doc/{id}', + message: 'Path parameters must match the parameters in the path: {id}, {index}.' + }, + { + file: 'invalid_files/invalid_spec.yaml', + location: 'Operation: GET /{index}/_doc/{id}', + message: "The 200 response must be a reference object to '#/components/responses/invalid_spec.fetch@200'." + } + ]) + }) -test('validate() - When the files are valid but the folder is not', () => { - const validator = new NamespacesFolder('./tools/tests/linter/fixtures/folder_validators/namespaces/invalid_folder') - expect(validator.validate()).toEqual([ - { - file: 'invalid_folder/', - location: 'Folder', - message: "Duplicate path '/{index}' found in namespaces: dup_path_a, dup_path_c." - }, - { - file: 'invalid_folder/', - location: 'Folder', - message: "Duplicate path '/{index}/_rollover' found in namespaces: dup_path_a, dup_path_b, dup_path_c." - } - ]) -}) + test('when the files are valid but the folder is not', () => { + const validator = new NamespacesFolder('./tools/tests/linter/fixtures/folder_validators/namespaces/invalid_folder') + expect(validator.validate()).toEqual([ + { + file: 'invalid_folder/', + location: 'Folder', + message: "Duplicate path '/{index}' found in namespaces: dup_path_a, dup_path_c." + }, + { + file: 'invalid_folder/', + location: 'Folder', + message: "Duplicate path '/{index}/_rollover' found in namespaces: dup_path_a, dup_path_b, dup_path_c." + } + ]) + }) + + describe('with invalid YAML', () => { + const invalid_yaml_path = path.join(os.tmpdir(), 'invalid_yaml') + + beforeAll(() => { + fs.mkdirSync(invalid_yaml_path) + fs.writeFileSync(`${invalid_yaml_path}/invalid_yaml.yaml`, "```\nInvalid YAML file\n```") + }) + + afterAll(() => { + fs.rmdirSync(invalid_yaml_path, { recursive: true }) + }) + + test('fails unable to parse YAML', () => { + const validator = new NamespacesFolder(invalid_yaml_path) + expect(validator.validate()).toEqual([ + { + file: 'invalid_yaml/invalid_yaml.yaml', + location: 'File Content', + message: 'Unable to read or parse YAML.' + } + ]) + }) + }) +}) \ No newline at end of file diff --git a/tools/tests/linter/fixtures/folder_validators/namespaces/invalid_files/invalid_yaml.yaml b/tools/tests/linter/fixtures/folder_validators/namespaces/invalid_files/invalid_yaml.yaml deleted file mode 100644 index b8b999b89..000000000 --- a/tools/tests/linter/fixtures/folder_validators/namespaces/invalid_files/invalid_yaml.yaml +++ /dev/null @@ -1,3 +0,0 @@ -``` -Invalid YAML file -``` \ No newline at end of file