From e6df35330205aa6bdbe56cd40e641fab4958af8c Mon Sep 17 00:00:00 2001 From: "(GalaxyMaster)" Date: Mon, 4 May 2020 01:45:25 +1000 Subject: [PATCH] Set "modified:" from "date:" when the latter was set from the filesystem (#2748) Sets the `modified:` metadata from `date:` if the user asked us to use the filesystem for determining timestamps. Fixes #2497 * Added tests (ab)using the HTML reader a bit There seems to be no way to test this generically since we need to produce a valid document with meta information to be able to compare, hence I used the lightest reader out there to do the parsing of the samples. * Fixed tests for generators since there were 4 more articles introduced to the test content directory. --- pelican/readers.py | 1 + ...adata_explicit_date_implicit_modified.html | 15 +++++++ .../article_with_metadata_explicit_dates.html | 16 +++++++ ...adata_implicit_date_explicit_modified.html | 15 +++++++ .../article_with_metadata_implicit_dates.html | 14 ++++++ pelican/tests/test_generators.py | 8 ++++ pelican/tests/test_readers.py | 44 +++++++++++++++++++ 7 files changed, 113 insertions(+) create mode 100644 pelican/tests/content/article_with_metadata_explicit_date_implicit_modified.html create mode 100644 pelican/tests/content/article_with_metadata_explicit_dates.html create mode 100644 pelican/tests/content/article_with_metadata_implicit_date_explicit_modified.html create mode 100644 pelican/tests/content/article_with_metadata_implicit_dates.html diff --git a/pelican/readers.py b/pelican/readers.py index e6d91760..8174aa79 100644 --- a/pelican/readers.py +++ b/pelican/readers.py @@ -685,6 +685,7 @@ def path_metadata(full_path, source_path, settings=None): if settings.get('DEFAULT_DATE', None) == 'fs': metadata['date'] = datetime.datetime.fromtimestamp( os.stat(full_path).st_mtime) + metadata['modified'] = metadata['date'] # Apply EXTRA_PATH_METADATA for the source path and the paths of any # parent directories. Sorting EPM first ensures that the most specific diff --git a/pelican/tests/content/article_with_metadata_explicit_date_implicit_modified.html b/pelican/tests/content/article_with_metadata_explicit_date_implicit_modified.html new file mode 100644 index 00000000..b501ea29 --- /dev/null +++ b/pelican/tests/content/article_with_metadata_explicit_date_implicit_modified.html @@ -0,0 +1,15 @@ + + + This is a super article ! + + + + + + + + + Multi-line metadata should be supported + as well as inline markup. + + diff --git a/pelican/tests/content/article_with_metadata_explicit_dates.html b/pelican/tests/content/article_with_metadata_explicit_dates.html new file mode 100644 index 00000000..1c74f40e --- /dev/null +++ b/pelican/tests/content/article_with_metadata_explicit_dates.html @@ -0,0 +1,16 @@ + + + This is a super article ! + + + + + + + + + + Multi-line metadata should be supported + as well as inline markup. + + diff --git a/pelican/tests/content/article_with_metadata_implicit_date_explicit_modified.html b/pelican/tests/content/article_with_metadata_implicit_date_explicit_modified.html new file mode 100644 index 00000000..8cd2885b --- /dev/null +++ b/pelican/tests/content/article_with_metadata_implicit_date_explicit_modified.html @@ -0,0 +1,15 @@ + + + This is a super article ! + + + + + + + + + Multi-line metadata should be supported + as well as inline markup. + + diff --git a/pelican/tests/content/article_with_metadata_implicit_dates.html b/pelican/tests/content/article_with_metadata_implicit_dates.html new file mode 100644 index 00000000..14d7e1e0 --- /dev/null +++ b/pelican/tests/content/article_with_metadata_implicit_dates.html @@ -0,0 +1,14 @@ + + + This is a super article ! + + + + + + + + Multi-line metadata should be supported + as well as inline markup. + + diff --git a/pelican/tests/test_generators.py b/pelican/tests/test_generators.py index 50e08983..5dd5c3ec 100644 --- a/pelican/tests/test_generators.py +++ b/pelican/tests/test_generators.py @@ -263,6 +263,10 @@ class TestArticlesGenerator(unittest.TestCase): ['This is a super article !', 'published', 'yeah', 'article'], ['This is a super article !', 'published', 'yeah', 'article'], ['This is a super article !', 'published', 'yeah', 'article'], + ['This is a super article !', 'published', 'yeah', 'article'], + ['This is a super article !', 'published', 'yeah', 'article'], + ['This is a super article !', 'published', 'yeah', 'article'], + ['This is a super article !', 'published', 'yeah', 'article'], ['This is a super article !', 'published', 'Default', 'article'], ['Article with an inline SVG', 'published', 'Default', 'article'], ['This is an article with category !', 'published', 'yeah', @@ -579,6 +583,10 @@ class TestArticlesGenerator(unittest.TestCase): 'This is a super article !', 'This is a super article !', 'This is a super article !', + 'This is a super article !', + 'This is a super article !', + 'This is a super article !', + 'This is a super article !', 'This is an article with category !', ('This is an article with multiple authors in lastname, ' 'firstname format!'), diff --git a/pelican/tests/test_readers.py b/pelican/tests/test_readers.py index 3aced39e..eb755338 100644 --- a/pelican/tests/test_readers.py +++ b/pelican/tests/test_readers.py @@ -81,6 +81,50 @@ class DefaultReaderTest(ReaderTest): with self.assertRaises(TypeError): self.read_file(path='article_with_metadata.unknownextension') + def test_readfile_path_metadata_implicit_dates(self): + test_file = 'article_with_metadata_implicit_dates.html' + page = self.read_file(path=test_file, DEFAULT_DATE='fs') + expected = { + 'date': SafeDatetime.fromtimestamp( + os.stat(_path(test_file)).st_mtime), + 'modified': SafeDatetime.fromtimestamp( + os.stat(_path(test_file)).st_mtime) + } + + self.assertDictHasSubset(page.metadata, expected) + + def test_readfile_path_metadata_explicit_dates(self): + test_file = 'article_with_metadata_explicit_dates.html' + page = self.read_file(path=test_file, DEFAULT_DATE='fs') + expected = { + 'date': SafeDatetime(2010, 12, 2, 10, 14), + 'modified': SafeDatetime(2010, 12, 31, 23, 59) + } + + self.assertDictHasSubset(page.metadata, expected) + + def test_readfile_path_metadata_implicit_date_explicit_modified(self): + test_file = 'article_with_metadata_implicit_date_explicit_modified.html' + page = self.read_file(path=test_file, DEFAULT_DATE='fs') + expected = { + 'date': SafeDatetime.fromtimestamp( + os.stat(_path(test_file)).st_mtime), + 'modified': SafeDatetime(2010, 12, 2, 10, 14), + } + + self.assertDictHasSubset(page.metadata, expected) + + def test_readfile_path_metadata_explicit_date_implicit_modified(self): + test_file = 'article_with_metadata_explicit_date_implicit_modified.html' + page = self.read_file(path=test_file, DEFAULT_DATE='fs') + expected = { + 'date': SafeDatetime(2010, 12, 2, 10, 14), + 'modified': SafeDatetime.fromtimestamp( + os.stat(_path(test_file)).st_mtime) + } + + self.assertDictHasSubset(page.metadata, expected) + @unittest.skipUnless(patch, 'Needs Mock module') def test_find_empty_alt(self): with patch('pelican.readers.logger') as log_mock: