Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Ruby tests (part 2) #8101

Merged
merged 10 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,17 @@ jobs:
ruby-version: '3.0'
env:
BUNDLE_WITH: 'tests'
- name: Validate JSON structure
run: bundle exec ruby ./tests/validate-json.rb
- name: Validate Categories
run: bundle exec ruby ./tests/validate-categories.rb
- name: Validate file extensions and permissions
run: tests/validate-fs.sh
- name: Validate Images
run: bundle exec ruby ./tests/validate-images.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

node-tests:
name: JavaScript tests
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -57,12 +46,18 @@ jobs:
id: diff
run: |
echo "::debug:: Fetching files from ${{ github.api_url }}/repos/${{ github.repository }}/pulls/${{ github.event.number }}/files"
FILES=$(curl -s "${{ github.api_url }}/repos/${{ github.repository }}/pulls/${{ github.event.number }}/files" | jq -r '.[].filename' | tr '\n' ' ')
echo "name=${FILES}" >> $GITHUB_OUTPUT
FILES=$(curl -s "${{ github.api_url }}/repos/${{ github.repository }}/pulls/${{ github.event.number }}/files" | jq -r '.[] | select(.status != "removed") | .filename' | tr '\n' ' ')
ENTRIES=$(echo "$FILES" | tr ' ' '\n' | grep -E '^entries/.*\.json$' | tr '\n' ' ')
if [ -n "$ENTRIES" ]; then
echo "entries=${ENTRIES}" >> $GITHUB_OUTPUT
fi
IMAGES=$(echo "$FILES" | tr ' ' '\n' | grep -E '^img/.*$' | tr '\n' ' ')
if [ -n "IMAGES" ]; then
echo "images=${IMAGES}" >> $GITHUB_OUTPUT
fi

- name: Validate file extensions and permissions
run: tests/validate-fs.sh

- name: Validate Language codes
if: steps.diff.outputs.entries
Expand All @@ -72,6 +67,18 @@ jobs:
if: steps.diff.outputs.entries
run: node tests/regions.js ${{ steps.diff.outputs.entries }}

- name: Validate Categories
if: steps.diff.outputs.entries
run: node tests/categories.js ${{ steps.diff.outputs.entries }}

- name: Validate Images
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
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ source 'https://rubygems.org'

group :tests, optional: true do
gem 'activesupport'
gem 'addressable'
gem 'json_schemer'
gem 'nokogiri'
gem 'rubocop'
gem 'twitter'
end

gem 'algolia', '~> 2.3'
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"private": true,
"dependencies": {
"@actions/core": "^1.10.1",
"dotenv": "^16.4.5"
"ajv": "^8.16.0",
"ajv-errors": "^3.0.0",
"ajv-formats": "^3.0.1",
"glob": "^10.4.1"
}
}
37 changes: 37 additions & 0 deletions tests/categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require('fs').promises;
const core = require('@actions/core');

async function main() {
let errors = false;
const files = process.argv.slice(2);
const res = await fetch(
'https://raw.githubusercontent.com/2factorauth/frontend/master/data/categories.json',
{
'accept': 'application/json',
'user-agent': '2factorauth/twofactorauth +https://2fa.directory/bots',
});

if (!res.ok) throw new Error('Unable to fetch categories');

const data = await res.json();
const allowed_categories = Object.keys(data);

if (files) {
for (const file of files) {
const data = await fs.readFile(file, 'utf8');
const json = await JSON.parse(data);
const entry = json[Object.keys(json)[0]];
const {categories} = entry;

for (const category of categories || []) {
if (!allowed_categories.includes(category)) {
core.error(`${category} is not a valid category.`,{file});
errors = true;
}
}
}
}
process.exit(+errors);
}

module.exports = main();
70 changes: 70 additions & 0 deletions tests/images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const fs = require('fs').promises;
const core = require('@actions/core');
const {glob} = require('glob');

// Allowed image dimensions
const PNG_RES = [
[16, 16], [32, 32], [64, 64], [128, 128]];

let seen_images = [];
let errors = false;

async function main() {

await parseEntries(await glob('entries/**/*.json'));

await parseImages(await glob('img/*/*.*'));

process.exit(+errors);
}

async function parseEntries(entries) {
for (const file of entries) {
const data = await fs.readFile(file, 'utf8');
const json = await JSON.parse(data);
const entry = json[Object.keys(json)[0]];
const {img, domain} = entry;
const path = `img/${img ? `${img[0]}/${img}`:`${domain[0]}/${domain}.svg`}`;

try {
await fs.readFile(path);
} catch (e) {
core.error(`Image ${path} not found.`, {file});
errors = true;
}
seen_images.push(path);
}
}

async function parseImages(images) {
for (const image of images) {
if (!seen_images.includes(image)) {
core.error(`Unused image`, {file: image});
errors = true;
}

if (image.endsWith('.png')) {
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;
}
}
}
}

function dimensionsAreValid(dimensions, validSizes) {
return validSizes.some(
size => size[0] === dimensions[0] && size[1] === dimensions[1]);
}

async function getPNGDimensions(file) {
const buffer = await fs.readFile(file);
if (buffer.toString('ascii', 1, 4) !== 'PNG') throw new Error(
`${file} is not a valid PNG file`);

// Return [width, height]
return [buffer.readUInt32BE(16), buffer.readUInt32BE(20)];
}

main().catch(e => core.setFailed(e));
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
20 changes: 0 additions & 20 deletions tests/validate-categories.rb

This file was deleted.

43 changes: 0 additions & 43 deletions tests/validate-images.rb

This file was deleted.

Loading