Browse Source

Merge "Handle correctly http error raise in run_http_exc"

Jenkins 4 years ago
parent
commit
0861f1fa88
3 changed files with 51 additions and 4 deletions
  1. 4
    0
      git-review.1
  2. 9
    4
      git_review/cmd.py
  3. 38
    0
      git_review/tests/test_unit.py

+ 4
- 0
git-review.1 View File

@@ -321,6 +321,10 @@ or code.  This exit status will also be returned if the patchset is already
321 321
 applied to the current branch.
322 322
 .It 70
323 323
 Cannot determine top level Git directory or .git subdirectory path.
324
+.It 101
325
+Unauthorized (401) http request done by git-review.
326
+.It 104
327
+Not Found (404) http request done by git-review.
324 328
 .El
325 329
 .Pp
326 330
 Exit status larger than 31 indicates problem with

+ 9
- 4
git_review/cmd.py View File

@@ -154,6 +154,11 @@ def run_command_exc(klazz, *argv, **env):
154 154
     return output
155 155
 
156 156
 
157
+def http_code_2_return_code(code):
158
+    """Tranform http status code to system return code."""
159
+    return (code - 301) % 255 + 1
160
+
161
+
157 162
 def run_http_exc(klazz, url, **env):
158 163
     """Run http GET request url, on failure raise klazz
159 164
 
@@ -168,12 +173,12 @@ def run_http_exc(klazz, url, **env):
168 173
 
169 174
     try:
170 175
         res = requests.get(url, **env)
171
-        if not 200 <= res.status_code < 300:
172
-            code = (res.status_code - 301) % 255 + 1
173
-            raise klazz(code, res.text, ('GET', url), env)
174
-        return res
175 176
     except Exception as err:
176 177
         raise klazz(255, str(err), ('GET', url), env)
178
+    if not 200 <= res.status_code < 300:
179
+        raise klazz(http_code_2_return_code(res.status_code),
180
+                    res.text, ('GET', url), env)
181
+    return res
177 182
 
178 183
 
179 184
 def get_version():

+ 38
- 0
git_review/tests/test_unit.py View File

@@ -152,3 +152,41 @@ class GitReviewConsole(testtools.TestCase, fixtures.TestWithFixtures):
152 152
         self.assertTrue(cmd.check_use_color_output(),
153 153
                         "Failed to use fallback to color.ui when "
154 154
                         "color.review not present")
155
+
156
+
157
+class FakeResponse(object):
158
+
159
+    def __init__(self, code, text=""):
160
+        self.status_code = code
161
+        self.text = text
162
+
163
+
164
+class FakeException(Exception):
165
+
166
+    def __init__(self, code, *args, **kwargs):
167
+        super(FakeException, self).__init__(*args, **kwargs)
168
+        self.code = code
169
+
170
+
171
+class GitReviewUnitTest(testtools.TestCase):
172
+    """Class for misc unit tests."""
173
+
174
+    @mock.patch('requests.get', return_value=FakeResponse(404))
175
+    def test_run_http_exc_raise_http_error(self, mock_get):
176
+        url = 'http://gerrit.example.com'
177
+        try:
178
+            cmd.run_http_exc(FakeException, url)
179
+            self.fails('Exception expected')
180
+        except FakeException as err:
181
+            self.assertEqual(cmd.http_code_2_return_code(404), err.code)
182
+            mock_get.assert_called_once_with(url)
183
+
184
+    @mock.patch('requests.get', side_effect=Exception())
185
+    def test_run_http_exc_raise_unknown_error(self, mock_get):
186
+        url = 'http://gerrit.example.com'
187
+        try:
188
+            cmd.run_http_exc(FakeException, url)
189
+            self.fails('Exception expected')
190
+        except FakeException as err:
191
+            self.assertEqual(255, err.code)
192
+            mock_get.assert_called_once_with(url)

Loading…
Cancel
Save