Skip to content

Commit

Permalink
Merge pull request #76 from GrantBirki/fix-file-validation
Browse files Browse the repository at this point in the history
Fix file validation in action summary
  • Loading branch information
GrantBirki authored Aug 24, 2024
2 parents 8dca03d + 9c3b7ad commit e42e6ec
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
19 changes: 19 additions & 0 deletions __tests__/functions/json-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,25 @@ test('successfully validates json files with a schema when files is defined', as
expect(debugMock).toHaveBeenCalledWith(`using files: ${files.join(', ')}`)
})

test('successfully validates json files with a schema when files is defined and there are duplicates', async () => {
const files = [
'__tests__/fixtures/json/valid/json1.json',
'__tests__/fixtures/json/valid/json1.json',
'__tests__/fixtures/json/project_dir/data/config/json1.json'
]
process.env.INPUT_FILES = files.join('\n')

expect(await jsonValidator(excludeMock)).toStrictEqual({
failed: 0,
passed: 2,
skipped: 0,
success: true,
violations: []
})

expect(debugMock).toHaveBeenCalledWith(`using files: ${files.join(', ')}`)
})

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_BASE_DIR = '__tests__/fixtures/yaml_as_json/invalid'
Expand Down
5 changes: 3 additions & 2 deletions __tests__/functions/yaml-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ test('fails to validate a yaml file without using a schema', async () => {
)
})

test('successfully validates yaml files with a schema when files is defined', async () => {
test('successfully validates yaml files with a schema when files is defined and there are duplicates', async () => {
// this file should only be validated once and not duplicated
const files = [
'__tests__/fixtures/yaml/valid/yaml1.yaml',
'__tests__/fixtures/yaml/valid/yaml1.yaml'
Expand All @@ -137,7 +138,7 @@ test('successfully validates yaml files with a schema when files is defined', as

expect(await yamlValidator(excludeMock)).toStrictEqual({
failed: 0,
passed: 2,
passed: 1,
skipped: 0,
success: true,
violations: []
Expand Down
24 changes: 24 additions & 0 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.

12 changes: 12 additions & 0 deletions src/functions/json-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ export async function jsonValidator(exclude) {
.withPromise()
}

// Create a Set to track processed files
const processedFiles = new Set()

for (const fullPath of files) {
core.debug(`found file: ${fullPath}`)

Expand Down Expand Up @@ -188,6 +191,12 @@ export async function jsonValidator(exclude) {
continue
}

// Check if the file has already been processed
if (processedFiles.has(fullPath)) {
core.debug(`skipping duplicate file: ${fullPath}`)
continue
}

var data
try {
// if the file is a yaml file but being treated as json and yamlAsJson is true
Expand Down Expand Up @@ -250,6 +259,9 @@ export async function jsonValidator(exclude) {
continue
}

// Add the file to the processedFiles Set
processedFiles.add(fullPath)

result.passed++
core.info(`${fullPath} is valid`)
}
Expand Down
12 changes: 12 additions & 0 deletions src/functions/yaml-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export async function yamlValidator(exclude) {
.withPromise()
}

// Create a Set to track processed files
const processedFiles = new Set()

for (const fullPath of files) {
core.debug(`found file: ${fullPath}`)

Expand Down Expand Up @@ -93,6 +96,12 @@ export async function yamlValidator(exclude) {
continue
}

// Check if the file has already been processed
if (processedFiles.has(fullPath)) {
core.debug(`skipping duplicate file: ${fullPath}`)
continue
}

let multipleDocuments = false

try {
Expand Down Expand Up @@ -176,6 +185,9 @@ export async function yamlValidator(exclude) {
continue
}

// Add the file to the processedFiles Set
processedFiles.add(fullPath)

result.passed++
core.info(`${fullPath} is valid`)
}
Expand Down

0 comments on commit e42e6ec

Please sign in to comment.