djLint/tests/conftest.py

46 lines
1.1 KiB
Python
Raw Normal View History

"""Djlint test config."""
# pylint: disable=W0621,C0116
2021-07-13 17:45:25 +00:00
import os
2021-07-13 15:45:57 +00:00
import tempfile
from pathlib import Path
2021-11-29 11:19:07 +00:00
from types import SimpleNamespace
from typing import Generator, TextIO
2021-07-13 15:45:57 +00:00
import pytest
from click.testing import CliRunner
from src.djlint import main as djlint
2021-07-13 15:45:57 +00:00
@pytest.fixture()
def runner() -> CliRunner:
"""Click runner for djlint tests."""
return CliRunner()
2021-07-13 15:45:57 +00:00
@pytest.fixture()
def tmp_file() -> Generator:
"""Create a temp file for formatting."""
# pylint: disable=R1732
2021-07-13 17:45:25 +00:00
tmp = tempfile.NamedTemporaryFile(delete=False)
yield tmp
2021-07-13 17:53:19 +00:00
tmp.close()
2021-07-13 17:45:25 +00:00
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)
2021-11-29 11:19:07 +00:00
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"])
2021-11-29 11:19:07 +00:00
return SimpleNamespace(
**{
"text": Path(the_file.name).read_text(encoding="utf8"),
"exit_code": result.exit_code,
}
2021-11-29 11:19:07 +00:00
)