-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
67 lines (56 loc) · 2.34 KB
/
tests.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
import os
import requests
import subprocess as sp
import unittest
from github_compare import AccessToken, Repository
class TestRepository(unittest.TestCase):
def setUp(self):
self.session = requests.Session()
self.session.headers.update(
{
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
)
self.session.auth = AccessToken(os.environ.get("GH_TOKEN"))
self.repo = Repository(
organization="ijkim88", repository="veritone", session=self.session
)
def tearDown(self):
self.session.close()
def test_can_get_compare_commits(self):
commits = self.repo.get_compare_commits(
base="0984cda", head="71c4e08", per_page=2
)
self.assertEqual(len(list(commits)), 5)
def test_get_compare_commits_with_invalid_shas_raises_exception(self):
with self.assertRaises(requests.exceptions.HTTPError):
list(self.repo.get_compare_commits(base="bogus", head="sha"))
def test_can_print_compare_commit_messages(self):
with self.assertLogs("github_compare", level="INFO") as cm:
self.repo.print_compare_commit_messages(base="0984cda", head="71c4e08")
self.assertEqual(len(cm.output), 5)
def test_print_compare_commit_messages_with_invalid_shas_logs_error(self):
with self.assertLogs("github_compare", level="ERROR") as cm:
self.repo.print_compare_commit_messages(base="bogus", head="sha")
self.assertIn("ERROR:github_compare:Failed to get compare commit(s)", cm.output)
class TestCli(unittest.TestCase):
def test_can_print_compare_commit_messages(self):
process = sp.run(
"./github_compare.py ijkim88 veritone 0984cda 71c4e08",
capture_output=True,
shell=True,
)
commits = process.stderr.decode("utf-8").splitlines()
self.assertEqual(len(commits), 5)
def test_github_token_environment_variable_required(self):
env = dict(os.environ)
del env["GH_TOKEN"]
with self.assertRaises(sp.CalledProcessError):
sp.run(
"./github_compare.py ijkim88 veritone 0984cda 71c4e08",
check=True,
env=env,
shell=True,
stderr=sp.DEVNULL,
)