import subprocess import tempfile from pathlib import Path from aider_gitea import contains_only_ruff_changes class TestContainsOnlyRuffChanges: """Test the contains_only_ruff_changes function.""" def setup_test_repo(self) -> Path: """Create a test git repository.""" temp_dir = Path(tempfile.mkdtemp()) # Initialize git repo subprocess.run(['git', 'init'], cwd=temp_dir, check=True) subprocess.run( ['git', 'config', 'user.name', 'Test User'], cwd=temp_dir, check=True, ) subprocess.run( ['git', 'config', 'user.email', 'test@example.com'], cwd=temp_dir, check=True, ) # Create initial content and commit test_file = temp_dir / 'test.py' test_file.write_text('def hello():\n print("hello")\n') subprocess.run(['git', 'add', '.'], cwd=temp_dir, check=True) subprocess.run( ['git', 'commit', '-m', 'Initial commit'], cwd=temp_dir, check=True, ) # Rename master to main for consistency subprocess.run( ['git', 'branch', '-m', 'master', 'main'], cwd=temp_dir, check=True, ) return temp_dir def test_no_commits_returns_true(self): """Test that branches with no commits return True.""" temp_dir = self.setup_test_repo() # Create a new branch but don't commit anything subprocess.run( ['git', 'checkout', '-b', 'test-branch'], cwd=temp_dir, check=True, ) result = contains_only_ruff_changes(temp_dir, 'main', 'test-branch') assert result is True def test_only_ruff_commits_returns_true(self): """Test that branches with only ruff-related commits return True.""" temp_dir = self.setup_test_repo() # Create a new branch and make ruff-related commits subprocess.run( ['git', 'checkout', '-b', 'test-branch'], cwd=temp_dir, check=True, ) # Modify the file with formatting changes test_file = temp_dir / 'test.py' test_file.write_text('def hello():\n print("hello")\n\n') # Added newline subprocess.run(['git', 'add', '.'], cwd=temp_dir, check=True) subprocess.run( ['git', 'commit', '-m', 'ruff format changes'], cwd=temp_dir, check=True, ) # Another ruff commit test_file.write_text( 'def hello():\n print("hello")\n', ) # Removed newline again subprocess.run(['git', 'add', '.'], cwd=temp_dir, check=True) subprocess.run( ['git', 'commit', '-m', 'auto-fix lint issues'], cwd=temp_dir, check=True, ) result = contains_only_ruff_changes(temp_dir, 'main', 'test-branch') assert result is True def test_substantive_changes_returns_false(self): """Test that branches with substantive changes return False.""" temp_dir = self.setup_test_repo() # Create a new branch and make substantive changes subprocess.run( ['git', 'checkout', '-b', 'test-branch'], cwd=temp_dir, check=True, ) # Add a new function (substantive change) test_file = temp_dir / 'test.py' test_file.write_text( 'def hello():\n print("hello")\n\ndef goodbye():\n print("goodbye")\n', ) subprocess.run(['git', 'add', '.'], cwd=temp_dir, check=True) subprocess.run( ['git', 'commit', '-m', 'Add goodbye function'], cwd=temp_dir, check=True, ) result = contains_only_ruff_changes(temp_dir, 'main', 'test-branch') assert result is False def test_mixed_commits_returns_false(self): """Test that branches with both ruff and substantive commits return False.""" temp_dir = self.setup_test_repo() # Create a new branch subprocess.run( ['git', 'checkout', '-b', 'test-branch'], cwd=temp_dir, check=True, ) # First, a substantive change test_file = temp_dir / 'test.py' test_file.write_text( 'def hello():\n print("hello world")\n', ) # Changed string subprocess.run(['git', 'add', '.'], cwd=temp_dir, check=True) subprocess.run( ['git', 'commit', '-m', 'Update greeting message'], cwd=temp_dir, check=True, ) # Then, a ruff change test_file.write_text( 'def hello():\n print("hello world")\n\n', ) # Added newline subprocess.run(['git', 'add', '.'], cwd=temp_dir, check=True) subprocess.run(['git', 'commit', '-m', 'ruff format'], cwd=temp_dir, check=True) result = contains_only_ruff_changes(temp_dir, 'main', 'test-branch') assert result is False def test_ruff_keywords_in_commit_messages(self): """Test various ruff-related keywords in commit messages.""" temp_dir = self.setup_test_repo() ruff_messages = [ 'ruff format', 'Ruff after aider', 'auto-fix lint issues', 'code style formatting', 'Apply formatting changes', 'Lint fixes', ] for i, message in enumerate(ruff_messages): # Create a branch for each test branch_name = f'test-branch-{i}' subprocess.run(['git', 'checkout', 'main'], cwd=temp_dir, check=True) subprocess.run( ['git', 'checkout', '-b', branch_name], cwd=temp_dir, check=True, ) # Make a minor formatting change that's always different test_file = temp_dir / 'test.py' content = test_file.read_text() # Add a unique comment to make each change different content += f'# formatting change {i}\n' test_file.write_text(content) subprocess.run(['git', 'add', '.'], cwd=temp_dir, check=True) subprocess.run(['git', 'commit', '-m', message], cwd=temp_dir, check=True) result = contains_only_ruff_changes(temp_dir, 'main', branch_name) assert result is True, f'Failed for message: {message}'