Open Malnati opened 8 months ago
Below is a Bash script to test your commit-msg
hook. This script will create a temporary Git repository, make some changes, and then commit them. It will then manually invoke your commit-msg
hook to verify its functionality.
#!/bin/bash
# Test Script for commit-msg
# Author: Ricardo Malnati
# Creation Date: 2023-10-13
# Description: This script tests the commit-msg hook by creating a temporary Git repository,
# making some changes, and then committing them.
# Create a temporary directory and initialize a Git repository
temp_dir=$(mktemp -d -t commit-msg-test-XXXX)
git init "$temp_dir"
# Copy the commit-msg hook into the temporary Git repository
cp /path/to/your/commit-msg "$temp_dir/.git/hooks/"
chmod +x "$temp_dir/.git/hooks/commit-msg"
# Change to the temporary directory
cd "$temp_dir"
# Create a test file and add some content
echo "This is a test file." > test.txt
# Add and commit the changes
git add test.txt
COMMIT_MSG=$(echo "Initial commit" | tee /dev/tty)
echo "$COMMIT_MSG" > .git/COMMIT_EDITMSG
git commit -F .git/COMMIT_EDITMSG
# Run the commit-msg hook manually (this should be done automatically during commit in real use)
sh .git/hooks/commit-msg .git/COMMIT_EDITMSG
# Display the updated commit message
echo "Updated Commit Message:"
cat .git/COMMIT_EDITMSG
# Cleanup
rm -rf "$temp_dir"
To run this script, save it to a file, give it execute permission (chmod +x <filename>
), and then run it. Make sure to run it as the same user that would run commit-msg
to ensure environment consistency.
Note: Replace /path/to/your/commit-msg
with the actual path to your commit-msg
script.
Below is a Bash script to test your auto-commit-msg
utility. This script will simulate the behavior of your utility by creating a temporary Git repository, making some changes, and then committing them. It will then call your auto-commit-msg
script to see if it works as expected.
#!/bin/bash
# Test Script for auto-commit-msg
# Author: Ricardo Malnati
# Creation Date: 2023-10-13
# Description: This script tests the auto-commit-msg utility by creating a temporary Git repository,
# making some changes, and then committing them.
# Create a temporary directory and initialize a Git repository
temp_dir=$(mktemp -d -t auto-commit-msg-test-XXXX)
git init "$temp_dir"
# Copy the commit-msg hook into the temporary Git repository
cp /etc/autocommitmsg/commit-msg "$temp_dir/.git/hooks/"
chmod +x "$temp_dir/.git/hooks/commit-msg"
# Change to the temporary directory
cd "$temp_dir"
# Create a test file and add some content
echo "This is a test file." > test.txt
# Add and commit the changes
git add test.txt
COMMIT_MSG=$(echo "Initial commit" | tee /dev/tty)
echo "$COMMIT_MSG" > .git/COMMIT_EDITMSG
git commit -F .git/COMMIT_EDITMSG
# Run the commit-msg hook manually (this should be done automatically during commit in real use)
sh .git/hooks/commit-msg .git/COMMIT_EDITMSG
# Display the updated commit message
echo "Updated Commit Message:"
cat .git/COMMIT_EDITMSG
# Cleanup
rm -rf "$temp_dir"
To run this script, save it to a file, give it execute permission (chmod +x <filename>
), and then run it. Make sure to run it as the same user that would run auto-commit-msg
to ensure environment consistency.
Note: This script assumes that your commit-msg
hook is located in /etc/autocommitmsg/commit-msg
. Modify the path if it's different.
Tool: Utilize Bats (Bash Automated Testing System) for writing and running unit tests.
Bats
for Bash.Scripts to Test:
auto-commit-msg
,commit-msg
Implementation Steps:
tests
directory within the repository.Directory Structure:
tests/
directory.Links: Bats GitHub Repository bats-core docs