Skip to content

Commit b22ed78

Browse files
CopilotGrantBirki
andcommitted
Fix file duplication issue by adding JSON file skipping logic to YAML validator
Co-authored-by: GrantBirki <[email protected]>
1 parent b805108 commit b22ed78

File tree

3 files changed

+30
-61
lines changed

3 files changed

+30
-61
lines changed

__tests__/functions/yaml-validator.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ beforeEach(() => {
1717
jest.clearAllMocks()
1818
process.env.INPUT_YAML_SCHEMA = '__tests__/fixtures/schemas/schema1.yaml'
1919
process.env.INPUT_BASE_DIR = '__tests__/fixtures/yaml/valid'
20+
process.env.INPUT_JSON_EXTENSION = '.json'
2021
process.env.INPUT_YAML_EXTENSION = '.yaml'
2122
process.env.INPUT_YAML_EXTENSION_SHORT = '.yml'
2223
process.env.INPUT_YAML_EXCLUDE_REGEX = '.*bad.*\\.yaml'
@@ -437,3 +438,20 @@ test('edge case: yaml with undefined/null values in error paths', async () => {
437438
// Cleanup
438439
fs.unlinkSync(tempFile)
439440
})
441+
442+
test('skips json files when yaml_as_json is false', async () => {
443+
process.env.INPUT_YAML_AS_JSON = 'false'
444+
process.env.INPUT_FILES = '__tests__/fixtures/json/valid/json1.json\n__tests__/fixtures/yaml/valid/yaml1.yaml'
445+
446+
expect(await yamlValidator(excludeMock)).toStrictEqual({
447+
failed: 0,
448+
passed: 1,
449+
skipped: 0,
450+
success: true,
451+
violations: []
452+
})
453+
454+
expect(debugMock).toHaveBeenCalledWith(
455+
'the yaml-validator found a json file so it will be skipped here: \'__tests__/fixtures/json/valid/json1.json\''
456+
)
457+
})

__tests__/test-bug-reproduction.test.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/functions/yaml-validator.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const INVALID_YAML_MESSAGE = 'Invalid YAML'
1111
// Helper function to validate all yaml files in the baseDir
1212
export async function yamlValidator(exclude) {
1313
const baseDir = core.getInput('base_dir')
14+
const jsonExtension = core.getInput('json_extension')
1415
const yamlExtension = core.getInput('yaml_extension')
1516
const yamlExtensionShort = core.getInput('yaml_extension_short')
1617
const yamlSchema = core.getInput('yaml_schema')
@@ -76,6 +77,17 @@ export async function yamlValidator(exclude) {
7677
continue
7778
}
7879

80+
// if the file is a json file but it should not be treated as yaml
81+
// skipped++ does not need to be called here as the file should be validated later...
82+
// ...on as json with the json-validator
83+
const isJsonFile = jsonExtension && fullPath.endsWith(jsonExtension)
84+
if (yamlAsJson === false && isJsonFile) {
85+
core.debug(
86+
`the yaml-validator found a json file so it will be skipped here: '${fullPath}'`
87+
)
88+
continue
89+
}
90+
7991
if (yamlAsJson) {
8092
core.debug(
8193
`skipping yaml since it should be treated as json: ${fullPath}`

0 commit comments

Comments
 (0)