-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathtest_git.py
70 lines (53 loc) · 1.93 KB
/
test_git.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
63
64
65
66
67
68
69
70
"""Test git library functions in ``planemo.git``."""
import contextlib
import os
from planemo import git
from .test_utils import io
EXPECTED_HELLO_REV = "1c36390f585f8baa953548c00fc18c58e32fcf8b"
COMMITTER_DATE = "GIT_COMMITTER_DATE='2000-01-01T00:00:00+0000'"
COMMITTER_NAME = "GIT_COMMITTER_NAME='a' GIT_COMMITTER_EMAIL='[email protected]'"
COMMIT = ("git commit --date='2000-01-01T00:00:00+0000' "
"--author='a <[email protected]>' -m 'initial'")
def test_rev():
with _git_directory() as t:
rev = git.rev(None, t)
assert rev == EXPECTED_HELLO_REV, rev
def test_rev_if_git():
with io.temp_directory() as t:
rev = git.rev_if_git(None, t)
assert rev is None
def test_rev_dirty_if_git():
with _git_directory() as t:
io.write_file(os.path.join(t, "README"), "Hello World!")
rev = git.rev_if_git(None, t)
assert rev == EXPECTED_HELLO_REV + "-dirty", rev
def test_diff():
with _git_directory() as t:
io.write_file(os.path.join(t, "README"), "new docs")
_add_and_commit(t, ["README"])
io.write_file(os.path.join(t, "file1"), "file1")
_add_and_commit(t, ["file1"])
io.write_file(os.path.join(t, "file1"), "file1 changed")
_add_and_commit(t, ["file1"])
assert git.diff(None, t, "HEAD~..HEAD") == ["file1"]
assert "README" in git.diff(None, t, "HEAD~~~..HEAD")
@contextlib.contextmanager
def _git_directory():
with io.temp_directory() as t:
io.write_file(os.path.join(t, "README"), "Hello!")
_add_and_commit(t, ["README"], init=True)
yield t
def _add_and_commit(t, files, init=False):
cmd = " && ".join([
"cd '{0}'",
"git init ." if init else "true",
"git add %s" % " ".join(files),
_commit_command(),
]).format(t)
assert io.shell(cmd) == 0
def _commit_command():
return "%s %s %s" % (
COMMITTER_DATE,
COMMITTER_NAME,
COMMIT
)