mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Merge pull request #2750 from avaris/autoreload
Refactor file/folder watchers and autoreload
This commit is contained in:
commit
2eb9c26cdb
3 changed files with 273 additions and 184 deletions
|
|
@ -10,6 +10,7 @@ import pytz
|
|||
|
||||
from pelican import utils
|
||||
from pelican.generators import TemplatePagesGenerator
|
||||
from pelican.readers import Readers
|
||||
from pelican.settings import read_settings
|
||||
from pelican.tests.support import (LoggedTestCase, get_article,
|
||||
locale_available, unittest)
|
||||
|
|
@ -361,47 +362,91 @@ class TestUtils(LoggedTestCase):
|
|||
self.assertNotIn(a_arts[4], b_arts[5].translations)
|
||||
self.assertNotIn(a_arts[5], b_arts[4].translations)
|
||||
|
||||
def test_watchers(self):
|
||||
# Test if file changes are correctly detected
|
||||
# Make sure to handle not getting any files correctly.
|
||||
def test_filesystemwatcher(self):
|
||||
def create_file(name, content):
|
||||
with open(name, 'w') as f:
|
||||
f.write(content)
|
||||
|
||||
dirname = os.path.join(os.path.dirname(__file__), 'content')
|
||||
folder_watcher = utils.folder_watcher(dirname, ['rst'])
|
||||
# disable logger filter
|
||||
from pelican.utils import logger
|
||||
logger.disable_filter()
|
||||
|
||||
path = os.path.join(dirname, 'article_with_metadata.rst')
|
||||
file_watcher = utils.file_watcher(path)
|
||||
# create a temp "project" dir
|
||||
root = mkdtemp()
|
||||
content_path = os.path.join(root, 'content')
|
||||
static_path = os.path.join(root, 'content', 'static')
|
||||
config_file = os.path.join(root, 'config.py')
|
||||
theme_path = os.path.join(root, 'mytheme')
|
||||
|
||||
# first check returns True
|
||||
self.assertEqual(next(folder_watcher), True)
|
||||
self.assertEqual(next(file_watcher), True)
|
||||
# populate
|
||||
os.mkdir(content_path)
|
||||
os.mkdir(theme_path)
|
||||
create_file(config_file,
|
||||
'PATH = "content"\n'
|
||||
'THEME = "mytheme"\n'
|
||||
'STATIC_PATHS = ["static"]')
|
||||
|
||||
# next check without modification returns False
|
||||
self.assertEqual(next(folder_watcher), False)
|
||||
self.assertEqual(next(file_watcher), False)
|
||||
t = time.time() - 1000 # make sure it's in the "past"
|
||||
os.utime(config_file, (t, t))
|
||||
settings = read_settings(config_file)
|
||||
|
||||
# after modification, returns True
|
||||
t = time.time()
|
||||
os.utime(path, (t, t))
|
||||
self.assertEqual(next(folder_watcher), True)
|
||||
self.assertEqual(next(file_watcher), True)
|
||||
watcher = utils.FileSystemWatcher(config_file, Readers, settings)
|
||||
# should get a warning for static not not existing
|
||||
self.assertLogCountEqual(1, 'Watched path does not exist: .*static')
|
||||
|
||||
# file watcher with None or empty path should return None
|
||||
self.assertEqual(next(utils.file_watcher('')), None)
|
||||
self.assertEqual(next(utils.file_watcher(None)), None)
|
||||
# create it and update config
|
||||
os.mkdir(static_path)
|
||||
watcher.update_watchers(settings)
|
||||
# no new warning
|
||||
self.assertLogCountEqual(1, 'Watched path does not exist: .*static')
|
||||
|
||||
empty_path = os.path.join(os.path.dirname(__file__), 'empty')
|
||||
try:
|
||||
os.mkdir(empty_path)
|
||||
os.mkdir(os.path.join(empty_path, "empty_folder"))
|
||||
shutil.copy(__file__, empty_path)
|
||||
# get modified values
|
||||
modified = watcher.check()
|
||||
# empty theme and content should raise warnings
|
||||
self.assertLogCountEqual(1, 'No valid files found in content')
|
||||
self.assertLogCountEqual(1, 'Empty theme folder. Using `basic` theme')
|
||||
|
||||
# if no files of interest, returns None
|
||||
watcher = utils.folder_watcher(empty_path, ['rst'])
|
||||
self.assertEqual(next(watcher), None)
|
||||
except OSError:
|
||||
self.fail("OSError Exception in test_files_changed test")
|
||||
finally:
|
||||
shutil.rmtree(empty_path, True)
|
||||
self.assertIsNone(modified['content']) # empty
|
||||
self.assertIsNone(modified['theme']) # empty
|
||||
self.assertIsNone(modified['[static]static']) # empty
|
||||
self.assertTrue(modified['settings']) # modified, first time
|
||||
|
||||
# add a content, add file to theme and check again
|
||||
create_file(os.path.join(content_path, 'article.md'),
|
||||
'Title: test\n'
|
||||
'Date: 01-01-2020')
|
||||
|
||||
create_file(os.path.join(theme_path, 'dummy'),
|
||||
'test')
|
||||
|
||||
modified = watcher.check()
|
||||
# no new warning
|
||||
self.assertLogCountEqual(1, 'No valid files found in content')
|
||||
self.assertLogCountEqual(1, 'Empty theme folder. Using `basic` theme')
|
||||
|
||||
self.assertIsNone(modified['[static]static']) # empty
|
||||
self.assertFalse(modified['settings']) # not modified
|
||||
self.assertTrue(modified['theme']) # modified
|
||||
self.assertTrue(modified['content']) # modified
|
||||
|
||||
# change config, remove static path
|
||||
create_file(config_file,
|
||||
'PATH = "content"\n'
|
||||
'THEME = "mytheme"\n'
|
||||
'STATIC_PATHS = []')
|
||||
|
||||
settings = read_settings(config_file)
|
||||
watcher.update_watchers(settings)
|
||||
|
||||
modified = watcher.check()
|
||||
self.assertNotIn('[static]static', modified) # should be gone
|
||||
self.assertTrue(modified['settings']) # modified
|
||||
self.assertFalse(modified['content']) # not modified
|
||||
self.assertFalse(modified['theme']) # not modified
|
||||
|
||||
# cleanup
|
||||
logger.enable_filter()
|
||||
shutil.rmtree(root)
|
||||
|
||||
def test_clean_output_dir(self):
|
||||
retention = ()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue