OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import base64 | 6 import base64 |
7 import json | 7 import json |
8 import unittest | 8 import unittest |
9 | 9 |
10 from extensions_paths import SERVER2 | 10 from extensions_paths import SERVER2 |
11 from file_system import StatInfo | 11 from file_system import StatInfo |
12 from future import Future | 12 from future import Future |
13 from gitiles_file_system import (_CreateStatInfo, | 13 from gitiles_file_system import (_CreateStatInfo, |
14 _ParseGitilesJson, | 14 _ParseGitilesJson, |
15 GitilesFileSystem) | 15 GitilesFileSystem) |
16 from test_file_system import TestFileSystem | 16 from test_file_system import TestFileSystem |
17 from test_util import ReadFile | 17 from test_util import ReadFile |
18 | 18 |
19 | 19 |
20 _BASE_URL = '' | 20 _BASE_URL = '' |
21 _REAL_DATA_DIR = 'chrome/common/extensions/docs/templates/public/extensions/' | 21 _REAL_DATA_DIR = 'chrome/common/extensions/docs/templates/public/extensions/' |
22 _TEST_DATA = (SERVER2, 'test_data', 'gitiles_file_system', 'public_extensions') | |
22 # GitilesFileSystem expects file content to be encoded in base64. | 23 # GitilesFileSystem expects file content to be encoded in base64. |
23 _TEST_FS = { | 24 _TEST_FS = { |
24 'test1.txt': base64.b64encode('test1'), | 25 'test1.txt': base64.b64encode('test1'), |
25 'dir1': { | 26 'dir1': { |
26 'test2.txt': base64.b64encode('test2'), | 27 'test2.txt': base64.b64encode('test2'), |
27 'dir2': { | 28 'dir2': { |
28 'test3.txt': base64.b64encode('test3') | 29 'test3.txt': base64.b64encode('test3') |
29 } | 30 } |
30 } | 31 } |
31 } | 32 } |
(...skipping 15 matching lines...) Expand all Loading... | |
47 | 48 |
48 def FetchAsync(self, url): | 49 def FetchAsync(self, url): |
49 def resolve(): | 50 def resolve(): |
50 assert '?' in url | 51 assert '?' in url |
51 if url == _BASE_URL + '?format=JSON': | 52 if url == _BASE_URL + '?format=JSON': |
52 return _Response(json.dumps({'commit': 'a_commit'})) | 53 return _Response(json.dumps({'commit': 'a_commit'})) |
53 path, fmt = url.split('?') | 54 path, fmt = url.split('?') |
54 # Fetch urls are of the form <base_url>/<path>. We only want <path>. | 55 # Fetch urls are of the form <base_url>/<path>. We only want <path>. |
55 path = path.split('/', 1)[1] | 56 path = path.split('/', 1)[1] |
56 if path == _REAL_DATA_DIR: | 57 if path == _REAL_DATA_DIR: |
57 return _Response(ReadFile(SERVER2, 'test_data', 'gitiles_file_system', | 58 return _Response(ReadFile(*_TEST_DATA)) |
58 'public_extensions')) | |
59 # ALWAYS skip not found here. | 59 # ALWAYS skip not found here. |
60 content = self._fs.Read((path,), | 60 content = self._fs.Read((path,), |
61 skip_not_found=True).Get().get(path, None) | 61 skip_not_found=True).Get().get(path, None) |
62 if content is None: | 62 if content is None: |
63 # GitilesFS expects a DownloadError if the file wasn't found. | 63 # GitilesFS expects a DownloadError if the file wasn't found. |
64 raise DownloadError | 64 raise DownloadError |
65 # GitilesFS expects directory content as a JSON string. | 65 # GitilesFS expects directory content as a JSON string. |
66 if 'JSON' in fmt: | 66 if 'JSON' in fmt: |
67 content = json.dumps({ | 67 content = json.dumps({ |
68 'entries': [{'name': name} for name in content] | 68 'entries': [{ |
69 # GitilesFS expects directory names to not have a trailing '/'. | |
70 'name': name.rstrip('/'), | |
71 'type': 'tree' if name.endswith('/') else 'blob' | |
not at google - send to devlin
2014/08/21 20:58:29
You can use IsDirectory in path_util.py.
| |
72 } for name in content] | |
69 }) | 73 }) |
70 return _Response(content) | 74 return _Response(content) |
71 return Future(callback=resolve) | 75 return Future(callback=resolve) |
72 | 76 |
73 | 77 |
74 class GitilesFileSystemTest(unittest.TestCase): | 78 class GitilesFileSystemTest(unittest.TestCase): |
75 def setUp(self): | 79 def setUp(self): |
76 fetcher = _FakeGitilesFetcher(TestFileSystem(_TEST_FS)) | 80 fetcher = _FakeGitilesFetcher(TestFileSystem(_TEST_FS)) |
77 self._gitiles_fs = GitilesFileSystem(fetcher, _BASE_URL, '', '') | 81 self._gitiles_fs = GitilesFileSystem(fetcher, _BASE_URL, '', '') |
78 | 82 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 # Test skip not found. | 140 # Test skip not found. |
137 f = self._gitiles_fs.Read(['fakefile'], skip_not_found=True) | 141 f = self._gitiles_fs.Read(['fakefile'], skip_not_found=True) |
138 self.assertEqual(f.Get(), {}) | 142 self.assertEqual(f.Get(), {}) |
139 | 143 |
140 def testGetCommitID(self): | 144 def testGetCommitID(self): |
141 self.assertEqual(self._gitiles_fs.GetCommitID().Get(), 'a_commit') | 145 self.assertEqual(self._gitiles_fs.GetCommitID().Get(), 'a_commit') |
142 | 146 |
143 def testStat(self): | 147 def testStat(self): |
144 self.assertEqual(self._gitiles_fs.Stat(_REAL_DATA_DIR).version, | 148 self.assertEqual(self._gitiles_fs.Stat(_REAL_DATA_DIR).version, |
145 'ec21e736a3f00db2c0580e3cf71d91951656caec') | 149 'ec21e736a3f00db2c0580e3cf71d91951656caec') |
146 | 150 |
not at google - send to devlin
2014/08/21 20:58:29
Add a test for the GetIdentity method (you don't h
| |
147 | 151 |
148 if __name__ == '__main__': | 152 if __name__ == '__main__': |
149 unittest.main() | 153 unittest.main() |
OLD | NEW |