2012-03-11 15:51:48 +01:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2013-01-11 02:57:43 +01:00
|
|
|
|
from __future__ import unicode_literals, print_function
|
2013-01-18 07:33:42 -05:00
|
|
|
|
import logging
|
2012-07-04 12:32:20 -07:00
|
|
|
|
import shutil
|
2012-03-14 12:36:55 +01:00
|
|
|
|
import os
|
2012-03-11 15:51:48 +01:00
|
|
|
|
import datetime
|
2012-03-14 12:36:55 +01:00
|
|
|
|
import time
|
2013-04-16 20:53:27 -04:00
|
|
|
|
import locale
|
|
|
|
|
|
from sys import platform
|
2012-03-11 15:51:48 +01:00
|
|
|
|
|
|
|
|
|
|
from pelican import utils
|
2013-04-16 20:53:27 -04:00
|
|
|
|
from .support import get_article, LoggedTestCase, locale_available, unittest
|
2012-08-23 13:13:41 -07:00
|
|
|
|
from pelican.utils import NoFilesError
|
2012-03-11 15:51:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
2013-01-18 07:33:42 -05:00
|
|
|
|
class TestUtils(LoggedTestCase):
|
2013-01-04 13:02:30 -05:00
|
|
|
|
_new_attribute = 'new_value'
|
|
|
|
|
|
|
|
|
|
|
|
@utils.deprecated_attribute(
|
|
|
|
|
|
old='_old_attribute', new='_new_attribute',
|
|
|
|
|
|
since=(3, 1, 0), remove=(4, 1, 3))
|
2013-03-03 19:44:57 -08:00
|
|
|
|
def _old_attribute():
|
|
|
|
|
|
return None
|
2013-01-04 13:02:30 -05:00
|
|
|
|
|
|
|
|
|
|
def test_deprecated_attribute(self):
|
|
|
|
|
|
value = self._old_attribute
|
2013-04-13 16:36:05 -04:00
|
|
|
|
self.assertEqual(value, self._new_attribute)
|
2013-01-18 07:33:42 -05:00
|
|
|
|
self.assertLogCountEqual(
|
|
|
|
|
|
count=1,
|
|
|
|
|
|
msg=('_old_attribute has been deprecated since 3.1.0 and will be '
|
|
|
|
|
|
'removed by version 4.1.3. Use _new_attribute instead'),
|
|
|
|
|
|
level=logging.WARNING)
|
2012-03-11 15:51:48 +01:00
|
|
|
|
|
|
|
|
|
|
def test_get_date(self):
|
|
|
|
|
|
# valid ones
|
|
|
|
|
|
date = datetime.datetime(year=2012, month=11, day=22)
|
|
|
|
|
|
date_hour = datetime.datetime(year=2012, month=11, day=22, hour=22,
|
|
|
|
|
|
minute=11)
|
|
|
|
|
|
date_hour_sec = datetime.datetime(year=2012, month=11, day=22, hour=22,
|
|
|
|
|
|
minute=11, second=10)
|
|
|
|
|
|
dates = {'2012-11-22': date,
|
|
|
|
|
|
'2012/11/22': date,
|
|
|
|
|
|
'2012-11-22 22:11': date_hour,
|
|
|
|
|
|
'2012/11/22 22:11': date_hour,
|
|
|
|
|
|
'22-11-2012': date,
|
|
|
|
|
|
'22/11/2012': date,
|
|
|
|
|
|
'22.11.2012': date,
|
|
|
|
|
|
'2012-22-11': date,
|
|
|
|
|
|
'22.11.2012 22:11': date_hour,
|
|
|
|
|
|
'2012-11-22 22:11:10': date_hour_sec}
|
|
|
|
|
|
|
|
|
|
|
|
for value, expected in dates.items():
|
2013-04-13 16:36:05 -04:00
|
|
|
|
self.assertEqual(utils.get_date(value), expected, value)
|
2012-03-11 15:51:48 +01:00
|
|
|
|
|
|
|
|
|
|
# invalid ones
|
|
|
|
|
|
invalid_dates = ('2010-110-12', 'yay')
|
|
|
|
|
|
for item in invalid_dates:
|
|
|
|
|
|
self.assertRaises(ValueError, utils.get_date, item)
|
|
|
|
|
|
|
|
|
|
|
|
def test_slugify(self):
|
|
|
|
|
|
|
|
|
|
|
|
samples = (('this is a test', 'this-is-a-test'),
|
|
|
|
|
|
('this is a test', 'this-is-a-test'),
|
2013-01-11 02:57:43 +01:00
|
|
|
|
('this → is ← a ↑ test', 'this-is-a-test'),
|
2012-07-16 11:36:20 +08:00
|
|
|
|
('this--is---a test', 'this-is-a-test'),
|
2013-03-03 19:44:57 -08:00
|
|
|
|
('unicode測試許功蓋,你看到了嗎?',
|
|
|
|
|
|
'unicodece-shi-xu-gong-gai-ni-kan-dao-liao-ma'),
|
|
|
|
|
|
('大飯原発4号機、18日夜起動へ',
|
|
|
|
|
|
'da-fan-yuan-fa-4hao-ji-18ri-ye-qi-dong-he'),)
|
2012-03-11 15:51:48 +01:00
|
|
|
|
|
|
|
|
|
|
for value, expected in samples:
|
2013-04-13 16:36:05 -04:00
|
|
|
|
self.assertEqual(utils.slugify(value), expected)
|
2012-03-11 15:51:48 +01:00
|
|
|
|
|
|
|
|
|
|
def test_get_relative_path(self):
|
|
|
|
|
|
|
2013-03-11 08:38:33 -04:00
|
|
|
|
samples = ((os.path.join('test', 'test.html'), os.pardir),
|
|
|
|
|
|
(os.path.join('test', 'test', 'test.html'),
|
|
|
|
|
|
os.path.join(os.pardir, os.pardir)),
|
2012-12-10 03:03:48 +05:00
|
|
|
|
('test.html', os.curdir),
|
|
|
|
|
|
(os.path.join('/test', 'test.html'), os.pardir),
|
|
|
|
|
|
(os.path.join('/test', 'test', 'test.html'),
|
|
|
|
|
|
os.path.join(os.pardir, os.pardir)),
|
|
|
|
|
|
('/test.html', os.curdir),)
|
2012-03-11 15:51:48 +01:00
|
|
|
|
|
|
|
|
|
|
for value, expected in samples:
|
2013-04-13 16:36:05 -04:00
|
|
|
|
self.assertEqual(utils.get_relative_path(value), expected)
|
2012-03-11 15:51:48 +01:00
|
|
|
|
|
|
|
|
|
|
def test_process_translations(self):
|
|
|
|
|
|
# create a bunch of articles
|
2013-04-06 17:48:19 +01:00
|
|
|
|
# 1: no translation metadata
|
2012-03-11 15:51:48 +01:00
|
|
|
|
fr_article1 = get_article(lang='fr', slug='yay', title='Un titre',
|
|
|
|
|
|
content='en français')
|
|
|
|
|
|
en_article1 = get_article(lang='en', slug='yay', title='A title',
|
|
|
|
|
|
content='in english')
|
2013-04-06 17:48:19 +01:00
|
|
|
|
# 2: reverse which one is the translation thanks to metadata
|
|
|
|
|
|
fr_article2 = get_article(lang='fr', slug='yay2', title='Un titre',
|
|
|
|
|
|
content='en français')
|
|
|
|
|
|
en_article2 = get_article(lang='en', slug='yay2', title='A title',
|
|
|
|
|
|
content='in english',
|
|
|
|
|
|
extra_metadata={'translation': 'true'})
|
|
|
|
|
|
# 3: back to default language detection if all items have the
|
|
|
|
|
|
# translation metadata
|
|
|
|
|
|
fr_article3 = get_article(lang='fr', slug='yay3', title='Un titre',
|
|
|
|
|
|
content='en français',
|
|
|
|
|
|
extra_metadata={'translation': 'yep'})
|
|
|
|
|
|
en_article3 = get_article(lang='en', slug='yay3', title='A title',
|
|
|
|
|
|
content='in english',
|
|
|
|
|
|
extra_metadata={'translation': 'yes'})
|
|
|
|
|
|
|
|
|
|
|
|
articles = [fr_article1, en_article1, fr_article2, en_article2,
|
|
|
|
|
|
fr_article3, en_article3]
|
2012-03-11 15:51:48 +01:00
|
|
|
|
|
|
|
|
|
|
index, trans = utils.process_translations(articles)
|
|
|
|
|
|
|
|
|
|
|
|
self.assertIn(en_article1, index)
|
|
|
|
|
|
self.assertIn(fr_article1, trans)
|
|
|
|
|
|
self.assertNotIn(en_article1, trans)
|
|
|
|
|
|
self.assertNotIn(fr_article1, index)
|
2012-03-14 12:36:55 +01:00
|
|
|
|
|
2013-04-06 17:48:19 +01:00
|
|
|
|
self.assertIn(fr_article2, index)
|
|
|
|
|
|
self.assertIn(en_article2, trans)
|
|
|
|
|
|
self.assertNotIn(fr_article2, trans)
|
|
|
|
|
|
self.assertNotIn(en_article2, index)
|
|
|
|
|
|
|
|
|
|
|
|
self.assertIn(en_article3, index)
|
|
|
|
|
|
self.assertIn(fr_article3, trans)
|
|
|
|
|
|
self.assertNotIn(en_article3, trans)
|
|
|
|
|
|
self.assertNotIn(fr_article3, index)
|
|
|
|
|
|
|
2012-03-14 12:36:55 +01:00
|
|
|
|
def test_files_changed(self):
|
2013-03-10 22:42:46 -07:00
|
|
|
|
# Test if file changes are correctly detected
|
|
|
|
|
|
# Make sure to handle not getting any files correctly.
|
2012-03-14 12:36:55 +01:00
|
|
|
|
|
2013-01-04 11:22:49 -05:00
|
|
|
|
dirname = os.path.join(os.path.dirname(__file__), 'content')
|
|
|
|
|
|
path = os.path.join(dirname, 'article_with_metadata.rst')
|
|
|
|
|
|
changed = utils.files_changed(dirname, 'rst')
|
2013-04-13 16:36:05 -04:00
|
|
|
|
self.assertEqual(changed, True)
|
2012-03-14 12:36:55 +01:00
|
|
|
|
|
2013-01-04 11:22:49 -05:00
|
|
|
|
changed = utils.files_changed(dirname, 'rst')
|
2013-04-13 16:36:05 -04:00
|
|
|
|
self.assertEqual(changed, False)
|
2012-03-14 12:36:55 +01:00
|
|
|
|
|
|
|
|
|
|
t = time.time()
|
2013-01-04 11:22:49 -05:00
|
|
|
|
os.utime(path, (t, t))
|
|
|
|
|
|
changed = utils.files_changed(dirname, 'rst')
|
2013-04-13 16:36:05 -04:00
|
|
|
|
self.assertEqual(changed, True)
|
2012-03-24 14:36:47 +09:00
|
|
|
|
self.assertAlmostEqual(utils.LAST_MTIME, t, delta=1)
|
2012-07-04 12:32:20 -07:00
|
|
|
|
|
2012-08-23 11:05:07 -07:00
|
|
|
|
empty_path = os.path.join(os.path.dirname(__file__), 'empty')
|
|
|
|
|
|
try:
|
|
|
|
|
|
os.mkdir(empty_path)
|
|
|
|
|
|
os.mkdir(os.path.join(empty_path, "empty_folder"))
|
|
|
|
|
|
shutil.copy(__file__, empty_path)
|
2012-08-23 12:44:22 -07:00
|
|
|
|
with self.assertRaises(NoFilesError):
|
|
|
|
|
|
utils.files_changed(empty_path, 'rst')
|
2012-08-23 11:05:07 -07:00
|
|
|
|
except OSError:
|
|
|
|
|
|
self.fail("OSError Exception in test_files_changed test")
|
|
|
|
|
|
finally:
|
|
|
|
|
|
shutil.rmtree(empty_path, True)
|
|
|
|
|
|
|
2012-07-04 12:32:20 -07:00
|
|
|
|
def test_clean_output_dir(self):
|
2013-03-03 19:44:57 -08:00
|
|
|
|
test_directory = os.path.join(os.path.dirname(__file__),
|
|
|
|
|
|
'clean_output')
|
2012-07-04 12:32:20 -07:00
|
|
|
|
content = os.path.join(os.path.dirname(__file__), 'content')
|
|
|
|
|
|
shutil.copytree(content, test_directory)
|
|
|
|
|
|
utils.clean_output_dir(test_directory)
|
|
|
|
|
|
self.assertTrue(os.path.isdir(test_directory))
|
|
|
|
|
|
self.assertListEqual([], os.listdir(test_directory))
|
|
|
|
|
|
shutil.rmtree(test_directory)
|
2012-10-01 19:46:02 -04:00
|
|
|
|
|
|
|
|
|
|
def test_clean_output_dir_not_there(self):
|
2013-03-03 19:44:57 -08:00
|
|
|
|
test_directory = os.path.join(os.path.dirname(__file__),
|
|
|
|
|
|
'does_not_exist')
|
2012-10-01 19:46:02 -04:00
|
|
|
|
utils.clean_output_dir(test_directory)
|
|
|
|
|
|
self.assertTrue(not os.path.exists(test_directory))
|
|
|
|
|
|
|
|
|
|
|
|
def test_clean_output_dir_is_file(self):
|
2013-03-03 19:44:57 -08:00
|
|
|
|
test_directory = os.path.join(os.path.dirname(__file__),
|
|
|
|
|
|
'this_is_a_file')
|
2012-10-01 19:46:02 -04:00
|
|
|
|
f = open(test_directory, 'w')
|
|
|
|
|
|
f.write('')
|
|
|
|
|
|
f.close()
|
|
|
|
|
|
utils.clean_output_dir(test_directory)
|
|
|
|
|
|
self.assertTrue(not os.path.exists(test_directory))
|
2013-04-16 20:53:27 -04:00
|
|
|
|
|
|
|
|
|
|
def test_strftime(self):
|
|
|
|
|
|
d = datetime.date(2012, 8, 29)
|
|
|
|
|
|
|
|
|
|
|
|
# simple formatting
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%d/%m/%y'), '29/08/12')
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%d/%m/%Y'), '29/08/2012')
|
|
|
|
|
|
|
|
|
|
|
|
# % escaped
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%d%%%m%%%y'), '29%08%12')
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%d %% %m %% %y'), '29 % 08 % 12')
|
|
|
|
|
|
# not valid % formatter
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '10% reduction in %Y'),
|
|
|
|
|
|
'10% reduction in 2012')
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%10 reduction in %Y'),
|
|
|
|
|
|
'%10 reduction in 2012')
|
|
|
|
|
|
|
|
|
|
|
|
# with text
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, 'Published in %d-%m-%Y'),
|
|
|
|
|
|
'Published in 29-08-2012')
|
|
|
|
|
|
|
|
|
|
|
|
# with non-ascii text
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%d/%m/%Y Øl trinken beim Besäufnis'),
|
|
|
|
|
|
'29/08/2012 Øl trinken beim Besäufnis')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# test the output of utils.strftime in a different locale
|
|
|
|
|
|
# right now, this uses Turkish locale
|
|
|
|
|
|
# why Turkish? because I know Turkish :). And it produces non-ascii output
|
|
|
|
|
|
# Feel free to extend with different locales
|
|
|
|
|
|
@unittest.skipUnless(locale_available('tr_TR') or
|
|
|
|
|
|
locale_available('Turkish'),
|
|
|
|
|
|
'Turkish locale needed')
|
|
|
|
|
|
def test_strftime_locale_dependent(self):
|
|
|
|
|
|
# store current locale
|
|
|
|
|
|
old_locale = locale.setlocale(locale.LC_TIME)
|
|
|
|
|
|
|
|
|
|
|
|
if platform == 'win32':
|
|
|
|
|
|
locale.setlocale(locale.LC_TIME, str('Turkish'))
|
|
|
|
|
|
else:
|
|
|
|
|
|
locale.setlocale(locale.LC_TIME, str('tr_TR'))
|
|
|
|
|
|
|
|
|
|
|
|
d = datetime.date(2012, 8, 29)
|
|
|
|
|
|
|
|
|
|
|
|
# simple
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%d %B %Y'), '29 Ağustos 2012')
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%d %b %Y'), '29 Ağu 2012')
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%a, %d %b %Y'),
|
|
|
|
|
|
'Çrş, 29 Ağu 2012')
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%A, %d %B %Y'),
|
|
|
|
|
|
'Çarşamba, 29 Ağustos 2012')
|
|
|
|
|
|
|
|
|
|
|
|
# with text
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, 'Yayınlanma tarihi: %A, %d %B %Y'),
|
|
|
|
|
|
'Yayınlanma tarihi: Çarşamba, 29 Ağustos 2012')
|
|
|
|
|
|
|
|
|
|
|
|
# non-ascii format candidate (someone might pass it... for some reason)
|
|
|
|
|
|
self.assertEqual(utils.strftime(d, '%Y yılında %üretim artışı'),
|
|
|
|
|
|
'2012 yılında %üretim artışı')
|
|
|
|
|
|
|
|
|
|
|
|
# restore locale back
|
|
|
|
|
|
locale.setlocale(locale.LC_TIME, old_locale)
|