Malnati / autocommitmsg

This repository contains a Bash script designed to analyze the content of a Git commit and automatically suggest a semantic versioning message. It utilizes OpenAI's GPT-3.5 Turbo engine to generate the commit message based on the changes staged for commit.
MIT License
0 stars 0 forks source link

Automated Tests #2

Open Malnati opened 8 months ago

Malnati commented 8 months ago

Tool: Utilize Bats (Bash Automated Testing System) for writing and running unit tests.

  1. Tool: Use a testing framework like Bats for Bash.
  2. Implementation: Write unit tests for each function in your script.
  3. CI/CD: Integrate with a CI/CD service like GitHub Actions to run tests automatically on each commit or pull request.

Scripts to Test: auto-commit-msg, commit-msg

Implementation Steps:

  1. Identify all functions within the script that require testing.
  2. For each function, write a corresponding Bats test case.
  3. Ensure that the test cases cover all possible edge cases and scenarios.
  4. Organize the test cases in a dedicated tests directory within the repository.

Directory Structure:

Links: Bats GitHub Repository bats-core docs

Malnati commented 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.

Malnati commented 8 months ago

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.