Skip to content

Commit

Permalink
fix: unnecessary modification (#1695)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Oct 9, 2023
1 parent 8f06f0a commit adba4e4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
25 changes: 14 additions & 11 deletions src/ape/api/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,22 @@ def cached_manifest(self) -> Optional[PackageManifest]:

@property
def contracts(self) -> Dict[str, ContractType]:
if self._contracts is None:
contracts = {}
for p in self._cache_folder.glob("*.json"):
if p == self.manifest_cachefile:
continue
if self._contracts is not None:
return self._contracts

contracts = {}
for p in self._cache_folder.glob("*.json"):
if p == self.manifest_cachefile:
continue

contract_name = p.stem
contract_type = ContractType.parse_file(p)
if contract_type.name is None:
contract_type.name = contract_name

contract_name = p.stem
contract_type = ContractType.parse_file(p)
if contract_type.name is None:
contract_type.name = contract_name
contracts[contract_type.name] = contract_type

contracts[contract_type.name] = contract_type
self._contracts = contracts
self._contracts = contracts
return self._contracts

@property
Expand Down
2 changes: 1 addition & 1 deletion src/ape/managers/project/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def contracts(self) -> Dict[str, ContractType]:
Returns:
Dict[str, ``ContractType``]
"""
if self.local_project._cached_manifest is None:
if self.local_project.cached_manifest is None:
return self.load_contracts()

return self.local_project.contracts
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/test_project.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import shutil
from pathlib import Path
from urllib.parse import urlparse
Expand Down Expand Up @@ -394,3 +395,21 @@ def test_getattr_contract_not_exists(project):
contract.touch()
with pytest.raises(AttributeError, match=expected):
_ = project.ThisIsNotAContractThatExists


def test_build_file_only_modified_once(project_with_contract):
project = project_with_contract
artifact = project.path / ".build" / "__local__.json"
_ = project.contracts # Ensure compiled.

# NOTE: This is how re-create the bug. Delete the underscore-prefixed
# cached object and attempt to re-compile. Previously, the ProjectManager
# was relying on an internal cache rather than the external one, and thus
# caused the file to get unnecessarily re-made (modified).
project.local_project._cached_manifest = None

# Prove the file is not unnecessarily modified.
time_before = os.path.getmtime(artifact)
_ = project.contracts
time_after = os.path.getmtime(artifact)
assert time_before == time_after

0 comments on commit adba4e4

Please sign in to comment.