File: conftest.py

package info (click to toggle)
blag 2.3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 520 kB
  • sloc: python: 1,037; makefile: 71
file content (96 lines) | stat: -rw-r--r-- 2,559 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""Pytest fixtures."""

import os
from argparse import Namespace
from collections.abc import Callable, Iterator
from tempfile import TemporaryDirectory

import pytest
from jinja2 import Environment, Template

from blag import blag, quickstart


@pytest.fixture
def environment(cleandir: str) -> Iterator[Environment]:
    """Create a Jinja2 environment."""
    site = {
        "base_url": "site base_url",
        "title": "site title",
        "description": "site description",
        "author": "site author",
    }
    env = blag.environment_factory("templates", globals_=dict(site=site))
    yield env


@pytest.fixture
def page_template(environment: Environment) -> Iterator[Template]:
    """Create a Jinja2 page-template."""
    yield environment.get_template("page.html")


@pytest.fixture
def article_template(environment: Environment) -> Iterator[Template]:
    """Create a Jinja2 article-template."""
    yield environment.get_template("article.html")


@pytest.fixture
def index_template(environment: Environment) -> Iterator[Template]:
    """Create a Jinja2 index-template."""
    yield environment.get_template("index.html")


@pytest.fixture
def archive_template(environment: Environment) -> Iterator[Template]:
    """Create a Jinja2 archive-template."""
    yield environment.get_template("archive.html")


@pytest.fixture
def tags_template(environment: Environment) -> Iterator[Template]:
    """Create a Jinja2 tags-template."""
    yield environment.get_template("tags.html")


@pytest.fixture
def tag_template(environment: Environment) -> Iterator[Template]:
    """Create a Jinja2 tag-template."""
    yield environment.get_template("tag.html")


@pytest.fixture
def cleandir() -> Iterator[str]:
    """Create a temporary working directory and cwd."""
    config = """
[main]
base_url = https://example.com/
title = title
description = description
author = a. u. thor
    """

    with TemporaryDirectory() as dir:
        os.mkdir(f"{dir}/build")
        with open(f"{dir}/config.ini", "w") as fh:
            fh.write(config)
        # change directory
        old_cwd = os.getcwd()
        os.chdir(dir)
        quickstart.copy_default_theme()
        yield dir
        # and change back afterwards
        os.chdir(old_cwd)


@pytest.fixture
def args(cleandir: Callable[[], Iterator[str]]) -> Iterator[Namespace]:
    """Create a Namespace with default arguments."""
    args = Namespace(
        input_dir="content",
        output_dir="build",
        static_dir="static",
        template_dir="templates",
    )
    yield args