Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Jul 21, 2021
1 parent 045d0df commit dd4ff5a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/darker/tests/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from darker import black_diff
from darker.__main__ import main
from darker.command_line import make_argument_parser, parse_command_line
from darker.config import ConfigurationError
from darker.git import RevisionRange
from darker.tests.helpers import filter_dict, isort_present, raises_if_exception
from darker.utils import TextDocument, joinlines
Expand Down Expand Up @@ -132,16 +133,16 @@ def test_parse_command_line_config_src(
expect_modified=("stdout", ...),
),
dict(
argv=["--stdout", "."],
argv=["--stdout", "dummy.py"],
expect_value=("stdout", True),
expect_config=("stdout", True),
expect_modified=("stdout", True),
),
dict(
argv=["--diff", "--stdout", "."],
expect_value=("stdout", True),
expect_config=("stdout", True),
expect_modified=("stdout", True),
argv=["--diff", "--stdout", "dummy.py"],
expect_value=ConfigurationError,
expect_config=ConfigurationError,
expect_modified=ConfigurationError,
),
dict(
argv=["."],
Expand Down Expand Up @@ -289,26 +290,32 @@ def test_parse_command_line_config_src(
),
)
def test_parse_command_line(
tmpdir, monkeypatch, argv, expect_value, expect_config, expect_modified
tmp_path, monkeypatch, argv, expect_value, expect_config, expect_modified
):
"""``parse_command_line()`` parses options correctly"""
monkeypatch.chdir(tmpdir)
args, effective_cfg, modified_cfg = parse_command_line(argv)
monkeypatch.chdir(tmp_path)
(tmp_path / "dummy.py").touch()

arg_name, expect_arg_value = expect_value
assert getattr(args, arg_name) == expect_arg_value
with raises_if_exception(expect_value):

option, expect_config_value = expect_config
if expect_config_value is ...:
assert option not in effective_cfg
else:
assert effective_cfg[option] == expect_config_value # type: ignore
args, effective_cfg, modified_cfg = parse_command_line(argv)

modified_option, expect_modified_value = expect_modified
if expect_modified_value is ...:
assert modified_option not in modified_cfg
else:
assert modified_cfg[modified_option] == expect_modified_value # type: ignore
arg_name, expect_arg_value = expect_value
assert getattr(args, arg_name) == expect_arg_value

option, expect_config_value = expect_config
if expect_config_value is ...:
assert option not in effective_cfg
else:
assert effective_cfg[option] == expect_config_value # type: ignore

modified_option, expect_modified_value = expect_modified
if expect_modified_value is ...:
assert modified_option not in modified_cfg
else:
assert (
modified_cfg[modified_option] == expect_modified_value # type: ignore
)


def test_help_description_without_isort_package(capsys):
Expand Down Expand Up @@ -543,7 +550,7 @@ def test_options(git_repo, options, expect):
retval = main(options)

expect = (Path(git_repo.root), expect[1]) + expect[2:]
format_edited_parts.assert_called_once_with(*expect)
format_edited_parts.assert_called_once_with(*expect, report_unmodified=False)
assert retval == 0


Expand Down
3 changes: 3 additions & 0 deletions src/darker/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_replace_log_level_name(log_level, expect):
dict(diff=True, stdout=True, expect=ConfigurationError),
)
def test_output_mode_validate_diff_stdout(diff, stdout, expect):
"""Validation fails only if ``--diff`` and ``--stdout`` are both enabled"""
with raises_if_exception(expect):
OutputMode.validate_diff_stdout(diff, stdout)

Expand All @@ -100,6 +101,7 @@ def test_output_mode_validate_diff_stdout(diff, stdout, expect):
dict(stdout=True, src=["directory"], expect=ConfigurationError),
)
def test_output_mode_validate_stdout_src(tmp_path, monkeypatch, stdout, expect, src):
"""Validation fails only if exactly one file isn't provided for ``--stdout``"""
monkeypatch.chdir(tmp_path)
Path("first.py").touch()
Path("second.py").touch()
Expand All @@ -115,6 +117,7 @@ def test_output_mode_validate_stdout_src(tmp_path, monkeypatch, stdout, expect,
dict(diff=True, stdout=True, expect=ConfigurationError),
)
def test_output_mode_from_args(diff, stdout, expect):
"""Correct output mode results from the ``--diff`` and ``stdout`` options"""
args = Namespace()
args.diff = diff
args.stdout = stdout
Expand Down

0 comments on commit dd4ff5a

Please sign in to comment.