forked from TheKevJames/coveralls-python
/
git_test.py
156 lines (131 loc) · 4.9 KB
/
git_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# pylint: disable=no-self-use
import os
import re
import shutil
import subprocess
import tempfile
import unittest
from unittest import mock
import coveralls.git
from coveralls.exception import CoverallsException
GIT_COMMIT_MSG = 'first commit'
GIT_EMAIL = 'me@here.com'
GIT_NAME = 'Daniël'
GIT_REMOTE = 'origin'
GIT_URL = 'https://github.com/username/Hello-World.git'
class GitTest(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp()
os.chdir(self.dir)
open('README', 'a').close()
subprocess.call(['git', 'init'], cwd=self.dir)
subprocess.call(['git', 'config', 'user.name',
'"{}"'.format(GIT_NAME)], cwd=self.dir)
subprocess.call(['git', 'config', 'user.email',
'"{}"'.format(GIT_EMAIL)], cwd=self.dir)
subprocess.call(['git', 'add', 'README'], cwd=self.dir)
subprocess.call(['git', 'commit', '-m', GIT_COMMIT_MSG], cwd=self.dir)
subprocess.call(['git', 'remote', 'add', GIT_REMOTE, GIT_URL],
cwd=self.dir)
def tearDown(self):
shutil.rmtree(self.dir)
@mock.patch.dict(os.environ, {'TRAVIS_BRANCH': 'master'}, clear=True)
def test_git(self):
git_info = coveralls.git.git_info()
commit_id = git_info['git']['head'].pop('id')
assert re.match(r'^[a-f0-9]{40}$', commit_id)
assert git_info == {
'git': {
'head': {
'committer_email': GIT_EMAIL,
'author_email': GIT_EMAIL,
'author_name': GIT_NAME,
'message': GIT_COMMIT_MSG,
'committer_name': GIT_NAME,
},
'remotes': [{
'url': GIT_URL,
'name': GIT_REMOTE
}],
'branch': 'master'
}
}
class GitLogTest(GitTest):
def test_gitlog(self):
git_info = coveralls.git.gitlog('%H')
assert re.match(r'^[a-f0-9]{40}$', git_info)
assert coveralls.git.gitlog('%aN') == GIT_NAME
assert coveralls.git.gitlog('%ae') == GIT_EMAIL
assert coveralls.git.gitlog('%cN') == GIT_NAME
assert coveralls.git.gitlog('%ce') == GIT_EMAIL
assert coveralls.git.gitlog('%s') == GIT_COMMIT_MSG
class GitInfoTestEnvVars(unittest.TestCase):
@mock.patch.dict(os.environ, {
'GIT_ID': '5e837ce92220be64821128a70f6093f836dd2c05',
'GIT_BRANCH': 'master',
'GIT_AUTHOR_NAME': GIT_NAME,
'GIT_AUTHOR_EMAIL': GIT_EMAIL,
'GIT_COMMITTER_NAME': GIT_NAME,
'GIT_COMMITTER_EMAIL': GIT_EMAIL,
'GIT_MESSAGE': GIT_COMMIT_MSG,
'GIT_URL': GIT_URL,
'GIT_REMOTE': GIT_REMOTE,
}, clear=True)
def test_gitlog_envvars(self):
git_info = coveralls.git.git_info()
commit_id = git_info['git']['head'].pop('id')
assert re.match(r'^[a-f0-9]{40}$', commit_id)
assert git_info == {
'git': {
'head': {
'committer_email': GIT_EMAIL,
'author_email': GIT_EMAIL,
'author_name': GIT_NAME,
'message': GIT_COMMIT_MSG,
'committer_name': GIT_NAME,
},
'remotes': [{
'url': GIT_URL,
'name': GIT_REMOTE,
}],
'branch': 'master',
},
}
class GitInfoTestNotAGitRepository(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp()
os.chdir(self.dir)
def tearDown(self):
shutil.rmtree(self.dir)
def test_gitlog_not_a_git_repo(self):
git_info = coveralls.git.git_info()
self.assertRaises(CoverallsException)
assert git_info == {}
class GitInfoTestBranch(GitTest):
@mock.patch.dict(os.environ, {
'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/pull/1234/merge',
'GITHUB_SHA': 'bb0e00166b28f49db04d6a8b8cb4bddb5afa529f',
'GITHUB_HEAD_REF': 'fixup-branch'
}, clear=True)
def test_gitinfo_github_pr(self):
git_info = coveralls.git.git_info()
assert git_info['git']['branch'] == 'fixup-branch'
@mock.patch.dict(os.environ, {
'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/heads/master',
'GITHUB_SHA': 'bb0e00166b28f49db04d6a8b8cb4bddb5afa529f',
'GITHUB_HEAD_REF': ''
}, clear=True)
def test_gitinfo_github_branch(self):
git_info = coveralls.git.git_info()
assert git_info['git']['branch'] == 'master'
@mock.patch.dict(os.environ, {
'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/tags/v1.0',
'GITHUB_SHA': 'bb0e00166b28f49db04d6a8b8cb4bddb5afa529f',
'GITHUB_HEAD_REF': ''
}, clear=True)
def test_gitinfo_github_tag(self):
git_info = coveralls.git.git_info()
assert git_info['git']['branch'] == 'v1.0'