Skip to content

Commit

Permalink
fixed path mocking to avoid false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
bandophahita committed Apr 5, 2024
1 parent 779c53b commit 9a174d9
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import os
from pathlib import Path
from unittest import mock

from screenpy import settings as screenpy_settings
Expand All @@ -11,8 +10,10 @@


class TestPyprojectTomlConfig:
def test__parse_pyproject_toml_file_does_not_exist(self) -> None:
MockedPath = mock.MagicMock(spec=Path)
@mock.patch("screenpy.configuration.Path", autospec=True)
def test__parse_pyproject_toml_file_does_not_exist(
self, MockedPath: mock.Mock
) -> None:
MockedPath.cwd.return_value.__truediv__.return_value = MockedPath
MockedPath.is_file.return_value = False

Expand All @@ -21,19 +22,20 @@ def test__parse_pyproject_toml_file_does_not_exist(self) -> None:

assert pyproject_config.toml_config == {}

def test__parse_pyproject_toml_file_exists(self) -> None:
MockedPath = mock.MagicMock(spec=Path)
@mock.patch("screenpy.configuration.Path", autospec=True)
def test__parse_pyproject_toml_file_exists(self, MockedPath: mock.Mock) -> None:
MockedPath.cwd.return_value.__truediv__.return_value = MockedPath
MockedPath.is_file.return_value = True
test_data = (
b"[tool.screenpy]\nTIMEOUT = 500"
b"\n\n[tool.screenpy.stdoutadapter]\nINDENT_SIZE = 500"
)
mock_open = mock.mock_open(read_data=test_data)
MockedPath.open.side_effect = mock_open.side_effect
MockedPath.open.return_value = mock_open.return_value

with mock.patch("pathlib.Path.open", mock_open):
pyproject_config = PyprojectTomlConfig(ScreenPySettings)
pyproject_config._parse_pyproject_toml()
pyproject_config = PyprojectTomlConfig(ScreenPySettings)
pyproject_config._parse_pyproject_toml()

assert pyproject_config.toml_config == {
"TIMEOUT": 500,
Expand Down

0 comments on commit 9a174d9

Please sign in to comment.