Skip to content

Commit

Permalink
add working tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Aug 14, 2023
1 parent a0d514d commit 69d97fb
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions __tests__/functions/json-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as core from '@actions/core'
const debugMock = jest.spyOn(core, 'debug').mockImplementation(() => {})
const infoMock = jest.spyOn(core, 'info').mockImplementation(() => {})
const errorMock = jest.spyOn(core, 'error').mockImplementation(() => {})
const warningMock = jest.spyOn(core, 'warning').mockImplementation(() => {})

class Exclude {
isExcluded() {
Expand All @@ -25,6 +26,7 @@ beforeEach(() => {
process.env.INPUT_YAML_EXTENSION = '.yaml'
process.env.INPUT_YAML_EXTENSION_SHORT = '.yml'
process.env.INPUT_FILES = ''
process.env.INPUT_JSON_SCHEMA_VERSION = 'draft-07'
})

test('successfully validates a json file with a schema', async () => {
Expand All @@ -41,6 +43,23 @@ test('successfully validates a json file with a schema', async () => {
)
})

test('successfully validates a json file with a schema and defaults to the draft-07 schema version when none is set', async () => {
const badJsonSchemaVersion = 'evil-draft-999'
process.env.INPUT_JSON_SCHEMA_VERSION = badJsonSchemaVersion
expect(await jsonValidator(excludeMock)).toStrictEqual({
failed: 0,
passed: 1,
skipped: 0,
success: true,
violations: []
})

expect(debugMock).toHaveBeenCalledWith(
'using ajv-formats with json-validator'
)
expect(warningMock).toHaveBeenCalledWith(`json_schema_version '${badJsonSchemaVersion}' is not supported. Defaulting to 'draft-07'`)
})

test('successfully validates a json file without using a schema', async () => {
process.env.INPUT_JSON_SCHEMA = ''
expect(await jsonValidator(excludeMock)).toStrictEqual({
Expand Down Expand Up @@ -299,6 +318,90 @@ test('processes a real world example when yaml_as_json is true and the single fi
)
})

test('processes a real world example when yaml_as_json is true and the single file contains multiple schema errors and uses the 2020 ajv schema', async () => {
process.env.INPUT_YAML_AS_JSON = 'true'
process.env.INPUT_JSON_SCHEMA = '__tests__/fixtures/schemas/challenge.json'
process.env.INPUT_BASE_DIR = '__tests__/fixtures/real_world/challenges'
process.env.INPUT_JSON_SCHEMA_VERSION = 'draft-2020-12'

expect(await jsonValidator(excludeMock)).toStrictEqual({
failed: 1,
passed: 0,
skipped: 0,
success: false,
violations: [
{
file: '__tests__/fixtures/real_world/challenges/challenge.yml',
errors: [
{
path: null,
message: `must have required property 'inputFormat'`
},
{
path: null,
message: `must have required property 'publicTests'`
}
]
}
]
})

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

test('processes a real world example when yaml_as_json is true and the single file contains multiple schema errors and uses the 2019 ajv schema', async () => {
process.env.INPUT_YAML_AS_JSON = 'true'
process.env.INPUT_JSON_SCHEMA = '__tests__/fixtures/schemas/challenge.json'
process.env.INPUT_BASE_DIR = '__tests__/fixtures/real_world/challenges'
process.env.INPUT_JSON_SCHEMA_VERSION = 'draft-2019-09'

expect(await jsonValidator(excludeMock)).toStrictEqual({
failed: 1,
passed: 0,
skipped: 0,
success: false,
violations: [
{
file: '__tests__/fixtures/real_world/challenges/challenge.yml',
errors: [
{
path: null,
message: `must have required property 'inputFormat'`
},
{
path: null,
message: `must have required property 'publicTests'`
}
]
}
]
})

expect(debugMock).toHaveBeenCalledWith(
'using ajv-formats with json-validator'
)
expect(debugMock).toHaveBeenCalledWith(
'json - using baseDir: __tests__/fixtures/real_world/challenges'
)
expect(debugMock).toHaveBeenCalledWith(
'json - using glob: **/*{.json,yaml,yml}'
)
expect(debugMock).toHaveBeenCalledWith(
`attempting to process yaml file: '__tests__/fixtures/real_world/challenges/challenge.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 Down

0 comments on commit 69d97fb

Please sign in to comment.