-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the npm API's error handling and test coverage
- Loading branch information
1 parent
1247b5c
commit 431e008
Showing
2 changed files
with
40 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,41 @@ | ||
from pathlib import Path | ||
from subprocess import CalledProcessError | ||
|
||
import pytest | ||
from pytest_mock import MockerFixture | ||
|
||
from betty._npm import NpmRequirement, NpmUnavailable | ||
from betty._npm import NpmRequirement, NpmUnavailable, npm | ||
|
||
|
||
class TestNpm: | ||
async def test(self) -> None: | ||
await npm(["--version"]) | ||
|
||
async def test_command_not_found(self, mocker: MockerFixture) -> None: | ||
mocker.patch("betty.subprocess.run_process", side_effect=FileNotFoundError) | ||
with pytest.raises(NpmUnavailable): | ||
await npm(["--version"]) | ||
|
||
async def test_command_error(self, mocker: MockerFixture, tmp_path: Path) -> None: | ||
mocker.patch( | ||
"betty.subprocess.run_process", | ||
side_effect=CalledProcessError(1, "", "", ""), | ||
) | ||
with pytest.raises(CalledProcessError): | ||
await npm(["--version"]) | ||
|
||
|
||
class TestNpmRequirement: | ||
async def test_check_met(self) -> None: | ||
async def test_is_met(self) -> None: | ||
sut = await NpmRequirement.new() | ||
assert sut.is_met() | ||
|
||
async def test_check_unmet(self, mocker: MockerFixture) -> None: | ||
m_npm = mocker.patch("betty._npm.npm") | ||
m_npm.side_effect = NpmUnavailable() | ||
async def test_is_met_with_command_not_found(self, mocker: MockerFixture) -> None: | ||
mocker.patch("betty._npm.npm", side_effect=NpmUnavailable) | ||
sut = await NpmRequirement.new() | ||
assert not sut.is_met() | ||
|
||
async def test_is_met_with_command_error(self, mocker: MockerFixture) -> None: | ||
mocker.patch("betty._npm.npm", side_effect=CalledProcessError(1, "", "", "")) | ||
sut = await NpmRequirement.new() | ||
assert not sut.is_met() |