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 1 commit
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
Prev Previous commit
Next Next commit
Migrate image tests
  • Loading branch information
Carlgo11 committed Jun 16, 2024
commit 4e71ff8aaf7d570f78ab32b187c92267a7630320
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
Carlgo11 marked this conversation as resolved.
Show resolved Hide resolved

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.