summaryrefslogtreecommitdiff
path: root/pelican/tests/test_utils.py
blob: 449a68146b8d66629b8d9c3d54b74f32d1bbbf61 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
import locale
import logging
import os
import shutil
import time
from sys import platform
from tempfile import mkdtemp

import pytz

from pelican import utils
from pelican.generators import TemplatePagesGenerator
from pelican.readers import Readers
from pelican.settings import read_settings
from pelican.tests.support import (LoggedTestCase, get_article,
                                   locale_available, unittest)
from pelican.writers import Writer


class TestUtils(LoggedTestCase):
    _new_attribute = 'new_value'

    def setUp(self):
        super().setUp()
        self.temp_output = mkdtemp(prefix='pelicantests.')

    def tearDown(self):
        super().tearDown()
        shutil.rmtree(self.temp_output)

    @utils.deprecated_attribute(
        old='_old_attribute', new='_new_attribute',
        since=(3, 1, 0), remove=(4, 1, 3))
    def _old_attribute():
        return None

    def test_deprecated_attribute(self):
        value = self._old_attribute
        self.assertEqual(value, self._new_attribute)
        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)

    def test_get_date(self):
        # valid ones
        date = utils.SafeDatetime(year=2012, month=11, day=22)
        date_hour = utils.SafeDatetime(
            year=2012, month=11, day=22, hour=22, minute=11)
        date_hour_z = utils.SafeDatetime(
            year=2012, month=11, day=22, hour=22, minute=11,
            tzinfo=pytz.timezone('UTC'))
        date_hour_est = utils.SafeDatetime(
            year=2012, month=11, day=22, hour=22, minute=11,
            tzinfo=pytz.timezone('EST'))
        date_hour_sec = utils.SafeDatetime(
            year=2012, month=11, day=22, hour=22, minute=11, second=10)
        date_hour_sec_z = utils.SafeDatetime(
            year=2012, month=11, day=22, hour=22, minute=11, second=10,
            tzinfo=pytz.timezone('UTC'))
        date_hour_sec_est = utils.SafeDatetime(
            year=2012, month=11, day=22, hour=22, minute=11, second=10,
            tzinfo=pytz.timezone('EST'))
        date_hour_sec_frac_z = utils.SafeDatetime(
            year=2012, month=11, day=22, hour=22, minute=11, second=10,
            microsecond=123000, tzinfo=pytz.timezone('UTC'))
        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,
            '22.11.2012 22:11': date_hour,
            '2012-11-22T22:11Z': date_hour_z,
            '2012-11-22T22:11-0500': date_hour_est,
            '2012-11-22 22:11:10': date_hour_sec,
            '2012-11-22T22:11:10Z': date_hour_sec_z,
            '2012-11-22T22:11:10-0500': date_hour_sec_est,
            '2012-11-22T22:11:10.123Z': date_hour_sec_frac_z,
        }

        # examples from http://www.w3.org/TR/NOTE-datetime
        iso_8601_date = utils.SafeDatetime(year=1997, month=7, day=16)
        iso_8601_date_hour_tz = utils.SafeDatetime(
            year=1997, month=7, day=16, hour=19, minute=20,
            tzinfo=pytz.timezone('CET'))
        iso_8601_date_hour_sec_tz = utils.SafeDatetime(
            year=1997, month=7, day=16, hour=19, minute=20, second=30,
            tzinfo=pytz.timezone('CET'))
        iso_8601_date_hour_sec_ms_tz = utils.SafeDatetime(
            year=1997, month=7, day=16, hour=19, minute=20, second=30,
            microsecond=450000, tzinfo=pytz.timezone('CET'))
        iso_8601 = {
            '1997-07-16': iso_8601_date,
            '1997-07-16T19:20+01:00': iso_8601_date_hour_tz,
            '1997-07-16T19:20:30+01:00': iso_8601_date_hour_sec_tz,
            '1997-07-16T19:20:30.45+01:00': iso_8601_date_hour_sec_ms_tz,
        }

        # invalid ones
        invalid_dates = ['2010-110-12', 'yay']

        for value, expected in dates.items():
            self.assertEqual(utils.get_date(value), expected, value)

        for value, expected in iso_8601.items():
            self.assertEqual(utils.get_date(value), expected, value)

        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'),
                   ('this → is ← a ↑ test', 'this-is-a-test'),
                   ('this--is---a test', 'this-is-a-test'),
                   ('unicode測試許功蓋,你看到了嗎?',
                    'unicodece-shi-xu-gong-gai-ni-kan-dao-liao-ma'),
                   ('大飯原発4号機、18日夜起動へ',
                    'da-fan-yuan-fa-4hao-ji-18ri-ye-qi-dong-he'),)

        settings = read_settings()
        subs = settings['SLUG_REGEX_SUBSTITUTIONS']

        for value, expected in samples:
            self.assertEqual(utils.slugify(value, regex_subs=subs), expected)

        self.assertEqual(utils.slugify('Cat', regex_subs=subs), 'cat')
        self.assertEqual(
            utils.slugify('Cat', regex_subs=subs, preserve_case=False), 'cat')
        self.assertEqual(
            utils.slugify('Cat', regex_subs=subs, preserve_case=True), 'Cat')

    def test_slugify_use_unicode(self):

        samples = (
            ('this is a test', 'this-is-a-test'),
            ('this        is a test', 'this-is-a-test'),
            ('this → is ← a ↑ test', 'this-is-a-test'),
            ('this--is---a test', 'this-is-a-test'),
            ('unicode測試許功蓋,你看到了嗎?', 'unicode測試許功蓋你看到了嗎'),
            ('Çığ', 'çığ')
        )

        settings = read_settings()
        subs = settings['SLUG_REGEX_SUBSTITUTIONS']

        for value, expected in samples:
            self.assertEqual(
                utils.slugify(value, regex_subs=subs, use_unicode=True),
                expected)

        # check with preserve case
        for value, expected in samples:
            self.assertEqual(
                utils.slugify('Çığ', regex_subs=subs,
                              preserve_case=True, use_unicode=True),
                'Çığ')

        # check normalization
        samples = (
            ('大飯原発4号機、18日夜起動へ', '大飯原発4号機18日夜起動へ'),
            (
                '\N{LATIN SMALL LETTER C}\N{COMBINING CEDILLA}',
                '\N{LATIN SMALL LETTER C WITH CEDILLA}'
            )
        )
        for value, expected in samples:
            self.assertEqual(
                utils.slugify(value, regex_subs=subs, use_unicode=True),
                expected)

    def test_slugify_substitute(self):

        samples = (('C++ is based on C', 'cpp-is-based-on-c'),
                   ('C+++ test C+ test', 'cpp-test-c-test'),
                   ('c++, c#, C#, C++', 'cpp-c-sharp-c-sharp-cpp'),
                   ('c++-streams', 'cpp-streams'),)

        settings = read_settings()
        subs = [
            (r'C\+\+', 'CPP'),
            (r'C#', 'C-SHARP'),
        ] + settings['SLUG_REGEX_SUBSTITUTIONS']
        for value, expected in samples:
            self.assertEqual(utils.slugify(value, regex_subs=subs), expected)

    def test_slugify_substitute_and_keeping_non_alphanum(self):

        samples = (('Fedora QA', 'fedora.qa'),
                   ('C++ is used by Fedora QA', 'cpp is used by fedora.qa'),
                   ('C++ is based on C', 'cpp is based on c'),
                   ('C+++ test C+ test', 'cpp+ test c+ test'),)

        subs = [
            (r'Fedora QA', 'fedora.qa'),
            (r'c\+\+', 'cpp'),
        ]
        for value, expected in samples:
            self.assertEqual(utils.slugify(value, regex_subs=subs), expected)

    def test_get_relative_path(self):

        samples = ((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),
                   (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),)

        for value, expected in samples:
            self.assertEqual(utils.get_relative_path(value), expected)

    def test_truncate_html_words(self):
        # Plain text.
        self.assertEqual(
            utils.truncate_html_words('short string', 20),
            'short string')
        self.assertEqual(
            utils.truncate_html_words('word ' * 100, 20),
            'word ' * 20 + '…')

        # Plain text with Unicode content.
        self.assertEqual(
            utils.truncate_html_words(
                '我愿意这样,朋友——我独自远行,不但没有你,\
                 并且再没有别的影在黑暗里。', 12
            ),
            '我愿意这样,朋友——我独自远行' + ' …')
        self.assertEqual(
            utils.truncate_html_words(
                'Ты мелькнула, ты предстала, Снова сердце задрожало,', 3
            ),
            'Ты мелькнула, ты' + ' …')
        self.assertEqual(
            utils.truncate_html_words(
                'Trong đầm gì đẹp bằng sen', 4
            ),
            'Trong đầm gì đẹp' + ' …')

        # Words enclosed or intervaled by HTML tags.
        self.assertEqual(
            utils.truncate_html_words('<p>' + 'word ' * 100 + '</p>', 20),
            '<p>' + 'word ' * 20 + '…</p>')
        self.assertEqual(
            utils.truncate_html_words(
                '<span\nstyle="\n…\n">' + 'word ' * 100 + '</span>', 20),
            '<span\nstyle="\n…\n">' + 'word ' * 20 + '…</span>')
        self.assertEqual(
            utils.truncate_html_words('<br>' + 'word ' * 100, 20),
            '<br>' + 'word ' * 20 + '…')
        self.assertEqual(
            utils.truncate_html_words('<!-- comment -->' + 'word ' * 100, 20),
            '<!-- comment -->' + 'word ' * 20 + '…')

        # Words with hypens and apostrophes.
        self.assertEqual(
            utils.truncate_html_words("a-b " * 100, 20),
            "a-b " * 20 + '…')
        self.assertEqual(
            utils.truncate_html_words("it's " * 100, 20),
            "it's " * 20 + '…')

        # Words with HTML entity references.
        self.assertEqual(
            utils.truncate_html_words("&eacute; " * 100, 20),
            "&eacute; " * 20 + '…')
        self.assertEqual(
            utils.truncate_html_words("caf&eacute; " * 100, 20),
            "caf&eacute; " * 20 + '…')
        self.assertEqual(
            utils.truncate_html_words("&egrave;lite " * 100, 20),
            "&egrave;lite " * 20 + '…')
        self.assertEqual(
            utils.truncate_html_words("cafeti&eacute;re " * 100, 20),
            "cafeti&eacute;re " * 20 + '…')
        self.assertEqual(
            utils.truncate_html_words("&int;dx " * 100, 20),
            "&int;dx " * 20 + '…')

        # Words with HTML character references inside and outside
        # the ASCII range.
        self.assertEqual(
            utils.truncate_html_words("&#xe9; " * 100, 20),
            "&#xe9; " * 20 + '…')
        self.assertEqual(
            utils.truncate_html_words("&#x222b;dx " * 100, 20),
            "&#x222b;dx " * 20 + '…')

        # Words with invalid or broken HTML references.
        self.assertEqual(
            utils.truncate_html_words('&invalid;', 20), '&invalid;')
        self.assertEqual(
            utils.truncate_html_words('&#9999999999;', 20), '&#9999999999;')
        self.assertEqual(
            utils.truncate_html_words('&#xfffffffff;', 20), '&#xfffffffff;')
        self.assertEqual(
            utils.truncate_html_words('&mdash text', 20), '&mdash text')
        self.assertEqual(
            utils.truncate_html_words('&#1234 text', 20), '&#1234 text')
        self.assertEqual(
            utils.truncate_html_words('&#xabc text', 20), '&#xabc text')

    def test_process_translations(self):
        fr_articles = []
        en_articles = []

        # create a bunch of articles
        # 0: no translation metadata
        fr_articles.append(get_article(lang='fr', slug='yay0', title='Titre',
                                       content='en français'))
        en_articles.append(get_article(lang='en', slug='yay0', title='Title',
                                       content='in english'))
        # 1: translation metadata on default lang
        fr_articles.append(get_article(lang='fr', slug='yay1', title='Titre',
                                       content='en français'))
        en_articles.append(get_article(lang='en', slug='yay1', title='Title',
                                       content='in english',
                                       translation='true'))
        # 2: translation metadata not on default lang
        fr_articles.append(get_article(lang='fr', slug='yay2', title='Titre',
                                       content='en français',
                                       translation='true'))
        en_articles.append(get_article(lang='en', slug='yay2', title='Title',
                                       content='in english'))
        # 3: back to default language detection if all items have the
        #    translation metadata
        fr_articles.append(get_article(lang='fr', slug='yay3', title='Titre',
                                       content='en français',
                                       translation='yep'))
        en_articles.append(get_article(lang='en', slug='yay3', title='Title',
                                       content='in english',
                                       translation='yes'))
        # 4-5: translation pairs with the same slug but different category
        fr_articles.append(get_article(lang='fr', slug='yay4', title='Titre',
                                       content='en français', category='foo'))
        en_articles.append(get_article(lang='en', slug='yay4', title='Title',
                                       content='in english', category='foo'))
        fr_articles.append(get_article(lang='fr', slug='yay4', title='Titre',
                                       content='en français', category='bar'))
        en_articles.append(get_article(lang='en', slug='yay4', title='Title',
                                       content='in english', category='bar'))

        # try adding articles in both orders
        for lang0_articles, lang1_articles in ((fr_articles, en_articles),
                                               (en_articles, fr_articles)):
            articles = lang0_articles + lang1_articles

            # test process_translations with falsy translation_id
            index, trans = utils.process_translations(
                articles, translation_id=None)
            for i in range(6):
                for lang_articles in [en_articles, fr_articles]:
                    self.assertIn(lang_articles[i], index)
                    self.assertNotIn(lang_articles[i], trans)

            # test process_translations with simple and complex translation_id
            for translation_id in ['slug', {'slug', 'category'}]:
                index, trans = utils.process_translations(
                    articles, translation_id=translation_id)

                for a in [en_articles[0], fr_articles[1], en_articles[2],
                          en_articles[3], en_articles[4], en_articles[5]]:
                    self.assertIn(a, index)
                    self.assertNotIn(a, trans)

                for a in [fr_articles[0], en_articles[1], fr_articles[2],
                          fr_articles[3], fr_articles[4], fr_articles[5]]:
                    self.assertIn(a, trans)
                    self.assertNotIn(a, index)

                for i in range(6):
                    self.assertIn(en_articles[i], fr_articles[i].translations)
                    self.assertIn(fr_articles[i], en_articles[i].translations)

                for a_arts in [en_articles, fr_articles]:
                    for b_arts in [en_articles, fr_articles]:
                        if translation_id == 'slug':
                            self.assertIn(a_arts[4], b_arts[5].translations)
                            self.assertIn(a_arts[5], b_arts[4].translations)
                        elif translation_id == {'slug', 'category'}:
                            self.assertNotIn(a_arts[4], b_arts[5].translations)
                            self.assertNotIn(a_arts[5], b_arts[4].translations)

    def test_filesystemwatcher(self):
        def create_file(name, content):
            with open(name, 'w') as f:
                f.write(content)

        # disable logger filter
        from pelican.utils import logger
        logger.disable_filter()

        # create a temp "project" dir
        root = mkdtemp()
        content_path = os.path.join(root, 'content')
        static_path = os.path.join(root, 'content', 'static')
        config_file = os.path.join(root, 'config.py')
        theme_path = os.path.join(root, 'mytheme')

        # populate
        os.mkdir(content_path)
        os.mkdir(theme_path)
        create_file(config_file,
                    'PATH = "content"\n'
                    'THEME = "mytheme"\n'
                    'STATIC_PATHS = ["static"]')

        t = time.time() - 1000  # make sure it's in the "past"
        os.utime(config_file, (t, t))
        settings = read_settings(config_file)

        watcher = utils.FileSystemWatcher(config_file, Readers, settings)
        # should get a warning for static not not existing
        self.assertLogCountEqual(1, 'Watched path does not exist: .*static')

        # create it and update config
        os.mkdir(static_path)
        watcher.update_watchers(settings)
        # no new warning
        self.assertLogCountEqual(1, 'Watched path does not exist: .*static')

        # get modified values
        modified = watcher.check()
        # empty theme and content should raise warnings
        self.assertLogCountEqual(1, 'No valid files found in content')
        self.assertLogCountEqual(1, 'Empty theme folder. Using `basic` theme')

        self.assertIsNone(modified['content'])  # empty
        self.assertIsNone(modified['theme'])  # empty
        self.assertIsNone(modified['[static]static'])  # empty
        self.assertTrue(modified['settings'])  # modified, first time

        # add a content, add file to theme and check again
        create_file(os.path.join(content_path, 'article.md'),
                    'Title: test\n'
                    'Date: 01-01-2020')

        create_file(os.path.join(theme_path, 'dummy'),
                    'test')

        modified = watcher.check()
        # no new warning
        self.assertLogCountEqual(1, 'No valid files found in content')
        self.assertLogCountEqual(1, 'Empty theme folder. Using `basic` theme')

        self.assertIsNone(modified['[static]static'])  # empty
        self.assertFalse(modified['settings'])  # not modified
        self.assertTrue(modified['theme'])  # modified
        self.assertTrue(modified['content'])  # modified

        # change config, remove static path
        create_file(config_file,
                    'PATH = "content"\n'
                    'THEME = "mytheme"\n'
                    'STATIC_PATHS = []')

        settings = read_settings(config_file)
        watcher.update_watchers(settings)

        modified = watcher.check()
        self.assertNotIn('[static]static', modified)  # should be gone
        self.assertTrue(modified['settings'])  # modified
        self.assertFalse(modified['content'])  # not modified
        self.assertFalse(modified['theme'])  # not modified

        # cleanup
        logger.enable_filter()
        shutil.rmtree(root)

    def test_clean_output_dir(self):
        retention = ()
        test_directory = os.path.join(self.temp_output,
                                      'clean_output')
        content = os.path.join(os.path.dirname(__file__), 'content')
        shutil.copytree(content, test_directory)
        utils.clean_output_dir(test_directory, retention)
        self.assertTrue(os.path.isdir(test_directory))
        self.assertListEqual([], os.listdir(test_directory))
        shutil.rmtree(test_directory)

    def test_clean_output_dir_not_there(self):
        retention = ()
        test_directory = os.path.join(self.temp_output,
                                      'does_not_exist')
        utils.clean_output_dir(test_directory, retention)
        self.assertFalse(os.path.exists(test_directory))

    def test_clean_output_dir_is_file(self):
        retention = ()
        test_directory = os.path.join(self.temp_output,
                                      'this_is_a_file')
        f = open(test_directory, 'w')
        f.write('')
        f.close()
        utils.clean_output_dir(test_directory, retention)
        self.assertFalse(os.path.exists(test_directory))

    def test_strftime(self):
        d = utils.SafeDatetime(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')

        # RFC 3339
        self.assertEqual(
            utils.strftime(d, '%Y-%m-%dT%H:%M:%SZ'),
            '2012-08-29T00:00:00Z')

        # % 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')

        # alternative formatting options
        self.assertEqual(utils.strftime(d, '%-d/%-m/%y'), '29/8/12')
        self.assertEqual(utils.strftime(d, '%-H:%-M:%-S'), '0:0:0')

        d = utils.SafeDatetime(2012, 8, 9)
        self.assertEqual(utils.strftime(d, '%-d/%-m/%y'), '9/8/12')

        d = utils.SafeDatetime(2021, 1, 8)
        self.assertEqual(utils.strftime(d, '%G - %-V - %u'), '2021 - 1 - 5')

    # test the output of utils.strftime in a different locale
    # Turkish locale
    @unittest.skipUnless(locale_available('tr_TR.UTF-8') or
                         locale_available('Turkish'),
                         'Turkish locale needed')
    def test_strftime_locale_dependent_turkish(self):
        # store current locale
        old_locale = locale.setlocale(locale.LC_ALL)

        if platform == 'win32':
            locale.setlocale(locale.LC_ALL, 'Turkish')
        else:
            locale.setlocale(locale.LC_ALL, 'tr_TR.UTF-8')

        d = utils.SafeDatetime(2012, 8, 29)

        # simple
        self.assertEqual(utils.strftime(d, '%d %B %Y'), '29 Ağustos 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_ALL, old_locale)

    # test the output of utils.strftime in a different locale
    # French locale
    @unittest.skipUnless(locale_available('fr_FR.UTF-8') or
                         locale_available('French'),
                         'French locale needed')
    def test_strftime_locale_dependent_french(self):
        # store current locale
        old_locale = locale.setlocale(locale.LC_ALL)

        if platform == 'win32':
            locale.setlocale(locale.LC_ALL, 'French')
        else:
            locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')

        d = utils.SafeDatetime(2012, 8, 29)

        # simple
        self.assertEqual(utils.strftime(d, '%d %B %Y'), '29 août 2012')

        # depending on OS, the first letter is m or M
        self.assertTrue(utils.strftime(d, '%A') in ('mercredi', 'Mercredi'))

        # with text
        self.assertEqual(
            utils.strftime(d, 'Écrit le %d %B %Y'),
            'Écrit le 29 août 2012')

        # non-ascii format candidate (someone might pass it… for some reason)
        self.assertEqual(
            utils.strftime(d, '%écrits en %Y'),
            '%écrits en 2012')

        # restore locale back
        locale.setlocale(locale.LC_ALL, old_locale)

    def test_maybe_pluralize(self):
        self.assertEqual(
            utils.maybe_pluralize(0, 'Article', 'Articles'),
            '0 Articles')
        self.assertEqual(
            utils.maybe_pluralize(1, 'Article', 'Articles'),
            '1 Article')
        self.assertEqual(
            utils.maybe_pluralize(2, 'Article', 'Articles'),
            '2 Articles')


class TestCopy(unittest.TestCase):
    '''Tests the copy utility'''

    def setUp(self):
        self.root_dir = mkdtemp(prefix='pelicantests.')
        self.old_locale = locale.setlocale(locale.LC_ALL)
        locale.setlocale(locale.LC_ALL, 'C')

    def tearDown(self):
        shutil.rmtree(self.root_dir)
        locale.setlocale(locale.LC_ALL, self.old_locale)

    def _create_file(self, *path):
        with open(os.path.join(self.root_dir, *path), 'w') as f:
            f.write('42\n')

    def _create_dir(self, *path):
        os.makedirs(os.path.join(self.root_dir, *path))

    def _exist_file(self, *path):
        path = os.path.join(self.root_dir, *path)
        self.assertTrue(os.path.isfile(path), 'File does not exist: %s' % path)

    def _exist_dir(self, *path):
        path = os.path.join(self.root_dir, *path)
        self.assertTrue(os.path.exists(path),
                        'Directory does not exist: %s' % path)

    def test_copy_file_same_path(self):
        self._create_file('a.txt')
        utils.copy(os.path.join(self.root_dir, 'a.txt'),
                   os.path.join(self.root_dir, 'b.txt'))
        self._exist_file('b.txt')

    def test_copy_file_different_path(self):
        self._create_dir('a')
        self._create_dir('b')
        self._create_file('a', 'a.txt')
        utils.copy(os.path.join(self.root_dir, 'a', 'a.txt'),
                   os.path.join(self.root_dir, 'b', 'b.txt'))
        self._exist_dir('b')
        self._exist_file('b', 'b.txt')

    def test_copy_file_create_dirs(self):
        self._create_file('a.txt')
        utils.copy(
            os.path.join(self.root_dir, 'a.txt'),
            os.path.join(self.root_dir, 'b0', 'b1', 'b2', 'b3', 'b.txt'))
        self._exist_dir('b0')
        self._exist_dir('b0', 'b1')
        self._exist_dir('b0', 'b1', 'b2')
        self._exist_dir('b0', 'b1', 'b2', 'b3')
        self._exist_file('b0', 'b1', 'b2', 'b3', 'b.txt')

    def test_copy_dir_same_path(self):
        self._create_dir('a')
        self._create_file('a', 'a.txt')
        utils.copy(os.path.join(self.root_dir, 'a'),
                   os.path.join(self.root_dir, 'b'))
        self._exist_dir('b')
        self._exist_file('b', 'a.txt')

    def test_copy_dir_different_path(self):
        self._create_dir('a0')
        self._create_dir('a0', 'a1')
        self._create_file('a0', 'a1', 'a.txt')
        self._create_dir('b0')
        utils.copy(os.path.join(self.root_dir, 'a0', 'a1'),
                   os.path.join(self.root_dir, 'b0', 'b1'))
        self._exist_dir('b0', 'b1')
        self._exist_file('b0', 'b1', 'a.txt')

    def test_copy_dir_create_dirs(self):
        self._create_dir('a')
        self._create_file('a', 'a.txt')
        utils.copy(os.path.join(self.root_dir, 'a'),
                   os.path.join(self.root_dir, 'b0', 'b1', 'b2', 'b3', 'b'))
        self._exist_dir('b0')
        self._exist_dir('b0', 'b1')
        self._exist_dir('b0', 'b1', 'b2')
        self._exist_dir('b0', 'b1', 'b2', 'b3')
        self._exist_dir('b0', 'b1', 'b2', 'b3', 'b')
        self._exist_file('b0', 'b1', 'b2', 'b3', 'b', 'a.txt')


class TestDateFormatter(unittest.TestCase):
    '''Tests that the output of DateFormatter jinja filter is same as
    utils.strftime'''

    def setUp(self):
        # prepare a temp content and output folder
        self.temp_content = mkdtemp(prefix='pelicantests.')
        self.temp_output = mkdtemp(prefix='pelicantests.')

        # prepare a template file
        template_dir = os.path.join(self.temp_content, 'template')
        template_path = os.path.join(template_dir, 'source.html')
        os.makedirs(template_dir)
        with open(template_path, 'w') as template_file:
            template_file.write('date = {{ date|strftime("%A, %d %B %Y") }}')
        self.date = utils.SafeDatetime(2012, 8, 29)

    def tearDown(self):
        shutil.rmtree(self.temp_content)
        shutil.rmtree(self.temp_output)
        # reset locale to default
        locale.setlocale(locale.LC_ALL, '')

    @unittest.skipUnless(locale_available('fr_FR.UTF-8') or
                         locale_available('French'),
                         'French locale needed')
    def test_french_strftime(self):
        # This test tries to reproduce an issue that
        # occurred with python3.3 under macos10 only
        if platform == 'win32':
            locale.setlocale(locale.LC_ALL, 'French')
        else:
            locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
        date = utils.SafeDatetime(2014, 8, 14)
        # we compare the lower() dates since macos10 returns
        # "Jeudi" for %A whereas linux reports "jeudi"
        self.assertEqual(
            'jeudi, 14 août 2014',
            utils.strftime(date, date_format="%A, %d %B %Y").lower())
        df = utils.DateFormatter()
        self.assertEqual(
            'jeudi, 14 août 2014',
            df(date, date_format="%A, %d %B %Y").lower())
        # Let us now set the global locale to C:
        locale.setlocale(locale.LC_ALL, 'C')
        # DateFormatter should still work as expected
        # since it is the whole point of DateFormatter
        # (This is where pre-2014/4/15 code fails on macos10)
        df_date = df(date, date_format="%A, %d %B %Y").lower()
        self.assertEqual('jeudi, 14 août 2014', df_date)

    @unittest.skipUnless(locale_available('fr_FR.UTF-8') or
                         locale_available('French'),
                         'French locale needed')
    def test_french_locale(self):
        if platform == 'win32':
            locale_string = 'French'
        else:
            locale_string = 'fr_FR.UTF-8'
        settings = read_settings(
            override={
                'LOCALE': locale_string,
                'TEMPLATE_PAGES': {
                    'template/source.html': 'generated/file.html'
                }
            })

        generator = TemplatePagesGenerator(
            {'date': self.date}, settings,
            self.temp_content, '', self.temp_output)
        generator.env.filters.update({'strftime': utils.DateFormatter()})

        writer = Writer(self.temp_output, settings=settings)
        generator.generate_output(writer)

        output_path = os.path.join(
            self.temp_output, 'generated', 'file.html')

        # output file has been generated
        self.assertTrue(os.path.exists(output_path))

        # output content is correct
        with utils.pelican_open(output_path) as output_file:
            self.assertEqual(output_file,
                             utils.strftime(self.date, 'date = %A, %d %B %Y'))

    @unittest.skipUnless(locale_available('tr_TR.UTF-8') or
                         locale_available('Turkish'),
                         'Turkish locale needed')
    def test_turkish_locale(self):
        if platform == 'win32':
            locale_string = 'Turkish'
        else:
            locale_string = 'tr_TR.UTF-8'
        settings = read_settings(
            override={
                'LOCALE': locale_string,
                'TEMPLATE_PAGES': {
                    'template/source.html': 'generated/file.html'
                }
            })

        generator = TemplatePagesGenerator(
            {'date': self.date}, settings,
            self.temp_content, '', self.temp_output)
        generator.env.filters.update({'strftime': utils.DateFormatter()})

        writer = Writer(self.temp_output, settings=settings)
        generator.generate_output(writer)

        output_path = os.path.join(
            self.temp_output, 'generated', 'file.html')

        # output file has been generated
        self.assertTrue(os.path.exists(output_path))

        # output content is correct
        with utils.pelican_open(output_path) as output_file:
            self.assertEqual(output_file,
                             utils.strftime(self.date, 'date = %A, %d %B %Y'))


class TestSanitisedJoin(unittest.TestCase):
    def test_detect_parent_breakout(self):
        with self.assertRaisesRegex(
                RuntimeError,
                "Attempted to break out of output directory to "
                "(.*?:)?/foo/test"):  # (.*?:)? accounts for Windows root
            utils.sanitised_join(
                "/foo/bar",
                "../test"
            )

    def test_detect_root_breakout(self):
        with self.assertRaisesRegex(
                RuntimeError,
                "Attempted to break out of output directory to "
                "(.*?:)?/test"):  # (.*?:)? accounts for Windows root
            utils.sanitised_join(
                "/foo/bar",
                "/test"
            )

    def test_pass_deep_subpaths(self):
        self.assertEqual(
            utils.sanitised_join(
                "/foo/bar",
                "test"
            ),
            utils.posixize_path(
                os.path.abspath(os.path.join("/foo/bar", "test")))
        )