aboutsummaryrefslogtreecommitdiffstats
path: root/tests/integration/test_relative_links.py
blob: 3b158cfecc3364ec6047f6dd876d298c9fda4e9a (plain) (blame)
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
"""Check that SITE_URL with a path doesn't break links."""

import io
import os

import lxml
import pytest

from nikola import __main__

from .helper import cd, patch_config
from .test_demo_build import prepare_demo_site
from .test_empty_build import (  # NOQA
    test_archive_exists,
    test_avoid_double_slash_in_rss,
    test_check_files,
    test_check_links,
)


def test_relative_links(build, output_dir):
    """Check that the links in output/index.html are correct"""
    test_path = os.path.join(output_dir, "index.html")

    with io.open(test_path, "rb") as inf:
        data = inf.read()

    assert not any(
        url.startswith("..")
        for _, _, url, _ in lxml.html.iterlinks(data)
        if url.endswith("css")
    )


def test_index_in_sitemap(build, output_dir):
    """Test that the correct path is in sitemap, and not the wrong one."""
    sitemap_path = os.path.join(output_dir, "sitemap.xml")
    with io.open(sitemap_path, "r", encoding="utf8") as inf:
        sitemap_data = inf.read()

    assert "<loc>https://example.com/</loc>" not in sitemap_data
    assert "<loc>https://example.com/foo/bar/</loc>" in sitemap_data


@pytest.fixture(scope="module")
def build(target_dir):
    """Fill the site with demo content and build it."""
    prepare_demo_site(target_dir)

    # Set the SITE_URL to have a path with subfolder
    patch_config(
        target_dir,
        (
            'SITE_URL = "https://example.com/"',
            'SITE_URL = "https://example.com/foo/bar/"',
        ),
    )

    with cd(target_dir):
        __main__.main(["build"])