Skip to content

Commit

Permalink
Adds test suite and CI to run them on PR
Browse files Browse the repository at this point in the history
  • Loading branch information
versecafe authored and ADKaster committed Jul 7, 2024
1 parent 02bcf8f commit 0107355
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Build & Test

on:
# Trigger the workflow every time a push is made to 'master'
push:
branches: [master]
pull_request:
branches: [master]
# Allows you to run this workflow manually from the Actions tab on GitHub.
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout your repository using git
uses: actions/checkout@v4
- name: Install & Build
uses: withastro/action@v2
- name: Test
run: bun test
9 changes: 3 additions & 6 deletions generate-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ fs.readdir(srcDir, (err, files) => {
files.forEach((file) => {
if (!file.startsWith("index.")) {
const fileNameWithoutExt = path.parse(file).name;
const htmlContent = `<meta charset="utf-8" />
const htmlContent = `<meta charset="utf-8"/>
<title>Redirecting to https://ladybird.org/${fileNameWithoutExt}/</title>
<meta
http-equiv="refresh"
content="0; URL=http://ladybird.org/${fileNameWithoutExt}/"
/>
<link rel="canonical" href="http://ladybird.org/${fileNameWithoutExt}/" />`;
<meta http-equiv="refresh" content="0; URL=http://ladybird.org/${fileNameWithoutExt}/"/>
<link rel="canonical" href="http://ladybird.org/${fileNameWithoutExt}/"/>`;
const distFilePath = path.join(distDir, `${fileNameWithoutExt}.html`);

fs.writeFile(distFilePath, htmlContent, (err) => {
Expand Down
36 changes: 36 additions & 0 deletions tests/markdown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect, test, describe } from "bun:test";
import fs from "fs";
import path from "path";

const rootDir: string = path.join(__dirname, "../");

describe("Markdown Rendering", () => {
const srcDir = path.join(rootDir, "/src/pages");
const distDir = path.join(rootDir, "/dist");

const mdFiles = fs.readdirSync(srcDir).filter((file) => file.endsWith(".md"));
//<h1>{frontmatter.title}</h1>
test("Pages Exist", () => {
mdFiles.forEach((file) => {
const htmlFile = file.replace(".md", ".html");
const htmlFilePath = path.join(distDir, htmlFile);
expect(fs.existsSync(htmlFilePath)).toBe(true);
});
});

test("Layouts Applied", () => {
mdFiles.forEach((file) => {
const htmlFile = file.replace(".md", "/index.html");
const htmlFilePath = path.join(distDir, htmlFile);
const htmlContent = fs.readFileSync(htmlFilePath, "utf-8");
const fileContent = fs.readFileSync(path.join(srcDir, file), "utf-8");
const frontMatterMatch = fileContent.match(/^title:\s*(.*)$/m);
const frontMatterTitle = frontMatterMatch
? frontMatterMatch[1]
: file.replace(".md", "");
const h1Tag = `<h1>${frontMatterTitle}</h1>`;

expect(htmlContent.includes(h1Tag)).toBe(true);
});
});
});
33 changes: 33 additions & 0 deletions tests/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect, test, describe } from "bun:test";
import fs from "fs";
import path from "path";

const rootDir: string = path.join(__dirname, "../");

describe("Middleware Compilation", () => {
const srcDir = path.join(rootDir, "/src/pages");
const distDir = path.join(rootDir, "/dist");

const mdFiles = fs.readdirSync(srcDir).filter((file) => file.endsWith(".md"));

test("HTML Redirects Exist", () => {
mdFiles.forEach((file) => {
const htmlFile = file.replace(".md", ".html");
const htmlFilePath = path.join(distDir, htmlFile);
expect(fs.existsSync(htmlFilePath)).toBe(true);
});
});

test("Redirects Work", () => {
mdFiles.forEach((file) => {
const htmlFile = file.replace(".md", ".html");
const htmlFilePath = path.join(distDir, htmlFile);
const htmlContent = fs.readFileSync(htmlFilePath, "utf-8");
const metaTag = `<meta http-equiv="refresh" content="0; URL=http://ladybird.org/${file.replace(".md", "")}/"/>`;
const linkTag = `<link rel="canonical" href="http://ladybird.org/${file.replace(".md", "")}/"/>`;

expect(htmlContent.includes(metaTag)).toBe(true);
expect(htmlContent.includes(linkTag)).toBe(true);
});
});
});

0 comments on commit 0107355

Please sign in to comment.