Skip to content

Commit 250fa0d

Browse files
authored
Merge pull request #91 from GrantBirki/copilot/fix-70
Fix file duplication issue - YAML validator now skips JSON files when not in yamlAsJson mode
2 parents 9413783 + 2e8494a commit 250fa0d

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

__tests__/functions/yaml-validator.test.js

Lines changed: 19 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,21 @@ 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 =
445+
'__tests__/fixtures/json/valid/json1.json\n__tests__/fixtures/yaml/valid/yaml1.yaml'
446+
447+
expect(await yamlValidator(excludeMock)).toStrictEqual({
448+
failed: 0,
449+
passed: 1,
450+
skipped: 0,
451+
success: true,
452+
violations: []
453+
})
454+
455+
expect(debugMock).toHaveBeenCalledWith(
456+
"the yaml-validator found a json file so it will be skipped here: '__tests__/fixtures/json/valid/json1.json'"
457+
)
458+
})

dist/index.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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)