Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Jan 26, 2021
1 parent 2a76696 commit de9af7c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
10 changes: 2 additions & 8 deletions formate/__init__.py
Expand Up @@ -191,11 +191,8 @@ def get_diff(self) -> str:
# Based on yapf
# Apache 2.0 License

if self._reformatted_source is None:
raise ValueError("'Reformatter.run()' must be called first!")

after = self.to_string().split('\n')
before = self._unformatted_source.split('\n')
after = self._reformatted_source.split('\n')
return coloured_diff(
before,
after,
Expand All @@ -221,10 +218,7 @@ def to_file(self) -> None:
Write the reformatted source to the original file.
"""

if self._reformatted_source is None:
raise ValueError("'Reformatter.run()' must be called first!")

self.file_to_format.write_text(self._reformatted_source)
self.file_to_format.write_text(self.to_string())


def reformat_file(filename: PathLike, config: FormateConfigDict, colour: ColourTrilean = None):
Expand Down
5 changes: 2 additions & 3 deletions formate/dynamic_quotes.py
Expand Up @@ -34,7 +34,6 @@

# stdlib
import ast
import json
import re
import sys
from io import StringIO
Expand All @@ -43,11 +42,11 @@
# 3rd party
import asttokens # type: ignore

__all__ = ["dynamic_quotes"]

# this package
from formate.utils import double_repr

__all__ = ["dynamic_quotes"]


class QuoteRewriter(ast.NodeVisitor): # noqa: D101

Expand Down
49 changes: 43 additions & 6 deletions tests/test_integration.py
@@ -1,17 +1,16 @@
# stdlib
import re
import tempfile

# 3rd party
import pytest
from coincidence import AdvancedDataRegressionFixture, check_file_output
from coincidence.regressions import AdvancedDataRegressionFixture, check_file_output, check_file_regression
from consolekit.terminal_colours import strip_ansi
from consolekit.testing import CliRunner, Result
from domdf_python_tools.paths import PathPlus, in_directory
from pytest_regressions.file_regression import FileRegressionFixture

# this package
from formate import reformat_file
from formate import Reformatter, reformat_file
from formate.__main__ import main
from formate.config import load_toml

Expand Down Expand Up @@ -68,6 +67,44 @@ def test_integration(
check_file_output(tmp_pathplus / "code.py", file_regression)


def test_reformatter_class(
tmp_pathplus,
file_regression: FileRegressionFixture,
capsys,
advanced_data_regression: AdvancedDataRegressionFixture,
demo_environment,
):

config = load_toml(tmp_pathplus / "formate.toml")

r = Reformatter(tmp_pathplus / "code.py", config)

with pytest.raises(ValueError, match=r"'Reformatter.run\(\)' must be called first!"):
r.to_string()

with pytest.raises(ValueError, match=r"'Reformatter.run\(\)' must be called first!"):
r.to_file()

with pytest.raises(ValueError, match=r"'Reformatter.run\(\)' must be called first!"):
r.get_diff()

assert r.run() == 1
r.to_file()

check_file_output(tmp_pathplus / "code.py", file_regression)
check_file_regression(r.to_string(), file_regression, extension="._py_")

captured = capsys.readouterr()

assert not captured.out
assert not captured.err

# Calling a second time shouldn't change anything
assert reformat_file(tmp_pathplus / "code.py", config) == 0

check_file_output(tmp_pathplus / "code.py", file_regression)


def test_cli(
tmp_pathplus,
file_regression: FileRegressionFixture,
Expand All @@ -80,15 +117,15 @@ def test_cli(

with in_directory(tmp_pathplus):
runner = CliRunner(mix_stderr=False)
result = runner.invoke(main, catch_exceptions=False, args=["code.py"])
result = runner.invoke(main, catch_exceptions=False, args=["code.py", "--no-colour", "--verbose"])

assert result.exit_code == 1

check_file_output(tmp_pathplus / "code.py", file_regression)

data_dict = {
"out": strip_ansi(path_sub.sub(" ...", result.stdout)).split('\n'),
"err": strip_ansi(path_sub.sub(" ...", result.stderr)).split('\n')
"out": path_sub.sub(" ...", result.stdout).split('\n'),
"err": path_sub.sub(" ...", result.stderr).split('\n'),
}

advanced_data_regression.check(data_dict)
Expand Down
1 change: 1 addition & 0 deletions tests/test_integration_/test_cli.yml
Expand Up @@ -21,4 +21,5 @@ out:
- +print("hello world")
- +
- ''
- Reformatting code.py.
- ''
10 changes: 10 additions & 0 deletions tests/test_integration_/test_reformatter_class._py_
@@ -0,0 +1,10 @@
class F:
# stdlib
from collections import Counter
from collections.abc import Iterable

def foo(self):
pass


print("hello world")

0 comments on commit de9af7c

Please sign in to comment.