Skip to content

Commit

Permalink
feat: support global npm installs (#1716)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Nov 1, 2023
1 parent beb115b commit 201c334
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
13 changes: 12 additions & 1 deletion src/ape/managers/project/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,17 @@ def version_id(self) -> str:
f"Missing version for NPM dependency '{self.name}'. " "Have you run `npm install`?"
)

@property
def package_suffix(self) -> Path:
return Path("node_modules") / str(self.npm)

@property
def package_folder(self) -> Path:
return Path.cwd() / "node_modules" / str(self.npm)
return Path.cwd() / self.package_suffix

@property
def global_package_folder(self) -> Path:
return Path.home() / self.package_suffix

@cached_property
def version_from_json(self) -> Optional[str]:
Expand Down Expand Up @@ -417,6 +425,9 @@ def extract_manifest(self, use_cache: bool = True) -> PackageManifest:

return self._extract_local_manifest(self.package_folder, use_cache=use_cache)

elif self.global_package_folder.is_dir():
return self._extract_local_manifest(self.global_package_folder, use_cache=use_cache)

else:
raise ProjectError(f"NPM package '{self.npm}' not installed.")

Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,11 @@ def assert_last_log_with_retries(
pytest.fail(self.fail_message)

return ApeCaplog()


@pytest.fixture
def mock_home_directory(tmp_path):
original_home = Path.home()
Path.home = lambda: tmp_path # type: ignore[method-assign]
yield tmp_path
Path.home = lambda: original_home # type: ignore[method-assign]
33 changes: 20 additions & 13 deletions tests/functional/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,31 @@ def test_dependency_using_reference(ref, recwarn, dependency_manager):
assert DeprecationWarning not in [w.category for w in recwarn.list]


def test_npm_dependency():
def test_npm_dependency(mock_home_directory):
name = "@gnosis.pm"
package = "safe-singleton-factory"
version = "1.0.0"
dependency = NpmDependency(name=package, npm=f"{name}/{package}", version=version)
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
package_folder = Path(temp_dir) / "node_modules" / name / package
contracts_folder = package_folder / "contracts"
contracts_folder.mkdir(parents=True)
package_json = package_folder / "package.json"
package_json.write_text(f'{{"version": "{version}"}}')
file = contracts_folder / "contract.json"
source_content = '{"abi": []}'
file.write_text(source_content)
dependency = NpmDependency(name=package, npm=f"{name}/{package}", version=version)
manifest = dependency.extract_manifest()
assert manifest.sources
assert str(manifest.sources["contract.json"].content) == f"{source_content}\n"

# Test with both local and global node modules install.
for base in (Path(temp_dir), mock_home_directory):
package_folder = base / "node_modules" / name / package
contracts_folder = package_folder / "contracts"
contracts_folder.mkdir(parents=True)
package_json = package_folder / "package.json"
package_json.write_text(f'{{"version": "{version}"}}')
file = contracts_folder / "contract.json"
source_content = '{"abi": []}'
file.write_text(source_content)

manifest = dependency.extract_manifest(use_cache=False)

assert manifest.sources
assert str(manifest.sources["contract.json"].content) == f"{source_content}\n"

shutil.rmtree(package_folder)


def test_compile(project_with_downloaded_dependencies):
Expand Down

0 comments on commit 201c334

Please sign in to comment.