| 1 |
"""Test cases for Snippets.""" |
| 2 |
from .. import util |
| 3 |
import os |
| 4 |
from pymdownx.snippets import SnippetMissingError |
| 5 |
from unittest.mock import patch, MagicMock |
| 6 |
import urllib.error |
| 7 |
|
| 8 |
BASE = os.path.abspath(os.path.dirname(__file__)) |
| 9 |
|
| 10 |
|
| 11 |
class TestSnippetDedent(util.MdCase): |
| 12 |
"""Test snippet cases.""" |
| 13 |
|
| 14 |
extension = [ |
| 15 |
'pymdownx.snippets', 'pymdownx.superfences' |
| 16 |
] |
| 17 |
|
| 18 |
extension_configs = { |
| 19 |
'pymdownx.snippets': { |
| 20 |
'base_path': [os.path.join(BASE, '_snippets')], |
| 21 |
'dedent_subsections': True |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
def test_dedent_section(self): |
| 26 |
"""Test dedenting sections.""" |
| 27 |
|
| 28 |
self.check_markdown( |
| 29 |
R''' |
| 30 |
```text |
| 31 |
---8<--- "indented.txt:py-section" |
| 32 |
``` |
| 33 |
''', |
| 34 |
R''' |
| 35 |
<div class="highlight"><pre><span></span><code>def some_method(self, param): |
| 36 |
"""Docstring.""" |
| 37 |
|
| 38 |
return param |
| 39 |
</code></pre></div> |
| 40 |
''', |
| 41 |
True |
| 42 |
) |
| 43 |
|
| 44 |
def test_dedent_lines(self): |
| 45 |
"""Test dedenting lines.""" |
| 46 |
|
| 47 |
self.check_markdown( |
| 48 |
R''' |
| 49 |
```text |
| 50 |
---8<--- "indented.txt:5:8" |
| 51 |
``` |
| 52 |
''', |
| 53 |
R''' |
| 54 |
<div class="highlight"><pre><span></span><code>def some_method(self, param): |
| 55 |
"""Docstring.""" |
| 56 |
|
| 57 |
return param |
| 58 |
</code></pre></div> |
| 59 |
''', |
| 60 |
True |
| 61 |
) |
| 62 |
|
| 63 |
def test_dedent_indented(self): |
| 64 |
"""Test dedenting sections that has indented insertion.""" |
| 65 |
|
| 66 |
self.check_markdown( |
| 67 |
R''' |
| 68 |
Paragraph |
| 69 |
|
| 70 |
---8<--- "indented.txt:py-section" |
| 71 |
''', |
| 72 |
R''' |
| 73 |
<p>Paragraph</p> |
| 74 |
<pre><code>def some_method(self, param): |
| 75 |
"""Docstring.""" |
| 76 |
|
| 77 |
return param |
| 78 |
</code></pre> |
| 79 |
''', |
| 80 |
True |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
class TestSnippets(util.MdCase): |
| 85 |
"""Test snippet cases.""" |
| 86 |
|
| 87 |
extension = [ |
| 88 |
'pymdownx.snippets', 'pymdownx.superfences' |
| 89 |
] |
| 90 |
|
| 91 |
extension_configs = { |
| 92 |
'pymdownx.snippets': { |
| 93 |
'base_path': [os.path.join(BASE, '_snippets')] |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
def test_inline(self): |
| 98 |
"""Test inline.""" |
| 99 |
|
| 100 |
self.check_markdown( |
| 101 |
R''' |
| 102 |
---8<--- "loop.txt" |
| 103 |
---8<--- "a.txt" |
| 104 |
---8<--- "b.txt" |
| 105 |
;---8<--- "b.txt" |
| 106 |
|
| 107 |
- Testing indentation |
| 108 |
|
| 109 |
---8<--- "b.txt" |
| 110 |
''', |
| 111 |
R''' |
| 112 |
<p>Snippet</p> |
| 113 |
<p>Snippet</p> |
| 114 |
<p>---8<--- "b.txt"</p> |
| 115 |
<ul> |
| 116 |
<li> |
| 117 |
<p>Testing indentation</p> |
| 118 |
<p>Snippet</p> |
| 119 |
</li> |
| 120 |
</ul> |
| 121 |
''', |
| 122 |
True |
| 123 |
) |
| 124 |
|
| 125 |
def test_block(self): |
| 126 |
"""Test block.""" |
| 127 |
|
| 128 |
self.check_markdown( |
| 129 |
R''' |
| 130 |
---8<--- |
| 131 |
loop_block.txt |
| 132 |
c.txt |
| 133 |
|
| 134 |
d.txt |
| 135 |
---8<--- |
| 136 |
|
| 137 |
;---8<--- |
| 138 |
d.txt |
| 139 |
;---8<--- |
| 140 |
|
| 141 |
- Testing indentation |
| 142 |
|
| 143 |
---8<--- |
| 144 |
d.txt |
| 145 |
|
| 146 |
; d.txt |
| 147 |
# Nested inline won't work |
| 148 |
--8<-- "a.txt" |
| 149 |
--8<-- "; b.txt" |
| 150 |
---8<--- |
| 151 |
|
| 152 |
# Un-nested Inline |
| 153 |
--8<-- "a.txt" |
| 154 |
--8<-- "; b.txt" |
| 155 |
''', |
| 156 |
R''' |
| 157 |
<p>Snippet</p> |
| 158 |
<p>Snippet</p> |
| 159 |
<p>---8<--- |
| 160 |
d.txt |
| 161 |
---8<---</p> |
| 162 |
<ul> |
| 163 |
<li> |
| 164 |
<p>Testing indentation</p> |
| 165 |
<p>Snippet</p> |
| 166 |
<h1>Un-nested Inline</h1> |
| 167 |
<p>Snippet</p> |
| 168 |
</li> |
| 169 |
</ul> |
| 170 |
''', |
| 171 |
True |
| 172 |
) |
| 173 |
|
| 174 |
def test_mixed(self): |
| 175 |
"""Test mixed.""" |
| 176 |
|
| 177 |
self.check_markdown( |
| 178 |
R''' |
| 179 |
---8<--- "a.txt" |
| 180 |
|
| 181 |
---8<--- |
| 182 |
loop_block.txt |
| 183 |
c.txt |
| 184 |
|
| 185 |
d.txt |
| 186 |
---8<--- |
| 187 |
''', |
| 188 |
R''' |
| 189 |
<p>Snippet</p> |
| 190 |
<p>Snippet</p> |
| 191 |
<p>Snippet</p> |
| 192 |
''', |
| 193 |
True |
| 194 |
) |
| 195 |
|
| 196 |
def test_start_line_inline(self): |
| 197 |
"""Test starting line with inline syntax.""" |
| 198 |
|
| 199 |
self.check_markdown( |
| 200 |
R''' |
| 201 |
---8<--- "lines.txt:4" |
| 202 |
''', |
| 203 |
''' |
| 204 |
<p>Content resides on various lines. |
| 205 |
If we use line specifiers, |
| 206 |
we can select any number of lines we want.</p> |
| 207 |
<p>This is the end of the file. |
| 208 |
There is no more.</p> |
| 209 |
''', |
| 210 |
True |
| 211 |
) |
| 212 |
|
| 213 |
def test_start_1_1(self): |
| 214 |
"""Test beginning of file, single line range.""" |
| 215 |
|
| 216 |
self.check_markdown( |
| 217 |
R''' |
| 218 |
---8<--- "lines.txt:1:1" |
| 219 |
''', |
| 220 |
''' |
| 221 |
<p>This is a multi-line</p> |
| 222 |
''', |
| 223 |
True |
| 224 |
) |
| 225 |
|
| 226 |
def test_start_1_2(self): |
| 227 |
"""Test beginning of file span of multiple lines.""" |
| 228 |
|
| 229 |
self.check_markdown( |
| 230 |
R''' |
| 231 |
---8<--- "lines.txt:1:2" |
| 232 |
''', |
| 233 |
''' |
| 234 |
<p>This is a multi-line |
| 235 |
snippet.</p> |
| 236 |
''', |
| 237 |
True |
| 238 |
) |
| 239 |
|
| 240 |
def test_start_multi(self): |
| 241 |
"""Test multiple line specifiers.""" |
| 242 |
|
| 243 |
self.check_markdown( |
| 244 |
R''' |
| 245 |
---8<--- "lines.txt:1:2,8:9" |
| 246 |
''', |
| 247 |
''' |
| 248 |
<p>This is a multi-line |
| 249 |
snippet. |
| 250 |
This is the end of the file. |
| 251 |
There is no more.</p> |
| 252 |
''', |
| 253 |
True |
| 254 |
) |
| 255 |
|
| 256 |
def test_start_multi_no_end(self): |
| 257 |
"""Test multiple line specifiers but the last has no end.""" |
| 258 |
|
| 259 |
self.check_markdown( |
| 260 |
R''' |
| 261 |
---8<--- "lines.txt:1:2,8" |
| 262 |
''', |
| 263 |
''' |
| 264 |
<p>This is a multi-line |
| 265 |
snippet. |
| 266 |
This is the end of the file. |
| 267 |
There is no more.</p> |
| 268 |
''', |
| 269 |
True |
| 270 |
) |
| 271 |
|
| 272 |
def test_start_multi_no_start(self): |
| 273 |
"""Test multiple line specifiers but the last has no start.""" |
| 274 |
|
| 275 |
self.check_markdown( |
| 276 |
R''' |
| 277 |
---8<--- "lines.txt:1:2,:8" |
| 278 |
''', |
| 279 |
''' |
| 280 |
<p>This is a multi-line |
| 281 |
snippet. |
| 282 |
This is a multi-line |
| 283 |
snippet.</p> |
| 284 |
<p>Content resides on various lines. |
| 285 |
If we use line specifiers, |
| 286 |
we can select any number of lines we want.</p> |
| 287 |
<p>This is the end of the file.</p> |
| 288 |
''', |
| 289 |
True |
| 290 |
) |
| 291 |
|
| 292 |
def test_start_multi_hanging_comma(self): |
| 293 |
"""Test multiple line specifiers but there is a hanging comma.""" |
| 294 |
|
| 295 |
self.check_markdown( |
| 296 |
R''' |
| 297 |
---8<--- "lines.txt:1:2," |
| 298 |
''', |
| 299 |
''' |
| 300 |
''', |
| 301 |
True |
| 302 |
) |
| 303 |
|
| 304 |
def test_negative_range(self): |
| 305 |
"""Test negative indexing range.""" |
| 306 |
|
| 307 |
self.check_markdown( |
| 308 |
R''' |
| 309 |
---8<--- "lines.txt:-3:-2" |
| 310 |
''', |
| 311 |
''' |
| 312 |
<p>This is the end of the file. |
| 313 |
There is no more.</p> |
| 314 |
''', |
| 315 |
True |
| 316 |
) |
| 317 |
|
| 318 |
def test_negative_single(self): |
| 319 |
"""Test negative indexing single line.""" |
| 320 |
|
| 321 |
self.check_markdown( |
| 322 |
R''' |
| 323 |
---8<--- "lines.txt:-2:-2" |
| 324 |
''', |
| 325 |
''' |
| 326 |
<p>There is no more.</p> |
| 327 |
''', |
| 328 |
True |
| 329 |
) |
| 330 |
|
| 331 |
def test_mixed_negative(self): |
| 332 |
"""Test negative indexing single line.""" |
| 333 |
|
| 334 |
self.check_markdown( |
| 335 |
R''' |
| 336 |
---8<--- "lines.txt:8:-2" |
| 337 |
|
| 338 |
---8<--- "lines.txt:-3:9" |
| 339 |
''', |
| 340 |
''' |
| 341 |
<p>This is the end of the file. |
| 342 |
There is no more.</p> |
| 343 |
<p>This is the end of the file. |
| 344 |
There is no more.</p> |
| 345 |
''', |
| 346 |
True |
| 347 |
) |
| 348 |
|
| 349 |
def test_start_negative_multi(self): |
| 350 |
"""Test multiple line specifiers with negative indexes.""" |
| 351 |
|
| 352 |
self.check_markdown( |
| 353 |
R''' |
| 354 |
---8<--- "lines.txt:1:2,-3:-2" |
| 355 |
''', |
| 356 |
''' |
| 357 |
<p>This is a multi-line |
| 358 |
snippet. |
| 359 |
This is the end of the file. |
| 360 |
There is no more.</p> |
| 361 |
''', |
| 362 |
True |
| 363 |
) |
| 364 |
|
| 365 |
def test_end_line_inline(self): |
| 366 |
"""Test ending line with inline syntax.""" |
| 367 |
|
| 368 |
self.check_markdown( |
| 369 |
R''' |
| 370 |
---8<--- "lines.txt::6" |
| 371 |
''', |
| 372 |
''' |
| 373 |
<p>This is a multi-line |
| 374 |
snippet.</p> |
| 375 |
<p>Content resides on various lines. |
| 376 |
If we use line specifiers, |
| 377 |
we can select any number of lines we want.</p> |
| 378 |
''', |
| 379 |
True |
| 380 |
) |
| 381 |
|
| 382 |
def test_start_end_line_inline(self): |
| 383 |
"""Test starting and ending line with inline syntax.""" |
| 384 |
|
| 385 |
self.check_markdown( |
| 386 |
R''' |
| 387 |
---8<--- "lines.txt:4:6" |
| 388 |
''', |
| 389 |
''' |
| 390 |
<p>Content resides on various lines. |
| 391 |
If we use line specifiers, |
| 392 |
we can select any number of lines we want.</p> |
| 393 |
''', |
| 394 |
True |
| 395 |
) |
| 396 |
|
| 397 |
def test_start_line_block(self): |
| 398 |
"""Test starting line with block syntax.""" |
| 399 |
|
| 400 |
self.check_markdown( |
| 401 |
R''' |
| 402 |
--8<-- |
| 403 |
lines.txt:4 |
| 404 |
--8<-- |
| 405 |
''', |
| 406 |
''' |
| 407 |
<p>Content resides on various lines. |
| 408 |
If we use line specifiers, |
| 409 |
we can select any number of lines we want.</p> |
| 410 |
<p>This is the end of the file. |
| 411 |
There is no more.</p> |
| 412 |
''', |
| 413 |
True |
| 414 |
) |
| 415 |
|
| 416 |
def test_end_line_block(self): |
| 417 |
"""Test ending line with block syntax.""" |
| 418 |
|
| 419 |
self.check_markdown( |
| 420 |
R''' |
| 421 |
--8<-- |
| 422 |
lines.txt::6 |
| 423 |
--8<-- |
| 424 |
''', |
| 425 |
''' |
| 426 |
<p>This is a multi-line |
| 427 |
snippet.</p> |
| 428 |
<p>Content resides on various lines. |
| 429 |
If we use line specifiers, |
| 430 |
we can select any number of lines we want.</p> |
| 431 |
''', |
| 432 |
True |
| 433 |
) |
| 434 |
|
| 435 |
def test_start_end_line_block(self): |
| 436 |
"""Test starting and ending line with block syntax.""" |
| 437 |
|
| 438 |
self.check_markdown( |
| 439 |
R''' |
| 440 |
--8<-- |
| 441 |
lines.txt:4:6 |
| 442 |
--8<-- |
| 443 |
''', |
| 444 |
''' |
| 445 |
<p>Content resides on various lines. |
| 446 |
If we use line specifiers, |
| 447 |
we can select any number of lines we want.</p> |
| 448 |
''', |
| 449 |
True |
| 450 |
) |
| 451 |
|
| 452 |
def test_section_inline(self): |
| 453 |
"""Test section partial in inline snippet.""" |
| 454 |
|
| 455 |
self.check_markdown( |
| 456 |
R''' |
| 457 |
``` |
| 458 |
--8<-- "section.txt:css-section" |
| 459 |
``` |
| 460 |
''', |
| 461 |
''' |
| 462 |
<div class="highlight"><pre><span></span><code>div { |
| 463 |
color: red; |
| 464 |
} |
| 465 |
</code></pre></div> |
| 466 |
''', |
| 467 |
True |
| 468 |
) |
| 469 |
|
| 470 |
def test_section_inline_min(self): |
| 471 |
"""Test section partial in inline snippet using minimum tokens.""" |
| 472 |
|
| 473 |
self.check_markdown( |
| 474 |
R''' |
| 475 |
``` |
| 476 |
-8<- "section.txt:css-section" |
| 477 |
``` |
| 478 |
''', |
| 479 |
''' |
| 480 |
<div class="highlight"><pre><span></span><code>div { |
| 481 |
color: red; |
| 482 |
} |
| 483 |
</code></pre></div> |
| 484 |
''', |
| 485 |
True |
| 486 |
) |
| 487 |
|
| 488 |
def test_section_inline_ignore_other_section(self): |
| 489 |
"""Test nested sections.""" |
| 490 |
|
| 491 |
self.check_markdown( |
| 492 |
R''' |
| 493 |
``` |
| 494 |
-8<- "section_nested.txt:css-section" |
| 495 |
``` |
| 496 |
''', |
| 497 |
''' |
| 498 |
<div class="highlight"><pre><span></span><code>div { |
| 499 |
color: red; |
| 500 |
background-color: white; |
| 501 |
padding: 16px |
| 502 |
} |
| 503 |
</code></pre></div> |
| 504 |
''', |
| 505 |
True |
| 506 |
) |
| 507 |
|
| 508 |
def test_section_inline_escaped_other_section(self): |
| 509 |
"""Test nested escaped sections.""" |
| 510 |
|
| 511 |
self.check_markdown( |
| 512 |
R''' |
| 513 |
``` |
| 514 |
-8<- "section_nested.txt:css-section3" |
| 515 |
``` |
| 516 |
''', |
| 517 |
''' |
| 518 |
<div class="highlight"><pre><span></span><code>div { |
| 519 |
color: red; |
| 520 |
/* --8<-- [start: css-section4] */ |
| 521 |
background-color: white; |
| 522 |
padding: 16px |
| 523 |
/* --8<-- [end: css-section4] */ |
| 524 |
} |
| 525 |
</code></pre></div> |
| 526 |
''', |
| 527 |
True |
| 528 |
) |
| 529 |
|
| 530 |
def test_whole_file_with_sections(self): |
| 531 |
"""Test whole file with sections.""" |
| 532 |
|
| 533 |
self.check_markdown( |
| 534 |
R''' |
| 535 |
``` |
| 536 |
-8<- "section_nested.txt" |
| 537 |
``` |
| 538 |
''', |
| 539 |
''' |
| 540 |
<div class="highlight"><pre><span></span><code>div { |
| 541 |
color: red; |
| 542 |
background-color: white; |
| 543 |
padding: 16px |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
div { |
| 548 |
color: red; |
| 549 |
/* --8<-- [start: css-section4] */ |
| 550 |
background-color: white; |
| 551 |
padding: 16px |
| 552 |
/* --8<-- [end: css-section4] */ |
| 553 |
} |
| 554 |
|
| 555 |
div { |
| 556 |
color: red; |
| 557 |
background-color: white; |
| 558 |
padding: 16px |
| 559 |
} |
| 560 |
</code></pre></div> |
| 561 |
''', |
| 562 |
True |
| 563 |
) |
| 564 |
|
| 565 |
def test_section_ignore_double_start_section(self): |
| 566 |
"""Test nested sections.""" |
| 567 |
|
| 568 |
self.check_markdown( |
| 569 |
R''' |
| 570 |
``` |
| 571 |
-8<- "section_nested.txt:css-section5" |
| 572 |
``` |
| 573 |
''', |
| 574 |
''' |
| 575 |
<div class="highlight"><pre><span></span><code>div { |
| 576 |
color: red; |
| 577 |
background-color: white; |
| 578 |
padding: 16px |
| 579 |
} |
| 580 |
</code></pre></div> |
| 581 |
''', |
| 582 |
True |
| 583 |
) |
| 584 |
|
| 585 |
def test_section_block(self): |
| 586 |
"""Test section partial in block snippet.""" |
| 587 |
|
| 588 |
self.check_markdown( |
| 589 |
R''' |
| 590 |
--8<-- |
| 591 |
section.txt:html-section |
| 592 |
--8<-- |
| 593 |
''', |
| 594 |
''' |
| 595 |
<div><p>content</p></div> |
| 596 |
''', |
| 597 |
True |
| 598 |
) |
| 599 |
|
| 600 |
def test_section_block_min(self): |
| 601 |
"""Test section partial in block snippet using minimum tokens.""" |
| 602 |
|
| 603 |
self.check_markdown( |
| 604 |
R''' |
| 605 |
-8<- |
| 606 |
section.txt:html-section |
| 607 |
-8<- |
| 608 |
''', |
| 609 |
''' |
| 610 |
<div><p>content</p></div> |
| 611 |
''', |
| 612 |
True |
| 613 |
) |
| 614 |
|
| 615 |
def test_section_end_first(self): |
| 616 |
"""Test section when the end is specified first.""" |
| 617 |
|
| 618 |
self.check_markdown( |
| 619 |
R''' |
| 620 |
--8<-- |
| 621 |
section.txt:css-section2 |
| 622 |
--8<-- |
| 623 |
''', |
| 624 |
''' |
| 625 |
''', |
| 626 |
True |
| 627 |
) |
| 628 |
|
| 629 |
def test_section_no_end(self): |
| 630 |
"""Test section when the end is not specified.""" |
| 631 |
|
| 632 |
self.check_markdown( |
| 633 |
R''' |
| 634 |
--8<-- |
| 635 |
section.txt:html-section2 |
| 636 |
--8<-- |
| 637 |
''', |
| 638 |
''' |
| 639 |
<div><p>content</p></div> |
| 640 |
''', |
| 641 |
True |
| 642 |
) |
| 643 |
|
| 644 |
|
| 645 |
class _PathLikeExampleObject: |
| 646 |
def __fspath__(self): |
| 647 |
return os.path.join(BASE, '_snippets') |
| 648 |
|
| 649 |
|
| 650 |
class TestSnippetsPathLike(util.MdCase): |
| 651 |
"""Test snippet cases with path-like objects.""" |
| 652 |
|
| 653 |
extension = [ |
| 654 |
'pymdownx.snippets' |
| 655 |
] |
| 656 |
|
| 657 |
extension_configs = { |
| 658 |
'pymdownx.snippets': { |
| 659 |
'base_path': _PathLikeExampleObject() |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
def test_inline(self): |
| 664 |
"""Test inline.""" |
| 665 |
|
| 666 |
self.check_markdown( |
| 667 |
R''' |
| 668 |
---8<--- "a.txt" |
| 669 |
''', |
| 670 |
R''' |
| 671 |
<p>Snippet</p> |
| 672 |
''', |
| 673 |
True |
| 674 |
) |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
class TestSnippetsFile(util.MdCase): |
| 679 |
"""Test snippet file case.""" |
| 680 |
|
| 681 |
extension = [ |
| 682 |
'pymdownx.snippets', |
| 683 |
] |
| 684 |
|
| 685 |
extension_configs = { |
| 686 |
'pymdownx.snippets': { |
| 687 |
'base_path': os.path.join(BASE, '_snippets', 'b.txt') |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
def test_user(self): |
| 692 |
"""Test file.""" |
| 693 |
|
| 694 |
self.check_markdown( |
| 695 |
R''' |
| 696 |
--8<-- "b.txt" |
| 697 |
''', |
| 698 |
''' |
| 699 |
<p>Snippet</p> |
| 700 |
''', |
| 701 |
True |
| 702 |
) |
| 703 |
|
| 704 |
|
| 705 |
class TestSnippetsNested(util.MdCase): |
| 706 |
"""Test nested restriction.""" |
| 707 |
|
| 708 |
extension = [ |
| 709 |
'pymdownx.snippets', |
| 710 |
] |
| 711 |
|
| 712 |
extension_configs = { |
| 713 |
'pymdownx.snippets': { |
| 714 |
'base_path': os.path.join(BASE, '_snippets', 'nested'), |
| 715 |
'check_paths': True |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
def test_restricted(self): |
| 720 |
"""Test file restriction.""" |
| 721 |
|
| 722 |
with self.assertRaises(SnippetMissingError): |
| 723 |
self.check_markdown( |
| 724 |
R''' |
| 725 |
--8<-- "../b.txt" |
| 726 |
''', |
| 727 |
''' |
| 728 |
<p>Snippet</p> |
| 729 |
''', |
| 730 |
True |
| 731 |
) |
| 732 |
|
| 733 |
def test_restricted_directory_traversal(self): |
| 734 |
"""Test file restriction directory traversal.""" |
| 735 |
|
| 736 |
with self.assertRaises(SnippetMissingError): |
| 737 |
self.check_markdown( |
| 738 |
R''' |
| 739 |
--8<-- "../nested_b.txt" |
| 740 |
''', |
| 741 |
''' |
| 742 |
<p>Snippet</p> |
| 743 |
''', |
| 744 |
True |
| 745 |
) |
| 746 |
|
| 747 |
|
| 748 |
class TestSnippetsMultiNested(util.MdCase): |
| 749 |
"""Test multi-nested restriction.""" |
| 750 |
|
| 751 |
extension = [ |
| 752 |
'pymdownx.snippets', |
| 753 |
] |
| 754 |
|
| 755 |
extension_configs = { |
| 756 |
'pymdownx.snippets': { |
| 757 |
'base_path': os.path.join(BASE), |
| 758 |
'check_paths': True |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
def test_restricted_multi_nested(self): |
| 763 |
"""Test file restriction.""" |
| 764 |
|
| 765 |
self.check_markdown( |
| 766 |
R''' |
| 767 |
--8<-- "_snippets/b.txt" |
| 768 |
''', |
| 769 |
''' |
| 770 |
<p>Snippet</p> |
| 771 |
''', |
| 772 |
True |
| 773 |
) |
| 774 |
|
| 775 |
|
| 776 |
class TestSnippetsNestedUnrestricted(util.MdCase): |
| 777 |
"""Test nested no bounds.""" |
| 778 |
|
| 779 |
extension = [ |
| 780 |
'pymdownx.snippets', |
| 781 |
] |
| 782 |
|
| 783 |
extension_configs = { |
| 784 |
'pymdownx.snippets': { |
| 785 |
'base_path': os.path.join(BASE, '_snippets', 'nested'), |
| 786 |
'restrict_base_path': False |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
def test_restricted(self): |
| 791 |
"""Test file restriction.""" |
| 792 |
|
| 793 |
self.check_markdown( |
| 794 |
R''' |
| 795 |
--8<-- "../b.txt" |
| 796 |
''', |
| 797 |
''' |
| 798 |
<p>Snippet</p> |
| 799 |
''', |
| 800 |
True |
| 801 |
) |
| 802 |
|
| 803 |
def test_restricted_directory_traversal(self): |
| 804 |
"""Test file restriction directory traversal.""" |
| 805 |
|
| 806 |
self.check_markdown( |
| 807 |
R''' |
| 808 |
--8<-- "../nested_b.txt" |
| 809 |
''', |
| 810 |
''' |
| 811 |
<p>Snippet</p> |
| 812 |
''', |
| 813 |
True |
| 814 |
) |
| 815 |
|
| 816 |
|
| 817 |
class TestSnippetsAutoAppend(util.MdCase): |
| 818 |
"""Test snippet file case.""" |
| 819 |
|
| 820 |
extension = [ |
| 821 |
'pymdownx.snippets', |
| 822 |
] |
| 823 |
|
| 824 |
extension_configs = { |
| 825 |
'pymdownx.snippets': { |
| 826 |
'base_path': [os.path.join(BASE, '_snippets')], |
| 827 |
'auto_append': ['b.txt'] |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
def test_auto_append(self): |
| 832 |
"""Test auto append.""" |
| 833 |
|
| 834 |
self.check_markdown( |
| 835 |
R''' |
| 836 |
Test |
| 837 |
''', |
| 838 |
''' |
| 839 |
<p>Test</p> |
| 840 |
<p>Snippet</p> |
| 841 |
''', |
| 842 |
True |
| 843 |
) |
| 844 |
|
| 845 |
|
| 846 |
class TestSnippetsMissing(util.MdCase): |
| 847 |
"""Test snippet file case.""" |
| 848 |
|
| 849 |
extension = [ |
| 850 |
'pymdownx.snippets', |
| 851 |
] |
| 852 |
|
| 853 |
extension_configs = { |
| 854 |
'pymdownx.snippets': { |
| 855 |
'base_path': [os.path.join(BASE, '_snippets')], |
| 856 |
'check_paths': True |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
def test_good(self): |
| 861 |
"""Test found file.""" |
| 862 |
|
| 863 |
self.check_markdown( |
| 864 |
''' |
| 865 |
--8<--- "d.txt" |
| 866 |
''', |
| 867 |
''' |
| 868 |
<p>Snippet</p> |
| 869 |
''', |
| 870 |
True |
| 871 |
) |
| 872 |
|
| 873 |
def test_top_level(self): |
| 874 |
"""Test top level.""" |
| 875 |
|
| 876 |
with self.assertRaises(SnippetMissingError): |
| 877 |
self.check_markdown( |
| 878 |
R''' |
| 879 |
--8<-- "not-here.txt" |
| 880 |
''', |
| 881 |
''' |
| 882 |
''', |
| 883 |
True |
| 884 |
) |
| 885 |
|
| 886 |
def test_nested(self): |
| 887 |
"""Test nested.""" |
| 888 |
|
| 889 |
with self.assertRaises(SnippetMissingError): |
| 890 |
self.check_markdown( |
| 891 |
R''' |
| 892 |
--8<-- "missing.txt" |
| 893 |
''', |
| 894 |
''' |
| 895 |
''', |
| 896 |
True |
| 897 |
) |
| 898 |
|
| 899 |
def test_missing_file_lines(self): |
| 900 |
"""Test missing file with line numbers.""" |
| 901 |
|
| 902 |
with self.assertRaises(SnippetMissingError): |
| 903 |
self.check_markdown( |
| 904 |
R''' |
| 905 |
--8<-- ":3:4" |
| 906 |
''', |
| 907 |
''' |
| 908 |
''', |
| 909 |
True |
| 910 |
) |
| 911 |
|
| 912 |
def test_missing_section(self): |
| 913 |
"""Test missing section.""" |
| 914 |
|
| 915 |
with self.assertRaises(SnippetMissingError): |
| 916 |
self.check_markdown( |
| 917 |
R''' |
| 918 |
--8<-- "section.txt:missing-section" |
| 919 |
''', |
| 920 |
''' |
| 921 |
''', |
| 922 |
True |
| 923 |
) |
| 924 |
|
| 925 |
|
| 926 |
class TestSnippetsGracefulMissing(util.MdCase): |
| 927 |
"""Test snippet file case.""" |
| 928 |
|
| 929 |
extension = [ |
| 930 |
'pymdownx.snippets', |
| 931 |
] |
| 932 |
|
| 933 |
extension_configs = { |
| 934 |
'pymdownx.snippets': { |
| 935 |
'base_path': [os.path.join(BASE, '_snippets')] |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
def test_top_level(self): |
| 940 |
"""Test top level.""" |
| 941 |
|
| 942 |
self.check_markdown( |
| 943 |
R''' |
| 944 |
--8<-- "not-here.txt" |
| 945 |
''', |
| 946 |
''' |
| 947 |
''', |
| 948 |
True |
| 949 |
) |
| 950 |
|
| 951 |
def test_nested(self): |
| 952 |
"""Test nested.""" |
| 953 |
|
| 954 |
self.check_markdown( |
| 955 |
R''' |
| 956 |
--8<-- "missing.txt" |
| 957 |
''', |
| 958 |
''' |
| 959 |
''', |
| 960 |
True |
| 961 |
) |
| 962 |
|
| 963 |
def test_missing_lines(self): |
| 964 |
"""Test missing file with lines.""" |
| 965 |
|
| 966 |
self.check_markdown( |
| 967 |
R''' |
| 968 |
--8<-- ":3:4" |
| 969 |
''', |
| 970 |
''' |
| 971 |
''', |
| 972 |
True |
| 973 |
) |
| 974 |
|
| 975 |
|
| 976 |
class TestURLSnippets(util.MdCase): |
| 977 |
"""Test snippet URL cases.""" |
| 978 |
|
| 979 |
extension = [ |
| 980 |
'pymdownx.snippets', |
| 981 |
] |
| 982 |
|
| 983 |
extension_configs = { |
| 984 |
'pymdownx.snippets': { |
| 985 |
'base_path': [os.path.join(BASE, '_snippets')], |
| 986 |
'url_download': True |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
@patch('urllib.request.urlopen') |
| 991 |
def test_url(self, mock_urlopen): |
| 992 |
"""Test URL.""" |
| 993 |
|
| 994 |
cm = MagicMock() |
| 995 |
cm.status = 200 |
| 996 |
cm.code = 200 |
| 997 |
cm.read.return_value = b'contents' |
| 998 |
cm.headers = {'content-length': '8'} |
| 999 |
cm.__enter__.return_value = cm |
| 1000 |
mock_urlopen.return_value = cm |
| 1001 |
|
| 1002 |
self.check_markdown( |
| 1003 |
R''' |
| 1004 |
--8<-- "https://test.com/myfile.md" |
| 1005 |
''', |
| 1006 |
''' |
| 1007 |
<p>contents</p> |
| 1008 |
''', |
| 1009 |
True |
| 1010 |
) |
| 1011 |
|
| 1012 |
@patch('urllib.request.urlopen') |
| 1013 |
def test_url_nested(self, mock_urlopen): |
| 1014 |
"""Test nested URLs.""" |
| 1015 |
|
| 1016 |
cm = MagicMock() |
| 1017 |
cm.status = 200 |
| 1018 |
cm.code = 200 |
| 1019 |
cm.read.side_effect = [b'content\n\n--8<-- "https://test.com/myfile2.md"', b'other'] |
| 1020 |
cm.headers = {'content-length': '8'} |
| 1021 |
cm.__enter__.return_value = cm |
| 1022 |
mock_urlopen.return_value = cm |
| 1023 |
|
| 1024 |
self.check_markdown( |
| 1025 |
R''' |
| 1026 |
--8<-- "https://test.com/myfile.md" |
| 1027 |
''', |
| 1028 |
''' |
| 1029 |
<p>content</p> |
| 1030 |
<p>other</p> |
| 1031 |
''', |
| 1032 |
True |
| 1033 |
) |
| 1034 |
|
| 1035 |
@patch('urllib.request.urlopen') |
| 1036 |
def test_url_nested_duplicatqe(self, mock_urlopen): |
| 1037 |
"""Test nested duplicate file.""" |
| 1038 |
|
| 1039 |
cm = MagicMock() |
| 1040 |
cm.status = 200 |
| 1041 |
cm.code = 200 |
| 1042 |
cm.read.side_effect = [b'content\n\n--8<-- "https://test.com/myfile.md"', b'other'] |
| 1043 |
cm.headers = {'content-length': '8'} |
| 1044 |
cm.__enter__.return_value = cm |
| 1045 |
mock_urlopen.return_value = cm |
| 1046 |
|
| 1047 |
self.check_markdown( |
| 1048 |
R''' |
| 1049 |
--8<-- "https://test.com/myfile.md" |
| 1050 |
''', |
| 1051 |
''' |
| 1052 |
<p>content</p> |
| 1053 |
''', |
| 1054 |
True |
| 1055 |
) |
| 1056 |
|
| 1057 |
@patch('urllib.request.urlopen') |
| 1058 |
def test_url_nested_file(self, mock_urlopen): |
| 1059 |
"""Test nested file in URL.""" |
| 1060 |
|
| 1061 |
cm = MagicMock() |
| 1062 |
cm.status = 200 |
| 1063 |
cm.code = 200 |
| 1064 |
cm.read.return_value = b'content\n\n--8<-- "b.txt"' |
| 1065 |
cm.headers = {'content-length': '8'} |
| 1066 |
cm.__enter__.return_value = cm |
| 1067 |
mock_urlopen.return_value = cm |
| 1068 |
|
| 1069 |
self.check_markdown( |
| 1070 |
R''' |
| 1071 |
--8<-- "https://test.com/myfile.md" |
| 1072 |
''', |
| 1073 |
''' |
| 1074 |
<p>content</p> |
| 1075 |
''', |
| 1076 |
True |
| 1077 |
) |
| 1078 |
|
| 1079 |
@patch('urllib.request.urlopen') |
| 1080 |
def test_url_lines(self, mock_urlopen): |
| 1081 |
"""Test specifying specific lines in a URL.""" |
| 1082 |
|
| 1083 |
with open('tests/test_extensions/_snippets/lines.txt', 'rb') as f: |
| 1084 |
content = f.read() |
| 1085 |
length = len(content) |
| 1086 |
|
| 1087 |
cm = MagicMock() |
| 1088 |
cm.status = 200 |
| 1089 |
cm.code = 200 |
| 1090 |
cm.read.return_value = content |
| 1091 |
cm.headers = {'content-length': length} |
| 1092 |
cm.__enter__.return_value = cm |
| 1093 |
mock_urlopen.return_value = cm |
| 1094 |
|
| 1095 |
self.check_markdown( |
| 1096 |
R''' |
| 1097 |
--8<-- "https://test.com/myfile.md:4:6" |
| 1098 |
''', |
| 1099 |
''' |
| 1100 |
<p>Content resides on various lines. |
| 1101 |
If we use line specifiers, |
| 1102 |
we can select any number of lines we want.</p> |
| 1103 |
''', |
| 1104 |
True |
| 1105 |
) |
| 1106 |
|
| 1107 |
@patch('urllib.request.urlopen') |
| 1108 |
def test_missing(self, mock_urlopen): |
| 1109 |
"""Test missing URL.""" |
| 1110 |
|
| 1111 |
cm = MagicMock() |
| 1112 |
cm.status = 404 |
| 1113 |
cm.code = 404 |
| 1114 |
cm.read.return_value = b'' |
| 1115 |
cm.headers = {'content-length': '0'} |
| 1116 |
cm.__enter__.return_value = cm |
| 1117 |
mock_urlopen.return_value = cm |
| 1118 |
|
| 1119 |
self.check_markdown( |
| 1120 |
R''' |
| 1121 |
--8<-- "https://test.com/myfile.md" |
| 1122 |
''', |
| 1123 |
'', |
| 1124 |
True |
| 1125 |
) |
| 1126 |
|
| 1127 |
@patch('urllib.request.urlopen') |
| 1128 |
def test_missing_content_length(self, mock_urlopen): |
| 1129 |
"""Test missing content length header.""" |
| 1130 |
|
| 1131 |
cm = MagicMock() |
| 1132 |
cm.status = 200 |
| 1133 |
cm.code = 200 |
| 1134 |
cm.read.return_value = b'' |
| 1135 |
cm.headers = {} |
| 1136 |
cm.__enter__.return_value = cm |
| 1137 |
mock_urlopen.return_value = cm |
| 1138 |
|
| 1139 |
self.check_markdown( |
| 1140 |
R''' |
| 1141 |
--8<-- "https://test.com/myfile.md" |
| 1142 |
''', |
| 1143 |
''' |
| 1144 |
''', |
| 1145 |
True |
| 1146 |
) |
| 1147 |
|
| 1148 |
@patch('urllib.request.urlopen') |
| 1149 |
def test_missing_content_length_too_big(self, mock_urlopen): |
| 1150 |
"""Test content length too big.""" |
| 1151 |
|
| 1152 |
cm = MagicMock() |
| 1153 |
cm.status = 200 |
| 1154 |
cm.code = 200 |
| 1155 |
cm.read.return_value = b'' |
| 1156 |
cm.headers = {'content-length': str(1024 * 1024 * 48)} |
| 1157 |
cm.__enter__.return_value = cm |
| 1158 |
mock_urlopen.return_value = cm |
| 1159 |
|
| 1160 |
with self.assertRaises(ValueError): |
| 1161 |
self.check_markdown( |
| 1162 |
R''' |
| 1163 |
--8<-- "https://test.com/myfile.md" |
| 1164 |
''', |
| 1165 |
''' |
| 1166 |
''', |
| 1167 |
True |
| 1168 |
) |
| 1169 |
|
| 1170 |
@patch('urllib.request.urlopen') |
| 1171 |
def test_content_too_big_no_content_length(self, mock_urlopen): |
| 1172 |
"""Test content length too big.""" |
| 1173 |
|
| 1174 |
cm = MagicMock() |
| 1175 |
cm.status = 200 |
| 1176 |
cm.code = 200 |
| 1177 |
cm.read = lambda count: b"-" * (1024 * 1024 * 48) |
| 1178 |
cm.__enter__.return_value = cm |
| 1179 |
mock_urlopen.return_value = cm |
| 1180 |
|
| 1181 |
with self.assertRaises(ValueError): |
| 1182 |
self.check_markdown( |
| 1183 |
R''' |
| 1184 |
--8<-- "https://test.com/myfile.md" |
| 1185 |
''', |
| 1186 |
''' |
| 1187 |
''', |
| 1188 |
True |
| 1189 |
) |
| 1190 |
@patch('urllib.request.urlopen') |
| 1191 |
def test_content_length_zero(self, mock_urlopen): |
| 1192 |
"""Test empty content.""" |
| 1193 |
|
| 1194 |
cm = MagicMock() |
| 1195 |
cm.status = 200 |
| 1196 |
cm.code = 200 |
| 1197 |
cm.read.return_value = b'' |
| 1198 |
cm.headers = {'content-length': '0'} |
| 1199 |
cm.__enter__.return_value = cm |
| 1200 |
mock_urlopen.return_value = cm |
| 1201 |
|
| 1202 |
self.check_markdown( |
| 1203 |
R''' |
| 1204 |
--8<-- "https://test.com/myfile.md" |
| 1205 |
''', |
| 1206 |
'', |
| 1207 |
True |
| 1208 |
) |
| 1209 |
|
| 1210 |
@patch('urllib.request.urlopen') |
| 1211 |
def test_url_sections(self, mock_urlopen): |
| 1212 |
"""Test specifying a section in a URL.""" |
| 1213 |
|
| 1214 |
with open('tests/test_extensions/_snippets/section.txt', 'rb') as f: |
| 1215 |
content = f.read() |
| 1216 |
length = len(content) |
| 1217 |
|
| 1218 |
cm = MagicMock() |
| 1219 |
cm.status = 200 |
| 1220 |
cm.code = 200 |
| 1221 |
cm.read.return_value = content |
| 1222 |
cm.headers = {'content-length': length} |
| 1223 |
cm.__enter__.return_value = cm |
| 1224 |
mock_urlopen.return_value = cm |
| 1225 |
|
| 1226 |
self.check_markdown( |
| 1227 |
R''' |
| 1228 |
--8<-- "https://test.com/myfile.md:html-section" |
| 1229 |
''', |
| 1230 |
''' |
| 1231 |
<div><p>content</p></div> |
| 1232 |
''', |
| 1233 |
True |
| 1234 |
) |
| 1235 |
|
| 1236 |
|
| 1237 |
class TestURLDedentSnippets(util.MdCase): |
| 1238 |
"""Test snippet URL cases.""" |
| 1239 |
|
| 1240 |
extension = [ |
| 1241 |
'pymdownx.snippets', 'pymdownx.superfences' |
| 1242 |
] |
| 1243 |
|
| 1244 |
extension_configs = { |
| 1245 |
'pymdownx.snippets': { |
| 1246 |
'base_path': [os.path.join(BASE, '_snippets')], |
| 1247 |
'url_download': True, |
| 1248 |
'dedent_subsections': True |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
@patch('urllib.request.urlopen') |
| 1253 |
def test_url_sections(self, mock_urlopen): |
| 1254 |
"""Test specifying a section in a URL.""" |
| 1255 |
|
| 1256 |
with open('tests/test_extensions/_snippets/indented.txt', 'rb') as f: |
| 1257 |
content = f.read() |
| 1258 |
length = len(content) |
| 1259 |
|
| 1260 |
cm = MagicMock() |
| 1261 |
cm.status = 200 |
| 1262 |
cm.code = 200 |
| 1263 |
cm.read.return_value = content |
| 1264 |
cm.headers = {'content-length': length} |
| 1265 |
cm.__enter__.return_value = cm |
| 1266 |
mock_urlopen.return_value = cm |
| 1267 |
|
| 1268 |
self.check_markdown( |
| 1269 |
R''' |
| 1270 |
``` |
| 1271 |
--8<-- "https://test.com/myfile.md:py-section" |
| 1272 |
``` |
| 1273 |
''', |
| 1274 |
''' |
| 1275 |
<div class="highlight"><pre><span></span><code>def some_method(self, param): |
| 1276 |
"""Docstring.""" |
| 1277 |
|
| 1278 |
return param |
| 1279 |
</code></pre></div> |
| 1280 |
''', |
| 1281 |
True |
| 1282 |
) |
| 1283 |
|
| 1284 |
|
| 1285 |
class TestURLSnippetsNoMax(util.MdCase): |
| 1286 |
"""Test snippet URL cases no max size.""" |
| 1287 |
|
| 1288 |
extension = [ |
| 1289 |
'pymdownx.snippets', |
| 1290 |
] |
| 1291 |
|
| 1292 |
extension_configs = { |
| 1293 |
'pymdownx.snippets': { |
| 1294 |
'base_path': [os.path.join(BASE, '_snippets')], |
| 1295 |
'url_download': True, |
| 1296 |
'url_max_size': 0 |
| 1297 |
} |
| 1298 |
} |
| 1299 |
|
| 1300 |
@patch('urllib.request.urlopen') |
| 1301 |
def test_content_length_zero(self, mock_urlopen): |
| 1302 |
"""Test empty content.""" |
| 1303 |
|
| 1304 |
cm = MagicMock() |
| 1305 |
cm.status = 200 |
| 1306 |
cm.code = 200 |
| 1307 |
cm.read.return_value = b'contents' |
| 1308 |
cm.headers = {'content-length': str(1024 * 1024 * 48)} |
| 1309 |
cm.__enter__.return_value = cm |
| 1310 |
mock_urlopen.return_value = cm |
| 1311 |
|
| 1312 |
self.check_markdown( |
| 1313 |
R''' |
| 1314 |
--8<-- "https://test.com/myfile.md" |
| 1315 |
''', |
| 1316 |
''' |
| 1317 |
<p>contents</p> |
| 1318 |
''', |
| 1319 |
True |
| 1320 |
) |
| 1321 |
|
| 1322 |
|
| 1323 |
class TestURLSnippetsMissing(util.MdCase): |
| 1324 |
"""Test snippet URL cases with missing URL and 'check paths'.""" |
| 1325 |
|
| 1326 |
extension = [ |
| 1327 |
'pymdownx.snippets', |
| 1328 |
] |
| 1329 |
|
| 1330 |
extension_configs = { |
| 1331 |
'pymdownx.snippets': { |
| 1332 |
'base_path': [os.path.join(BASE, '_snippets')], |
| 1333 |
'url_download': True, |
| 1334 |
'url_max_size': 0, |
| 1335 |
'check_paths': True |
| 1336 |
} |
| 1337 |
} |
| 1338 |
|
| 1339 |
@patch('urllib.request.urlopen') |
| 1340 |
def test_missing(self, mock_urlopen): |
| 1341 |
"""Test missing URL.""" |
| 1342 |
|
| 1343 |
cm = MagicMock() |
| 1344 |
cm.status = 404 |
| 1345 |
cm.code = 404 |
| 1346 |
cm.read.return_value = b'' |
| 1347 |
cm.headers = {'content-length': '0'} |
| 1348 |
cm.__enter__.return_value = cm |
| 1349 |
mock_urlopen.return_value = cm |
| 1350 |
|
| 1351 |
with self.assertRaises(SnippetMissingError): |
| 1352 |
self.check_markdown( |
| 1353 |
R''' |
| 1354 |
--8<-- "https://test.com/myfile.md" |
| 1355 |
''', |
| 1356 |
'', |
| 1357 |
True |
| 1358 |
) |
| 1359 |
|
| 1360 |
|
| 1361 |
class TestURLSnippetsRetry(util.MdCase): |
| 1362 |
"""Test snippet URL retry cases.""" |
| 1363 |
|
| 1364 |
extension = [ |
| 1365 |
'pymdownx.snippets', |
| 1366 |
] |
| 1367 |
extension_configs = { |
| 1368 |
'pymdownx.snippets': { |
| 1369 |
'base_path': [os.path.join(BASE, '_snippets')], |
| 1370 |
'url_download': True, |
| 1371 |
'check_paths': True |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
@patch('urllib.request.urlopen') |
| 1376 |
@patch('time.sleep') |
| 1377 |
def test_successful_retry(self, mock_sleep, mock_urlopen): |
| 1378 |
"""Test successful retry after 429.""" |
| 1379 |
|
| 1380 |
cm = MagicMock() |
| 1381 |
cm.status = 200 |
| 1382 |
cm.code = 200 |
| 1383 |
cm.read.return_value = b'content\n' |
| 1384 |
cm.headers = {'content-length': '8'} |
| 1385 |
cm.__enter__.return_value = cm |
| 1386 |
cm.__exit__.return_value = None # Mock __exit__ |
| 1387 |
|
| 1388 |
mock_urlopen.side_effect = [ |
| 1389 |
urllib.error.HTTPError('http://example.com', 429, 'Too Many Requests', {}, None), |
| 1390 |
urllib.error.HTTPError('http://example.com', 429, 'Too Many Requests', {}, None), |
| 1391 |
cm |
| 1392 |
] |
| 1393 |
|
| 1394 |
self.check_markdown( |
| 1395 |
R''' |
| 1396 |
--8<-- "http://example.com" |
| 1397 |
''', |
| 1398 |
''' |
| 1399 |
<p>content</p> |
| 1400 |
''', |
| 1401 |
True |
| 1402 |
) |
| 1403 |
|
| 1404 |
mock_sleep.assert_any_call(2) |
| 1405 |
mock_sleep.assert_any_call(4) |
| 1406 |
|
| 1407 |
@patch('urllib.request.urlopen') |
| 1408 |
@patch('time.sleep') |
| 1409 |
def test_exhaust_retries(self, mock_sleep, mock_urlopen): |
| 1410 |
"""Test exhausting all retries.""" |
| 1411 |
|
| 1412 |
mock_urlopen.side_effect = [ |
| 1413 |
urllib.error.HTTPError('http://example.com', 429, 'Too Many Requests', {}, None), |
| 1414 |
urllib.error.HTTPError('http://example.com', 429, 'Too Many Requests', {}, None), |
| 1415 |
urllib.error.HTTPError('http://example.com', 429, 'Too Many Requests', {}, None), |
| 1416 |
urllib.error.HTTPError('http://example.com', 429, 'Too Many Requests', {}, None), |
| 1417 |
] |
| 1418 |
|
| 1419 |
with self.assertRaises(SnippetMissingError) as cm: |
| 1420 |
self.md.convert( |
| 1421 |
R''' |
| 1422 |
--8<-- "http://example.com" |
| 1423 |
''' |
| 1424 |
) |
| 1425 |
|
| 1426 |
self.assertIn("Cannot download snippet 'http://example.com' (HTTP Error 429)", str(cm.exception)) |
| 1427 |
|
| 1428 |
mock_sleep.assert_any_call(2) |
| 1429 |
mock_sleep.assert_any_call(4) |
| 1430 |
mock_sleep.assert_any_call(6) |
| 1431 |
|
| 1432 |
|
| 1433 |
class TestURLSnippetsRetryCustomBackoff(util.MdCase): |
| 1434 |
"""Test snippet URL retry cases with custom backoff.""" |
| 1435 |
|
| 1436 |
extension = [ |
| 1437 |
'pymdownx.snippets', |
| 1438 |
] |
| 1439 |
extension_configs = { |
| 1440 |
'pymdownx.snippets': { |
| 1441 |
'base_path': [os.path.join(BASE, '_snippets')], |
| 1442 |
'url_download': True, |
| 1443 |
'backoff_factor': 1.5, |
| 1444 |
'check_paths': True |
| 1445 |
} |
| 1446 |
} |
| 1447 |
|
| 1448 |
@patch('urllib.request.urlopen') |
| 1449 |
@patch('time.sleep') |
| 1450 |
def test_custom_backoff(self, mock_sleep, mock_urlopen): |
| 1451 |
"""Test custom backoff factor.""" |
| 1452 |
|
| 1453 |
cm = MagicMock() |
| 1454 |
cm.status = 200 |
| 1455 |
cm.code = 200 |
| 1456 |
cm.read.return_value = b'content\n' |
| 1457 |
cm.headers = {'content-length': '8'} |
| 1458 |
cm.__enter__.return_value = cm |
| 1459 |
cm.__exit__.return_value = None # Mock __exit__ |
| 1460 |
|
| 1461 |
mock_urlopen.side_effect = [ |
| 1462 |
urllib.error.HTTPError('http://example.com', 429, 'Too Many Requests', {}, None), |
| 1463 |
cm |
| 1464 |
] |
| 1465 |
|
| 1466 |
self.check_markdown( |
| 1467 |
R''' |
| 1468 |
--8<-- "http://example.com" |
| 1469 |
''', |
| 1470 |
''' |
| 1471 |
<p>content</p> |
| 1472 |
''', |
| 1473 |
True |
| 1474 |
) |
| 1475 |
|
| 1476 |
mock_sleep.assert_called_once_with(1.5) |
| 1477 |
|
| 1478 |
|
| 1479 |
class TestURLSnippetsRetryCustomRetries(util.MdCase): |
| 1480 |
"""Test snippet URL retry cases with custom retries.""" |
| 1481 |
|
| 1482 |
extension = [ |
| 1483 |
'pymdownx.snippets', |
| 1484 |
] |
| 1485 |
extension_configs = { |
| 1486 |
'pymdownx.snippets': { |
| 1487 |
'base_path': [os.path.join(BASE, '_snippets')], |
| 1488 |
'url_download': True, |
| 1489 |
'max_retries': 2, |
| 1490 |
'check_paths': True |
| 1491 |
} |
| 1492 |
} |
| 1493 |
|
| 1494 |
@patch('urllib.request.urlopen') |
| 1495 |
@patch('time.sleep') |
| 1496 |
def test_custom_max_retries(self, mock_sleep, mock_urlopen): |
| 1497 |
"""Test custom max retries.""" |
| 1498 |
|
| 1499 |
mock_urlopen.side_effect = [ |
| 1500 |
urllib.error.HTTPError('http://example.com', 429, 'Too Many Requests', {}, None), |
| 1501 |
urllib.error.HTTPError('http://example.com', 429, 'Too Many Requests', {}, None), |
| 1502 |
urllib.error.HTTPError('http://example.com', 429, 'Too Many Requests', {}, None), |
| 1503 |
] |
| 1504 |
|
| 1505 |
with self.assertRaises(SnippetMissingError) as cm: |
| 1506 |
self.md.convert( |
| 1507 |
R''' |
| 1508 |
--8<-- "http://example.com" |
| 1509 |
''' |
| 1510 |
) |
| 1511 |
self.assertIn("Cannot download snippet 'http://example.com' (HTTP Error 429)", str(cm.exception)) |
| 1512 |
|
| 1513 |
mock_sleep.assert_any_call(2) |
| 1514 |
mock_sleep.assert_any_call(4) |