Skip to content

Commit

Permalink
Merge pull request #3676 from tartley/test-gallery-rss
Browse files Browse the repository at this point in the history
Add a test for gallery RSS content
  • Loading branch information
Kwpolska committed Jul 7, 2023
2 parents 5ed15d1 + 03b1fce commit 84f6a2b
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 6 deletions.
4 changes: 3 additions & 1 deletion nikola/nikola.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import logging
import operator
import os
import pathlib
import sys
import mimetypes
from collections import defaultdict
Expand Down Expand Up @@ -1926,7 +1927,8 @@ def path(self, kind, name, lang=None, is_link=False, **kwargs):
else:
return link
else:
return os.path.join(*path)
# URLs should always use forward slash separators, even on Windows
return str(pathlib.PurePosixPath(*path))

def post_path(self, name, lang):
"""Link to the destination of an element in the POSTS/PAGES settings.
Expand Down
13 changes: 9 additions & 4 deletions nikola/plugins/task/galleries.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import json
import mimetypes
import os
import pathlib
from collections import OrderedDict
from urllib.parse import urljoin

Expand Down Expand Up @@ -544,7 +545,7 @@ def get_excluded_images(self, gallery_path):
except IOError:
excluded_image_name_list = []

excluded_image_list = ["{0}/{1}".format(gallery_path, i) for i in excluded_image_name_list]
excluded_image_list = [os.path.join(gallery_path, i) for i in excluded_image_name_list]
return excluded_image_list

def get_image_list(self, gallery_path):
Expand Down Expand Up @@ -737,18 +738,22 @@ def make_url(url):
else:
img_list, dest_img_list, img_titles = [], [], []

def forward_slashes(path):
"""Given a path, convert directory separators to forward slash, on all platforms."""
return str(pathlib.PurePosixPath(*path.split(os.path.sep)))

items = []
for img, srcimg, title in list(zip(dest_img_list, img_list, img_titles))[:self.kw["feed_length"]]:
img_size = os.stat(
os.path.join(
self.site.config['OUTPUT_FOLDER'], img)).st_size
args = {
'title': title,
'link': make_url(img),
'guid': rss.Guid(img, False),
'link': make_url(forward_slashes(img)),
'guid': rss.Guid(forward_slashes(img), False),
'pubDate': self.image_date(srcimg),
'enclosure': rss.Enclosure(
make_url(img),
make_url(forward_slashes(img)),
img_size,
mimetypes.guess_type(img)[0]
),
Expand Down
1 change: 1 addition & 0 deletions requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
coverage>=4.5.1
pytest>=4.1.0
pytest-cov>=2.6.1
feedparser>=6.0.10
freezegun>=0.3.10
flake8>=3.7.7
136 changes: 135 additions & 1 deletion tests/integration/test_demo_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
settings.
"""

import datetime
import email
import itertools
import os
import time

import feedparser
import freezegun
import pytest

import nikola.plugins.command.init
Expand All @@ -23,14 +29,142 @@
test_index_in_sitemap,
)

BUILDTIME = datetime.datetime(2023, 4, 5, 23, 59, 58, tzinfo=datetime.timezone.utc)


class Any:
"""Compare equal with anything. Use for expected values we don't care about."""
def __eq__(self, _):
return True


def rfc822(t):
"""Format a datetime according to RFC822, eg 'Wed, 05 Apr 2023 23:59:58 GMT'"""
return email.utils.formatdate(
time.mktime(BUILDTIME.astimezone().timetuple()),
usegmt=True,
)


def test_gallery_rss(build, output_dir):
# Given a build of the demo samplesite in 'output_dir'
# When we look for the RSS file of the "Demo" gallery
rss_path = os.path.join(output_dir, 'galleries', 'demo', 'rss.xml')

# Then it exists
assert os.path.isfile(rss_path)
# and it contains text
with open(rss_path) as fp:
content = fp.read()
assert isinstance(content, str)
assert len(content) > 0
# and the text can be parsed as valid RSS
parsed = feedparser.parse(content)
# and the RSS contains top level attributes:
assert parsed.version == 'rss20'
# and the RSS contains feed attributes, about the gallery:
assert parsed.feed.language == 'en'
assert parsed.feed.link == 'https://example.com/galleries/demo/rss.xml'
# TODO I think the following is a bug: The feed title should be the Gallery name,
# not the name of the gallery's final image.
assert parsed.feed.title == 'Tesla tower1 lg'
# TODO I think the following is a bug: The feed's subtitle (aka description) should
# contain the content of the gallery's index.txt.
assert parsed.feed.subtitle == '' # From the XML field 'description'
assert parsed.feed.updated == rfc822(BUILDTIME)
# and the images, as items in the RSS feed, are:
expected_items = [
dict(
id='galleries/demo/tesla4_lg.jpg',
link='https://example.com/galleries/demo/tesla4_lg.jpg',
links=[
Any(),
dict(
href='https://example.com/galleries/demo/tesla4_lg.jpg',
length='30200',
rel='enclosure',
type='image/jpeg',
),
],
published='Wed, 01 Jan 2014 00:01:00 GMT',
title='Tesla4 lg',
),
dict(
id='galleries/demo/tesla_conducts_lg.webp',
link='https://example.com/galleries/demo/tesla_conducts_lg.webp',
links=[
Any(),
dict(
href='https://example.com/galleries/demo/tesla_conducts_lg.webp',
length='9620',
rel='enclosure',
type='image/webp',
),
],
published='Wed, 01 Jan 2014 00:02:00 GMT',
title='Tesla conducts lg',
),
dict(
id='galleries/demo/tesla_lightning1_lg.jpg',
link='https://example.com/galleries/demo/tesla_lightning1_lg.jpg',
links=[
Any(),
dict(
href='https://example.com/galleries/demo/tesla_lightning1_lg.jpg',
length='41123',
rel='enclosure',
type='image/jpeg',
),
],
published='Wed, 01 Jan 2014 00:03:00 GMT',
title='Tesla lightning1 lg',
),
dict(
id='galleries/demo/tesla_lightning2_lg.jpg',
link='https://example.com/galleries/demo/tesla_lightning2_lg.jpg',
links=[
Any(),
dict(
href='https://example.com/galleries/demo/tesla_lightning2_lg.jpg',
length='36994',
rel='enclosure',
type='image/jpeg',
),
],
published='Wed, 01 Jan 2014 00:04:00 GMT',
title='Tesla lightning2 lg',
),
dict(
id='galleries/demo/tesla_tower1_lg.jpg',
link='https://example.com/galleries/demo/tesla_tower1_lg.jpg',
links=[
Any(),
dict(
href='https://example.com/galleries/demo/tesla_tower1_lg.jpg',
length='18105',
rel='enclosure',
type='image/jpeg',
)
],
published='Wed, 01 Jan 2014 00:05:00 GMT',
title='Tesla tower1 lg',
),
]
for index, (actual, expected) in enumerate(
itertools.zip_longest(parsed.entries, expected_items)
):
for key, value in expected.items():
assert actual[key] == value, f'item [{index}][{key!r}] {actual}'


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

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


def prepare_demo_site(target_dir):
Expand Down

0 comments on commit 84f6a2b

Please sign in to comment.