mirror of
https://github.com/creyD/prettier_action.git
synced 2026-04-15 04:40:33 +02:00
Add comprehensive testing infrastructure
This commit adds a complete testing setup for the prettier_action: - Adds BATS (Bash Automated Testing System) testing framework - Creates unit tests for _git_setup() and _git_changed() functions - Creates plugin validation tests to ensure proper prettier plugin format - Creates integration tests for end-to-end workflows - Adds automated test runner script (tests/run_tests.sh) - Adds GitHub Actions workflow for CI/CD testing - Includes ShellCheck linting for bash scripts - Updates README with comprehensive testing documentation - Updates .gitignore to exclude test artifacts Test coverage includes: - Git configuration with different identity modes - File change detection - Plugin name validation (official, community, and scoped formats) - Working directory handling - node_modules cleanup - package-lock.json restoration - only_changed file filtering - Dry run behavior The test suite can be run locally with ./tests/run_tests.sh and runs automatically on all pushes and pull requests.
This commit is contained in:
124
tests/unit_tests.bats
Normal file
124
tests/unit_tests.bats
Normal file
@@ -0,0 +1,124 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
# Unit tests for prettier_action entrypoint.sh functions
|
||||
|
||||
load 'test_helper'
|
||||
|
||||
setup() {
|
||||
setup_test_repo
|
||||
mock_github_env
|
||||
set_default_inputs
|
||||
|
||||
# Load the functions from entrypoint.sh
|
||||
export SCRIPT_DIR="$(cd "$(dirname "${BATS_TEST_DIRNAME}")" && pwd)"
|
||||
load_script_functions "$SCRIPT_DIR/entrypoint.sh"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
teardown_test_repo
|
||||
}
|
||||
|
||||
# Test _git_setup function with 'actions' identity
|
||||
@test "_git_setup creates .netrc file with correct permissions" {
|
||||
export INPUT_GIT_IDENTITY="actions"
|
||||
|
||||
run _git_setup
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[ -f "$HOME/.netrc" ]
|
||||
|
||||
# Check file permissions (should be 600)
|
||||
local perms=$(stat -c "%a" "$HOME/.netrc")
|
||||
[ "$perms" = "600" ]
|
||||
}
|
||||
|
||||
@test "_git_setup configures git with 'actions' identity" {
|
||||
export INPUT_GIT_IDENTITY="actions"
|
||||
|
||||
run _git_setup
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
|
||||
# Check git config
|
||||
local git_name=$(git config --global user.name)
|
||||
local git_email=$(git config --global user.email)
|
||||
|
||||
[ "$git_name" = "GitHub Action" ]
|
||||
[ "$git_email" = "actions@github.com" ]
|
||||
}
|
||||
|
||||
@test "_git_setup configures git with 'author' identity" {
|
||||
export INPUT_GIT_IDENTITY="author"
|
||||
export GITHUB_ACTOR="test-user"
|
||||
export GITHUB_ACTOR_ID="54321"
|
||||
|
||||
run _git_setup
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
|
||||
# Check git config
|
||||
local git_name=$(git config --global user.name)
|
||||
local git_email=$(git config --global user.email)
|
||||
|
||||
[ "$git_name" = "test-user" ]
|
||||
[ "$git_email" = "54321+test-user@users.noreply.github.com" ]
|
||||
}
|
||||
|
||||
@test "_git_setup fails with invalid identity" {
|
||||
export INPUT_GIT_IDENTITY="invalid"
|
||||
|
||||
run _git_setup
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" =~ "GIT_IDENTITY must be either 'author' or 'actions'" ]]
|
||||
}
|
||||
|
||||
@test "_git_changed returns true when files are modified" {
|
||||
# Create and commit a file
|
||||
echo "test" > test.txt
|
||||
git add test.txt
|
||||
git commit -m "Initial commit"
|
||||
|
||||
# Modify the file
|
||||
echo "modified" > test.txt
|
||||
|
||||
run _git_changed
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "_git_changed returns false when no files are modified" {
|
||||
# Create and commit a file
|
||||
echo "test" > test.txt
|
||||
git add test.txt
|
||||
git commit -m "Initial commit"
|
||||
|
||||
# No modifications
|
||||
run _git_changed
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "_git_changed returns true for untracked files" {
|
||||
# Create a file without committing
|
||||
echo "test" > untracked.txt
|
||||
|
||||
run _git_changed
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "_git_changed returns true for staged files" {
|
||||
# Create and commit a file
|
||||
echo "test" > test.txt
|
||||
git add test.txt
|
||||
git commit -m "Initial commit"
|
||||
|
||||
# Add a new file and stage it
|
||||
echo "new file" > new.txt
|
||||
git add new.txt
|
||||
|
||||
run _git_changed
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
Reference in New Issue
Block a user