Skip to content

Commit

Permalink
Add unit tests for configfile/cmdline processing
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Jun 25, 2020
1 parent 58e05a2 commit be2ddb5
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src/darker/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import sys
import types
from subprocess import check_call
from typing import Dict
from unittest.mock import patch

import pytest
from py.path import local as LocalPath


@pytest.fixture
Expand All @@ -15,3 +18,28 @@ def without_isort():
def with_isort():
with patch.dict(sys.modules, {"isort": types.ModuleType("isort")}):
yield


class GitRepoFixture:
def __init__(self, root: LocalPath):
self.root = root

def add(
self, paths_and_contents: Dict[str, str], commit: str = None
) -> Dict[str, LocalPath]:
absolute_paths = {
relative_path: self.root / relative_path
for relative_path in paths_and_contents
}
for relative_path, content in paths_and_contents.items():
absolute_paths[relative_path].write(content)
check_call(["git", "add", relative_path], cwd=self.root)
if commit:
check_call(["git", "commit", "-m", commit], cwd=self.root)
return absolute_paths


@pytest.fixture
def git_repo(tmpdir):
check_call(["git", "init"], cwd=tmpdir)
return GitRepoFixture(tmpdir)
31 changes: 25 additions & 6 deletions src/darker/tests/test_black_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,32 @@ def test_mixed():
]


@pytest.mark.parametrize("config,line_length", ((None, 79), ("custom.toml", 99)))
def test_black_config(tmpdir, config, line_length):
@pytest.mark.parametrize(
"config_path, config_lines, expect",
[
(None, ['line-length = 79'], {'line_length': 79}),
("custom.toml", ['line-length = 99'], {'line_length': 99}),
(
"custom.toml",
['skip-string-normalization = true'],
{'skip_string_normalization': True},
),
(
"custom.toml",
['skip-string-normalization = false'],
{'skip_string_normalization': False},
),
("custom.toml", ["target-version = ['py37']"], {}),
("custom.toml", ["include = '\\.pyi$'"], {}),
("custom.toml", ["exclude = '\\.pyx$'"], {}),
],
)
def test_black_config(tmpdir, config_path, config_lines, expect):
tmpdir = Path(tmpdir)
src = tmpdir / "src.py"
toml = tmpdir / (config or "pyproject.toml")
toml = tmpdir / (config_path or "pyproject.toml")

toml.write_text("[tool.black]\nline-length = {}\n".format(line_length))
toml.write_text("[tool.black]\n{}\n".format('\n'.join(config_lines)))

config = read_black_config(src, config and str(toml))
assert config == {"line_length": line_length}
config = read_black_config(src, config_path and str(toml))
assert config == expect
50 changes: 50 additions & 0 deletions src/darker/tests/test_command_line.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import re
from textwrap import dedent
from unittest.mock import call, patch

import pytest

from darker import black_diff
from darker.__main__ import main
from darker.command_line import parse_command_line


Expand All @@ -28,3 +32,49 @@ def test_help_isort_option_without_isort_package(without_isort, darker_help_outp

def test_help_with_isort_package(with_isort, darker_help_output):
assert "Please run" not in darker_help_output


@pytest.mark.parametrize(
"options, expect",
[
([], call()),
(['-c', 'black.cfg'], call(line_length=81, string_normalization=True)),
(['--config', 'black.cfg'], call(line_length=81, string_normalization=True)),
(['-S'], call(string_normalization=False)),
(['--skip-string-normalization'], call(string_normalization=False)),
(['-l', '90'], call(line_length=90)),
(['--line-length', '90'], call(line_length=90)),
(['-c', 'black.cfg', '-S'], call(line_length=81, string_normalization=False)),
(
['-c', 'black.cfg', '-l', '90'],
call(line_length=90, string_normalization=True),
),
(['-l', '90', '-S'], call(line_length=90, string_normalization=False)),
(
['-c', 'black.cfg', '-l', '90', '-S'],
call(line_length=90, string_normalization=False),
),
],
)
def test_black_options(monkeypatch, tmpdir, git_repo, options, expect):
monkeypatch.chdir(tmpdir)
(tmpdir / 'pyproject.toml').write("[tool.black]\n")
(tmpdir / 'black.cfg').write(
dedent(
"""
[tool.black]
line-length = 81
skip-string-normalization = false
"""
)
)
added_files = git_repo.add(
{"main.py": 'print("Hello World!")\n'}, commit="Initial commit"
)
added_files["main.py"].write('print ("Hello World!")\n')
with patch.object(black_diff, 'FileMode', wraps=black_diff.FileMode) as FileMode:

main(options + [str(path) for path in added_files.values()])

_, expect_args, expect_kwargs = expect
FileMode.assert_called_once_with(*expect_args, **expect_kwargs)

0 comments on commit be2ddb5

Please sign in to comment.