Skip to content

Commit

Permalink
run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
k.panithi committed Nov 8, 2023
1 parent a62e2a9 commit 5b538b5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
28 changes: 16 additions & 12 deletions __tests__/functions/yaml-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ test('fails to validate a yaml file without using a schema', async () => {
{
path: null,
message: 'Invalid YAML',
error: "YAMLParseError Nested mappings are not allowed in compact mappings at line 4, column 17"
error:
'YAMLParseError Nested mappings are not allowed in compact mappings at line 4, column 17'
}
]
}
Expand Down Expand Up @@ -248,16 +249,19 @@ test('successfully validates a yaml file with multiple documents but fails on th
passed: 1,
skipped: 0,
success: false,
violations: [{
file: '__tests__/fixtures/yaml/multiple/invalid.yaml',
errors: [
{
path: null,
message: 'Invalid YAML',
error: 'YAMLParseError Nested mappings are not allowed in compact mappings at line 13, column 9'
}
]
}]
violations: [
{
file: '__tests__/fixtures/yaml/multiple/invalid.yaml',
errors: [
{
path: null,
message: 'Invalid YAML',
error:
'YAMLParseError Nested mappings are not allowed in compact mappings at line 13, column 9'
}
]
}
]
})
expect(infoMock).toHaveBeenCalledWith(
`multiple documents found in file: __tests__/fixtures/yaml/multiple/yaml1.yaml`
Expand All @@ -267,4 +271,4 @@ test('successfully validates a yaml file with multiple documents but fails on th
'❌ failed to parse YAML file: __tests__/fixtures/yaml/multiple/invalid.yaml'
)
)
})
})
29 changes: 15 additions & 14 deletions src/functions/yaml-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from '@actions/core'
import validateSchema from 'yaml-schema-validator'
import {readFileSync} from 'fs'
import {fdir} from 'fdir'
import {parse,parseAllDocuments} from 'yaml'
import {parse, parseAllDocuments} from 'yaml'

// Helper function to validate all yaml files in the baseDir
export async function yamlValidator(exclude) {
Expand All @@ -13,7 +13,9 @@ export async function yamlValidator(exclude) {
const yamlExcludeRegex = core.getInput('yaml_exclude_regex')
const yamlAsJson = core.getBooleanInput('yaml_as_json')
const useDotMatch = core.getBooleanInput('use_dot_match')
const allowMultipleDocuments = core.getBooleanInput('allow_multiple_documents')
const allowMultipleDocuments = core.getBooleanInput(
'allow_multiple_documents'
)
let files = core.getMultilineInput('files').filter(Boolean)

// remove trailing slash from baseDir
Expand Down Expand Up @@ -89,21 +91,20 @@ export async function yamlValidator(exclude) {
try {
// try to parse the yaml file
if (allowMultipleDocuments) {
let documents = parseAllDocuments(readFileSync(fullPath, 'utf8'))
for (let doc of documents) {
if (doc.errors.length > 0) {
// format and show the first error
throw doc.errors[0]
}
parse(doc.toString()) // doc.toString() will throw an error if the document is invalid
let documents = parseAllDocuments(readFileSync(fullPath, 'utf8'))
for (let doc of documents) {
if (doc.errors.length > 0) {
// format and show the first error
throw doc.errors[0]
}
core.info(`multiple documents found in file: ${fullPath}`)
multipleDocuments = true
}
else {
parse(doc.toString()) // doc.toString() will throw an error if the document is invalid
}
core.info(`multiple documents found in file: ${fullPath}`)
multipleDocuments = true
} else {
parse(readFileSync(fullPath, 'utf8'))
}
} catch(err) {
} catch (err) {
// if the yaml file is invalid, log an error and set success to false
core.error(`❌ failed to parse YAML file: ${fullPath}`)
result.success = false
Expand Down

0 comments on commit 5b538b5

Please sign in to comment.