This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitApi_test.py
112 lines (88 loc) · 2.83 KB
/
gitApi_test.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python
import pytest
import gitApi
import os
import shutil
import commands
import re
test_repo = 'just_for_test'
class TestGitApi:
############################
@pytest.fixture
def initializedRepo(self):
gitApi.init(test_repo)
return test_repo
@pytest.fixture
def editedFile(self):
filename = os.path.join(test_repo, 'foo.txt')
writeChange(filename)
return filename
@pytest.fixture
def stagedFile(self, editedFile):
gitApi.stage(editedFile) # I don't like calling the Api here... but intention better
return editedFile
############################
def setup_method(self, method):
if os.path.exists(test_repo):
shutil.rmtree(test_repo)
os.mkdir(test_repo)
def teardown_method(self, method):
shutil.rmtree(test_repo)
############################
def test_can_init_repo(self):
gitApi.init(test_repo)
assert isInitialized(test_repo)
def test_initialized_happy(self, initializedRepo):
assert gitApi.initialized(initializedRepo)
def test_initialized_sad(self):
assert not gitApi.initialized(test_repo)
def test_can_stage_change(self, initializedRepo, editedFile):
gitApi.stage(editedFile)
assert isStaged(editedFile)
def test_can_commit_change(self, initializedRepo, stagedFile):
gitApi.commit(initializedRepo, 'test commit')
assert isCommitted(stagedFile)
def test_can_commit_author_name(self, initializedRepo, stagedFile):
gitApi.commit(initializedRepo, 'test commit', author='Joe Blow <[email protected]>')
log = getCommitLog(initializedRepo)
assert 'Joe Blow' in log
def test_can_commit_another_date(self, initializedRepo, stagedFile):
gitApi.commit(initializedRepo, 'test commit', date='2010/01/01 0:00:00')
log = getCommitLog(initializedRepo)
assert '2010' in log
############################
def writeChange(filename):
f = open(filename, 'w')
f.write('garbage')
f.close()
def initializeRepo(repoPath):
filename = os.path.join(repoPath, '.git')
f = open(filename, 'w')
f.write('')
f.close()
def isInitialized(repoPath):
filename = os.path.join(test_repo, '.git')
return os.path.exists(filename)
def isStaged(filename):
basename = os.path.basename(filename)
dirname = os.path.dirname(filename)
origDir = os.getcwd()
os.chdir(dirname)
status = commands.getoutput('git status')
matches = re.findall('Changes to be committed:.*%s' % basename, status, flags=re.DOTALL)
os.chdir(origDir)
return len(matches) > 0
def isCommitted(filename):
basename = os.path.basename(filename)
dirname = os.path.dirname(filename)
origDir = os.getcwd()
os.chdir(dirname)
log = commands.getoutput('git log')
os.chdir(origDir)
return 'commit' in log
def getCommitLog(repo_path):
origDir = os.getcwd()
os.chdir(repo_path)
log = commands.getoutput('git log')
os.chdir(origDir)
return log