| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 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 json | 6 import json |
| 7 import unittest | 7 import unittest |
| 8 | 8 |
| 9 from compiled_file_system import CompiledFileSystem | 9 from compiled_file_system import CompiledFileSystem |
| 10 from content_providers import ContentProviders | 10 from content_providers import ContentProviders |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 'dir': 'apples' | 22 'dir': 'apples' |
| 23 }, | 23 }, |
| 24 'serveFrom': 'apples-dir', | 24 'serveFrom': 'apples-dir', |
| 25 }, | 25 }, |
| 26 'bananas': { | 26 'bananas': { |
| 27 'serveFrom': '', | 27 'serveFrom': '', |
| 28 'chromium': { | 28 'chromium': { |
| 29 'dir': '' | 29 'dir': '' |
| 30 }, | 30 }, |
| 31 }, | 31 }, |
| 32 'github-provider': { |
| 33 'serveFrom': 'gh', |
| 34 'github': { |
| 35 'owner': 'GoogleChrome', |
| 36 'repo': 'hello-world', |
| 37 }, |
| 38 }, |
| 39 'github-provider-with-dir': { |
| 40 'serveFrom': 'gh2', |
| 41 'github': { |
| 42 'dir': 'tomatoes/are/a', |
| 43 'owner': 'SomeOwner', |
| 44 'repo': 'some-repo', |
| 45 }, |
| 46 }, |
| 32 'tomatoes': { | 47 'tomatoes': { |
| 33 'serveFrom': 'tomatoes-dir/are/a', | 48 'serveFrom': 'tomatoes-dir/are/a', |
| 34 'chromium': { | 49 'chromium': { |
| 35 'dir': 'tomatoes/are/a' | 50 'dir': 'tomatoes/are/a' |
| 36 }, | 51 }, |
| 37 }, | 52 }, |
| 38 } | 53 } |
| 39 | 54 |
| 40 | 55 |
| 41 _FILE_SYSTEM_DATA = { | 56 _FILE_SYSTEM_DATA = { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 58 'vegetable.txt': 'no they aren\'t', | 73 'vegetable.txt': 'no they aren\'t', |
| 59 'fruit': { | 74 'fruit': { |
| 60 'cherry.txt': 'cherry tomatoes', | 75 'cherry.txt': 'cherry tomatoes', |
| 61 }, | 76 }, |
| 62 }, | 77 }, |
| 63 }, | 78 }, |
| 64 }, | 79 }, |
| 65 } | 80 } |
| 66 | 81 |
| 67 | 82 |
| 83 class _MockGithubFileSystemProvider(object): |
| 84 '''A GithubFileSystemProvider imitation which records every call to Create |
| 85 and returns them from GetAndReset. |
| 86 ''' |
| 87 |
| 88 def __init__(self, file_system): |
| 89 self._file_system = file_system |
| 90 self._calls = [] |
| 91 |
| 92 def Create(self, owner, repo): |
| 93 self._calls.append((owner, repo)) |
| 94 return self._file_system |
| 95 |
| 96 def GetAndReset(self): |
| 97 calls = self._calls |
| 98 self._calls = [] |
| 99 return calls |
| 100 |
| 101 |
| 68 class ContentProvidersTest(unittest.TestCase): | 102 class ContentProvidersTest(unittest.TestCase): |
| 69 def setUp(self): | 103 def setUp(self): |
| 104 test_file_system = TestFileSystem(_FILE_SYSTEM_DATA) |
| 105 self._github_fs_provider = _MockGithubFileSystemProvider(test_file_system) |
| 70 self._content_providers = ContentProviders( | 106 self._content_providers = ContentProviders( |
| 71 CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()), | 107 CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()), |
| 72 TestFileSystem(_FILE_SYSTEM_DATA)) | 108 test_file_system, |
| 109 self._github_fs_provider) |
| 73 | 110 |
| 74 def testSimpleRootPath(self): | 111 def testSimpleRootPath(self): |
| 75 provider = self._content_providers.GetByName('apples') | 112 provider = self._content_providers.GetByName('apples') |
| 76 self.assertEqual( | 113 self.assertEqual( |
| 77 'gala apples', | 114 'gala apples', |
| 78 provider.GetContentAndType(_HOST, 'gala.txt').Get().content) | 115 provider.GetContentAndType(_HOST, 'gala.txt').Get().content) |
| 79 self.assertEqual( | 116 self.assertEqual( |
| 80 'granny smith apples', | 117 'granny smith apples', |
| 81 provider.GetContentAndType(_HOST, 'green/granny smith.txt').Get() | 118 provider.GetContentAndType(_HOST, 'green/granny smith.txt').Get() |
| 82 .content) | 119 .content) |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 self.assertEqual('bananas', provider.name) | 157 self.assertEqual('bananas', provider.name) |
| 121 self.assertEqual('tomatoes-dir/are', path) | 158 self.assertEqual('tomatoes-dir/are', path) |
| 122 provider, path = self._content_providers.GetByServeFrom('') | 159 provider, path = self._content_providers.GetByServeFrom('') |
| 123 self.assertEqual('bananas', provider.name) | 160 self.assertEqual('bananas', provider.name) |
| 124 self.assertEqual('', path) | 161 self.assertEqual('', path) |
| 125 | 162 |
| 126 @DisableLogging('error') | 163 @DisableLogging('error') |
| 127 def testProviderNotFound(self): | 164 def testProviderNotFound(self): |
| 128 self.assertEqual(None, self._content_providers.GetByName('cabbages')) | 165 self.assertEqual(None, self._content_providers.GetByName('cabbages')) |
| 129 | 166 |
| 167 def testGithubContentProvider(self): |
| 168 provider, path = self._content_providers.GetByServeFrom( |
| 169 'gh/apples/green/granny smith.txt') |
| 170 self.assertEqual('github-provider', provider.name) |
| 171 self.assertEqual('apples/green/granny smith.txt', path) |
| 172 self.assertEqual([('GoogleChrome', 'hello-world')], |
| 173 self._github_fs_provider.GetAndReset()) |
| 174 self.assertEqual( |
| 175 'granny smith apples', |
| 176 provider.GetContentAndType(_HOST, path).Get().content) |
| 177 |
| 178 def testGithubContentProviderWithDir(self): |
| 179 provider, path = self._content_providers.GetByServeFrom( |
| 180 'gh2/fruit/cherry.txt') |
| 181 self.assertEqual('github-provider-with-dir', provider.name) |
| 182 self.assertEqual('fruit/cherry.txt', path) |
| 183 self.assertEqual([('SomeOwner', 'some-repo')], |
| 184 self._github_fs_provider.GetAndReset()) |
| 185 self.assertEqual( |
| 186 'cherry tomatoes', |
| 187 provider.GetContentAndType(_HOST, path).Get().content) |
| 130 | 188 |
| 131 if __name__ == '__main__': | 189 if __name__ == '__main__': |
| 132 unittest.main() | 190 unittest.main() |
| OLD | NEW |