Skip to content

Commit e42e6ec

Browse files
authored
Merge pull request #76 from GrantBirki/fix-file-validation
Fix file validation in action summary
2 parents 8dca03d + 9c3b7ad commit e42e6ec

File tree

6 files changed

+71
-3
lines changed

6 files changed

+71
-3
lines changed

__tests__/functions/json-validator.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,25 @@ test('successfully validates json files with a schema when files is defined', as
462462
expect(debugMock).toHaveBeenCalledWith(`using files: ${files.join(', ')}`)
463463
})
464464

465+
test('successfully validates json files with a schema when files is defined and there are duplicates', async () => {
466+
const files = [
467+
'__tests__/fixtures/json/valid/json1.json',
468+
'__tests__/fixtures/json/valid/json1.json',
469+
'__tests__/fixtures/json/project_dir/data/config/json1.json'
470+
]
471+
process.env.INPUT_FILES = files.join('\n')
472+
473+
expect(await jsonValidator(excludeMock)).toStrictEqual({
474+
failed: 0,
475+
passed: 2,
476+
skipped: 0,
477+
success: true,
478+
violations: []
479+
})
480+
481+
expect(debugMock).toHaveBeenCalledWith(`using files: ${files.join(', ')}`)
482+
})
483+
465484
test('fails to validate a yaml file with an incorrect schema when yaml_as_json is true', async () => {
466485
process.env.INPUT_YAML_AS_JSON = 'true'
467486
process.env.INPUT_BASE_DIR = '__tests__/fixtures/yaml_as_json/invalid'

__tests__/functions/yaml-validator.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ test('fails to validate a yaml file without using a schema', async () => {
128128
)
129129
})
130130

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

138139
expect(await yamlValidator(excludeMock)).toStrictEqual({
139140
failed: 0,
140-
passed: 2,
141+
passed: 1,
141142
skipped: 0,
142143
success: true,
143144
violations: []

dist/index.js

Lines changed: 24 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/json-validator.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ export async function jsonValidator(exclude) {
149149
.withPromise()
150150
}
151151

152+
// Create a Set to track processed files
153+
const processedFiles = new Set()
154+
152155
for (const fullPath of files) {
153156
core.debug(`found file: ${fullPath}`)
154157

@@ -188,6 +191,12 @@ export async function jsonValidator(exclude) {
188191
continue
189192
}
190193

194+
// Check if the file has already been processed
195+
if (processedFiles.has(fullPath)) {
196+
core.debug(`skipping duplicate file: ${fullPath}`)
197+
continue
198+
}
199+
191200
var data
192201
try {
193202
// if the file is a yaml file but being treated as json and yamlAsJson is true
@@ -250,6 +259,9 @@ export async function jsonValidator(exclude) {
250259
continue
251260
}
252261

262+
// Add the file to the processedFiles Set
263+
processedFiles.add(fullPath)
264+
253265
result.passed++
254266
core.info(`${fullPath} is valid`)
255267
}

src/functions/yaml-validator.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ export async function yamlValidator(exclude) {
6262
.withPromise()
6363
}
6464

65+
// Create a Set to track processed files
66+
const processedFiles = new Set()
67+
6568
for (const fullPath of files) {
6669
core.debug(`found file: ${fullPath}`)
6770

@@ -93,6 +96,12 @@ export async function yamlValidator(exclude) {
9396
continue
9497
}
9598

99+
// Check if the file has already been processed
100+
if (processedFiles.has(fullPath)) {
101+
core.debug(`skipping duplicate file: ${fullPath}`)
102+
continue
103+
}
104+
96105
let multipleDocuments = false
97106

98107
try {
@@ -176,6 +185,9 @@ export async function yamlValidator(exclude) {
176185
continue
177186
}
178187

188+
// Add the file to the processedFiles Set
189+
processedFiles.add(fullPath)
190+
179191
result.passed++
180192
core.info(`${fullPath} is valid`)
181193
}

0 commit comments

Comments
 (0)