/
test_repos.py
63 lines (50 loc) · 1.76 KB
/
test_repos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import subprocess
from contextlib import contextmanager
from mayo.git import Git
from mayo.hg import Hg
from mayo.files import temporary_directory, write_file
def create_repo(vcs_name, path):
return {
"git": create_git_repo,
"hg": create_hg_repo
}.get(vcs_name, create_unrecognised_repo)(path)
def create_unrecognised_repo(path):
raise RuntimeError("Unrecognised VCS")
def create_git_repo(path):
repository = Git().local_repo(path)
execute(repository, ["init"])
write_file(os.path.join(path, "README"), "Run it.")
execute(repository, ["add", "README"])
execute(repository, ["commit", "-mAdding README"])
return repository
def create_hg_repo(path):
repository = Hg().local_repo(path)
execute(repository, ["init"])
write_file(os.path.join(path, "README"), "Run it.")
execute(repository, ["add", "README"])
execute(repository, ["commit", "-mAdding README"])
return repository
@contextmanager
def temporary_repo(vcs):
with temporary_directory() as path:
yield create_repo(vcs, path)
@contextmanager
def temporary_git_repo():
with temporary_directory() as path:
yield create_git_repo(path)
@contextmanager
def temporary_hg_repo():
with temporary_directory() as path:
yield create_hg_repo(path)
def add_commit_to_repo(repo):
write_file(os.path.join(repo.working_directory, "README"), "Run away!")
execute(repo, ["add", "README"])
execute(repo, ["commit", "-mUpdating README"])
def tag_git_repo(repo, tag_name):
execute(repo, ["tag", tag_name])
_dev_null = open('/dev/null', 'w')
def execute(repo, command):
command = [repo.type] + command
subprocess.check_call(command, cwd=repo.working_directory,
stdout=_dev_null, stderr=subprocess.STDOUT)