Skip to content

Commit

Permalink
Dynamically create an invalid YAML file in tests to avoid tripping th…
Browse files Browse the repository at this point in the history
…e linter.

Signed-off-by: dblock <dblock@amazon.com>
  • Loading branch information
dblock committed Jun 6, 2024
1 parent 53f59b4 commit d23da05
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 53 deletions.
124 changes: 74 additions & 50 deletions tools/tests/linter/NamespacesFolder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
}
])
})
})
})

This file was deleted.

0 comments on commit d23da05

Please sign in to comment.