Files
Stirling-PDF/frontend/src/core/tests/translation.test.ts
T
James BruntonandGitHub a3e45bc182 Add frontend autoformatting and set CI to require formatted code for all languages (#6052)
# Description of Changes
Changes the strategy for autoformatting to reject PRs if they are not
formatted correctly instead of allowing them to merge and then spawning
a new PR to fix the formatting. The old strategy just caused more work
for us because we'd have to manually approve the followup PR and get it
merged, which required 2 reviewers so in practice it rarely got done and
just meant everyone's PRs ended up containing reformatting for unrelated
files, which makes code review unnecessarily difficult. If the PR's code
is not formatted correctly after this PR, a comment will be added
automatically to tell the author how to run the formatter script to fix
their code so it can go in.

This also enables autoformatting for the frontend code, using Prettier.
I've enabled it for pretty much everything in the frontend folder, other
than 3rd party files and files it doesn't make sense for. I also
excluded Markdown because it sounds likely to be more annoying to have
to autoformat the Markdown in the frontend folder but nowhere else. Open
to changing this though if people disagree.

> [!note]
> 
> Advice to reviewers: The first commit contains all of the actual logic
I've introduced (CI changes, Prettier config, etc.)
> The second commit is just the reformatting of the entire frontend
folder.
> The first commit needs proper review, the second one just give it a
spot-check that it's doing what you'd expect.
2026-04-10 17:41:19 +01:00

53 lines
1.5 KiB
TypeScript

import { describe, test, expect } from "vitest";
import fs from "fs";
import path from "path";
import { parse } from "smol-toml";
const LOCALES_DIR = path.join(__dirname, "../../../public/locales");
// Get all locale directories for parameterized tests
const getLocaleDirectories = () => {
if (!fs.existsSync(LOCALES_DIR)) {
return [];
}
return fs
.readdirSync(LOCALES_DIR, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
};
const localeDirectories = getLocaleDirectories();
describe("Translation TOML Validation", () => {
test("should find the locales directory", () => {
expect(fs.existsSync(LOCALES_DIR)).toBe(true);
});
test("should have at least one locale directory", () => {
expect(localeDirectories.length).toBeGreaterThan(0);
});
test.each(localeDirectories)("should have valid TOML in %s/translation.toml", (localeDir) => {
const translationFile = path.join(LOCALES_DIR, localeDir, "translation.toml");
// Check if file exists
expect(fs.existsSync(translationFile)).toBe(true);
// Read file content
const content = fs.readFileSync(translationFile, "utf8");
expect(content.trim()).not.toBe("");
// Parse TOML - this will throw if invalid TOML
let tomlData;
expect(() => {
tomlData = parse(content);
}).not.toThrow();
// Ensure it's an object at root level
expect(typeof tomlData).toBe("object");
expect(tomlData).not.toBeNull();
expect(Array.isArray(tomlData)).toBe(false);
});
});