-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.py
76 lines (61 loc) · 2.44 KB
/
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
import json
import os
import subprocess
import tempfile
import unittest
from unittest import mock
import git
import readmesfix
import util
TEST_REPO_ABSOLUTE_PATH = f'{os.getcwd()}/test-repo.git'
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
@property
def text(self):
return json.dumps(self.json_data)
def json(self):
return self.json_data
original_clone_from = git.Repo.clone_from
def mocked_clone(*args, **kwargs):
if args[0] == "[email protected]:test-repo.git.git":
new_args = list(args)
new_args[0] = TEST_REPO_ABSOLUTE_PATH
else:
new_args = args
return original_clone_from(*new_args, **kwargs)
# noinspection PyUnusedLocal
def mocked_post(url, *args, **kwargs):
if url == 'https://api.github.com/repos/test-repo.git/forks':
return MockResponse({
'default_branch': 'master',
'ssh_url': TEST_REPO_ABSOLUTE_PATH,
'owner': {
'login': 'bryant1410',
},
}, 202)
elif url == 'https://api.github.com/repos/test-repo.git/pulls':
return MockResponse({}, 201)
else:
assert False, "It shouldn't reach this point for the given test"
class MainTest(unittest.TestCase):
@mock.patch('requests.post', side_effect=mocked_post)
@mock.patch('git.Repo.clone_from', side_effect=mocked_clone)
def test_main(self, mock_clone, mock_post):
try:
readmesfix.main('test.tsv')
self.assertTrue(mock_clone.called)
self.assertEqual(2, mock_post.call_count)
with tempfile.TemporaryDirectory() as temp_dir, util.pushd(temp_dir):
repo = git.Repo.clone_from(TEST_REPO_ABSOLUTE_PATH, '.')
diff = subprocess.run(['git', 'diff', 'HEAD~'], check=True, stdout=subprocess.PIPE).stdout
repo.git.reset('--hard', 'HEAD~')
repo.remotes['origin'].push(force=True)
with open('test.diff', 'rb') as gold_diff_file:
self.assertEqual(gold_diff_file.read(), diff)
finally:
with util.pushd('test-repo.git'):
subprocess.run('git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 -c gc.rerereresolved=0 '
'-c gc.rerereunresolved=0 -c gc.pruneExpire=now gc "$@"', shell=True, check=True,
stdout=subprocess.PIPE)