Lesson 2 of 6
Add a post-commit test
Write a test script that checks the page text and add it as a Git post-commit hook.
13 minDevOps
After this lesson you will be able to write a small test script that checks your site's content, and run it automatically after every commit.
Why test on commit?
A static site is a set of files that a web server sends to visitors exactly as they are. If a file is missing or has the wrong content, the site breaks. You can catch many mistakes by checking the files after each commit. A test script does this for you.
Git has a feature called hooks: scripts that run at certain points in the Git workflow. The post-commit hook runs after a commit is made. You can use it to run your tests automatically.
Write a test script
You will write a script that checks that the home page contains the heading and paragraph you wrote in lesson 1. Create a file called test.sh in your my-site folder:
touch test.shOpen test.sh in your editor and add these lines:
#!/bin/bash
set -e
if ! grep -q "Welcome to My Site" index.html; then
echo "Error: heading not found in index.html"
exit 1
fi
if ! grep -q "This is a simple static website." index.html; then
echo "Error: paragraph not found in index.html"
exit 1
fi
echo "All tests passed"The first line tells the system to run this file with Bash. set -e makes the script stop if any command fails. The grep command searches for text in a file. The -q option makes it quiet, so it only returns success or failure. The ! in front of grep means "if the command fails". So the first if checks that the heading is not in index.html. If it is not there, the script prints an error and exits with code 1, which means failure. The second if does the same for the paragraph. If both checks pass, the script prints "All tests passed".
Make the script executable:
chmod +x test.shRun it to see that it works:
./test.shYou should see All tests passed.
Add the post-commit hook
Git stores hooks in the .git/hooks folder. The post-commit hook is a file called post-commit in that folder. If the file exists and is executable, Git runs it after each commit.
Create the hook file:
touch .git/hooks/post-commitOpen .git/hooks/post-commit in your editor and add:
#!/bin/bash
./test.shThis tells Git to run your test script after each commit. Make the hook executable:
chmod +x .git/hooks/post-commitNow make a small change to index.html to test the hook. Change the paragraph text to something else, for example:
<p>This is a simple static website. Updated.</p>Stage and commit the change:
git add index.htmlgit commit -m "Update paragraph"After the commit, you should see the output from the test script: All tests passed. The hook ran automatically.
Now change the paragraph back to the original text and commit again:
<p>This is a simple static website.</p>git add index.htmlgit commit -m "Revert paragraph"Again, the test runs and passes.
What if a test fails?
To see what happens when a test fails, temporarily change the heading in index.html to something else, for example:
<h1>Welcome</h1>Commit the change:
git add index.htmlgit commit -m "Break heading"The commit still happens, but the hook prints an error: Error: heading not found in index.html. The commit is recorded, but you know there is a problem. Change the heading back to Welcome to My Site and commit again:
<h1>Welcome to My Site</h1>git add index.htmlgit commit -m "Fix heading"The test passes again.
Keep the hook in your repository
The .git/hooks folder is not tracked by Git, so if you clone the repository elsewhere, the hook will not be there. For this course, you work on one machine, so that is fine. If you want to share the hook, you can put the script in a tracked folder and have the hook call it. You already have test.sh tracked, so you could change the hook to call ./test.sh from the repository root. That is what you did. The hook file itself remains untracked, but it is a small file that calls your tracked script.
You have now added an automatic test that runs after every commit. In the next lesson, you will package the site into an archive for deployment.
Key takeaways
- A post-commit hook is a script in
.git/hooks/post-committhat Git runs after each commit. - The hook can call a test script that checks your site files.
- Use
grep -qto check for text in a file without printing output. - A failing test does not undo the commit, but it tells you something is wrong.
- The hook file itself is not tracked by Git, but it can call a tracked script.
Try it
Add a test to test.sh that checks that the about.html page you created in the lesson 1 exercise contains the heading "About". Run the script to make sure it passes. Then make a temporary change to about.html that removes the heading, commit it, and observe that the hook fails. Finally, restore the heading, commit again, and confirm the test passes. You should see the error message from your new test when the heading is missing, and "All tests passed" after you fix it.
Official documentation
Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.