33from unittest .mock import Mock
44
55import pytest
6+ import textwrap
67import responses as rsps
78import click
89from click .testing import CliRunner
10+ from ruamel .yaml import YAML
911
10- from archery .bot import CommentBot , CommandError , group
12+ from archery .bot import (
13+ CommentBot , CommandError , CrossbowCommentFormatter , group
14+ )
1115
1216
1317@pytest .fixture
@@ -17,9 +21,15 @@ def responses():
1721
1822
1923def load_fixture (name ):
20- path = Path (__file__ ).parent / 'fixtures' / '{}.json' . format ( name )
24+ path = Path (__file__ ).parent / 'fixtures' / name
2125 with path .open ('r' ) as fp :
22- return json .load (fp )
26+ if name .endswith ('.json' ):
27+ return json .load (fp )
28+ elif name .endswith ('.yaml' ):
29+ yaml = YAML ()
30+ return yaml .load (fp )
31+ else :
32+ return fp .read ()
2333
2434
2535def github_url (path ):
@@ -60,13 +70,28 @@ def test_click_based_commands():
6070 assert custom_handler ('extra' , extra = 'data' ) == {'extra' : 'data' }
6171
6272
73+ def test_crossbow_comment_formatter ():
74+ job = load_fixture ('crossbow-job.yaml' )
75+ msg = load_fixture ('crossbow-success-message.md' )
76+
77+ formatter = CrossbowCommentFormatter (crossbow_repo = 'ursa-labs/crossbow' )
78+ response = formatter .render (job )
79+ expected = msg .format (
80+ repo = 'ursa-labs/crossbow' ,
81+ branch = 'ursabot-1' ,
82+ revision = 'f766a1d615dd1b7ee706d05102e579195951a61c' ,
83+ status = 'has been succeeded.'
84+ )
85+ assert response == textwrap .dedent (expected ).strip ()
86+
87+
6388@pytest .mark .parametrize ('fixture_name' , [
6489 # the bot is not mentiond, nothing to do
65- 'event-issue-comment-not-mentioning-ursabot' ,
90+ 'event-issue-comment-not-mentioning-ursabot.json ' ,
6691 # don't respond to itself, it prevents recursive comment storms!
67- 'event-issue-comment-by-ursabot' ,
92+ 'event-issue-comment-by-ursabot.json ' ,
6893 # non-authorized user sent the comment, do not respond
69- 'event-issue-comment-by-non-authorized-user' ,
94+ 'event-issue-comment-by-non-authorized-user.json ' ,
7095])
7196def test_noop_events (fixture_name ):
7297 payload = load_fixture (fixture_name )
@@ -82,7 +107,7 @@ def test_issue_comment_without_pull_request(responses):
82107 responses .add (
83108 responses .GET ,
84109 github_url ('/repositories/169101701/issues/19' ),
85- json = load_fixture ('issue-19' ),
110+ json = load_fixture ('issue-19.json ' ),
86111 status = 200
87112 )
88113 responses .add (
@@ -100,7 +125,7 @@ def test_issue_comment_without_pull_request(responses):
100125 def handler (command , ** kwargs ):
101126 pass
102127
103- payload = load_fixture ('event-issue-comment-without-pull-request' )
128+ payload = load_fixture ('event-issue-comment-without-pull-request.json ' )
104129 bot = CommentBot (name = 'ursabot' , token = '' , handler = handler )
105130 bot .handle ('issue_comment' , payload )
106131
@@ -114,19 +139,19 @@ def test_respond_with_usage(responses):
114139 responses .add (
115140 responses .GET ,
116141 github_url ('/repositories/169101701/issues/26' ),
117- json = load_fixture ('issue-26' ),
142+ json = load_fixture ('issue-26.json ' ),
118143 status = 200
119144 )
120145 responses .add (
121146 responses .GET ,
122147 github_url ('/repos/ursa-labs/ursabot/pulls/26' ),
123- json = load_fixture ('pull-request-26' ),
148+ json = load_fixture ('pull-request-26.json ' ),
124149 status = 200
125150 )
126151 responses .add (
127152 responses .GET ,
128153 github_url ('/repos/ursa-labs/ursabot/issues/comments/480243811' ),
129- json = load_fixture ('issue-comment-480243811' )
154+ json = load_fixture ('issue-comment-480243811.json ' )
130155 )
131156 responses .add (
132157 responses .POST ,
@@ -137,7 +162,7 @@ def test_respond_with_usage(responses):
137162 def handler (command , ** kwargs ):
138163 raise CommandError ('test-usage' )
139164
140- payload = load_fixture ('event-issue-comment-with-empty-command' )
165+ payload = load_fixture ('event-issue-comment-with-empty-command.json ' )
141166 bot = CommentBot (name = 'ursabot' , token = '' , handler = handler )
142167 bot .handle ('issue_comment' , payload )
143168
@@ -153,19 +178,19 @@ def test_issue_comment_with_commands(responses, command, reaction):
153178 responses .add (
154179 responses .GET ,
155180 github_url ('/repositories/169101701/issues/26' ),
156- json = load_fixture ('issue-26' ),
181+ json = load_fixture ('issue-26.json ' ),
157182 status = 200
158183 )
159184 responses .add (
160185 responses .GET ,
161186 github_url ('/repos/ursa-labs/ursabot/pulls/26' ),
162- json = load_fixture ('pull-request-26' ),
187+ json = load_fixture ('pull-request-26.json ' ),
163188 status = 200
164189 )
165190 responses .add (
166191 responses .GET ,
167192 github_url ('/repos/ursa-labs/ursabot/issues/comments/480248726' ),
168- json = load_fixture ('issue-comment-480248726' )
193+ json = load_fixture ('issue-comment-480248726.json ' )
169194 )
170195 responses .add (
171196 responses .POST ,
@@ -181,7 +206,7 @@ def handler(command, **kwargs):
181206 else :
182207 raise ValueError ('Only `build` command is supported.' )
183208
184- payload = load_fixture ('event-issue-comment-build-command' )
209+ payload = load_fixture ('event-issue-comment-build-command.json ' )
185210 payload ["comment" ]["body" ] = command
186211
187212 bot = CommentBot (name = 'ursabot' , token = '' , handler = handler )
0 commit comments