# Previous content of test_update_packages.py 

import pytest
import json
from unittest.mock import patch, mock_open, MagicMock, call
import os
import sys
from git.repo.fun import is_git_dir
from git import Blob, Repo
from gitdb import IStream
from gitdb.base import OStream
from pyfakefs.fake_filesystem_unittest import Patcher

# Add parent directory to path so we can import update_packages
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from update_packages import update_pkgbuild, commit_and_push, main

@pytest.fixture
def mock_config():
    return {
        'packages': {
            'test-package': {
                'github_repo': 'owner/repo',
                'version_method': 'release',
                'pkgbuild_path': 'test-package/PKGBUILD'
            }
        }
    }

@pytest.fixture
def mock_repo():
    repo = MagicMock(spec=Repo)
    repo.index = MagicMock()
    repo.odb = MagicMock()
    
    # Setup index entries
    mock_entry = MagicMock()
    mock_entry.hexsha = 'mock_sha'
    repo.index._entries = {
        'test-package/PKGBUILD': mock_entry,
        'test-package/.SRCINFO': mock_entry
    }
    
    # Setup ODB
    mock_ostream = MagicMock(spec=OStream)
    mock_ostream.stream = b'mock file content'
    mock_ostream.hexsha = 'mock_sha'
    repo.odb.store.return_value = mock_ostream
    
    return repo

@pytest.fixture
def fake_fs():
    with Patcher() as patcher:
        # Setup fake filesystem
        patcher.fs.create_file('.git/config', contents='''[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    worktree = .
''')
        patcher.fs.create_file('.git/HEAD')
        patcher.fs.create_dir('.git/objects')
        patcher.fs.create_dir('.git/refs')
        yield patcher.fs

def test_update_pkgbuild():
    package_config = {
        'pkgbuild_path': 'test-package/PKGBUILD'
    }
    
    pkgbuild_content = '''
# Maintainer: Test User <test@example.com>
pkgname=test-package
pkgver=1.2.3
pkgrel=1
    '''
    
    with patch('builtins.open', mock_open(read_data=pkgbuild_content)) as mock_file:
        with patch('subprocess.run') as mock_run:
            update_pkgbuild('test-package', package_config, '1.2.4')
            
            # Check if PKGBUILD was updated correctly
            write_calls = [call.args[0] for call in mock_file().write.call_args_list]
            written_content = ''.join(write_calls)
            assert 'pkgver=1.2.4' in written_content
            
            # Check if .SRCINFO was generated
            mock_run.assert_called_with(
                ['makepkg', '--printsrcinfo'],
                cwd='test-package',
                stdout=mock_file(),
                check=True
            )

def test_commit_and_push(mock_repo, fake_fs):
    from update_packages import commit_and_push
    
    # Setup test files
    fake_fs.create_file('test-package/PKGBUILD', contents='pkgver=1.2.3')
    fake_fs.create_file('test-package/.SRCINFO', contents='pkgver = 1.2.3')
    
    # Run the function with injected dependencies
    commit_and_push(
        package_name='test-package',
        new_version='1.2.4',
        pkgbuild_path='test-package/PKGBUILD',
        repo=mock_repo,
        fs_path=fake_fs.os.path
    )
    
    # Verify the changes
    mock_repo.index.add.assert_called_with(['test-package/PKGBUILD', 'test-package/.SRCINFO'])
    mock_repo.index.commit.assert_called_with(
        'Update test-package to 1.2.4',
        gpgsign=True
    )
    mock_repo.remote().push.assert_called_once()

def test_main():
    config_yaml = '''
packages:
  test-package:
    github_repo: owner/repo
    version_method: release
    pkgbuild_path: test-package/PKGBUILD
'''
    updates_json = '''[
        {
            "name": "test-package",
            "current": "1.2.3",
            "latest": "1.2.4"
        }
    ]'''
    
    with patch.dict('os.environ', {'UPDATES_LIST': updates_json}):
        with patch('builtins.open', mock_open(read_data=config_yaml)):
            with patch('update_packages.update_pkgbuild') as mock_update:
                with patch('update_packages.commit_and_push') as mock_commit:
                    main()
                    
                    # Check if update was attempted
                    mock_update.assert_called_with(
                        'test-package',
                        {'github_repo': 'owner/repo', 'version_method': 'release', 'pkgbuild_path': 'test-package/PKGBUILD'},
                        '1.2.4'
                    )
                    
                    # Check if commit and push was attempted
                    mock_commit.assert_called_with(
                        'test-package',
                        '1.2.4',
                        'test-package/PKGBUILD'
                    ) 