git @ Cat's Eye Technologies Feedmark / master src / feedmark / tests.py
master

Tree @master (Download .tar.gz)

tests.py @masterraw · history · 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
 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
# Copyright (c) 2019-2024 Chris Pressey, Cat's Eye Technologies
# This file is distributed under an MIT license.  See LICENSES/ directory.
# SPDX-License-Identifier: LicenseRef-MIT-X-Feedmark

import unittest

import json
import os
import sys
from subprocess import check_call
from tempfile import mkdtemp

from feedmark.checkers import Schema
from feedmark.main import main
from feedmark.loader import read_document_from
from feedmark.utils import StringIO


class TestFeedmarkFileCreation(unittest.TestCase):

    def setUp(self):
        super(TestFeedmarkFileCreation, self).setUp()
        self.saved_stdout = sys.stdout
        sys.stdout = StringIO()
        self.maxDiff = None
        self.dirname = mkdtemp()
        self.prevdir = os.getcwd()
        os.chdir(self.dirname)

    def tearDown(self):
        os.chdir(self.prevdir)
        check_call("rm -rf {}".format(self.dirname), shell=True)
        sys.stdout = self.saved_stdout
        super(TestFeedmarkFileCreation, self).tearDown()

    def assert_file_contains(self, filename, text):
        with open(filename, "r") as f:
            contents = f.read()
        self.assertIn(text, contents, contents)

    def test_atom_feed(self):
        main(
            [
                "{}/eg/Recent Llama Sightings.md".format(self.prevdir),
                "--output-atom=feed.xml",
            ]
        )
        self.assert_file_contains(
            "feed.xml",
            "<id>http://example.com/llama.xml/2 Llamas Spotted Near Mall</id>",
        )
        self.assert_file_contains(
            "feed.xml",
            "https://codeberg.org/catseye/Feedmark/src/branch/master/eg/Recent%20Llama%20Sightings.md#2-llamas-spotted-near-mall",
        )
        os.unlink("feed.xml")

    def test_rewrite_markdown_input_refdex(self):
        """When asked to `--rewrite-markdown`, the tool rewrites the input file,
        replacing reference links with links from the given `--input-refdex`.
        """
        with open("foo.md", "w") as f:
            f.write(
                """# Document

### Entry

Have you heard, [2 Llamas Spotted Near Mall]()?

[2 Llamas Spotted Near Mall]: TK
"""
            )
        main(
            [
                "foo.md",
                "--input-refdex={}/eg/refdex.json".format(self.prevdir),
                "--rewrite-markdown",
            ]
        )
        self.assert_file_contains(
            "foo.md",
            "[2 Llamas Spotted Near Mall]: eg/Recent%20Llama%20Sightings.md#2-llamas-spotted-near-mall",
        )
        os.unlink("foo.md")

    def test_rewrite_markdown_internal(self):
        """When asked to `--rewrite-markdown`, the tool rewrites the input file,
        replacing reference links with internal links to the matching sections
        in the document.
        """
        with open("foo.md", "w") as f:
            f.write(
                """# Document

### Bubble & Squeak

Have you heard, [Bubble & Squeak]()?

[Bubble & Squeak]: TK
"""
            )
        main(["foo.md", "--output-refdex", "--rewrite-markdown"])
        self.assert_file_contains("foo.md", "[Bubble & Squeak]: foo.md#bubble-squeak")
        os.unlink("foo.md")


class TestFeedmarkCommandLine(unittest.TestCase):

    def setUp(self):
        super(TestFeedmarkCommandLine, self).setUp()
        self.saved_stdout = sys.stdout
        sys.stdout = StringIO()
        self.maxDiff = None

    def tearDown(self):
        sys.stdout = self.saved_stdout
        super(TestFeedmarkCommandLine, self).tearDown()

    def test_schema(self):
        main(
            [
                "eg/Recent Llama Sightings.md",
                "eg/Ancient Llama Sightings.md",
                "--check-against-schema=eg/schema/Llama sighting.md",
            ]
        )
        output = sys.stdout.getvalue()
        self.assertEqual(output, "")

    def test_schema_failure(self):
        with self.assertRaises(SystemExit):
            main(
                [
                    "eg/Ill-formed Llama Sightings.md",
                    "eg/Recent Llama Sightings.md",
                    "--check-against-schema=eg/schema/Llama sighting.md",
                ]
            )
        data = json.loads(sys.stdout.getvalue())
        self.assertEqual(
            data,
            [
                {
                    "document": "Ill-formed Llama Sightings",
                    "result": [["extra", "excuse"], ["missing", "date"]],
                    "section": "Definite llama sighting with no date",
                }
            ],
        )

    def test_output_html(self):
        main(["eg/Recent Llama Sightings.md", "--output-html"])
        output = sys.stdout.getvalue()
        self.assertIn(
            '<h3 id="a-possible-llama-under-the-bridge">A Possible Llama Under the Bridge</h3>',
            output,
        )

    def test_output_json(self):
        main(["eg/Ancient Llama Sightings.md", "--output-json"])
        data = json.loads(sys.stdout.getvalue())
        self.assertDictEqual(
            data,
            {
                "documents": [
                    {
                        "filename": "eg/Ancient Llama Sightings.md",
                        "title": "Ancient Llama Sightings",
                        "preamble": "",
                        "properties": data["documents"][0]["properties"],
                        "sections": data["documents"][0]["sections"],
                    }
                ]
            },
        )
        self.assertDictEqual(
            data["documents"][0]["properties"],
            {
                "author": "Alfred J. Prufrock",
                "link-target-url": "https://codeberg.org/catseye/Feedmark/src/branch/master/eg/Ancient%20Llama%20Sightings.md",
                "url": "http://example.com/old_llama.xml",
            },
        )
        self.assertEqual(
            data["documents"][0]["sections"],
            [
                {
                    "body": data["documents"][0]["sections"][0]["body"],
                    "images": [
                        {
                            "description": "photo of possible llama",
                            "source": "https://static.catseye.tc/images/screenshots/Kolakoski_Kurve.jpg",
                        }
                    ],
                    "properties": {"date": "Jan 1 1984 12:00:00"},
                    "title": "Maybe sighting the llama",
                    "anchor": "maybe-sighting-the-llama",
                }
            ],
        )
        self.assertIn(
            "It was a possible llama sighting.\n\n",
            data["documents"][0]["sections"][0]["body"],
        )

    def test_output_json_with_multiple_images_and_linked_images(self):
        main(["eg/Recent Llama Sightings.md", "--output-json"])
        data = json.loads(sys.stdout.getvalue())
        self.assertEqual(
            data["documents"][0]["sections"][1]["images"],
            [
                {
                    "description": "photo of possible llama",
                    "source": "https://static.catseye.tc/images/screenshots/Heronsis_hermnonicii.jpg",
                    "link": "https://catseye.tc/article/Gewgaws.md",
                },
                {
                    "description": "another possible photo",
                    "source": "https://static.catseye.tc/images/screenshots/A_Non-Random_Walk.jpg",
                },
            ],
        )

    def test_output_htmlized_json(self):
        """When given the `--htmlized-json` flag, the tool will convert Markdown fields in the output to HTML."""
        main(["eg/Referenced Llama Sightings.md", "--output-json", "--htmlized-json"])
        data = json.loads(sys.stdout.getvalue())
        self.assertDictEqual(
            data,
            {
                "documents": [
                    {
                        "filename": "eg/Referenced Llama Sightings.md",
                        "title": "Referenced Llama Sightings",
                        "preamble": '<p>Some <strong>llamas</strong> have been <a href="spotted.html">spotted</a> recently.</p>',
                        "properties": data["documents"][0]["properties"],
                        "sections": data["documents"][0]["sections"],
                    }
                ]
            },
        )
        self.assertEqual(
            data["documents"][0]["sections"][0]["body"],
            "<p>I have strong opinions about this.  It's a <em>shame</em> more llamas aren't\nbeing spotted.  "
            "Sometimes they are <strong>striped</strong>, it's true, but<br />\nwhen<br />\nthey are, "
            '<a href="https://daringfireball.net/projects/markdown/">Markdown</a>\ncan be used.</p>\n'
            '<p>To <a href="https://en.wikipedia.org/wiki/Site">site</a> them.</p>\n<p>Sight them, sigh.</p>',
        )
        # note that property values are bare HTML fragments: there is no surrounding <p></p> or other element
        self.assertEqual(
            data["documents"][0]["properties"]["hopper"],
            '<a href="https://en.wikipedia.org/wiki/Stephen_Hopper">Stephen</a>',
        )
        self.assertEqual(
            data["documents"][0]["properties"]["spotted"],
            ['<a href="mall.html">the mall</a>', '<a href="beach.html">the beach</a>'],
        )
        self.assertEqual(
            data["documents"][0]["sections"][0]["properties"]["hopper"],
            '<a href="https://en.wikipedia.org/wiki/Grace_Hopper">Grace</a>',
        )
        self.assertEqual(
            data["documents"][0]["sections"][0]["properties"]["spotted"],
            [
                '<a href="mall.html">the mall</a>',
                '<a href="lumberyard.html">the lumberyard</a>',
            ],
        )

    def test_output_unordered_json(self):
        main(["eg/Referenced Llama Sightings.md", "--output-json"])
        data = json.loads(sys.stdout.getvalue())
        self.assertDictEqual(
            data["documents"][0]["properties"],
            {
                "author": "Alfred J. Prufrock",
                "link-target-url": "https://codeberg.org/catseye/Feedmark/src/branch/master/eg/Referenced%20Llama%20Sightings.md",
                "url": "http://example.com/refllama.xml",
                "hopper": "[Stephen](https://en.wikipedia.org/wiki/Stephen_Hopper)",
                "spotted": ["[the mall][]", "[the beach](beach.html)"],
            },
        )
        self.assertDictEqual(
            data["documents"][0]["sections"][0]["properties"],
            {
                "date": "Nov 1 2016 09:00:00",
                "hopper": "[Grace](https://en.wikipedia.org/wiki/Grace_Hopper)",
                "spotted": ["[the mall][]", "[the lumberyard](lumberyard.html)"],
            },
        )

    def test_output_ordered_json(self):
        main(["eg/Referenced Llama Sightings.md", "--output-json", "--ordered-json"])
        data = json.loads(sys.stdout.getvalue())
        self.assertEqual(
            data["documents"][0]["properties"],
            [
                ["author", "Alfred J. Prufrock"],
                ["url", "http://example.com/refllama.xml"],
                [
                    "link-target-url",
                    "https://codeberg.org/catseye/Feedmark/src/branch/master/eg/Referenced%20Llama%20Sightings.md",
                ],
                ["hopper", "[Stephen](https://en.wikipedia.org/wiki/Stephen_Hopper)"],
                ["spotted", ["[the mall][]", "[the beach](beach.html)"]],
            ],
        )
        self.assertEqual(
            data["documents"][0]["sections"][0]["properties"],
            [
                ["date", "Nov 1 2016 09:00:00"],
                ["hopper", "[Grace](https://en.wikipedia.org/wiki/Grace_Hopper)"],
                ["spotted", ["[the mall][]", "[the lumberyard](lumberyard.html)"]],
            ],
        )

    def test_parse_section_headings(self):
        with open("foo.md", "w") as f:
            f.write(
                r"""\
Some Resources
==============

### lo.logic - Most \'unintuitive\' application of the Axiom of Choice? - MathOverflow

*   url: https://mathoverflow.net/questions/20882/most-unintuitive-application-of-the-axiom-of-choice

### set theory - Set theories without \"junk\" theorems? - MathOverflow

*   url: http://mathoverflow.net/questions/90820/set-theories-without-junk-theorems/90945#90945

### The Origin of the Number Zero \| History \| Smithsonian

*   url: https://www.smithsonianmag.com/history/origin-number-zero-180953392/

### zajo/appler: Apple \]\[ emulator for MS-DOS, written in 8088 assembly

*   url: https://github.com/zajo/appler

### Computational \[Complexity\]\(Theory\)

That is not a link.
"""
            )
        main(["foo.md", "--output-json"])
        data = json.loads(sys.stdout.getvalue())
        self.assertDictEqual(
            data,
            {
                "documents": [
                    {
                        "filename": "foo.md",
                        "preamble": "",
                        "properties": {},
                        "sections": [
                            {
                                "anchor": "lo-logic-most-unintuitive-application-of-the-axiom-of-choice-mathoverflow",
                                "body": "",
                                "images": [],
                                "properties": {
                                    "url": "https://mathoverflow.net/questions/20882/most-unintuitive-application-of-the-axiom-of-choice"
                                },
                                "title": r"lo.logic - Most \'unintuitive\' application of the Axiom of Choice? - MathOverflow",
                            },
                            {
                                "anchor": "set-theory-set-theories-without-junk-theorems-mathoverflow",
                                "body": "",
                                "images": [],
                                "properties": {
                                    "url": "http://mathoverflow.net/questions/90820/set-theories-without-junk-theorems/90945#90945"
                                },
                                "title": r"set theory - Set theories without \"junk\" theorems? - MathOverflow",
                            },
                            {
                                "anchor": "the-origin-of-the-number-zero-history-smithsonian",
                                "body": "",
                                "images": [],
                                "properties": {
                                    "url": "https://www.smithsonianmag.com/history/origin-number-zero-180953392/"
                                },
                                "title": r"The Origin of the Number Zero \| History \| Smithsonian",
                            },
                            {
                                "anchor": "zajo-appler-apple-emulator-for-ms-dos-written-in-8088-assembly",
                                "body": "",
                                "images": [],
                                "properties": {"url": "https://github.com/zajo/appler"},
                                "title": r"zajo/appler: Apple \]\[ emulator for MS-DOS, written in 8088 assembly",
                            },
                            {
                                "anchor": "computational-complexitytheory",
                                "body": "That is not a link.\n",
                                "images": [],
                                "properties": {},
                                "title": r"Computational \[Complexity\]\(Theory\)",
                            },
                        ],
                        "title": "Some Resources",
                    }
                ]
            },
        )

    def test_accumulate_properties_only_from_first_list_in_section(self):
        with open("foo.md", "w") as f:
            f.write(
                """# Document

### Bubble & Squeak

*   property1: one
*   property2: two

Have you heard, [Bubble & Squeak]()?

*   property3: three
*   property4: four

### Filthy Rich & Catflap

*   property3: three

It seems that you have not heard.

*   property3: four

[Bubble & Squeak]: TK
"""
            )
        main(["foo.md", "--output-json"])
        data = json.loads(sys.stdout.getvalue())
        self.assertDictEqual(
            data,
            {
                "documents": [
                    {
                        "filename": "foo.md",
                        "preamble": "",
                        "properties": {},
                        "sections": [
                            {
                                "anchor": "bubble-squeak",
                                "body": "Have you heard, [Bubble & Squeak]()?\n\n*   property3: three\n*   property4: four\n",
                                "images": [],
                                "properties": {"property1": "one", "property2": "two"},
                                "title": "Bubble & Squeak",
                            },
                            {
                                "anchor": "filthy-rich-catflap",
                                "body": "It seems that you have not heard.\n\n*   property3: four\n",
                                "images": [],
                                "properties": {"property3": "three"},
                                "title": "Filthy Rich & Catflap",
                            },
                        ],
                        "title": "Document",
                    }
                ]
            },
        )
        os.unlink("foo.md")

    def test_accumulate_images_only_from_before_property_list(self):
        with open("foo.md", "w") as f:
            f.write(
                """# Document

### Bubble & Squeak

![first screenshot](https://example.com/bubble-and-squeak-001.png)  
![second screenshot](https://example.com/bubble-and-squeak-002.png)  

*   property1: one
*   property2: two

Have you heard, [Bubble & Squeak]()?

![third screenshot](https://example.com/bubble-and-squeak-003.png)  

[Bubble & Squeak]: TK
"""
            )
        main(["foo.md", "--output-json"])
        data = json.loads(sys.stdout.getvalue())
        self.assertEqual(
            data["documents"][0]["sections"][0]["images"],
            [
                {
                    "description": "first screenshot",
                    "source": "https://example.com/bubble-and-squeak-001.png",
                },
                {
                    "description": "second screenshot",
                    "source": "https://example.com/bubble-and-squeak-002.png",
                },
            ],
        )
        os.unlink("foo.md")

    def test_round_trip_preformatted_block(self):
        with open("foo.md", "w") as f:
            f.write(
                """\
# Document

### Bubble & Squeak

Sample:

    Have you heard?
    There are preformatted
    blocks in this section.
"""
            )
        main(["foo.md", "--output-markdown"])
        data = sys.stdout.getvalue()
        self.assertEqual(
            data,
            """\
Document
========

### Bubble & Squeak

Sample:

    Have you heard?
    There are preformatted
    blocks in this section.
""",
        )
        os.unlink("foo.md")

    def test_output_refdex(self):
        main(
            [
                "eg/Recent Llama Sightings.md",
                "eg/Ancient Llama Sightings.md",
                "--output-refdex",
            ]
        )
        data = json.loads(sys.stdout.getvalue())
        self.assertDictEqual(
            data,
            {
                "2 Llamas Spotted Near Mall": {
                    "anchor": "2-llamas-spotted-near-mall",
                    "filenames": ["eg/Recent Llama Sightings.md"],
                },
                "A Possible Llama Under the Bridge": {
                    "anchor": "a-possible-llama-under-the-bridge",
                    "filenames": ["eg/Recent Llama Sightings.md"],
                },
                "Llamas: It's Time to Spot Them": {
                    "anchor": "llamas-it-s-time-to-spot-them",
                    "filenames": ["eg/Recent Llama Sightings.md"],
                },
                "Maybe sighting the llama": {
                    "anchor": "maybe-sighting-the-llama",
                    "filenames": ["eg/Ancient Llama Sightings.md"],
                },
            },
        )

    def test_output_refdex_with_overlap(self):
        # Both of these files contain an entry called "Llamas: It's Time to Spot Them".
        # The refdex is created with entries pointing to all files where the entry occurs.
        main(
            [
                "eg/Recent Llama Sightings.md",
                "eg/Referenced Llama Sightings.md",
                "--output-refdex",
            ]
        )
        data = json.loads(sys.stdout.getvalue())
        self.assertDictEqual(
            data,
            {
                "2 Llamas Spotted Near Mall": {
                    "anchor": "2-llamas-spotted-near-mall",
                    "filenames": [
                        "eg/Recent Llama Sightings.md",
                    ],
                },
                "A Possible Llama Under the Bridge": {
                    "anchor": "a-possible-llama-under-the-bridge",
                    "filenames": [
                        "eg/Recent Llama Sightings.md",
                    ],
                },
                "Llamas: It's Time to Spot Them": {
                    "anchor": "llamas-it-s-time-to-spot-them",
                    "filenames": [
                        "eg/Recent Llama Sightings.md",
                        "eg/Referenced Llama Sightings.md",
                    ],
                },
            },
        )

    def test_output_refdex_with_overlap_forcing_single_filename(self):
        # Both of these files contain an entry called "Llamas: It's Time to Spot Them"
        # The refdex is created pointing only to the file that was mentioned last.
        main(
            [
                "eg/Recent Llama Sightings.md",
                "eg/Referenced Llama Sightings.md",
                "--output-refdex",
                "--output-refdex-single-filename",
            ]
        )
        data = json.loads(sys.stdout.getvalue())
        self.assertDictEqual(
            data,
            {
                "2 Llamas Spotted Near Mall": {
                    "anchor": "2-llamas-spotted-near-mall",
                    "filename": "eg/Recent Llama Sightings.md",
                },
                "A Possible Llama Under the Bridge": {
                    "anchor": "a-possible-llama-under-the-bridge",
                    "filename": "eg/Recent Llama Sightings.md",
                },
                "Llamas: It's Time to Spot Them": {
                    "anchor": "llamas-it-s-time-to-spot-them",
                    "filename": "eg/Referenced Llama Sightings.md",
                },
            },
        )

    def test_input_refdex_output_markdown(self):
        """When given an input refdex, the tool replaces reference links with links from the input refdex."""
        main(
            [
                "eg/Ill-formed Llama Sightings.md",
                "--input-refdex",
                "eg/refdex.json",
                "--output-markdown",
            ]
        )
        output = sys.stdout.getvalue()
        self.assertIn(
            "[2 Llamas Spotted Near Mall]: eg/Recent%20Llama%20Sightings.md#2-llamas-spotted-near-mall",
            output,
            output,
        )


class TestFeedmarkInternals(unittest.TestCase):

    def test_load_documents(self):
        doc1 = read_document_from("eg/Ancient Llama Sightings.md")
        self.assertEqual(doc1.title, "Ancient Llama Sightings")
        doc2 = read_document_from("eg/Recent Llama Sightings.md")
        self.assertEqual(doc2.title, "Recent Llama Sightings")
        self.assertEqual(len(doc2.sections), 3)

    def test_schema(self):
        schema_doc = read_document_from("eg/schema/Llama sighting.md")
        schema = Schema(schema_doc)

        doc1 = read_document_from("eg/Ancient Llama Sightings.md")
        doc2 = read_document_from("eg/Recent Llama Sightings.md")
        results = schema.check_documents([doc1, doc2])
        self.assertEqual(results, [])


if __name__ == "__main__":
    unittest.main()