Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: chrome/common/extensions/docs/server2/gitiles_file_system_test.py

Issue 498503002: Docserver: Fixes for GitilesFileSystem (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/common/extensions/docs/server2/gitiles_file_system.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 path_util import IsDirectory
16 from test_file_system import TestFileSystem 17 from test_file_system import TestFileSystem
17 from test_util import ReadFile 18 from test_util import ReadFile
18 19
19 20
20 _BASE_URL = '' 21 _BASE_URL = ''
21 _REAL_DATA_DIR = 'chrome/common/extensions/docs/templates/public/extensions/' 22 _REAL_DATA_DIR = 'chrome/common/extensions/docs/templates/public/extensions/'
23 _TEST_DATA = (SERVER2, 'test_data', 'gitiles_file_system', 'public_extensions')
22 # GitilesFileSystem expects file content to be encoded in base64. 24 # GitilesFileSystem expects file content to be encoded in base64.
23 _TEST_FS = { 25 _TEST_FS = {
24 'test1.txt': base64.b64encode('test1'), 26 'test1.txt': base64.b64encode('test1'),
25 'dir1': { 27 'dir1': {
26 'test2.txt': base64.b64encode('test2'), 28 'test2.txt': base64.b64encode('test2'),
27 'dir2': { 29 'dir2': {
28 'test3.txt': base64.b64encode('test3') 30 'test3.txt': base64.b64encode('test3')
29 } 31 }
30 } 32 }
31 } 33 }
(...skipping 15 matching lines...) Expand all
47 49
48 def FetchAsync(self, url): 50 def FetchAsync(self, url):
49 def resolve(): 51 def resolve():
50 assert '?' in url 52 assert '?' in url
51 if url == _BASE_URL + '?format=JSON': 53 if url == _BASE_URL + '?format=JSON':
52 return _Response(json.dumps({'commit': 'a_commit'})) 54 return _Response(json.dumps({'commit': 'a_commit'}))
53 path, fmt = url.split('?') 55 path, fmt = url.split('?')
54 # Fetch urls are of the form <base_url>/<path>. We only want <path>. 56 # Fetch urls are of the form <base_url>/<path>. We only want <path>.
55 path = path.split('/', 1)[1] 57 path = path.split('/', 1)[1]
56 if path == _REAL_DATA_DIR: 58 if path == _REAL_DATA_DIR:
57 return _Response(ReadFile(SERVER2, 'test_data', 'gitiles_file_system', 59 return _Response(ReadFile(*_TEST_DATA))
58 'public_extensions'))
59 # ALWAYS skip not found here. 60 # ALWAYS skip not found here.
60 content = self._fs.Read((path,), 61 content = self._fs.Read((path,),
61 skip_not_found=True).Get().get(path, None) 62 skip_not_found=True).Get().get(path, None)
62 if content is None: 63 if content is None:
63 # GitilesFS expects a DownloadError if the file wasn't found. 64 # GitilesFS expects a DownloadError if the file wasn't found.
64 raise DownloadError 65 raise DownloadError
65 # GitilesFS expects directory content as a JSON string. 66 # GitilesFS expects directory content as a JSON string.
66 if 'JSON' in fmt: 67 if 'JSON' in fmt:
67 content = json.dumps({ 68 content = json.dumps({
68 'entries': [{'name': name} for name in content] 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]
69 }) 74 })
70 return _Response(content) 75 return _Response(content)
71 return Future(callback=resolve) 76 return Future(callback=resolve)
72 77
73 78
74 class GitilesFileSystemTest(unittest.TestCase): 79 class GitilesFileSystemTest(unittest.TestCase):
75 def setUp(self): 80 def setUp(self):
76 fetcher = _FakeGitilesFetcher(TestFileSystem(_TEST_FS)) 81 fetcher = _FakeGitilesFetcher(TestFileSystem(_TEST_FS))
77 self._gitiles_fs = GitilesFileSystem(fetcher, _BASE_URL, '', '') 82 self._gitiles_fs = GitilesFileSystem(fetcher, _BASE_URL, 'master', None)
78 83
79 def testParseGitilesJson(self): 84 def testParseGitilesJson(self):
80 test_json = '\n'.join([ 85 test_json = '\n'.join([
81 ')]}\'', 86 ')]}\'',
82 json.dumps({'commit': 'blah'}) 87 json.dumps({'commit': 'blah'})
83 ]) 88 ])
84 self.assertEqual(_ParseGitilesJson(test_json), {'commit': 'blah'}) 89 self.assertEqual(_ParseGitilesJson(test_json), {'commit': 'blah'})
85 90
86 def testCreateStatInfo(self): 91 def testCreateStatInfo(self):
87 test_json = '\n'.join([ 92 test_json = '\n'.join([
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 f = self._gitiles_fs.Read(['fakefile'], skip_not_found=True) 142 f = self._gitiles_fs.Read(['fakefile'], skip_not_found=True)
138 self.assertEqual(f.Get(), {}) 143 self.assertEqual(f.Get(), {})
139 144
140 def testGetCommitID(self): 145 def testGetCommitID(self):
141 self.assertEqual(self._gitiles_fs.GetCommitID().Get(), 'a_commit') 146 self.assertEqual(self._gitiles_fs.GetCommitID().Get(), 'a_commit')
142 147
143 def testStat(self): 148 def testStat(self):
144 self.assertEqual(self._gitiles_fs.Stat(_REAL_DATA_DIR).version, 149 self.assertEqual(self._gitiles_fs.Stat(_REAL_DATA_DIR).version,
145 'ec21e736a3f00db2c0580e3cf71d91951656caec') 150 'ec21e736a3f00db2c0580e3cf71d91951656caec')
146 151
152 def testGetIdentity(self):
153 # Test that file systems at different commits still have the same identity.
not at google - send to devlin 2014/08/22 00:19:17 LGTM, could you add another test that a different
154 other_gitiles_fs = GitilesFileSystem.Create(commit='abcdefghijklmnop')
155 self.assertEqual(self._gitiles_fs.GetIdentity(),
156 other_gitiles_fs.GetIdentity())
147 157
148 if __name__ == '__main__': 158 if __name__ == '__main__':
149 unittest.main() 159 unittest.main()
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/gitiles_file_system.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698