Skip to content

Commit ee097ec

Browse files
authored
Merge pull request #26 from GrantBirki/json-as-yaml-fix
JSON as YAML - Bug Fix
2 parents 47d4be5 + ebc3ac1 commit ee097ec

File tree

10 files changed

+85
-9
lines changed

10 files changed

+85
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "world"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"bad data": 'oh no'
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo: 1
2+
bar: abc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo: 1
2+
bar: abc

__tests__/functions/json-validator.test.js

+44-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ beforeEach(() => {
2222
process.env.INPUT_YAML_AS_JSON = 'false'
2323
process.env.INPUT_USE_DOT_MATCH = 'true'
2424
process.env.INPUT_USE_AJV_FORMATS = true
25+
process.env.INPUT_YAML_EXTENSION = '.yaml'
26+
process.env.INPUT_YAML_EXTENSION_SHORT = '.yml'
2527
process.env.INPUT_FILES = ''
2628
})
2729

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

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

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

217+
test('processes multiple files when yaml_as_json is true and also a mixture of other json files with yaml are present', async () => {
218+
process.env.INPUT_YAML_AS_JSON = 'true'
219+
process.env.INPUT_JSON_SCHEMA = ''
220+
process.env.INPUT_BASE_DIR = '__tests__/fixtures/yaml_as_json/mixture'
221+
222+
expect(await jsonValidator(excludeMock)).toStrictEqual({
223+
failed: 1,
224+
passed: 3,
225+
skipped: 0,
226+
success: false,
227+
violations: [
228+
{
229+
file: '__tests__/fixtures/yaml_as_json/mixture/invalid-json.json',
230+
errors: [
231+
{
232+
path: null,
233+
message: 'Invalid JSON'
234+
}
235+
]
236+
}
237+
]
238+
})
239+
240+
expect(debugMock).toHaveBeenCalledWith(
241+
'using ajv-formats with json-validator'
242+
)
243+
expect(debugMock).toHaveBeenCalledWith(
244+
'json - using baseDir: __tests__/fixtures/yaml_as_json/mixture'
245+
)
246+
expect(debugMock).toHaveBeenCalledWith(
247+
'json - using glob: **/*{.json,yaml,yml}'
248+
)
249+
expect(debugMock).toHaveBeenCalledWith(
250+
`attempting to process yaml file: '__tests__/fixtures/yaml_as_json/mixture/yaml1.yaml' as json`
251+
)
252+
expect(debugMock).toHaveBeenCalledWith(
253+
`attempting to process yaml file: '__tests__/fixtures/yaml_as_json/mixture/yaml2.yml' as json`
254+
)
255+
})
256+
215257
test('successfully validates json files with a schema when files is defined', async () => {
216258
const files = [
217259
'__tests__/fixtures/json/valid/json1.json',
@@ -231,7 +273,7 @@ test('successfully validates json files with a schema when files is defined', as
231273
})
232274

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

237279
expect(await jsonValidator(excludeMock)).toStrictEqual({

dist/index.js

+13-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/acceptance-test-results.txt

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ skipping due to exclude match: __tests__/fixtures/json/invalid/skip-bad.json
1111
skipping due to exclude match: __tests__/fixtures/json/mixture/json1.json
1212
skipping due to exclude match: __tests__/fixtures/json/mixture/json2.json
1313
skipping due to exclude match: __tests__/fixtures/json/valid/json1.json
14+
skipping due to exclude match: __tests__/fixtures/yaml_as_json/mixture/good-json.json
15+
skipping due to exclude match: __tests__/fixtures/yaml_as_json/mixture/invalid-json.json
1416
skipping due to exclude match: __tests__/fixtures/json/project_dir/schemas/schema.json
1517
skipping due to exclude match: __tests__/fixtures/json/project_dir/data/config/json1.json
1618
skipping due to exclude match: __tests__/fixtures/json/project_dir/data/config/.github_mock_dir/json2.json
@@ -30,6 +32,8 @@ skipping due to exclude match: __tests__/fixtures/yaml/mixture/yaml1.yaml
3032
skipping due to exclude match: __tests__/fixtures/yaml/mixture/yaml2.yml
3133
skipping due to exclude match: __tests__/fixtures/yaml/valid/yaml1.yaml
3234
skipping due to exclude match: __tests__/fixtures/yaml_as_json/invalid/yaml1.yaml
35+
skipping due to exclude match: __tests__/fixtures/yaml_as_json/mixture/yaml1.yaml
36+
skipping due to exclude match: __tests__/fixtures/yaml_as_json/mixture/yaml2.yml
3337
skipping due to exclude match: __tests__/fixtures/yaml_as_json/valid/yaml1.yaml
3438
skipping due to exclude match: __tests__/fixtures/yaml/project_dir/schemas/schema.yml
3539
skipping due to exclude match: __tests__/fixtures/yaml/project_dir/data/.github_mock_dir/config.yml

src/functions/json-validator.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ export async function jsonValidator(exclude) {
7171
const yamlGlob = `${yamlExtension.replace(
7272
'.',
7373
''
74-
)}, ${yamlExtensionShort.replace('.', '')}`
74+
)},${yamlExtensionShort.replace('.', '')}`
7575

7676
const glob = yamlAsJson
7777
? `**/*{${jsonExtension},${yamlGlob}}`
7878
: `**/*${jsonExtension}`
7979

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

116118
var data
117119
try {
118-
// try to parse the file
119-
if (fullPath.endsWith('.yaml')) {
120+
// if the file is a yaml file but being treated as json and yamlAsJson is true
121+
if (
122+
yamlAsJson &&
123+
(fullPath.endsWith(yamlExtension) ||
124+
fullPath.endsWith(yamlExtensionShort))
125+
) {
126+
core.debug(`attempting to process yaml file: '${fullPath}' as json`)
120127
data = parse(readFileSync(fullPath, 'utf8'))
128+
// if the file is a json file
121129
} else {
122130
data = JSON.parse(readFileSync(fullPath, 'utf8'))
123131
}

src/functions/yaml-validator.js

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export async function yamlValidator(exclude) {
3838
''
3939
)},${yamlExtensionShort.replace('.', '')}}`
4040

41+
core.debug(`yaml - using baseDir: ${baseDirSanitized}`)
42+
core.debug(`yaml - using glob: ${glob}`)
4143
if (files.length > 0) core.debug(`using files: ${files.join(', ')}`)
4244
else {
4345
core.debug(`using baseDir: ${baseDirSanitized}`)

0 commit comments

Comments
 (0)