Skip to content

Commit

Permalink
If the server returns a message on toxresult upload, then print it as…
Browse files Browse the repository at this point in the history
… a warning.
  • Loading branch information
fschulze committed Apr 20, 2024
1 parent 9846021 commit 91d2398
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
14 changes: 12 additions & 2 deletions client/devpi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,17 @@ def get_error_message(self, debug):
message = getattr(self._response, 'text', '')
return message

@property
def content(self):
return self._response.content

@property
def headers(self):
return self._response.headers

def is_json(self):
return self.headers.get("content-type") == "application/json"

@property
def reason(self):
return self._response.reason
Expand All @@ -587,8 +598,7 @@ def result(self):
return self._json.get("result")

def json_get(self, jsonkey, default=None):
r = self._response
if not r.content or r.headers["content-type"] != "application/json":
if not self.content or not self.is_json():
return default
try:
return self._json[jsonkey]
Expand Down
3 changes: 3 additions & 0 deletions client/devpi/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ def post_tox_json_report(hub, href, jsondata):
r = hub.http_api("post", href, kvdict=jsondata)
if r.status_code == 200:
hub.info("successfully posted tox result data")
msg = r.json_get('message')
if msg:
hub.warn(msg)
else:
hub.error("could not post tox result data to: %s" % href)

Expand Down
1 change: 1 addition & 0 deletions client/news/toxresultupload.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If the server returns a message on toxresult upload, then print it as a warning.
38 changes: 29 additions & 9 deletions client/testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,19 +753,39 @@ class MockResponse:
status_code = reply_data["status"]
reason = reply_data.get("reason", "OK")

def __init__(self):
self.headers = {"content-type": "application/json"}
self.url = url
self.request = MockRequest()
self.request.method = method

def json(self):
return reply_data["json"]

response = MockResponse()
if response.status_code >= 400 and fatal:
hub.fatal(
f"{method} {url}\n"
f"{response.status_code} {response.reason}")
response.headers = {"content-type": "application/json"}
response.url = url
response.request = MockRequest()
response.request.method = method
return main.HTTPReply(response)
reply = main.HTTPReply(response)
if response.status_code in (200, 201):
# don't show any extra info on success code
return reply

if response.status_code >= 400:
if fatal:
out = hub.fatal
elif quiet:
return reply
else:
out = hub.error
elif quiet and not hub.args.debug:
return reply
else:
out = hub.info
if response.status_code >= 400 or hub.args.debug:
info = f"{method} {url}\n"
else:
info = ""
message = reply.get_error_message(hub.args.debug)
out(f"{info}{response.status_code} {response.reason}{message}")
return reply

def set(self, url, *, reason="OK", status=200, **kw):
""" Set a reply for all future uses. """
Expand Down
29 changes: 28 additions & 1 deletion client/testing/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ def test_post_tox_json_report(loghub, mock_http_api):
*posting*
*success*
""")
loghub._getmatcher().no_fnmatch_line('*200 OK*')


def test_post_tox_json_report_skip(loghub, mock_http_api):
mock_http_api.set("http://devpi.net", message="custom skip")
post_tox_json_report(loghub, "http://devpi.net", {"hello": "123"})
assert len(mock_http_api.called) == 1
loghub._getmatcher().fnmatch_lines("""
*posting*
*success*
custom skip
""")
loghub._getmatcher().no_fnmatch_line('*200 OK*')


def test_post_tox_json_report_error(loghub, mock_http_api):
Expand All @@ -40,7 +53,21 @@ def test_post_tox_json_report_forbidden(loghub, mock_http_api):
assert len(mock_http_api.called) == 1
loghub._getmatcher().fnmatch_lines("""
*posting*
*403 Forbidden*
*403 Forbidden
""")


def test_post_tox_json_report_forbidden_msg(loghub, mock_http_api):
mock_http_api.set(
"http://devpi.net/foo/bar/", reason="Forbidden", status=403,
message="custom forbidden")
with pytest.raises(SystemExit) as excinfo:
post_tox_json_report(loghub, "http://devpi.net/foo/bar/", {"hello": "123"})
assert excinfo.value.code == 1
assert len(mock_http_api.called) == 1
loghub._getmatcher().fnmatch_lines("""
*posting*
*403 Forbidden: custom forbidden
""")


Expand Down

0 comments on commit 91d2398

Please sign in to comment.