| OLD | NEW |
| (Empty) |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import io | |
| 6 import os.path | |
| 7 | |
| 8 from fetcher.dependency import Dependency | |
| 9 from fetcher.mojom_file import MojomFile | |
| 10 from fetcher.repository import Repository | |
| 11 | |
| 12 class FakeRepository(Repository): | |
| 13 data1 = """module test; | |
| 14 import "bar/baz.mojom"; | |
| 15 interface Foo {};""" | |
| 16 data2 = """module test; | |
| 17 import "services.domokit.org/foo/fiz.mojom"; | |
| 18 interface Baz {};""" | |
| 19 data3 = """module test; | |
| 20 import "services.fiz.org/foo/bar.mojom"; | |
| 21 interface Fiz {};""" | |
| 22 data4 = """module test; | |
| 23 interface SomeInterface {};""" | |
| 24 | |
| 25 def __init__(self, *args, **kwargs): | |
| 26 self.files_opened = [] | |
| 27 self.directories_walked = [] | |
| 28 self.all_files_available = False | |
| 29 | |
| 30 Repository.__init__(self, *args, **kwargs) | |
| 31 | |
| 32 def get_walk_base(self, directory): | |
| 33 data_base = [ | |
| 34 (directory, ["foo", "third_party"], []), | |
| 35 (os.path.join(directory, "foo"), ["bar"], ["foo.mojom"]), | |
| 36 (os.path.join(directory, "foo/bar"), [], ["baz.mojom"]), | |
| 37 (os.path.join(directory, "third_party"), ["external"], []), | |
| 38 (os.path.join(directory, "third_party/external"), | |
| 39 ["services.domokit.org"], []), | |
| 40 (os.path.join(directory, | |
| 41 "third_party/external/services.domokit.org"), | |
| 42 ["foo"], []), | |
| 43 (os.path.join(directory, | |
| 44 "third_party/external/services.domokit.org/foo"), | |
| 45 [], ["fiz.mojom"])] | |
| 46 if self.all_files_available: | |
| 47 data_base.extend([ | |
| 48 (os.path.join(directory, | |
| 49 "third_party/external/services.fiz.org"), | |
| 50 ["foo"], []), | |
| 51 (os.path.join(directory, | |
| 52 "third_party/external/services.fiz.org/foo"), | |
| 53 [], ["bar.mojom"])]) | |
| 54 return data_base | |
| 55 | |
| 56 def get_walk_external(self, directory): | |
| 57 data_external = [ | |
| 58 (directory, ["services.domokit.org"], []), | |
| 59 (os.path.join(directory, "services.domokit.org"), | |
| 60 ["foo"], []), | |
| 61 (os.path.join(directory, "services.domokit.org/foo"), | |
| 62 [], ["fiz.mojom"])] | |
| 63 if self.all_files_available: | |
| 64 data_external.extend([ | |
| 65 (os.path.join(directory, "services.fiz.org"), | |
| 66 ["foo"], []), | |
| 67 (os.path.join(directory, "services.fiz.org/foo"), | |
| 68 [], ["bar.mojom"])]) | |
| 69 return data_external | |
| 70 | |
| 71 | |
| 72 def _open(self, f): | |
| 73 self.files_opened.append(f) | |
| 74 | |
| 75 if "foo.mojom" in f: | |
| 76 val = io.BytesIO(self.data1) | |
| 77 elif "baz.mojom" in f: | |
| 78 val = io.BytesIO(self.data2) | |
| 79 elif "fiz.mojom" in f: | |
| 80 val = io.BytesIO(self.data3) | |
| 81 else: | |
| 82 val = io.BytesIO(self.data4) | |
| 83 return val | |
| 84 | |
| 85 def _os_walk(self, directory): | |
| 86 self.directories_walked.append(directory) | |
| 87 if directory == self._root_dir: | |
| 88 return iter(self.get_walk_base(directory)) | |
| 89 else: | |
| 90 return iter(self.get_walk_external(directory)) | |
| 91 | |
| 92 | |
| 93 class FakeDependency(Dependency): | |
| 94 IN_FILESYSTEM = [ | |
| 95 "/base/repo/third_party/external/example.com/dir/example.mojom", | |
| 96 "/base/repo/third_party/external/example.com/dir/dir.mojom", | |
| 97 "/base/repo/third_party/external/domokit.org/bar/baz/buzz.mojom", | |
| 98 "/base/repo/third_party/external/domokit.org/bar/foo/bar.mojom", | |
| 99 ] | |
| 100 | |
| 101 def _os_path_exists(self, path): | |
| 102 if path in self.IN_FILESYSTEM: | |
| 103 return True | |
| 104 elif os.path.join("/base/repo", path) in self.IN_FILESYSTEM: | |
| 105 return True | |
| 106 else: | |
| 107 return False | |
| 108 | |
| 109 | |
| 110 class FakeMojomFile(MojomFile): | |
| 111 def add_dependency(self, dependency): | |
| 112 """Declare a new dependency of this mojom.""" | |
| 113 self.deps.append(FakeDependency(self._repository, self.name, dependency)) | |
| OLD | NEW |