OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import base64 | 5 import base64 |
6 import httplib2 | 6 import httplib2 |
7 import os | 7 import os |
8 import time | 8 import time |
9 | 9 |
10 from infra.libs.gitiles import gitiles | 10 from infra.libs.gitiles import gitiles |
11 from testing_support import auto_stub | 11 from testing_support import auto_stub |
12 | 12 |
| 13 import mock |
13 | 14 |
14 DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') | 15 DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') |
15 | 16 |
16 | 17 |
17 class TestGitiles(auto_stub.TestCase): | 18 class TestGitiles(auto_stub.TestCase): |
18 def setUp(self): | 19 def setUp(self): |
19 super(TestGitiles, self).setUp() | 20 super(TestGitiles, self).setUp() |
20 | 21 |
21 self.response = {'status': '200'} | 22 self.response = {'status': 200} |
22 self.content = None | 23 self.content = None |
23 | 24 |
24 class FakeHttp(object): | 25 class FakeHttp(object): |
25 def __init__(self, response, content): | 26 def __init__(self, response, content): |
26 self.response = response | 27 self.response = response |
27 self.content = content | 28 self.content = content |
28 | 29 |
29 def request(self, *_args, **_kwargs): | 30 def request(self, *_args, **_kwargs): |
30 return self.response, self.content | 31 r = mock.Mock() |
| 32 r.status = self.response['status'] |
| 33 return r, self.content |
31 | 34 |
32 def http_maker(): | 35 def http_maker(): |
33 return FakeHttp(self.response, self.content) | 36 return FakeHttp(self.response, self.content) |
34 | 37 |
35 self.mock(httplib2, 'Http', http_maker) | 38 self.mock(httplib2, 'Http', http_maker) |
36 self.mock(time, 'sleep', lambda _x: None) | 39 self.mock(time, 'sleep', lambda _x: None) |
37 | 40 |
38 def testJson(self): | 41 def testJson(self): |
39 with open(os.path.join(DATA_DIR, 'scripts_slave')) as f: | 42 with open(os.path.join(DATA_DIR, 'scripts_slave')) as f: |
40 self.content = f.read() | 43 self.content = f.read() |
41 | 44 |
42 result = gitiles.call_gitiles( | 45 result = gitiles.call_gitiles( |
43 'http://bananas', | 46 'http://bananas', |
44 'json', | 47 'json', |
45 os.path.join(DATA_DIR, 'netrc')) | 48 os.path.join(DATA_DIR, 'netrc')) |
46 self.assertEqual(result['id'], '24a7f79e278700fab6dfd3866b1b8508c44ddb55') | 49 self.assertEqual(result['id'], '24a7f79e278700fab6dfd3866b1b8508c44ddb55') |
47 | 50 |
48 def testText(self): | 51 def testText(self): |
49 with open(os.path.join(DATA_DIR, 'init_py')) as f: | 52 with open(os.path.join(DATA_DIR, 'init_py')) as f: |
50 self.content = f.read() | 53 self.content = f.read() |
51 | 54 |
52 result = gitiles.call_gitiles( | 55 result = gitiles.call_gitiles( |
53 'http://bananas', | 56 'http://bananas', |
54 'text', | 57 'text', |
55 os.path.join(DATA_DIR, 'netrc')) | 58 os.path.join(DATA_DIR, 'netrc')) |
56 | 59 |
57 self.assertTrue(result.startswith('# Copyright 2015')) | 60 self.assertTrue(result.startswith('# Copyright 2015')) |
58 | 61 |
59 def testInvalidResponse(self): | 62 def testInvalidResponse(self): |
60 self.response = {'status': '500'} | 63 self.response = {'status': 500} |
61 with self.assertRaises(gitiles.GitilesError): | 64 with self.assertRaises(gitiles.GitilesError): |
62 gitiles.call_gitiles( | 65 gitiles.call_gitiles( |
63 'http://bananas', | 66 'http://bananas', |
| 67 'text', |
| 68 os.path.join(DATA_DIR, 'netrc')) |
| 69 |
| 70 def testErrorResponse(self): |
| 71 self.response = {'status': 400} |
| 72 with self.assertRaises(gitiles.GitilesError): |
| 73 gitiles.call_gitiles( |
| 74 'http://bananas', |
64 'text', | 75 'text', |
65 os.path.join(DATA_DIR, 'netrc')) | 76 os.path.join(DATA_DIR, 'netrc')) |
66 | 77 |
67 def testInvalidJsonResponse(self): | 78 def testInvalidJsonResponse(self): |
68 self.content = 'blerg\ndefinitely does not start with )]}\'' | 79 self.content = 'blerg\ndefinitely does not start with )]}\'' |
69 with self.assertRaises(gitiles.GitilesError): | 80 with self.assertRaises(gitiles.GitilesError): |
70 gitiles.call_gitiles( | 81 gitiles.call_gitiles( |
71 'http://bananas', | 82 'http://bananas', |
72 'json', | 83 'json', |
73 os.path.join(DATA_DIR, 'netrc')) | 84 os.path.join(DATA_DIR, 'netrc')) |
(...skipping 20 matching lines...) Expand all Loading... |
94 os.path.join(DATA_DIR, 'netrc')) | 105 os.path.join(DATA_DIR, 'netrc')) |
95 | 106 |
96 def testNoAuth(self): | 107 def testNoAuth(self): |
97 with open(os.path.join(DATA_DIR, 'scripts_slave')) as f: | 108 with open(os.path.join(DATA_DIR, 'scripts_slave')) as f: |
98 self.content = f.read() | 109 self.content = f.read() |
99 result = gitiles.call_gitiles( | 110 result = gitiles.call_gitiles( |
100 'http://twirly', | 111 'http://twirly', |
101 'json' | 112 'json' |
102 ) | 113 ) |
103 self.assertEqual(result['id'], '24a7f79e278700fab6dfd3866b1b8508c44ddb55') | 114 self.assertEqual(result['id'], '24a7f79e278700fab6dfd3866b1b8508c44ddb55') |
| 115 |
| 116 def testRepositoryCall(self): |
| 117 r = gitiles.Repository('http://example.com') |
| 118 self.content = ')]}\'\n{"commit": "foo"}' |
| 119 v = r.ref_info('master') |
| 120 self.assertEqual(v, {'commit': 'foo'}) |
| 121 |
| 122 def testRepositorySubpathCall(self): |
| 123 r = gitiles.Repository('http://example.com') |
| 124 self.content = ')]}\'\n{"commit": "foo"}' |
| 125 v = r(subpath='master/foo/bar/baz.py') |
| 126 self.assertEqual(v, {'commit': 'foo'}) |
OLD | NEW |