Skip to content

Commit

Permalink
Add tests for navbar
Browse files Browse the repository at this point in the history
  • Loading branch information
penelopeysm committed Feb 1, 2025
1 parent d101b5c commit e90fbd8
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 3 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/test_navbar_script.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow runs the insert_navbar script on several test HTML inputs,
# making sure that the output is as expected.

name: Test Navbar Script

on: [push, pull_request] # Runs on push and PRs

jobs:
test-html-diff:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Copy script to GHA scratch directory
run: cp DocsNav/scripts/insert_navbar.sh .github/workflows/test_navbar_script

- name: Compare all test inputs against out.html
working-directory: .github/workflows/test_navbar_script
run: |
# Run script on all files in `ins_html` directory
./insert_navbar.sh ./ins_html ./test_navbar.html
# Expected output
OUT="expected_out.html"
# Check if all files in `ins_html` match the expected output
for file in ins_html/*; do
if diff <(tr -d '[:space:]' < "$file") <(tr -d '[:space:]' < $OUT) > /dev/null; then
echo "✅ $file matches $OUT (ignoring whitespace)"
else
echo "❌ $file differs from $OUT"
exit 1 # Fail the job if any file is different
fi
done
7 changes: 7 additions & 0 deletions .github/workflows/test_navbar_script/expected_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<p>This is a navbar!</p>
<h1>Test 1</h1>
<p>Test 1</p>
</body>
</html>
6 changes: 6 additions & 0 deletions .github/workflows/test_navbar_script/ins_html/test1_in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<h1>Test 1</h1>
<p>Test 1</p>
</body>
</html>
4 changes: 4 additions & 0 deletions .github/workflows/test_navbar_script/ins_html/test2_in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
<body><h1>Test 1</h1><p>Test 1</p>
</body>
</html>
5 changes: 5 additions & 0 deletions .github/workflows/test_navbar_script/ins_html/test3_in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html><body>
<h1>Test 1</h1>
<p>Test 1</p>
</body>
</html>
2 changes: 2 additions & 0 deletions .github/workflows/test_navbar_script/ins_html/test4_in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<html><body><h1>Test 1</h1><p>Test 1</p>
</body></html>
1 change: 1 addition & 0 deletions .github/workflows/test_navbar_script/test_navbar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>This is a navbar!</p>
4 changes: 1 addition & 3 deletions DocsNav/scripts/insert_navbar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ find "$HTML_DIR" -type f -name "*.html" | while read -r file; do
# Insert the new navbar right after the first <body> tag using awk
awk -v nav="$NAVBAR_HTML" '
/<body>/ {
print $0
print nav
next
sub(/(<body[^>]*>)/, "&\n" nav);
}
{ print }
' "$file" > temp && mv temp "$file"
Expand Down

0 comments on commit e90fbd8

Please # to comment.