File size: 5,527 Bytes
a3dcdac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
diff --git a/tests/functional/project/test_project_manager_cache_contract.py b/tests/functional/project/test_project_manager_cache_contract.py
new file mode 100644
index 000000000..e9a040eb0
--- /dev/null
+++ b/tests/functional/project/test_project_manager_cache_contract.py
@@ -0,0 +1,157 @@
+from types import SimpleNamespace
+
+from ape.managers.project import ProjectManager
+from ape.types import ContractType
+
+
+class RecordingCompiler:
+    def __init__(self):
+        self.calls = []
+
+    def compile(self, paths):
+        paths = list(paths)
+        self.calls.append(tuple(str(path) for path in paths))
+        return [
+            ContractType(
+                contractName=f"{path.stem}:{path.read_text()}",
+                sourceId=str(path),
+                abi=[],
+            )
+            for path in paths
+        ]
+
+
+class CompilerManagerStub:
+    def __init__(self, compiler):
+        self.compiler = compiler
+        self.registered_compilers = {".foo": compiler}
+
+    def compile(self, paths):
+        return {
+            contract.contractName: contract
+            for contract in self.compiler.compile(paths)
+        }
+
+
+def make_project(tmp_path, compiler=None):
+    compiler = compiler or RecordingCompiler()
+    config = SimpleNamespace(PROJECT_FOLDER=tmp_path, name="", dependencies=[])
+    return ProjectManager(tmp_path, config, CompilerManagerStub(compiler)), compiler
+
+
+def test_missing_contract_directory_has_no_unknown_extensions(tmp_path):
+    project, _ = make_project(tmp_path)
+
+    assert project.sources_missing is True
+    assert project.extensions_with_missing_compilers == []
+
+
+def test_empty_contract_directory_is_reported_as_missing(tmp_path):
+    project, _ = make_project(tmp_path)
+    (tmp_path / "contracts").mkdir()
+    assert project.sources_missing is True
+    assert project.extensions_with_missing_compilers == []
+
+
+def make_nested_sources(tmp_path):
+    project, _ = make_project(tmp_path)
+    contracts = tmp_path / "contracts"
+    (contracts / "nested").mkdir(parents=True)
+    for relative in (
+        "z.foo",
+        "a.bar",
+        "q.zeta",
+        "nested/m.foo",
+        "nested/b.bar",
+        "nested/a.alpha",
+    ):
+        (contracts / relative).write_text(relative)
+    return project, contracts
+
+
+def test_sources_are_recursive_and_sorted(tmp_path):
+    project, contracts = make_nested_sources(tmp_path)
+    assert [path.relative_to(contracts).as_posix() for path in project.sources] == [
+        "nested/m.foo",
+        "z.foo",
+    ]
+
+
+def test_unknown_extensions_are_recursive_and_sorted(tmp_path):
+    project, _ = make_nested_sources(tmp_path)
+    assert project.extensions_with_missing_compilers == [".alpha", ".bar", ".zeta"]
+
+
+def test_corrupt_manifest_cache_is_ignored_and_rebuilt(tmp_path):
+    project, compiler = make_project(tmp_path)
+    contracts = tmp_path / "contracts"
+    contracts.mkdir()
+    source = contracts / "one.foo"
+    source.write_text("v1")
+    cache = tmp_path / ".build" / "__local__.json"
+    cache.parent.mkdir()
+    cache.write_text("not-json")
+
+    loaded = project.load_contracts()
+
+    assert set(loaded) == {"one:v1"}
+    assert compiler.calls == [(str(source),)]
+
+
+def setup_cached_sources(tmp_path):
+    project, compiler = make_project(tmp_path)
+    contracts = tmp_path / "contracts"
+    contracts.mkdir()
+    first = contracts / "first.foo"
+    second = contracts / "second.foo"
+    first.write_text("v1")
+    second.write_text("v1")
+
+    initial = project.load_contracts()
+    assert set(initial) == {"first:v1", "second:v1"}
+    assert compiler.calls[-1] == (str(first), str(second))
+    return project, compiler, contracts, first, second
+
+
+def test_incremental_cache_compiles_only_changed_and_new_sources(tmp_path):
+    project, compiler, contracts, first, second = setup_cached_sources(tmp_path)
+    first.write_text("v2")
+    third = contracts / "third.foo"
+    third.write_text("v1")
+    updated = project.load_contracts()
+    assert compiler.calls[-1] == (str(first), str(third))
+    assert "second:v1" in updated
+    assert "third:v1" in updated
+
+
+def test_changed_source_replaces_its_stale_contract_types(tmp_path):
+    project, _, _, first, _ = setup_cached_sources(tmp_path)
+    first.write_text("v2")
+    updated = project.load_contracts()
+    assert "first:v1" not in updated
+    assert updated["first:v2"].sourceId == str(first)
+
+
+def test_deleted_source_drops_its_cached_contract_types(tmp_path):
+    project, _, _, _, second = setup_cached_sources(tmp_path)
+    second.unlink()
+    project.load_contracts()
+
+    reloaded_project, _ = make_project(tmp_path)
+    after_delete = reloaded_project.load_contracts()
+    assert set(after_delete) == {"first:v1"}
+
+
+def test_force_rebuild_recompiles_all_current_sources(tmp_path):
+    project, compiler = make_project(tmp_path)
+    contracts = tmp_path / "contracts"
+    contracts.mkdir()
+    first = contracts / "first.foo"
+    second = contracts / "second.foo"
+    first.write_text("v1")
+    second.write_text("v1")
+
+    project.load_contracts()
+    project.load_contracts(use_cache=False)
+
+    assert compiler.calls[-1] == (str(first), str(second))
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+set -euo pipefail
+
+
+pytest -q tests/functional/project/test_project_manager_cache_contract.py
+