test_themes

tests/test_themes.py
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
import os
import shutil
import textwrap

import pytest

from lektor_ng.assets import Asset
from lektor_ng.builder import Builder
from lektor_ng.db import Database
from lektor_ng.environment import Environment
from lektor_ng.project import Project
from lektor_ng.reporter import Reporter

sep = os.path.sep


@pytest.fixture(scope="function")
def theme_project_tmpdir(tmp_path, data_path):
    """Copy themes-project to a temp dir, and copy demo-project content to it."""
    temp_dir = tmp_path / "temp/themes-project"
    temp_dir.parent.mkdir()
    shutil.copytree(data_path / "themes-project", temp_dir)
    shutil.copytree(data_path / "demo-project/content", temp_dir / "content")
    return temp_dir


@pytest.fixture(scope="function")
def theme_project(theme_project_tmpdir, request):
    """Return the theme project created in a temp dir.

    Could be parametrize, if request.param=False themes variables won't be set
    """
    try:
        with_themes_var = request.param
    except AttributeError:
        with_themes_var = True

    # Create the .lektorproject file
    lektorfile_text = textwrap.dedent(
        """
        [project]
        name = Themes Project
        {}
    """.format("themes = blog_theme, project_theme" if with_themes_var else "")
    )
    lektorfile = theme_project_tmpdir / "themes.lektorproject"
    lektorfile.write_text(lektorfile_text, "utf8")
    return Project.from_path(theme_project_tmpdir)


@pytest.fixture(scope="function")
def theme_env(theme_project, save_sys_path):
    return Environment(theme_project)


@pytest.fixture(scope="function")
def theme_pad(theme_env):
    return Database(theme_env).new_pad()


@pytest.fixture(scope="function")
def theme_builder(theme_pad, tmpdir):
    return Builder(theme_pad, str(tmpdir.mkdir("output")))


@pytest.mark.parametrize(
    "theme_project, themes",
    [
        (True, ["blog_theme", "project_theme"]),
        # when themes variables isn't set themes are only loaded in the env
        (False, []),
    ],
    indirect=["theme_project"],
)
def test_loading_theme_variable(theme_project, themes):
    assert theme_project.themes == themes


@pytest.mark.parametrize("theme_project", (True,), indirect=True)
def test_loading_theme_path(theme_env):
    assert [os.path.basename(p) for p in theme_env.theme_paths] == [
        "blog_theme",
        "project_theme",
    ]


@pytest.mark.parametrize("theme_project", (False,), indirect=True)
def test_loading_theme_path_variable_dont_set(theme_env):
    """Themes will be loaded, but the order could change."""
    paths = [os.path.basename(p) for p in theme_env.theme_paths]
    assert len(paths) == 2
    for path in paths:
        assert path in ["blog_theme", "project_theme"]


@pytest.mark.parametrize(
    "asset_name, found_in",
    [
        # - themes-project/assets/asset.txt
        # only exist in themes-project assets will be loaded from there
        ("asset.txt", "root"),
        # - themes-project/assets/dummy.txt
        # - themes-project/themes/blog_theme/assets/dummy.txt
        # wil be loaded from themes-project assets not from blog_theme assets
        ("dummy.txt", "root"),
        # - themes-project/themes/blog_theme/static/blog.css
        # only exist in blog_theme assets will be loaded from there
        ("static/blog.css", "blog"),
        # - themes-project/themes/project_theme/static/project.css
        # only exist in project_theme assets will be loaded from there
        ("static/project.css", "project"),
        # - themes-project/themes/blog_theme/assets/dummy2.txt
        # - themes-project/themes/project_theme/assets/dummy2.txt
        # wil be loaded from blog_theme assets because is included first
        ("dummy2.txt", "blog"),
    ],
)
def test_theme_asset_loading(theme_pad, asset_name, found_in):
    """Test loading assets from theme project.

    Loading should take in account the order of the themes
    """
    path_list = theme_pad.get_asset(asset_name).source_filename.split(sep)
    path_list_from_url = theme_pad.resolve_url_path(asset_name).source_filename.split(sep)

    assert path_list == path_list_from_url

    assert (found_in == "root") == ("themes" not in path_list)
    assert (found_in == "blog") == ("blog_theme" in path_list)
    assert (found_in == "project") == ("project_theme" in path_list)


@pytest.mark.parametrize(
    "url, datamodel_name, found_in",
    [
        # - themes-project/themes/blog_theme/models/blog.ini
        # only exist in blog_theme will be loaded from there
        ("/blog", "Blog", "blog"),
        # - themes-project/themes/project_theme/models/projects.ini
        # only exist in project_theme will be loaded from there
        ("/projects", "Projects", "project"),
        # - themes-project/models/blog-post.ini
        # - themes-project/themes/blog_theme/models/blog-post.ini
        # will be loaded from themes-project models
        ("/blog/post1", "Blog Post", "root"),
        # - themes-project/models/page.ini
        # only exist in themes-project will be loaded from there
        ("/", "Page", "root"),
        # - themes-project/themes/blog_theme/models/project.ini
        # - themes-project/themes/project_theme/models/project.ini
        # wil be loaded from blog_theme assets because is included first
        ("/projects/bagpipe", "Project", "blog"),
    ],
)
def test_theme_models_loading(theme_pad, url, datamodel_name, found_in):
    """Test loading models from theme project.

    Loading should take in account the order of the themes
    """
    assert datamodel_name == theme_pad.get(url).datamodel.name

    path_list = theme_pad.get(url).datamodel.filename.split(sep)

    assert (found_in == "root") == ("themes" not in path_list)
    assert (found_in == "blog") == ("blog_theme" in path_list)
    assert (found_in == "project") == ("project_theme" in path_list)


@pytest.mark.parametrize(
    "template_name, found_in",
    [
        # - themes-project/templates/layout.html
        # - themes-project/themes/blog_theme/templates/layout.html
        # - themes-project/themes/project_theme/templates/layout.html
        # will be loaded from themes-project templates
        ("layout.html", "root"),
        # - themes-project/themes/blog_theme/templates/blog.html
        # only exist in blog_theme will be loaded from there
        ("blog.html", "blog"),
        # - themes-project/themes/project_theme/templates/project.html
        # only exist in project_theme will be loaded from there
        ("project.html", "project"),
        # - themes-project/templates/page.html
        # only exist in themes-project will be loaded from there
        ("page.html", "root"),
        # - themes-project/themes/blog_theme/templates/dummy.html
        # - themes-project/themes/project_theme/templates/dummy.html
        # wil be loaded from blog_theme assets because is included first
        ("dummy.html", "blog"),
    ],
)
def test_theme_templates_loading(theme_env, template_name, found_in):
    """Test loading templates from theme project.

    Loading should take in account the order of the themes
    """
    path_list = theme_env.jinja_env.get_template(template_name).filename.split(sep)

    assert (found_in == "root") == ("themes" not in path_list)
    assert (found_in == "blog") == ("blog_theme" in path_list)
    assert (found_in == "project") == ("project_theme" in path_list)


class BuiltAssetsReporter(Reporter):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.built_assets = []

    def start_artifact_build(self, is_current):
        source_obj = self.current_artifact.source_obj
        if isinstance(source_obj, Asset):
            self.built_assets.append(source_obj)


def test_build_omits_shadowed_assets(theme_builder, theme_env):
    with BuiltAssetsReporter(theme_env) as reporter:
        theme_builder.build_all()
    asset_urls = [asset.url_path for asset in reporter.built_assets]
    assert len(asset_urls) > 0
    assert len(set(asset_urls)) == len(asset_urls)