djLint/tests/conftest.py
Christopher Pickering a10ac08686
added fix for #152
2021-11-29 12:19:07 +01:00

41 lines
1.1 KiB
Python

"""Djlint test config."""
import os
import tempfile
from pathlib import Path
from types import SimpleNamespace
from typing import Generator, TextIO
import pytest
from click.testing import CliRunner
from src.djlint import main as djlint
@pytest.fixture()
def runner() -> CliRunner:
"""Click runner for djlint tests."""
return CliRunner()
@pytest.fixture()
def tmp_file() -> Generator:
"""Create a temp file for formatting."""
# pylint: disable=R1732
tmp = tempfile.NamedTemporaryFile(delete=False)
yield tmp
tmp.close()
os.unlink(tmp.name)
def write_to_file(the_file: str, the_text: bytes) -> None:
"""Shortcode for write some bytes to a file."""
with open(the_file, mode="w+b") as open_file:
open_file.write(the_text)
def reformat(the_file: TextIO, runner: CliRunner, the_text: bytes) -> SimpleNamespace:
write_to_file(the_file.name, the_text)
result = runner.invoke(djlint, [the_file.name, "--reformat"])
return SimpleNamespace(
**{"text": Path(the_file.name).read_text(), "exit_code": result.exit_code}
)