Skip to content

Commit

Permalink
Fix and test file_sort_key
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed May 18, 2021
1 parent 078dafc commit 71a4825
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 5 additions & 3 deletions mkdocs_gen_files/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ def _normpath(*path: str):
return os.path.normpath(os.path.join(*path)).replace(os.sep, "/")


def _file_sort_key(f: File):
def file_sort_key(f: File):
parts = pathlib.PurePath(f.src_path).parts
return tuple(chr(i != len(parts) - 1) + chr(f.name != "index") + p for i, p in enumerate(parts))
return tuple(
chr(f.name != "index" if i == len(parts) - 1 else 2) + p for i, p in enumerate(parts)
)


class FilesEditor:
Expand Down Expand Up @@ -111,5 +113,5 @@ def files(self) -> Files:
[Files]: https://github.com/mkdocs/mkdocs/blob/master/mkdocs/structure/files.py
"""
files = sorted(self._files.values(), key=_file_sort_key)
files = sorted(self._files.values(), key=file_sort_key)
return Files(files)
20 changes: 20 additions & 0 deletions tests/test_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest
from mkdocs.structure.files import File

from mkdocs_gen_files import editor


@pytest.mark.parametrize(
"names",
[
["a/b.md", "b/index.md", "b/a.md"],
["SUMMARY.md", "foo/a.md", "foo/bar/index.md", "foo/bar/SUMMARY.md"],
],
)
@pytest.mark.parametrize("use_directory_urls", [False, True])
def test_file_sort_key(use_directory_urls, names):
files = [
File(name, src_dir="", dest_dir="", use_directory_urls=use_directory_urls) for name in names
]
sorted_names = [f.src_path for f in sorted(files, key=editor.file_sort_key)]
assert sorted_names == names

0 comments on commit 71a4825

Please sign in to comment.