forked from mozilla/bughub
/
test_bughub.py
129 lines (98 loc) · 3.84 KB
/
test_bughub.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
"""
Tests for bughub.
"""
import json
import sys
from StringIO import StringIO
import bughub
def test_main(monkeypatch):
monkeypatch.setattr(bughub, "urlopen", fake_urlopen)
argv = [
"prog",
"github:user:repo:state=open",
"bugzilla:state=NEW",
]
monkeypatch.setattr(sys, "argv", argv)
stdout = StringIO()
bughub.main(stdout)
stdout.seek(0)
output = stdout.read()
output = output.replace("\r\n", "\n")
expected = """source,id,url,assigned,status,title,product,module,patch,feature
github,123,https://github.com/user/repo/issues/123,nobody,open,Fix the things,repo,repo,n,n
github,124,https://github.com/user/repo/issues/124,nobody,open,Fix the things,repo,repo,n,y
github,125,https://github.com/user/repo/issues/125,nobody,open,Fix the things,repo,repo,n,n
github,123,https://github.com/user/repo/issues/123,nobody,open,Fix the things,repo,repo,n,n
github,124,https://github.com/user/repo/issues/124,nobody,open,Fix the things,repo,repo,n,y
github,125,https://github.com/user/repo/issues/125,nobody,open,Fix the things,repo,repo,n,n
bugzilla,123,https://bugzilla.mozilla.org/show_bug.cgi?id=123,foo,open,Fix the things,Core,Component,n,n
bugzilla,124,https://bugzilla.mozilla.org/show_bug.cgi?id=124,foo,open,Fix the things,Core,Component,n,y
bugzilla,125,https://bugzilla.mozilla.org/show_bug.cgi?id=125,foo,open,Fix the things,Core,Component,n,n
"""
# Uncomment these lines for debugging
#print "output: %s" % output
#print "expected: %s" % expected
assert output == expected
def test_parse_source_github():
s = bughub.parse_source("github:user:repo:foo=bar:foo=baz")
assert s.user == "user"
assert s.repo == "repo"
assert s.filters == {"foo": ["bar", "baz"]}
def test_parse_source_bugzilla():
s = bughub.parse_source("bugzilla:foo=bar:foo=baz")
assert s.filters == {"foo": ["bar", "baz"]}
class FakeResponse(object):
def __init__(self, body, headers):
self.body = body
self.headers = headers
def read(self):
return self.body
def fake_github_issue(**kwargs):
defaults ={
"number": 123,
"assignee": None,
"state": "open",
"title": "Fix the things",
"pull_request": {"html_url": None},
"labels": []
}
defaults.update(kwargs)
defaults.setdefault(
"html_url",
"https://github.com/user/repo/issues/{0}".format(defaults["number"]),
)
return defaults
def fake_bugzilla_issue(**kwargs):
defaults = {
"id": 123,
"assigned_to": {"name": "foo"},
"status": "NEW",
"summary": "Fix the things",
"product": "Core",
"component": "Component",
"keywords": ""
}
defaults.update(kwargs)
return defaults
def fake_urlopen(url):
githubissues = [fake_github_issue(),
fake_github_issue(number=124,labels=[{"name":"foo","color":"red"},{"name":"task","color": "black"}]),
fake_github_issue(number=125,labels=[{"name":"foo","color":"blue"}])]
bugzillaissues = [fake_bugzilla_issue(),
fake_bugzilla_issue(id=124, keywords=["foo","feature"]),
fake_bugzilla_issue(id=125, keywords=["foo"])]
if "github" in url:
body = json.dumps(githubissues)
if "page=2" in url:
headers = {
"Link": 'Link: <https://github.com/user/repo/issues?page=1>; rel="first"'
}
else:
headers = {
"Link": 'Link: <https://github.com/user/repo/issues?page=2&state=open>; rel="next", <https://github.com/user/repo/issues?page=2&state=open>; rel="last"'
}
return FakeResponse(body, headers)
elif "bugzilla" in url:
return FakeResponse(json.dumps({"bugs": bugzillaissues}), {})
else:
raise NotImplementedError