Skip to content

Commit 98cf8e8

Browse files
authored
Fix natural order by extension not correctly applied (#292)
1 parent 39df609 commit 98cf8e8

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ jobs:
7575
file_handler.write(env)
7676
- name: Install dependencies
7777
run: pip install -U hatch
78+
- name: Patch virtualenv on Python3.9
79+
if: matrix.py == '3.9'
80+
run: pip install --force-reinstall "virtualenv<21"
7881
- name: Run tests
7982
run: |
8083
hatch run +py=${{ steps.env.outputs.py }} tests:all

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mkdocs-include-markdown-plugin"
3-
version = "7.2.1"
3+
version = "7.2.2"
44
description = "Mkdocs Markdown includer plugin."
55
readme = "README.md"
66
license = "Apache-2.0"

src/mkdocs_include_markdown_plugin/process.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,11 @@ def key(p: str) -> str:
547547
return os.path.splitext(p)[1]
548548
elif order_type == 'natural':
549549
if order_by == 'extension':
550-
def key(p: str) -> str:
551-
return natural_sort_key(os.path.splitext(p)[1]) # type: ignore
550+
def key(p: str) -> tuple: # type: ignore
551+
return (
552+
natural_sort_key(os.path.splitext(p)[1]),
553+
natural_sort_key(os.path.basename(p)),
554+
)
552555
elif order_by == 'name':
553556
def key(p: str) -> str:
554557
return natural_sort_key(os.path.basename(p)) # type: ignore

tests/test_unit/test_order.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from mkdocs_include_markdown_plugin.directive import get_order_option_regex
88
from mkdocs_include_markdown_plugin.event import on_page_markdown
9+
from mkdocs_include_markdown_plugin.process import sort_paths
910
from testing_helpers import parametrize_directives, unix_only, windows_only
1011

1112

@@ -387,6 +388,29 @@ def test_natural_order_by_extension(directive, page, tmp_path, plugin):
387388
) == 'file1.md\nfile2.md\nfile10.txt\n'
388389

389390

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+
390414
@parametrize_directives
391415
@pytest.mark.parametrize('order_value', ('alpha-name', 'name'))
392416
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):
524548
page(tmp_path / 'includer.md'),
525549
tmp_path,
526550
plugin,
527-
) == 'file10.txt\nfile1.md\nfile2.md\n'
551+
) == 'file10.txt\nfile2.md\nfile1.md\n'
528552

529553

530554
@parametrize_directives

0 commit comments

Comments
 (0)