Skip to content

Commit

Permalink
Migrate category test
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed Jun 15, 2024
1 parent f3b2c62 commit 23bde2c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ jobs:
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
Expand Down Expand Up @@ -64,6 +61,9 @@ jobs:
echo "entries=${ENTRIES}" >> $GITHUB_OUTPUT
fi
- name: Validate file extensions and permissions
run: tests/validate-fs.sh

- name: Validate Language codes
if: steps.diff.outputs.entries
run: node tests/languages.js ${{ steps.diff.outputs.entries }}
Expand All @@ -72,6 +72,10 @@ 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 }}

external-tests:
name: External Tests
runs-on: ubuntu-latest
Expand Down
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();
20 changes: 0 additions & 20 deletions tests/validate-categories.rb

This file was deleted.

0 comments on commit 23bde2c

Please sign in to comment.