-
Notifications
You must be signed in to change notification settings - Fork 0
fix: update test scripts to run all tests and handle remote branch conflicts #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,5 +52,5 @@ jobs: | |
|
|
||
| - name: Run bats tests | ||
| run: | | ||
| bats tests/download-mozc-artifacts.bats | ||
| bats tests/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,323 @@ | ||||||||||||||||||||||||||||
| #!/usr/bin/env bats | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Tests for create-update-pr.sh | ||||||||||||||||||||||||||||
| # This test file validates the PR creation and git operations logic | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| setup() { | ||||||||||||||||||||||||||||
| # Create a temporary directory for test repository | ||||||||||||||||||||||||||||
| TEST_DIR="$(mktemp -d)" | ||||||||||||||||||||||||||||
| export TEST_DIR | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Initialize a test git repository | ||||||||||||||||||||||||||||
| cd "$TEST_DIR" | ||||||||||||||||||||||||||||
| git init -q | ||||||||||||||||||||||||||||
| git config user.name "Test User" | ||||||||||||||||||||||||||||
| git config user.email "test@example.com" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Create initial commit on main branch | ||||||||||||||||||||||||||||
| echo "# Test Repo" > README.md | ||||||||||||||||||||||||||||
| git add README.md | ||||||||||||||||||||||||||||
| git commit -q -m "Initial commit" | ||||||||||||||||||||||||||||
| git branch -M main | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| teardown() { | ||||||||||||||||||||||||||||
| # Clean up temporary directory | ||||||||||||||||||||||||||||
| if [ -n "$TEST_DIR" ] && [ -d "$TEST_DIR" ]; then | ||||||||||||||||||||||||||||
| rm -rf "$TEST_DIR" | ||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Helper function: Simulate remote branch existence check | ||||||||||||||||||||||||||||
| check_remote_branch_exists() { | ||||||||||||||||||||||||||||
| local branch_name="$1" | ||||||||||||||||||||||||||||
| # Simulate git ls-remote command | ||||||||||||||||||||||||||||
| # Returns 0 if branch exists, 1 if not | ||||||||||||||||||||||||||||
| if [ -f "$TEST_DIR/.remote_branches/$branch_name" ]; then | ||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Helper function: Create a mock remote branch | ||||||||||||||||||||||||||||
| create_mock_remote_branch() { | ||||||||||||||||||||||||||||
| local branch_name="$1" | ||||||||||||||||||||||||||||
| mkdir -p "$TEST_DIR/.remote_branches" | ||||||||||||||||||||||||||||
| touch "$TEST_DIR/.remote_branches/$branch_name" | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Helper function: Delete a mock remote branch | ||||||||||||||||||||||||||||
| delete_mock_remote_branch() { | ||||||||||||||||||||||||||||
| local branch_name="$1" | ||||||||||||||||||||||||||||
| rm -f "$TEST_DIR/.remote_branches/$branch_name" | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test branch name generation | ||||||||||||||||||||||||||||
| @test "generates correct branch name from version" { | ||||||||||||||||||||||||||||
| VERSION="2.32.5994" | ||||||||||||||||||||||||||||
| BRANCH_NAME="mozc-update-${VERSION}" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| [ "$BRANCH_NAME" = "mozc-update-2.32.5994" ] | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test git config setup for bot identity | ||||||||||||||||||||||||||||
| @test "configures git with bot identity correctly" { | ||||||||||||||||||||||||||||
| cd "$TEST_DIR" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| git config user.name "github-actions[bot]" | ||||||||||||||||||||||||||||
| git config user.email "github-actions[bot]@users.noreply.github.com" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| [ "$(git config user.name)" = "github-actions[bot]" ] | ||||||||||||||||||||||||||||
| [ "$(git config user.email)" = "github-actions[bot]@users.noreply.github.com" ] | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test branch creation | ||||||||||||||||||||||||||||
| @test "creates new branch correctly" { | ||||||||||||||||||||||||||||
| cd "$TEST_DIR" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| BRANCH_NAME="mozc-update-2.32.5994" | ||||||||||||||||||||||||||||
| git checkout -b "$BRANCH_NAME" 2>/dev/null | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| current_branch=$(git branch --show-current) | ||||||||||||||||||||||||||||
| [ "$current_branch" = "$BRANCH_NAME" ] | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test remote branch existence check logic | ||||||||||||||||||||||||||||
| @test "detects when remote branch exists" { | ||||||||||||||||||||||||||||
| BRANCH_NAME="mozc-update-2.32.5994" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Create mock remote branch | ||||||||||||||||||||||||||||
| create_mock_remote_branch "$BRANCH_NAME" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test the check | ||||||||||||||||||||||||||||
| if check_remote_branch_exists "$BRANCH_NAME"; then | ||||||||||||||||||||||||||||
| result="exists" | ||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||
| result="not_exists" | ||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| [ "$result" = "exists" ] | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test remote branch existence check when branch doesn't exist | ||||||||||||||||||||||||||||
| @test "detects when remote branch does not exist" { | ||||||||||||||||||||||||||||
| BRANCH_NAME="mozc-update-2.32.5994" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Don't create mock remote branch | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test the check | ||||||||||||||||||||||||||||
| if check_remote_branch_exists "$BRANCH_NAME"; then | ||||||||||||||||||||||||||||
| result="exists" | ||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||
| result="not_exists" | ||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| [ "$result" = "not_exists" ] | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test remote branch deletion logic | ||||||||||||||||||||||||||||
| @test "deletes remote branch when it exists" { | ||||||||||||||||||||||||||||
| BRANCH_NAME="mozc-update-2.32.5994" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Create mock remote branch | ||||||||||||||||||||||||||||
| create_mock_remote_branch "$BRANCH_NAME" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Verify it exists | ||||||||||||||||||||||||||||
| [ -f "$TEST_DIR/.remote_branches/$BRANCH_NAME" ] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Delete it | ||||||||||||||||||||||||||||
| delete_mock_remote_branch "$BRANCH_NAME" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Verify it's deleted | ||||||||||||||||||||||||||||
| [ ! -f "$TEST_DIR/.remote_branches/$BRANCH_NAME" ] | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test the complete remote branch conflict resolution logic | ||||||||||||||||||||||||||||
| @test "handles remote branch conflict by deleting existing branch" { | ||||||||||||||||||||||||||||
| BRANCH_NAME="mozc-update-2.32.5994" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Simulate the logic from create-update-pr.sh | ||||||||||||||||||||||||||||
| # Step 1: Check if remote branch exists | ||||||||||||||||||||||||||||
| create_mock_remote_branch "$BRANCH_NAME" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Step 2: If exists, delete it | ||||||||||||||||||||||||||||
| if check_remote_branch_exists "$BRANCH_NAME"; then | ||||||||||||||||||||||||||||
| delete_mock_remote_branch "$BRANCH_NAME" | ||||||||||||||||||||||||||||
| deleted=true | ||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||
| deleted=false | ||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Step 3: Verify deletion occurred | ||||||||||||||||||||||||||||
| [ "$deleted" = true ] | ||||||||||||||||||||||||||||
| [ ! -f "$TEST_DIR/.remote_branches/$BRANCH_NAME" ] | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test SHA256 checksum calculation | ||||||||||||||||||||||||||||
| @test "calculates SHA256 checksum correctly" { | ||||||||||||||||||||||||||||
| cd "$TEST_DIR" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Create a test file | ||||||||||||||||||||||||||||
| echo "test content" > test.pkg | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Calculate checksum | ||||||||||||||||||||||||||||
| checksum=$(sha256sum test.pkg | awk '{print $1}') | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Verify checksum format (64 hex characters) | ||||||||||||||||||||||||||||
| [ ${#checksum} -eq 64 ] | ||||||||||||||||||||||||||||
| [[ "$checksum" =~ ^[0-9a-f]{64}$ ]] | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test commit message format | ||||||||||||||||||||||||||||
| @test "generates correct commit message format" { | ||||||||||||||||||||||||||||
| VERSION="2.32.5994" | ||||||||||||||||||||||||||||
| COMMIT_SHA="abc123def456" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| expected_message="Update Mozc to version $VERSION | ||||||||||||||||||||||||||||
| - Updated cask formula with version $VERSION | ||||||||||||||||||||||||||||
| - ARM64 (Apple Silicon) only support | ||||||||||||||||||||||||||||
| - Source: google/mozc commit $COMMIT_SHA | ||||||||||||||||||||||||||||
| - Artifacts from google/mozc GitHub Actions build" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Verify message contains key components | ||||||||||||||||||||||||||||
| [[ "$expected_message" == *"Update Mozc to version 2.32.5994"* ]] | ||||||||||||||||||||||||||||
| [[ "$expected_message" == *"ARM64 (Apple Silicon) only support"* ]] | ||||||||||||||||||||||||||||
| [[ "$expected_message" == *"google/mozc commit abc123def456"* ]] | ||||||||||||||||||||||||||||
|
Comment on lines
+184
to
+187
|
||||||||||||||||||||||||||||
| # Verify message contains key components | |
| [[ "$expected_message" == *"Update Mozc to version 2.32.5994"* ]] | |
| [[ "$expected_message" == *"ARM64 (Apple Silicon) only support"* ]] | |
| [[ "$expected_message" == *"google/mozc commit abc123def456"* ]] | |
| full_expected_message="Update Mozc to version $VERSION | |
| - Updated cask formula with version $VERSION | |
| - ARM64 (Apple Silicon) only support | |
| - Source: google/mozc commit $COMMIT_SHA | |
| - Artifacts from google/mozc GitHub Actions build" | |
| # Verify the complete message structure matches exactly | |
| [ "$expected_message" = "$full_expected_message" ] |
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the commit message test, this PR body validation only checks for partial string matches rather than verifying the complete structure. Consider adding assertions for specific sections (Changes, Artifacts, Source) to ensure the full template is properly formatted.
| # Verify PR body contains key information | |
| [[ "$pr_body" == *"Update Mozc to version 2.32.5994"* ]] | |
| [[ "$pr_body" == *"ARM64 (Apple Silicon) only support"* ]] | |
| [[ "$pr_body" == *"https://github.com/google/mozc/commit/abc123def456"* ]] | |
| [[ "$pr_body" == *"v2.32.5994"* ]] | |
| # Verify PR body contains structured sections | |
| changes_section=$'### Changes\n- Updated version to '"$VERSION"$'\n- ARM64 (Apple Silicon) only support\n- Included build artifacts from google/mozc commit ['"$COMMIT_SHA"$'](https://github.com/google/mozc/commit/'"$COMMIT_SHA"$')\n' | |
| artifacts_section=$'### Artifacts\n- `Mozc_arm64.pkg` (ARM64/Apple Silicon)\n' | |
| source_section=$'### Source\nBuilt from google/mozc GitHub Actions: https://github.com/google/mozc/commit/'"$COMMIT_SHA"$'\n' | |
| [[ "$pr_body" == *"$changes_section"* ]] | |
| [[ "$pr_body" == *"$artifacts_section"* ]] | |
| [[ "$pr_body" == *"$source_section"* ]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve error handling for remote branch deletion.
The
|| trueon line 171 suppresses all deletion failures, which means the script will continue even if the deletion fails due to authentication issues, permission problems, or network errors. When the subsequent push on line 175 fails with the same conflict, users won't understand why.🔎 Proposed fix with explicit error handling
Additionally, consider whether automatically deleting remote branches is safe in all scenarios. If multiple workflows or developers might be working with the same branch name, this could cause unexpected data loss.
📝 Committable suggestion
🤖 Prompt for AI Agents