|
6 | 6 |
|
7 | 7 | from mkdocs_include_markdown_plugin.directive import get_order_option_regex |
8 | 8 | from mkdocs_include_markdown_plugin.event import on_page_markdown |
| 9 | +from mkdocs_include_markdown_plugin.process import sort_paths |
9 | 10 | from testing_helpers import parametrize_directives, unix_only, windows_only |
10 | 11 |
|
11 | 12 |
|
@@ -387,6 +388,29 @@ def test_natural_order_by_extension(directive, page, tmp_path, plugin): |
387 | 388 | ) == 'file1.md\nfile2.md\nfile10.txt\n' |
388 | 389 |
|
389 | 390 |
|
| 391 | +def test_natural_order_by_extension_is_deterministic(): |
| 392 | + """Regression test: natural-extension must be deterministic when |
| 393 | + multiple files share the same extension. |
| 394 | +
|
| 395 | + The original bug: if the filesystem returned file2.md before file1.md, |
| 396 | + the stable sort kept them in that order because both had the same |
| 397 | + key (.md). The result depended on the filesystem's file ordering. |
| 398 | +
|
| 399 | + See https://github.com/mondeja/mkdocs-include-markdown-plugin/issues/289 |
| 400 | + """ |
| 401 | + # Feed paths in the "wrong" order directly to sort_paths, |
| 402 | + # bypassing any filesystem/glob ordering entirely. |
| 403 | + paths = ['file2.md', 'file10.txt', 'file1.md'] |
| 404 | + |
| 405 | + result = sort_paths(paths, order=(False, 'natural', 'extension')) |
| 406 | + assert result == ['file1.md', 'file2.md', 'file10.txt'] |
| 407 | + |
| 408 | + # Same in reverse |
| 409 | + paths = ['file2.md', 'file10.txt', 'file1.md'] |
| 410 | + result = sort_paths(paths, order=(True, 'natural', 'extension')) |
| 411 | + assert result == ['file10.txt', 'file2.md', 'file1.md'] |
| 412 | + |
| 413 | + |
390 | 414 | @parametrize_directives |
391 | 415 | @pytest.mark.parametrize('order_value', ('alpha-name', 'name')) |
392 | 416 | def test_alpha_order_by_name(directive, order_value, page, tmp_path, plugin): |
@@ -524,7 +548,7 @@ def test_natural_order_by_extension_reverse(directive, page, tmp_path, plugin): |
524 | 548 | page(tmp_path / 'includer.md'), |
525 | 549 | tmp_path, |
526 | 550 | plugin, |
527 | | - ) == 'file10.txt\nfile1.md\nfile2.md\n' |
| 551 | + ) == 'file10.txt\nfile2.md\nfile1.md\n' |
528 | 552 |
|
529 | 553 |
|
530 | 554 | @parametrize_directives |
|
0 commit comments