Skip to content

Commit

Permalink
Migrate image tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed Jun 16, 2024
1 parent 23bde2c commit 4e71ff8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 46 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ jobs:
BUNDLE_WITH: 'tests'
- name: Validate JSON structure
run: bundle exec ruby ./tests/validate-json.rb

- 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
Expand Down Expand Up @@ -76,6 +73,9 @@ jobs:
if: steps.diff.outputs.entries
run: node tests/categories.js ${{ steps.diff.outputs.entries }}

- name: Validate Images
run: node ./tests/images.js

external-tests:
name: External Tests
runs-on: ubuntu-latest
Expand Down
76 changes: 76 additions & 0 deletions tests/images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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;
}

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);
}
}

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});
}
}
}
}

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));
43 changes: 0 additions & 43 deletions tests/validate-images.rb

This file was deleted.

0 comments on commit 4e71ff8

Please sign in to comment.