Back to Repositories

Testing Git Integration Operations in gpt-engineer

This test suite validates Git integration functionality in the gpt-engineer project, covering repository initialization, file staging, gitignore handling, and change detection. The tests ensure reliable Git operations and version control features.

Test Coverage Overview

The test suite provides comprehensive coverage of core Git operations and integration points.

Key functionality tested includes:
  • Git installation verification
  • Repository initialization
  • File staging operations
  • Gitignore pattern filtering
  • Uncommitted changes detection
Edge cases cover untracked files, staged changes, and various file states in the Git workflow.

Implementation Analysis

The testing approach utilizes Python’s tempfile module for isolated test environments and subprocess for Git command execution. The implementation follows a modular pattern with discrete test functions for each Git operation, using context managers for cleanup and Path objects for file handling.

Technical patterns include:
  • Temporary directory fixtures
  • Subprocess command execution
  • File system operations via pathlib
  • Git command output parsing

Technical Details

Testing tools and configuration:
  • Python’s built-in unittest framework
  • tempfile for temporary test directories
  • subprocess for Git command execution
  • pathlib for file system operations
  • Local Git installation required

Best Practices Demonstrated

The test suite demonstrates strong testing practices through isolated test environments, proper cleanup, and comprehensive state verification. Notable practices include:
  • Isolated test environments
  • Proper resource cleanup
  • Atomic test cases
  • Clear test naming conventions
  • Comprehensive state verification

gpt-engineer-org/gpt-engineer

tests/core/test_git.py

            
import subprocess
import tempfile

from pathlib import Path

from gpt_engineer.core.git import (
    filter_by_gitignore,
    filter_files_with_uncommitted_changes,
    init_git_repo,
    is_git_installed,
    is_git_repo,
    stage_files,
)


def test_verify_git_installed():
    # If git isn't installed we can't run any git tests either way
    assert is_git_installed()


def test_init_git_repo():
    with tempfile.TemporaryDirectory() as tmpdir:
        path = Path(tmpdir)
        init_git_repo(path)
        assert is_git_repo(path)


def test_stage_files():
    with tempfile.TemporaryDirectory() as tmpdir:
        path = Path(tmpdir)
        init_git_repo(path)

        # Create a file and stage it
        file = path / "test.txt"
        file.write_text("test")

        stage_files(path, ["test.txt"])

        # Check if the file is staged
        assert (
            subprocess.run(
                ["git", "diff", "--cached", "--name-only"],
                cwd=path,
                stdout=subprocess.PIPE,
            )
            .stdout.decode()
            .strip()
            == "test.txt"
        )


def test_filter_by_gitignore():
    with tempfile.TemporaryDirectory() as tmpdir:
        path = Path(tmpdir)
        init_git_repo(path)

        # Create a .gitignore file
        gitignore = path / ".gitignore"
        gitignore.write_text("*.txt")
        assert filter_by_gitignore(path, ["test.txt"]) == []


def test_filter_by_uncommitted_changes():
    with tempfile.TemporaryDirectory() as tmpdir:
        path = Path(tmpdir)
        init_git_repo(path)

        # Create a file and commit it
        file = path / "test.txt"
        file.write_text("test")

        subprocess.run(["git", "add", "test.txt"], cwd=path)
        subprocess.run(["git", "commit", "-m", "test"], cwd=path)

        # Update the file
        file.write_text("test2")

        # Check if the file is staged
        assert filter_files_with_uncommitted_changes(path, {"test.txt": "test"}) == [
            "test.txt"
        ]


def test_filter_by_uncommitted_changes_ignore_staged_files():
    with tempfile.TemporaryDirectory() as tmpdir:
        path = Path(tmpdir)
        init_git_repo(path)

        # Create a file but and stage it
        file = path / "test.txt"
        file.write_text("test")
        subprocess.run(["git", "add", "test.txt"], cwd=path)

        # Check if the file is staged
        assert filter_files_with_uncommitted_changes(path, {"test.txt": "test"}) == []


def test_filter_by_uncommitted_changes_ignore_untracked():
    with tempfile.TemporaryDirectory() as tmpdir:
        path = Path(tmpdir)
        init_git_repo(path)

        # Create a file but don't track it
        file = path / "test.txt"
        file.write_text("test")

        # Check if the file is staged
        assert filter_files_with_uncommitted_changes(path, {"test.txt": "test"}) == []