Skip to content

Commit

Permalink
Merge pull request #26 from GrantBirki/json-as-yaml-fix
Browse files Browse the repository at this point in the history
JSON as YAML - Bug Fix
  • Loading branch information
GrantBirki authored Aug 11, 2023
2 parents 47d4be5 + ebc3ac1 commit ee097ec
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 9 deletions.
3 changes: 3 additions & 0 deletions __tests__/fixtures/yaml_as_json/mixture/good-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
3 changes: 3 additions & 0 deletions __tests__/fixtures/yaml_as_json/mixture/invalid-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bad data": 'oh no'
}
2 changes: 2 additions & 0 deletions __tests__/fixtures/yaml_as_json/mixture/yaml1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo: 1
bar: abc
2 changes: 2 additions & 0 deletions __tests__/fixtures/yaml_as_json/mixture/yaml2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo: 1
bar: abc
46 changes: 44 additions & 2 deletions __tests__/functions/json-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ beforeEach(() => {
process.env.INPUT_YAML_AS_JSON = 'false'
process.env.INPUT_USE_DOT_MATCH = 'true'
process.env.INPUT_USE_AJV_FORMATS = true
process.env.INPUT_YAML_EXTENSION = '.yaml'
process.env.INPUT_YAML_EXTENSION_SHORT = '.yml'
process.env.INPUT_FILES = ''
})

Expand Down Expand Up @@ -200,7 +202,7 @@ test('fails to validate one json file with an incorrect schema and succeeds on t
})

test('successfully validates a yaml file with a schema when yaml_as_json is true', async () => {
process.env.INPUT_YAML_AS_JSON = true
process.env.INPUT_YAML_AS_JSON = 'true'
process.env.INPUT_BASE_DIR = '__tests__/fixtures/yaml_as_json/valid'

expect(await jsonValidator(excludeMock)).toStrictEqual({
Expand All @@ -212,6 +214,46 @@ test('successfully validates a yaml file with a schema when yaml_as_json is true
})
})

test('processes multiple files when yaml_as_json is true and also a mixture of other json files with yaml are present', async () => {
process.env.INPUT_YAML_AS_JSON = 'true'
process.env.INPUT_JSON_SCHEMA = ''
process.env.INPUT_BASE_DIR = '__tests__/fixtures/yaml_as_json/mixture'

expect(await jsonValidator(excludeMock)).toStrictEqual({
failed: 1,
passed: 3,
skipped: 0,
success: false,
violations: [
{
file: '__tests__/fixtures/yaml_as_json/mixture/invalid-json.json',
errors: [
{
path: null,
message: 'Invalid JSON'
}
]
}
]
})

expect(debugMock).toHaveBeenCalledWith(
'using ajv-formats with json-validator'
)
expect(debugMock).toHaveBeenCalledWith(
'json - using baseDir: __tests__/fixtures/yaml_as_json/mixture'
)
expect(debugMock).toHaveBeenCalledWith(
'json - using glob: **/*{.json,yaml,yml}'
)
expect(debugMock).toHaveBeenCalledWith(
`attempting to process yaml file: '__tests__/fixtures/yaml_as_json/mixture/yaml1.yaml' as json`
)
expect(debugMock).toHaveBeenCalledWith(
`attempting to process yaml file: '__tests__/fixtures/yaml_as_json/mixture/yaml2.yml' as json`
)
})

test('successfully validates json files with a schema when files is defined', async () => {
const files = [
'__tests__/fixtures/json/valid/json1.json',
Expand All @@ -231,7 +273,7 @@ test('successfully validates json files with a schema when files is defined', as
})

test('fails to validate a yaml file with an incorrect schema when yaml_as_json is true', async () => {
process.env.INPUT_YAML_AS_JSON = true
process.env.INPUT_YAML_AS_JSON = 'true'
process.env.INPUT_BASE_DIR = '__tests__/fixtures/yaml_as_json/invalid'

expect(await jsonValidator(excludeMock)).toStrictEqual({
Expand Down
16 changes: 13 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/acceptance-test-results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ skipping due to exclude match: __tests__/fixtures/json/invalid/skip-bad.json
skipping due to exclude match: __tests__/fixtures/json/mixture/json1.json
skipping due to exclude match: __tests__/fixtures/json/mixture/json2.json
skipping due to exclude match: __tests__/fixtures/json/valid/json1.json
skipping due to exclude match: __tests__/fixtures/yaml_as_json/mixture/good-json.json
skipping due to exclude match: __tests__/fixtures/yaml_as_json/mixture/invalid-json.json
skipping due to exclude match: __tests__/fixtures/json/project_dir/schemas/schema.json
skipping due to exclude match: __tests__/fixtures/json/project_dir/data/config/json1.json
skipping due to exclude match: __tests__/fixtures/json/project_dir/data/config/.github_mock_dir/json2.json
Expand All @@ -30,6 +32,8 @@ skipping due to exclude match: __tests__/fixtures/yaml/mixture/yaml1.yaml
skipping due to exclude match: __tests__/fixtures/yaml/mixture/yaml2.yml
skipping due to exclude match: __tests__/fixtures/yaml/valid/yaml1.yaml
skipping due to exclude match: __tests__/fixtures/yaml_as_json/invalid/yaml1.yaml
skipping due to exclude match: __tests__/fixtures/yaml_as_json/mixture/yaml1.yaml
skipping due to exclude match: __tests__/fixtures/yaml_as_json/mixture/yaml2.yml
skipping due to exclude match: __tests__/fixtures/yaml_as_json/valid/yaml1.yaml
skipping due to exclude match: __tests__/fixtures/yaml/project_dir/schemas/schema.yml
skipping due to exclude match: __tests__/fixtures/yaml/project_dir/data/.github_mock_dir/config.yml
Expand Down
14 changes: 11 additions & 3 deletions src/functions/json-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ export async function jsonValidator(exclude) {
const yamlGlob = `${yamlExtension.replace(
'.',
''
)}, ${yamlExtensionShort.replace('.', '')}`
)},${yamlExtensionShort.replace('.', '')}`

const glob = yamlAsJson
? `**/*{${jsonExtension},${yamlGlob}}`
: `**/*${jsonExtension}`

core.debug(`json - using baseDir: ${baseDirSanitized}`)
core.debug(`json - using glob: ${glob}`)
if (files.length > 0) core.debug(`using files: ${files.join(', ')}`)
else {
core.debug(`using baseDir: ${baseDirSanitized}`)
Expand Down Expand Up @@ -115,9 +117,15 @@ export async function jsonValidator(exclude) {

var data
try {
// try to parse the file
if (fullPath.endsWith('.yaml')) {
// if the file is a yaml file but being treated as json and yamlAsJson is true
if (
yamlAsJson &&
(fullPath.endsWith(yamlExtension) ||
fullPath.endsWith(yamlExtensionShort))
) {
core.debug(`attempting to process yaml file: '${fullPath}' as json`)
data = parse(readFileSync(fullPath, 'utf8'))
// if the file is a json file
} else {
data = JSON.parse(readFileSync(fullPath, 'utf8'))
}
Expand Down
2 changes: 2 additions & 0 deletions src/functions/yaml-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export async function yamlValidator(exclude) {
''
)},${yamlExtensionShort.replace('.', '')}}`

core.debug(`yaml - using baseDir: ${baseDirSanitized}`)
core.debug(`yaml - using glob: ${glob}`)
if (files.length > 0) core.debug(`using files: ${files.join(', ')}`)
else {
core.debug(`using baseDir: ${baseDirSanitized}`)
Expand Down

0 comments on commit ee097ec

Please sign in to comment.