forked from mozilla/agithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
166 lines (132 loc) · 3.94 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
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
157
158
159
160
161
162
163
164
165
166
from __future__ import print_function
from agithub import Github
##
# Test harness
###
# Test results
Pass = 'Pass'
Fail = 'Fail'
Skip = 'Skip'
class Test(object):
_the_label = 'test'
_the_testno = 0
tests = {}
def gatherTests(self, testObj):
for test in dir(self):
if test.startswith('test_'):
self.tests[test] = getattr(testObj, test)
print(self.tests)
def doTestsFor(self, api):
'''Run all tests over the given API session'''
results = []
for name, test in self.tests.items():
self._the_label = name
results.append(self.runTest(test, api))
fails = skips = passes = 0
for res in results:
if res == Pass:
passes +=1
elif res == Fail:
fails +=1
elif res == Skip:
skips +=1
else:
raise ValueError('Bad test result ' + (res))
print(
'\n'
' Results\n'
'--------------------------------------\n'
'Tests Run: ', len(results), '\n'
' Passed: ', passes, '\n'
' Failed: ', fails, '\n'
' Skipped: ', skips
)
def runTest(self, test, api):
'''Run a single test with the given API session'''
self._the_testno += 1
(stat, _) = test(api)
global Pass, Skip, Fail
if stat in [Pass, Fail, Skip]:
return stat
elif stat < 400:
result = Pass
elif stat >= 500:
result = Skip
else:
result = Fail
self.label(result)
return result
def setlabel(self, lbl):
'''Set the global field _the_label, which is used by runTest'''
self._the_label += ' ' + lbl
def label(self, result):
'''Print out a test label showing the result'''
print (result + ':', self._the_testno, self._the_label)
def haveAuth(self, api):
username = getattr(api.client, 'username', NotImplemented)
if username == NotImplemented or username == None:
return False
else:
return True
##
# Tests
###
class Basic (Test):
def __init__(self):
self.gatherTests(self)
def test_zen(self, api):
self.setlabel('Zen')
return api.zen.get()
def test_head(self, api):
self.setlabel('HEAD')
return api.head()
def test_userRepos(self, api):
if not self.haveAuth(api):
return (Skip, ())
return api.user.repos.head()
##
# Utility
###
# Session initializers
def initAnonymousSession(klass):
return klass()
def initAuthenticatedSession(klass, **kwargs):
for k in kwargs:
if k not in ['username', 'password', 'token']:
raise ValueError('Invalid test parameter: ' + str(k))
return klass(**kwargs)
# UI
def yesno(ans):
'''Convert user input (Yes or No) to a boolean'''
ans = ans.lower()
if ans == 'y' or ans == 'yes':
return True
else:
return False
##
# Main
###
if __name__ == '__main__':
anonSession = initAnonymousSession(Github)
authSession = None
ans = input(
'Some of the tests require an authenticated session.'
' Do you want to provide a username and password [y/N]? '
)
if yesno(ans):
username = input('Username: ')
password = input ('Password (plain text): ')
authSession = initAuthenticatedSession(
Github
, username=username, password=password
)
tests = filter(lambda var: var.startswith('test_'), globals().copy())
tester = Basic()
print('Unauthenticated tests')
tester.doTestsFor(anonSession)
print()
if authSession is None:
print('Skipping Authenticated tests')
else:
print('Authenticated tests')
tester.doTestsFor(authSession)