Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error getting content from cache #171

Merged
merged 9 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions examples/http-cache/docs/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Header

You need to install `platformdirs` to run this example.
:warning: You need to install `platformdirs` to run this example.

{%
include-markdown "https://raw.githubusercontent.com/mondeja/mkdocs-include-markdown-plugin/master/examples/basic/docs/included.md"
%}

## mkdocs.yml

```
{%
include-markdown "https://raw.githubusercontent.com/mondeja/mkdocs-include-markdown-plugin/master/examples/basic/mkdocs.yml"
include "https://raw.githubusercontent.com/mondeja/mkdocs-include-markdown-plugin/master/examples/basic/mkdocs.yml"
%}
```

## From cache

{%
include-markdown "https://raw.githubusercontent.com/mondeja/mkdocs-include-markdown-plugin/master/examples/basic/docs/included.md"
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ style = [

[tool.coverage.run]
source = ["src"]
omit = ["src/mkdocs_include_markdown_plugin/plugin.py"]
plugins = ["covdefaults"]
parallel = true
data_file = ".coverage/.coverage"
Expand Down
2 changes: 1 addition & 1 deletion src/mkdocs_include_markdown_plugin/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_(self, url: str) -> str | None: # noqa: D102
fpath = os.path.join(self.cache_dir, key)
if os.path.isfile(fpath):
creation_time = self.get_creation_time_from_fpath(fpath)
if time.time() > creation_time + self.expiration_seconds:
if time.time() < creation_time + self.expiration_seconds:
return self.read_file(fpath)
else:
os.remove(fpath)
Expand Down
16 changes: 8 additions & 8 deletions src/mkdocs_include_markdown_plugin/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class DirectiveBoolArgument(TypedDict): # noqa: D101
}


def bool_arg(arg: str) -> re.Pattern[str]:
def arg(arg: str) -> re.Pattern[str]:
"""Return a compiled regexp to match a boolean argument."""
return re.compile(rf'{arg}=(\w+)')
return re.compile(rf'{arg}=([-\w]*)')


def str_arg(arg: str) -> re.Pattern[str]:
Expand All @@ -80,14 +80,14 @@ def str_arg(arg: str) -> re.Pattern[str]:
'encoding': str_arg('encoding'),

# bool
'comments': bool_arg('comments'),
'preserve-includer-indent': bool_arg('preserve-includer-indent'),
'dedent': bool_arg('dedent'),
'trailing-newlines': bool_arg('trailing-newlines'),
'rewrite-relative-urls': bool_arg('rewrite-relative-urls'),
'comments': arg('comments'),
'preserve-includer-indent': arg('preserve-includer-indent'),
'dedent': arg('dedent'),
'trailing-newlines': arg('trailing-newlines'),
'rewrite-relative-urls': arg('rewrite-relative-urls'),

# int
'heading-offset': re.compile(r'heading-offset=(-?\d+)'),
'heading-offset': arg('heading-offset'),
}


Expand Down
33 changes: 22 additions & 11 deletions src/mkdocs_include_markdown_plugin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ def found_include_tag(match: re.Match[str]) -> str:

exclude_match = ARGUMENT_REGEXES['exclude'].search(arguments_string)
if exclude_match is None:
if defaults['exclude'] is None:
if defaults['exclude'] is None: # pragma: no branch
ignore_paths: list[str] = []
else:
else: # pragma: no cover
ignore_paths = glob.glob(defaults['exclude'])
else:
exclude_string = parse_string_argument(exclude_match)
Expand Down Expand Up @@ -121,7 +121,7 @@ def found_include_tag(match: re.Match[str]) -> str:
else:
file_paths_to_include = process.filter_paths(
glob.iglob(file_path_glob, recursive=True),
ignore_paths=ignore_paths,
ignore_paths,
)

if not file_paths_to_include:
Expand Down Expand Up @@ -209,7 +209,7 @@ def found_include_tag(match: re.Match[str]) -> str:
else:
new_text_to_include = process.read_file(file_path, encoding)

if start is not None or end is not None:
if start or end:
new_text_to_include, *expected_not_found = (
process.filter_inclusions(
start,
Expand Down Expand Up @@ -307,9 +307,9 @@ def found_include_markdown_tag(match: re.Match[str]) -> str:

exclude_match = ARGUMENT_REGEXES['exclude'].search(arguments_string)
if exclude_match is None:
if defaults['exclude'] is None:
if defaults['exclude'] is None: # pragma: no branch
ignore_paths: list[str] = []
else:
else: # pragma: no cover
ignore_paths = glob.glob(defaults['exclude'])
else:
exclude_string = parse_string_argument(exclude_match)
Expand Down Expand Up @@ -340,7 +340,7 @@ def found_include_markdown_tag(match: re.Match[str]) -> str:
else:
file_paths_to_include = process.filter_paths(
glob.iglob(file_path_glob, recursive=True),
ignore_paths=ignore_paths,
ignore_paths,
)

if not file_paths_to_include:
Expand Down Expand Up @@ -432,16 +432,27 @@ def found_include_markdown_tag(match: re.Match[str]) -> str:
arguments_string,
)
if offset_match:
offset = offset_match.group(1)
if offset == '':
lineno = lineno_from_content_start(
markdown,
directive_match_start,
)
raise BuildError(
"Invalid empty 'heading-offset' argument in"
" 'include-markdown' directive at"
f' {os.path.relpath(page_src_path, docs_dir)}:{lineno}',
)
try:
offset = int(offset_match.group(1))
offset = int(offset)
except ValueError:
lineno = lineno_from_content_start(
markdown,
directive_match_start,
)
raise BuildError(
"Invalid 'heading-offset' argument in 'include-markdown'"
' directive at '
f"Invalid 'heading-offset' argument \"{offset}\" in"
" 'include-markdown' directive at "
f'{os.path.relpath(page_src_path, docs_dir)}:{lineno}',
)
else:
Expand All @@ -468,7 +479,7 @@ def found_include_markdown_tag(match: re.Match[str]) -> str:
else:
new_text_to_include = process.read_file(file_path, encoding)

if start is not None or end is not None:
if start or end:
new_text_to_include, *expected_not_found = (
process.filter_inclusions(
start,
Expand Down
6 changes: 3 additions & 3 deletions src/mkdocs_include_markdown_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def on_config(self, config: MkDocsConfig, **kwargs: Any) -> MkDocsConfig:

return config

def _watch_included_files(self) -> None:
def _watch_included_files(self) -> None: # pragma: no cover
global FILES_WATCHER, SERVER
SERVER = cast(LiveReloadServer, SERVER)
FILES_WATCHER = cast(FilesWatcher, FILES_WATCHER)
Expand All @@ -83,7 +83,7 @@ def on_page_content(
config: MkDocsConfig,
files: Files,
) -> str:
if SERVER is not None:
if SERVER is not None: # pragma: no cover
self._watch_included_files()
return html

Expand All @@ -94,7 +94,7 @@ def on_serve(
builder: Callable[[Any], Any],
) -> None:
global SERVER
if SERVER is None:
if SERVER is None: # pragma: no cover
SERVER = server
self._watch_included_files()

Expand Down
7 changes: 2 additions & 5 deletions src/mkdocs_include_markdown_plugin/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def filter_inclusions(
expected_start_not_found = True
text_to_include = new_text_to_include

elif new_end is not None:
elif new_end is not None: # pragma: no branch
end = interpret_escapes(new_end)
if end in text_to_include:
text_to_include = text_to_include.split(
Expand Down Expand Up @@ -381,7 +381,7 @@ def rstrip_trailing_newlines(content: str) -> str:

def filter_paths(
filepaths: Iterator[str],
ignore_paths: list[str] | None = None,
ignore_paths: list[str],
) -> list[str]:
"""Filters a list of paths removing those defined in other list of paths.

Expand All @@ -400,9 +400,6 @@ def filter_paths(
Returns:
list: Non filtered paths ordered alphabetically.
"""
if ignore_paths is None:
ignore_paths = []

response = []
for filepath in filepaths:
# ignore by filepath
Expand Down
44 changes: 44 additions & 0 deletions tests/test_unit/test_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,50 @@ def test_empty_encoding_argument(directive, page, tmp_path, caplog):
)


@pytest.mark.parametrize(
('argument_value', 'exception_message'),
(
pytest.param(
'invalidoption', (
"Invalid 'heading-offset' argument \"invalidoption\" in"
" 'include-markdown' directive at includer.md:1"
),
id='invalidoption',
),
pytest.param(
'', (
"Invalid empty 'heading-offset' argument in"
" 'include-markdown' directive at includer.md:1"
),
id='empty',
),
),
)
def test_invalid_heading_offset_arguments(
argument_value,
exception_message,
page,
tmp_path,
caplog,
):
page_to_include_filepath = tmp_path / 'included.md'
page_to_include_filepath.write_text('# Content to include')

with pytest.raises(BuildError) as exc:
on_page_markdown(
f'''{{%
include-markdown "{page_to_include_filepath}"
comments=false
heading-offset={argument_value}
%}}''',
page(tmp_path / 'includer.md'),
tmp_path,
)

assert len(caplog.records) == 0
assert str(exc.value) == exception_message


class TestFilename:
double_quoted_filenames = [
'inc"luded.md', 'inc"lude"d.md', 'included.md"', '"included.md',
Expand Down
6 changes: 6 additions & 0 deletions tests/test_unit/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def test_cache_read_file(tmp_path):
)


def test_cache_expiration_on_get(tmp_path):
cache = Cache(tmp_path)
cache.set_('foo', f'{time.time() - 600*10}\nbar')
assert cache.get_('foo') is None


def test_cache_clean(tmp_path):
now_ts = int(time.time())

Expand Down
38 changes: 5 additions & 33 deletions tests/test_unit/test_include_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
id='dedent=true,preserve-includer-indent=true',
),

# Markdown heading offset 1
# Markdown heading offsets
pytest.param(
'''# Header

Expand All @@ -423,7 +423,6 @@
[],
id='heading-offset=1',
),
# Markdown heading offset 2
pytest.param(
'''# Header

Expand Down Expand Up @@ -494,30 +493,6 @@
id='heading-offset=0',
),

# Markdown heading offset string
pytest.param(
'''# Header

{%
include-markdown "{filepath}"
heading-offset=true
%}
''',
'''# This should be a first level heading.

Example data''',
'''# Header

<!-- BEGIN INCLUDE {filepath} -->
# This should be a first level heading.

Example data
<!-- END INCLUDE -->
''',
[],
id='heading-offset=<str>',
),

# Markdown heading negative offset
pytest.param(
'''# Header
Expand Down Expand Up @@ -622,10 +597,7 @@
pytest.param(
'''# Header

{%
include-markdown "{filepath}"
heading-offset=true
%}
{% include-markdown "{filepath}" %}
''',
'''Тест інклуде
азъ
Expand Down Expand Up @@ -664,7 +636,7 @@
id='russian-characters',
),

# right strip unix trailing newlines
# Right strip unix trailing newlines
pytest.param(
'''1. List item number 1
1. {% include-markdown "{filepath}" comments=false trailing-newlines=false %}
Expand All @@ -679,7 +651,7 @@
id='rstrip-unix-trailing-newlines',
),

# right strip windows trailing nwlines
# Right strip windows trailing newlines
pytest.param(
'''1. List item number 1
1. {%
Expand All @@ -698,7 +670,7 @@
id='rstrip-windows-trailing-newlines',
),

# right strip trailing newlines keeping comments
# Right strip trailing newlines keeping comments
pytest.param(
'''1. List item number 1
1. {% include-markdown "{filepath}" trailing-newlines=false %}
Expand Down