OLD | NEW |
(Empty) | |
| 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
| 2 # Copyright (C) 2006-present eyeo GmbH |
| 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. |
| 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. |
| 12 # |
| 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
| 16 """This module contains the tests for src/vcs.py.""" |
| 17 |
| 18 from __future__ import unicode_literals |
| 19 |
| 20 import io |
| 21 import os |
| 22 import subprocess |
| 23 |
| 24 import pytest |
| 25 |
| 26 from src.vcs import Vcs, Git, Mercurial |
| 27 |
| 28 DATA_DIR = os.path.join( |
| 29 os.path.realpath(os.path.dirname(__file__)), 'data' |
| 30 ) |
| 31 |
| 32 |
| 33 class _VcsCmd(object): |
| 34 def __init__(self, executable, cwd): |
| 35 self.executable = executable |
| 36 self.cwd = cwd |
| 37 |
| 38 def run(self, *cmd): |
| 39 return subprocess.check_output((self.executable,) + cmd, cwd=self.cwd) |
| 40 |
| 41 def __enter__(self): |
| 42 return self |
| 43 |
| 44 def __exit__(self, *exc): |
| 45 return |
| 46 |
| 47 |
| 48 @pytest.fixture(scope='module') |
| 49 def hg_repo(tmpdir_factory): |
| 50 """Create a mercurial repository for the tests.""" |
| 51 hg_dir = tmpdir_factory.mktemp('repos').mkdir('hg').mkdir('testrepo') |
| 52 hg = _VcsCmd('hg', str(hg_dir)) |
| 53 |
| 54 hg.run('init') |
| 55 hg.run('import', os.path.join(DATA_DIR, '0.diff')) |
| 56 hg.run('import', os.path.join(DATA_DIR, '1.diff')) |
| 57 |
| 58 hg.run('update', '-c', '0') |
| 59 hg.run('import', os.path.join(DATA_DIR, '2.diff')) |
| 60 hg.run('bookmark', 'master') |
| 61 |
| 62 with io.open(os.path.join(str(hg_dir), '.hg', 'hgrc'), 'w') as fp: |
| 63 fp.write('[paths]{}default = {}{}'.format(os.linesep, str(hg_dir), |
| 64 os.linesep)) |
| 65 return hg_dir |
| 66 |
| 67 |
| 68 @pytest.fixture |
| 69 def git_repo(tmpdir_factory): |
| 70 """Create a git repository for the tests.""" |
| 71 git_dir = tmpdir_factory.mktemp('repos').mkdir('git').mkdir('testrepo') |
| 72 git = _VcsCmd('git', str(git_dir)) |
| 73 |
| 74 git.run('init') |
| 75 git.run('am', os.path.join(DATA_DIR, '0.diff')) |
| 76 git.run('am', os.path.join(DATA_DIR, '2.diff')) |
| 77 |
| 78 git.run('checkout', '-b', 'splitted') |
| 79 git.run('am', os.path.join(DATA_DIR, '1.diff')) |
| 80 git.run('checkout', 'master') |
| 81 |
| 82 git.run('remote', 'add', 'origin', str(git_dir)) |
| 83 return git_dir |
| 84 |
| 85 |
| 86 def test_factory(git_repo, hg_repo): |
| 87 """Test VCS determination.""" |
| 88 assert Vcs.factory(str(git_repo)).__class__ == Git |
| 89 assert Vcs.factory(str(hg_repo)).__class__ == Mercurial |
| 90 |
| 91 |
| 92 def test_hash_lookup(git_repo, hg_repo): |
| 93 """Assert correct mirroring between Mercurial / Git.""" |
| 94 git = Vcs.factory(str(git_repo)) |
| 95 hg = Vcs.factory(str(hg_repo)) |
| 96 |
| 97 first_git_commit = git.run_cmd('rev-list', '--max-parents=0', |
| 98 'HEAD').strip() |
| 99 first_hg_commit = hg.run_cmd('log', '-r', '0', |
| 100 '--template="{node|short}"').strip() |
| 101 |
| 102 change_list_git = git.change_list(first_git_commit, 'master') |
| 103 change_list_hg = hg.change_list(first_hg_commit, 'master') |
| 104 |
| 105 git.enhance_changes_information(change_list_git, str(hg_repo), False) |
| 106 hg.enhance_changes_information(change_list_hg, str(git_repo), False) |
| 107 |
| 108 assert len(change_list_git) == len(change_list_hg) |
| 109 for i, change in enumerate(change_list_git): |
| 110 assert change['hg_hash'] == change_list_hg[i]['hg_hash'] |
| 111 assert change['git_hash'] == change_list_hg[i]['git_hash'] |
| 112 assert change['hg_url'] == change_list_hg[i]['hg_url'] |
| 113 assert change['git_url'] == change_list_hg[i]['git_url'] |
| 114 |
| 115 |
| 116 def test_dirty_check_and_clean(git_repo, hg_repo): |
| 117 """Test discovering and cleaning of a dirty repository.""" |
| 118 for repo in [git_repo, hg_repo]: |
| 119 vcs = Vcs.factory(str(repo)) |
| 120 |
| 121 repo.join('foobar.txt').write('foobar') |
| 122 assert vcs.repo_is_clean() is False |
| 123 |
| 124 vcs.undo_changes() |
| 125 assert vcs.repo_is_clean() |
| 126 |
| 127 repo.join('foo').write('bar') |
| 128 assert vcs.repo_is_clean() is False |
| 129 |
| 130 |
| 131 def test_tmp_cloning_and_cleanup(git_repo, hg_repo): |
| 132 """Test cleanup after temporary cloning a repository.""" |
| 133 for repo in [git_repo, hg_repo]: |
| 134 with Vcs.factory(str(repo), True) as tmp_repo: |
| 135 tmp_dir = tmp_repo._cwd |
| 136 tmp_repo._get_latest() |
| 137 |
| 138 assert os.path.exists(tmp_dir) is False |
| 139 |
| 140 |
| 141 def test_commit(git_repo, hg_repo): |
| 142 """Test commit functionality of Vcs.""" |
| 143 for repo in [git_repo, hg_repo]: |
| 144 vcs = Vcs.factory(str(repo)) |
| 145 |
| 146 repo.join('foobar.txt').write('foobar') |
| 147 |
| 148 vcs.commit_changes('Testing commit') |
| 149 |
| 150 assert 'Testing commit' in vcs.run_cmd('log') |
OLD | NEW |