Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"-s",
"./tests",
"-p",
"*_test.py"
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
Expand Down
7 changes: 4 additions & 3 deletions mkdocs_simple_plugin/simple.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Simple module handles document extraction from source files."""
import os
import fnmatch
import shutil
import stat
import pathlib

from shutil import copy2 as copy

from mkdocs import utils
from mkdocs_simple_plugin.semiliterate import Semiliterate

Expand Down Expand Up @@ -157,7 +158,7 @@ def merge_docs(self, from_dir, dirty=False):
destination_file).st_mtime:
continue
os.remove(destination_file)
shutil.copy(source_file, destination_directory)
copy(source_file, destination_directory)
utils.log.info(
"mkdocs-simple-plugin: %s/* --> %s/*",
source_file, destination_file)
Expand Down Expand Up @@ -210,7 +211,7 @@ def try_copy_file(self, from_dir: str, name: str, to_dir: str) -> bool:
return False
try:
os.makedirs(to_dir, exist_ok=True)
shutil.copy(original, new_file)
copy(original, new_file)
utils.log.info("mkdocs-simple-plugin: %s --> %s",
original, new_file)
return True
Expand Down
73 changes: 34 additions & 39 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env python
"""Test mkdocs_simple_plugin.simple"""
import unittest
from unittest.mock import patch
from unittest.mock import patch, MagicMock
import stat
import os
import shutil

from pyfakefs.fake_filesystem_unittest import TestCase

from mkdocs_simple_plugin import simple
Expand Down Expand Up @@ -273,44 +275,37 @@ def test_build_docs_dirty_copy(self):
test_filename = "foo/bar.md"
self.fs.create_file(test_filename)
built_filename = "/build_dir/foo/bar.md"

# Get the modification time of the original file
src_time = os.path.getmtime(test_filename)

paths = simple_test.build_docs(dirty=True, last_build_time=0)
# Check that the file was built
self.assertIn(test_filename, paths)
# Check that the output file exists
self.assertTrue(os.path.exists(built_filename))
# Get the modification time
dest_time = os.path.getmtime(built_filename)
# Check that the time is newer
self.assertLess(src_time, dest_time)
mock_copy = MagicMock(spec=shutil.copy2)
with patch('mkdocs_simple_plugin.simple.copy',
mock_copy,
create=True) as patched:
self.assertIs(patched, mock_copy)
simple_test.build_docs(dirty=True, last_build_time=0)
mock_copy.assert_called()

# Run again, but this time set the last build time
paths = simple_test.build_docs(dirty=True, last_build_time=dest_time)
# Check that the file was not built
self.assertNotIn(test_filename, paths)
# Check that the file still exists in the output
self.assertTrue(os.path.exists(built_filename))
# Get the modification time
dirty_time = os.path.getmtime(built_filename)
# Check that it is the same as the previous
self.assertEqual(dest_time, dirty_time)

# Update a fake file
self.fs.create_file(built_filename)
dest_time = os.path.getmtime(built_filename)
# Check that the file was not copied since the destination is the same
mock_copy = MagicMock(spec=shutil.copy2)
with patch('mkdocs_simple_plugin.simple.copy',
mock_copy,
create=True) as patched:
self.assertIs(patched, mock_copy)
simple_test.build_docs(dirty=True, last_build_time=dest_time)
mock_copy.assert_not_called()

# Update the file
with open(test_filename, 'w') as f:
f.write('Modified!')

paths = simple_test.build_docs(dirty=True, last_build_time=dirty_time)
# Check that path was built
self.assertIn(test_filename, paths)
# Check that destination file exists
self.assertTrue(os.path.exists(built_filename))
# Get the modification time
modified_time = os.path.getmtime(built_filename)
# Check that the modification time is newer
self.assertLess(dirty_time, modified_time)
# Check that the file was copied
mock_copy = MagicMock(spec=shutil.copy2)
with patch('mkdocs_simple_plugin.simple.copy',
mock_copy,
create=True) as patched:
self.assertIs(patched, mock_copy)
simple_test.build_docs(dirty=True, last_build_time=dest_time)
mock_copy.assert_called()

def test_build_docs_dirty_extract(self):
"""Test dirty build of doc extraction."""
Expand Down Expand Up @@ -420,7 +415,7 @@ def test_merge_docs_update(self):
dest_time = os.path.getmtime(built_filename)

# Copied and modified time should be the same
self.assertLess(src_time, dest_time)
self.assertEqual(src_time, dest_time)

# Update a fake file
with open(test_filename, 'w') as f:
Expand All @@ -441,16 +436,16 @@ def test_merge_docs_update(self):

# Original and copied file should not be the same
self.assertNotEqual(src_time, updated_dest_time)
# New mod time should be less than copied time
self.assertLess(updated_src_time, updated_dest_time)
# New mod time should be the same
self.assertEqual(updated_src_time, updated_dest_time)

# Merge the docs without updates
simple_test.merge_docs("/test")
self.assertTrue(os.path.exists(built_filename))

# Get the updated time
no_update_dest_time = os.path.getmtime(built_filename)
self.assertLess(updated_dest_time, no_update_dest_time)
self.assertEqual(updated_dest_time, no_update_dest_time)

# Test dirty copy no update
simple_test.merge_docs("/test", dirty=True)
Expand Down