summaryrefslogtreecommitdiff
path: root/tests/conftest.py
blob: dc92eb1304a425dfbf5493a90f54e0c3ea840c1b (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
import sys
import pytest
import shutil
from pathlib import Path
from typing import Any, Callable
from pytest import MonkeyPatch
from argparse import ArgumentParser
from datetime import datetime, timezone
from importlib.metadata import version as v
from logging import Logger, getLogger, DEBUG
from copy import deepcopy

from pyssg.arg_parser import get_parser
from pyssg.custom_logger import setup_logger
from pyssg.database_entry import DatabaseEntry
from pyssg.page import Page


@pytest.fixture(scope='session')
def version():
    return v('pyssg')


@pytest.fixture(scope='session')
def rss_date_fmt():
    return '%a, %d %b %Y %H:%M:%S GMT'


@pytest.fixture(scope='session')
def sitemap_date_fmt():
    return '%Y-%m-%d'


@pytest.fixture(scope='session')
def sample_files_path() -> str:
    return f'{str(os.path.dirname(os.path.abspath(__file__)))}/sample_files'


@pytest.fixture(scope='session')
def config_resource() -> str:
    return 'tests.sample_files.config'


@pytest.fixture(scope='session')
def default_yaml() -> str:
    return 'default.yaml'


@pytest.fixture(scope='session')
def arg_parser() -> ArgumentParser:
    return get_parser()


@pytest.fixture(scope='session')
def logger() -> Logger:
    setup_logger(__name__, DEBUG)
    return getLogger(__name__)


@pytest.fixture(scope='function')
def capture_stdout(monkeypatch: MonkeyPatch) -> dict[str, str | int]:
    buffer: dict[str, str | int] = {'stdout': '', 'write_calls': 0}

    def fake_writer(s):
        buffer['stdout'] += s
        buffer['write_calls'] += 1  # type: ignore

    monkeypatch.setattr(sys.stdout, 'write', fake_writer)
    return buffer


@pytest.fixture(scope='session')
def get_fmt_time() -> Callable[..., str]:
    def fmt_time(fmt: str) -> str:
        return datetime.now(tz=timezone.utc).strftime(fmt)
    return fmt_time


@pytest.fixture(scope='function')
def default_config() -> dict[str, Any]:
    return {'define': '$PYSSG_HOME/pyssg/site_example/',
            'title': 'Example site',
            'path': {
                'src': '/tmp/pyssg/pyssg/site_example/src',
                'dst': '/tmp/pyssg/pyssg/site_example/dst',
                'plt': '/tmp/pyssg/pyssg/site_example/plt',
                'db': '/tmp/pyssg/pyssg/site_example/.files'},
            'url': {
                'main': 'https://example.com'},
            'fmt': {
                'date': '%a, %b %d, %Y @ %H:%M %Z'},
            'dirs': {
                '/': {
                    'cfg': {
                        'plt': 'page.html',
                        'tags': False,
                        'index': False,
                        'rss': False,
                        'sitemap': False}}}}


@pytest.fixture(scope='function')
def root_dir_config() -> dict[str, Any]:
    return {'plt': 'page.html',
            'tags': False,
            'index': False,
            'rss': False,
            'sitemap': False,
            'src': '/tmp/pyssg/pyssg/site_example/src',
            'dst': '/tmp/pyssg/pyssg/site_example/dst',
            'url': 'https://example.com'}


@pytest.fixture(scope='function')
def tmp_dir_structure(tmp_path: Path) -> Path:
    root: Path = tmp_path/'dir_structure'
    # order matters
    dirs: list[Path] = [root,
                        root/'first',
                        root/'first/f1',
                        root/'first/f1/f2',
                        root/'second',
                        root/'second/s1']
    for i, d in enumerate(dirs):
        d.mkdir()
        for ext in ['txt', 'md', 'html']:
            (d/f'f{i}.{ext}').write_text('sample')
    return root


@pytest.fixture(scope='session')
def tmp_db_e1() -> DatabaseEntry:
    return DatabaseEntry(('first.md',
                          1671076311.823135,
                          0.0,
                          '778bce781d95730cd1e872a10130e20d',
                          '-'))


@pytest.fixture(scope='session')
def tmp_db_e2() -> DatabaseEntry:
    return DatabaseEntry(('a/second.md',
                          1671077831.63301,
                          # 1671078892.892921,
                          1677381461.8107588,
                          # '6092d6471d3a83135293e34ef6012939',
                          'a61d0116844b6ebc02db62b4b1bf453d',
                          'english,short,update'))


@pytest.fixture(scope='function')
def tmp_db(tmp_path: Path,
           tmp_db_e1: DatabaseEntry,
           tmp_db_e2: DatabaseEntry) -> Path:
    root: Path = tmp_path/'db'
    db_path: Path = tmp_path/'db/sample_db.psv'
    root.mkdir()
    e1: str = '|'.join(tmp_db_e1.get_raw_entry())
    e2: str = '|'.join(tmp_db_e2.get_raw_entry())
    db_path.write_text(f'{e1}\n{e2}\n')
    return db_path


@pytest.fixture(scope='function')
def tmp_db_wrong_col_num(tmp_path: Path) -> Path:
    root: Path = tmp_path/'db'
    db_path: Path = tmp_path/'db/sample_db_wrong_col_num.psv'
    root.mkdir()
    # missing tags, could be anything though
    db_path.write_text('name|0.0|0.0|cksm\n')
    return db_path


@pytest.fixture(scope='function')
def tmp_src_dir(tmp_path: Path,
                sample_files_path: str) -> Path:
    src: Path = tmp_path/'src'
    src_a: Path = src/'a'
    src.mkdir()
    src_a.mkdir()
    src_test: str = f'{sample_files_path}/md'

    files: list[str] = ['first.md', 'new.md', 'a/second.md']
    for f in files:
        shutil.copy2(f'{src_test}/{f}', f'{str(src)}/{f}')
    return src


@pytest.fixture(scope='function')
def page_simple(default_config: dict[str, Any],
                root_dir_config: dict[str, Any],
                rss_date_fmt: str,
                sitemap_date_fmt: str) -> Page:
    # adding the fmt as it is added on the code before being passed to the page
    config: dict[str, Any] = deepcopy(default_config)
    config['fmt']['rss_date'] = rss_date_fmt
    config['fmt']['sitemap_date'] = sitemap_date_fmt

    return Page(
        name='simple.md',
        ctime=1682418172.291993,
        mtime=0.0,
        html='<p>Simple converted md with nothing but this text.</p>',
        toc='<div class="toc">\n<ul></ul>\n</div>\n',
        toc_tokens=[],
        meta=dict(),
        config=config,
        dir_config=root_dir_config
    )


# this is basically page_simple with extras
@pytest.fixture(scope='function')
def page_simple_modified(default_config: dict[str, Any],
                         root_dir_config: dict[str, Any],
                         rss_date_fmt: str,
                         sitemap_date_fmt: str) -> Page:
    config: dict[str, Any] = deepcopy(default_config)
    config['fmt']['rss_date'] = rss_date_fmt
    config['fmt']['sitemap_date'] = sitemap_date_fmt

    dir_config: dict[str, Any] = deepcopy(root_dir_config)
    dir_config['tags'] = True
    tags: list[str] = ['blog', 'english', 'etc']

    basic_meta: dict[str, Any] = {
        'title': 'Example title',
        'author': 'Single Author',
        'summary': 'Some summary.',
        'lang': 'es',
        'tags': tags
    }

    return Page(
        name='simple_modified.md',
        ctime=1682418300.291993,
        mtime=1682418350.291993,
        html='<p>Simple converted md with nothing but this text, modified.</p>',
        toc='<div class="toc">\n<ul></ul>\n</div>\n',
        toc_tokens=[],
        meta=basic_meta,
        config=config,
        dir_config=dir_config
    )