Skip to content

Commit

Permalink
Migrate JSON tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed Jun 16, 2024
1 parent 1b411ec commit bdf7807
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 109 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ jobs:
ruby-version: '3.0'
env:
BUNDLE_WITH: 'tests'
- name: Validate JSON structure
run: bundle exec ruby ./tests/validate-json.rb
- name: Validate SVG
run: bundle exec ruby ./tests/svg-lint.rb
- name: Validate URL/Domain reachability
run: bundle exec ruby ./tests/validate-urls.rb
continue-on-error: true
- name: Quality Checks
run: bundle exec ruby ./tests/quality-checks.rb
- name: Validate Ruby scripts
run: bundle exec rubocop

Expand Down Expand Up @@ -79,6 +75,10 @@ jobs:
if: ${{ steps.diff.outputs.entries || steps.diff.outputs.images }}
run: node ./tests/images.js

- name: Validate JSON structure
if: steps.diff.outputs.entries
run: node tests/json.js ${{ steps.diff.outputs.entries }}

external-tests:
name: External Tests
runs-on: ubuntu-latest
Expand Down
8 changes: 1 addition & 7 deletions tests/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ async function parseEntries(entries) {
core.error(`Image ${path} not found.`, {file});
errors = true;
}

if (img && img === `${domain}.svg`) {
core.error(
`Defining the img property for ${domain} is not necessary. ${img} is the default value.`,
{file});
errors = true;
}
seen_images.push(path);
}
}
Expand All @@ -54,6 +47,7 @@ async function parseImages(images) {
if (!dimensionsAreValid(await getPNGDimensions(image), PNG_RES)) {
core.error(`PNGs must be one of the following dimensions: ${PNG_RES.map(
a => a.join('x')).join(', ')}`, {file: image});
errors = true;
}
}
}
Expand Down
97 changes: 97 additions & 0 deletions tests/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const fs = require('fs').promises;
const core = require('@actions/core');
const Ajv = require('ajv');
const addFormats = require('ajv-formats');
const schema = require('./schema.json');
const {basename} = require('node:path');

const ajv = new Ajv({strict: false, allErrors: true});
addFormats(ajv);
require('ajv-errors')(ajv);

const validate = ajv.compile(schema);
let errors = false;

/**
* Logs an error message and sets the errors flag to true.
*
* @param {string} message - The error message to log.
* @param {object} properties - Additional properties to log with the error.
*/
function error(message, properties) {
core.error(message, properties);
errors = true;
}

async function main() {
const files = process.argv.slice(2);

await Promise.all(files.map(async (file) => {
try {
const json = await JSON.parse(await fs.readFile(file, 'utf8'));
const entry = json[Object.keys(json)[0]];
validateJSONSchema(file, json);
validateFileContents(file, entry);
} catch (e) {
error(`Failed to process ${file}: ${err.message}`, {file});
}
}));

process.exit(+errors);
}

/**
* Validates the JSON schema of the provided file.
*
* @param {string} file - File path to be validated.
* @param {object} json - Parsed JSON content of the file.
*/
function validateJSONSchema(file, json) {
const valid = validate(json);
if (!valid) {
errors = true;
validate.errors.forEach((err) => {
const {message, instancePath, keyword: title} = err;
const instance = instancePath?.split('/');
if (message)
error(`${instance[instance.length - 1]} ${message}`, {file, title});
else
error(err, {file});
});
}
}

/**
* Validates the contents of the provided file according to custom rules.
*
* @param {string} file - File path to be validated.
* @param {object} entry - The main entry object within the JSON content.
*/
function validateFileContents(file, entry) {
const valid_name = `${entry.domain}.json`;

if (basename(file) !== valid_name)
error(`File name should be ${valid_name}`, {file, title: 'File name'});

if (entry.url === `https://${entry.domain}`)
error(`Unnecessary url element defined.`, {file});

if (entry.img === `${entry.domain}.svg`)
error(`Unnecessary img element defined.`, {file});

if (file !== `entries/${entry.domain[0]}/${valid_name}`)
error(`Entry should be placed in entries/${entry.domain[0]}/`, {file});

if (entry.tfa?.includes('custom-software') && !entry['custom-software'])
error('Missing custom-software element', {file});

if (entry.tfa?.includes('custom-hardware') && !entry['custom-hardware'])
error('Missing custom-hardware element', {file});

if (entry.tfa && !entry.documentation)
core.warning(
'No documentation set. Please provide screenshots in the pull request',
{file, title: 'Missing documentation'});
}

module.exports = main();
18 changes: 0 additions & 18 deletions tests/quality-checks.rb

This file was deleted.

3 changes: 1 addition & 2 deletions tests/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
},
"documentation": {
"type": "string",
"format": "uri-reference",
"pattern": "^(https:|http:)"
"format": "uri"
},
"recovery": {
"type": "string",
Expand Down
78 changes: 0 additions & 78 deletions tests/validate-json.rb

This file was deleted.

0 comments on commit bdf7807

Please sign in to comment.