OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 import base64 | |
7 import json | |
8 import unittest | |
9 | |
10 from extensions_paths import SERVER2 | |
11 from file_system import StatInfo | |
12 from future import Future | |
13 from gitiles_file_system import (_CreateStatInfo, | |
14 _ParseGitilesJson, | |
15 GitilesFileSystem) | |
16 from path_util import IsDirectory | |
17 from test_file_system import TestFileSystem | |
18 from test_util import ReadFile | |
19 | |
20 | |
21 _BASE_URL = '' | |
22 _REAL_DATA_DIR = 'chrome/common/extensions/docs/templates/public/extensions/' | |
23 _TEST_DATA = (SERVER2, 'test_data', 'gitiles_file_system', 'public_extensions') | |
24 # GitilesFileSystem expects file content to be encoded in base64. | |
25 _TEST_FS = { | |
26 'test1.txt': base64.b64encode('test1'), | |
27 'dir1': { | |
28 'test2.txt': base64.b64encode('test2'), | |
29 'dir2': { | |
30 'test3.txt': base64.b64encode('test3') | |
31 } | |
32 } | |
33 } | |
34 | |
35 | |
36 class _Response(object): | |
37 def __init__(self, content=''): | |
38 self.content = content | |
39 self.status_code = 200 | |
40 | |
41 | |
42 class DownloadError(Exception): | |
43 pass | |
44 | |
45 | |
46 class _FakeGitilesFetcher(object): | |
47 def __init__(self, fs): | |
48 self._fs = fs | |
49 | |
50 def FetchAsync(self, url, access_token=None): | |
51 def resolve(): | |
52 assert '?' in url | |
53 if url == _BASE_URL + '?format=JSON': | |
54 return _Response(json.dumps({'commit': 'a_commit'})) | |
55 path, fmt = url.split('?') | |
56 # Fetch urls are of the form <base_url>/<path>. We only want <path>. | |
57 path = path.split('/', 1)[1] | |
58 if path == _REAL_DATA_DIR: | |
59 return _Response(ReadFile(*_TEST_DATA)) | |
60 # ALWAYS skip not found here. | |
61 content = self._fs.Read((path,), | |
62 skip_not_found=True).Get().get(path, None) | |
63 if content is None: | |
64 # GitilesFS expects a DownloadError if the file wasn't found. | |
65 raise DownloadError | |
66 # GitilesFS expects directory content as a JSON string. | |
67 if 'JSON' in fmt: | |
68 content = json.dumps({ | |
69 'entries': [{ | |
70 # GitilesFS expects directory names to not have a trailing '/'. | |
71 'name': name.rstrip('/'), | |
72 'type': 'tree' if IsDirectory(name) else 'blob' | |
73 } for name in content] | |
74 }) | |
75 return _Response(content) | |
76 return Future(callback=resolve) | |
77 | |
78 | |
79 class GitilesFileSystemTest(unittest.TestCase): | |
80 def setUp(self): | |
81 fetcher = _FakeGitilesFetcher(TestFileSystem(_TEST_FS)) | |
82 self._gitiles_fs = GitilesFileSystem(fetcher, _BASE_URL, 'master', None) | |
83 | |
84 def testParseGitilesJson(self): | |
85 test_json = '\n'.join([ | |
86 ')]}\'', | |
87 json.dumps({'commit': 'blah'}) | |
88 ]) | |
89 self.assertEqual(_ParseGitilesJson(test_json), {'commit': 'blah'}) | |
90 | |
91 def testCreateStatInfo(self): | |
92 test_json = '\n'.join([ | |
93 ')]}\'', | |
94 json.dumps({ | |
95 'id': 'some_long_string', | |
96 'entries': [ | |
97 { | |
98 'mode': 33188, | |
99 'type': 'blob', | |
100 'id': 'long_id', | |
101 'name': '.gitignore' | |
102 }, | |
103 { | |
104 'mode': 33188, | |
105 'type': 'blob', | |
106 'id': 'another_long_id', | |
107 'name': 'PRESUBMIT.py' | |
108 }, | |
109 { | |
110 'mode': 33188, | |
111 'type': 'blob', | |
112 'id': 'yali', | |
113 'name': 'README' | |
114 } | |
115 ] | |
116 }) | |
117 ]) | |
118 expected_stat_info = StatInfo('some_long_string', { | |
119 '.gitignore': 'long_id', | |
120 'PRESUBMIT.py': 'another_long_id', | |
121 'README': 'yali' | |
122 }) | |
123 self.assertEqual(_CreateStatInfo(test_json), expected_stat_info) | |
124 | |
125 def testRead(self): | |
126 # Read a top-level file. | |
127 f = self._gitiles_fs.Read(['test1.txt']) | |
128 self.assertEqual(f.Get(), {'test1.txt': 'test1'}) | |
129 # Read a top-level directory. | |
130 f = self._gitiles_fs.Read(['dir1/']) | |
131 self.assertEqual(f.Get(), {'dir1/': sorted(['test2.txt', 'dir2/'])}) | |
132 # Read a nested file. | |
133 f = self._gitiles_fs.Read(['dir1/test2.txt']) | |
134 self.assertEqual(f.Get(), {'dir1/test2.txt': 'test2'}) | |
135 # Read a nested directory. | |
136 f = self._gitiles_fs.Read(['dir1/dir2/']) | |
137 self.assertEqual(f.Get(), {'dir1/dir2/': ['test3.txt']}) | |
138 # Read multiple paths. | |
139 f = self._gitiles_fs.Read(['test1.txt', 'dir1/test2.txt']) | |
140 self.assertEqual(f.Get(), {'test1.txt': 'test1', 'dir1/test2.txt': 'test2'}) | |
141 # Test skip not found. | |
142 f = self._gitiles_fs.Read(['fakefile'], skip_not_found=True) | |
143 self.assertEqual(f.Get(), {}) | |
144 | |
145 def testGetCommitID(self): | |
146 self.assertEqual(self._gitiles_fs.GetCommitID().Get(), 'a_commit') | |
147 | |
148 def testStat(self): | |
149 self.assertEqual(self._gitiles_fs.Stat(_REAL_DATA_DIR).version, | |
150 'ec21e736a3f00db2c0580e3cf71d91951656caec') | |
151 | |
152 def testGetIdentity(self): | |
153 # Test that file systems at different commits still have the same identity. | |
154 other_gitiles_fs = GitilesFileSystem.Create(commit='abcdefghijklmnop') | |
155 self.assertEqual(self._gitiles_fs.GetIdentity(), | |
156 other_gitiles_fs.GetIdentity()) | |
157 | |
158 yet_another_gitiles_fs = GitilesFileSystem.Create(branch='different') | |
159 self.assertNotEqual(self._gitiles_fs.GetIdentity(), | |
160 yet_another_gitiles_fs.GetIdentity()) | |
161 | |
162 if __name__ == '__main__': | |
163 unittest.main() | |
OLD | NEW |