Skip to content

Commit

Permalink
tests - all tests implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
jjpaulo2 committed Aug 10, 2024
1 parent 3e232f4 commit 539114d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 13 deletions.
13 changes: 5 additions & 8 deletions pipconf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rich.panel import Panel
from rich.padding import Padding
from pipconf.configs import PipConfigs
from pipconf.consts import PADDING, ExitCodes, Chars, HelpPanels
from pipconf.consts import PADDING, PADDING_LIST, ExitCodes, Chars, HelpPanels
from pipconf import __help__


Expand Down Expand Up @@ -38,12 +38,7 @@ def list():
console.print(
Padding(
'\n'.join(lines),
(
0,
0,
1,
1,
),
PADDING_LIST,
)
)

Expand All @@ -69,7 +64,9 @@ def current():


@app.command(rich_help_panel=HelpPanels.DISPLAY)
def show(name: Annotated[Optional[str], Argument()] = None, local: bool = False):
def show(
name: Annotated[Optional[str], Argument()] = None, local: bool = False
):
"""Shows a config file content"""
try:
if local:
Expand Down
3 changes: 1 addition & 2 deletions pipconf/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,4 @@ def create(self, path: Path):
def show(self, path: Path) -> str:
if not path.exists():
raise EnvironmentError(f'The file {path} does not exist!')
with open(path, 'r') as file:
return file.read()
return path.read_text()
5 changes: 5 additions & 0 deletions pipconf/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
1,
)

PADDING_LIST = (
1,
1,
)


class Chars:
FILLED_CIRCLE = b'\xe2\x97\x8f'
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ quote-style = "single"
docstring-code-format = true

[tool.taskipy.tasks]
tests = { cmd = "pytest", help = "Runs all unit tests" }
tests = { cmd = "pytest -vvv", help = "Runs all unit tests" }
lint = { cmd = "ruff format && ruff check --fix", help = "Format and lint the code" }
security = { cmd = "bandit -c pyproject.toml -r fastrpa", help = "Check security issues on the code" }
security = { cmd = "bandit -c pyproject.toml -r pipconf", help = "Check security issues on the code" }
check = { cmd = "ruff format --check && ruff check && mypy .", help = "Check code issues" }

[build-system]
Expand Down
94 changes: 93 additions & 1 deletion tests/test_configs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pipconf.configs import PipConfigs
from unittest.mock import MagicMock, patch
from pytest import raises
from pytest import mark, raises
from pathlib import Path


Expand Down Expand Up @@ -71,3 +71,95 @@ def test_local():
def test_available_configs_empty():
with raises(EnvironmentError):
PipConfigs().available_configs


@patch('pipconf.configs.Path.home', HOME_MOCK)
@patch('pipconf.configs.Path.exists', MagicMock(return_value=True))
@patch('pipconf.configs.Path.iterdir', MagicMock(return_value=[SOME_PATH]))
@patch('pipconf.configs.Path.is_symlink', MagicMock(return_value=True))
def test_available_configs_empty_cause_of_symlinks():
with raises(EnvironmentError):
PipConfigs().available_configs


@patch('pipconf.configs.Path.home', HOME_MOCK)
@patch('pipconf.configs.Path.exists', MagicMock(return_value=True))
@patch('pipconf.configs.Path.iterdir', MagicMock(return_value=[SOME_PATH]))
@patch('pipconf.configs.Path.is_symlink', MagicMock(return_value=False))
def test_available_configs():
configs = PipConfigs()
assert configs.available_configs == [SOME_PATH]


@mark.parametrize(
('input_name', 'expected_return'),
[
('file', Path(HOME_DIRECTORY, '.pip/file.conf')),
('file.conf', Path(HOME_DIRECTORY, '.pip/file.conf')),
],
)
@patch('pipconf.configs.Path.home', HOME_MOCK)
@patch('pipconf.configs.Path.exists', MagicMock(return_value=True))
def test_get_path(input_name: str, expected_return: Path):
configs = PipConfigs()
assert configs.get_path(input_name) == expected_return


@patch('pipconf.configs.Path.home', HOME_MOCK)
@patch('pipconf.configs.Path.exists', MagicMock(return_value=False))
def test_select_path_does_not_exists():
with raises(EnvironmentError):
PipConfigs().select(SOME_PATH)


@patch('pipconf.configs.Path.home', HOME_MOCK)
@patch('pipconf.configs.Path.exists', MagicMock(return_value=True))
@patch('pipconf.configs.Path.is_symlink', MagicMock(return_value=True))
@patch('pipconf.configs.Path.unlink')
@patch('pipconf.configs.Path.symlink_to')
def test_select(symlink_to_mock: MagicMock, unlink_mock: MagicMock):
PipConfigs().select(SOME_PATH)
assert symlink_to_mock.call_count == 1
assert unlink_mock.call_count == 1


@patch('pipconf.configs.Path.home', HOME_MOCK)
@patch('pipconf.configs.Path.exists', MagicMock(return_value=True))
@patch('pipconf.configs.Path.is_symlink', MagicMock(return_value=False))
@patch('pipconf.configs.Path.unlink')
@patch('pipconf.configs.Path.symlink_to')
@patch('pipconf.configs.copyfile')
def test_select_backup_original_conf(
copyfile_mock: MagicMock, symlink_to_mock: MagicMock, unlink_mock: MagicMock
):
PipConfigs().select(SOME_PATH)
assert copyfile_mock.call_count == 1
assert symlink_to_mock.call_count == 1
assert unlink_mock.call_count == 1


@patch('pipconf.configs.Path.exists', MagicMock(return_value=True))
def test_create_path_does_not_exists():
with raises(EnvironmentError):
PipConfigs().create(SOME_PATH)


@patch('pipconf.configs.Path.exists', MagicMock(return_value=False))
@patch('pipconf.configs.copyfile')
def test_create(copyfile_mock: MagicMock):
PipConfigs().create(SOME_PATH)
assert copyfile_mock.call_count == 1


@patch('pipconf.configs.Path.exists', MagicMock(return_value=False))
def test_show_path_does_not_exists():
with raises(EnvironmentError):
PipConfigs().show(SOME_PATH)


@patch('pipconf.configs.Path.exists', MagicMock(return_value=True))
@patch('pipconf.configs.Path.read_text')
def test_show(read_text_mock: MagicMock):
read_text_mock.return_value = 'some content'
config = PipConfigs()
assert config.show(SOME_PATH) == 'some content'

0 comments on commit 539114d

Please sign in to comment.