def test_theme_static_paths_copy(self): # the same thing with a specified set of settings should work settings = read_settings( path=SAMPLE_CONFIG, override={ "PATH": INPUT_PATH, "OUTPUT_PATH": self.temp_path, "CACHE_PATH": self.temp_cache, "THEME_STATIC_PATHS": [ os.path.join(SAMPLES_PATH, "very"), os.path.join(SAMPLES_PATH, "kinda"), os.path.join(SAMPLES_PATH, "theme_standard"), ], }, ) pelican = Pelican(settings=settings) mute(True)(pelican.run)() theme_output = os.path.join(self.temp_path, "theme") extra_path = os.path.join(theme_output, "exciting", "new", "files") for file in ["a_stylesheet", "a_template"]: self.assertTrue(os.path.exists(os.path.join(theme_output, file))) for file in ["wow!", "boom!", "bap!", "zap!"]: self.assertTrue(os.path.exists(os.path.join(extra_path, file)))
def setUp(self): self.temp_path = mkdtemp(prefix="pelicanfly.") pelicanfly_path, _ = os.path.join(os.path.split(pelicanfly.__file__)) self.pelicanfly_static = os.path.join(pelicanfly_path, "static") self.settings = read_settings( path=None, override={"PATH": INPUT_PATH, "OUTPUT_PATH": self.temp_path, "PLUGINS": [pelicanfly]} ) self.pelican = Pelican(self.settings) mute(True)(self.pelican.run)() pass
def test_custom_generation_works(self): # the same thing with a specified set of settings should work settings = read_settings(path=SAMPLE_CONFIG, override={ 'PATH': INPUT_PATH, 'OUTPUT_PATH': self.temp_path, 'LOCALE': locale.normalize('en_US'), }) pelican = Pelican(settings=settings) mute(True)(pelican.run)() self.assertDirsEqual(self.temp_path, os.path.join(OUTPUT_PATH, 'custom'))
def test_custom_generation_works(self): # the same thing with a specified set of settings should work settings = read_settings( path=SAMPLE_CONFIG, override={ "PATH": INPUT_PATH, "OUTPUT_PATH": self.temp_path, "CACHE_PATH": self.temp_cache, "LOCALE": locale.normalize("en_US"), }, ) pelican = Pelican(settings=settings) mute(True)(pelican.run)() self.assertDirsEqual(self.temp_path, os.path.join(OUTPUT_PATH, "custom"))
def test_theme_static_paths_copy_single_file(self): # the same thing with a specified set of settings should work settings = read_settings(path=SAMPLE_CONFIG, override={ 'PATH': INPUT_PATH, 'OUTPUT_PATH': self.temp_path, 'THEME_STATIC_PATHS': [os.path.join(SAMPLES_PATH, 'theme_standard')] }) pelican = Pelican(settings=settings) mute(True)(pelican.run)() theme_output = os.path.join(self.temp_path, 'theme') for file in ['a_stylesheet', 'a_template']: self.assertTrue(os.path.exists(os.path.join(theme_output, file)))
def test_parse_errors(self): # Verify that just an error is printed and the application doesn't # abort, exit or something. settings = read_settings(path=None, override={ 'PATH': os.path.abspath(os.path.join(CURRENT_DIR, 'parse_error')), 'OUTPUT_PATH': self.temp_path, 'CACHE_PATH': self.temp_cache, }) pelican = Pelican(settings=settings) mute(True)(pelican.run)() self.assertLogCountEqual( count=1, msg="Could not process .*parse_error.rst", level=logging.ERROR)
def test_md_extensions_deprecation(self): """Test that a warning is issued if MD_EXTENSIONS is used""" settings = read_settings(path=None, override={ 'PATH': INPUT_PATH, 'OUTPUT_PATH': self.temp_path, 'CACHE_PATH': self.temp_cache, 'MD_EXTENSIONS': {}, }) pelican = Pelican(settings=settings) mute(True)(pelican.run)() self.assertLogCountEqual( count=1, msg="MD_EXTENSIONS is deprecated use MARKDOWN instead.", level=logging.WARNING)
def test_md_extensions_list_deprecation(self): """Test that a warning is issued if MD_EXTENSIONS is a list""" settings = read_settings(path=None, override={ 'PATH': INPUT_PATH, 'OUTPUT_PATH': self.temp_path, 'CACHE_PATH': self.temp_cache, 'MD_EXTENSIONS': ['meta'], }) pelican = Pelican(settings=settings) mute(True)(pelican.run)() self.assertIsInstance(pelican.settings['MD_EXTENSIONS'], dict) self.assertLogCountEqual( count=1, msg="The format of the MD_EXTENSIONS setting has changed", level=logging.WARNING)
def test_basic_generation_works(self): # when running pelican without settings, it should pick up the default # ones and generate correct output without raising any exception settings = read_settings(path=None, override={ 'PATH': INPUT_PATH, 'OUTPUT_PATH': self.temp_path, 'LOCALE': locale.normalize('en_US'), }) pelican = Pelican(settings=settings) mute(True)(pelican.run)() self.assertDirsEqual(self.temp_path, os.path.join(OUTPUT_PATH, 'basic')) self.assertLogCountEqual( count=3, msg="Unable to find.*skipping url replacement", level=logging.WARNING)
def test_dirpage_directive_for_page_kind(self): silent_f2p = mute(True)(fields2pelican) test_post = filter(lambda p: p[0].startswith("Empty Page"), self.posts) with temporary_folder() as temp: fname = list(silent_f2p(test_post, 'markdown', temp, dirpage=True))[0] self.assertTrue(fname.endswith('pages%sempty.md' % os.path.sep))
def test_custom_locale_generation_works(self): '''Test that generation with fr_FR.UTF-8 locale works''' if sys.platform == 'win32': our_locale = str('French') else: our_locale = str('fr_FR.UTF-8') settings = read_settings(path=SAMPLE_FR_CONFIG, override={ 'PATH': INPUT_PATH, 'OUTPUT_PATH': self.temp_path, 'CACHE_PATH': self.temp_cache, 'LOCALE': our_locale, }) pelican = Pelican(settings=settings) mute(True)(pelican.run)() self.assertDirsEqual(self.temp_path, os.path.join(OUTPUT_PATH, 'custom_locale'))
def setUp(self, override=None): import assets self.temp_path = mkdtemp(prefix='pelicantests.') settings = { 'PATH': os.path.join(os.path.dirname(CUR_DIR), 'test_data', 'content'), 'OUTPUT_PATH': self.temp_path, 'PLUGINS': [assets], 'THEME': THEME_DIR, 'LOCALE': locale.normalize('en_US'), 'CACHE_CONTENT': False } if override: settings.update(override) self.settings = read_settings(override=settings) pelican = Pelican(settings=self.settings) mute(True)(pelican.run)()
def __execute_pelican(self, settings_override=None): """ Executes pelican. Uses the minimal config of self.settings_default (that will fail!) merged with the given additional settings. :param settings_override: dictionary with pelican setting values to set :type settings_override: dict """ if not settings_override: settings_override = {} settings = self.settings_default.copy() settings.update(settings_override) pelican_settings = read_settings( path=None, override=settings ) pelican = Pelican(settings=pelican_settings) mute(True)(pelican.run)()
def test_download_attachments(self): real_file = os.path.join(CUR_DIR, 'content/article.rst') good_url = path_to_file_url(real_file) bad_url = 'http://localhost:1/not_a_file.txt' silent_da = mute()(download_attachments) with temporary_folder() as temp: locations = list(silent_da(temp, [good_url, bad_url])) self.assertEqual(1, len(locations)) directory = locations[0] self.assertTrue(directory.endswith(os.path.join('content', 'article.rst')), directory)
def test_theme_static_paths_copy(self): # the same thing with a specified set of settings should work settings = read_settings(path=SAMPLE_CONFIG, override={ 'PATH': INPUT_PATH, 'OUTPUT_PATH': self.temp_path, 'CACHE_PATH': self.temp_cache, 'THEME_STATIC_PATHS': [os.path.join(SAMPLES_PATH, 'very'), os.path.join(SAMPLES_PATH, 'kinda'), os.path.join(SAMPLES_PATH, 'theme_standard')] }) pelican = Pelican(settings=settings) mute(True)(pelican.run)() theme_output = os.path.join(self.temp_path, 'theme') extra_path = os.path.join(theme_output, 'exciting', 'new', 'files') for file in ['a_stylesheet', 'a_template']: self.assertTrue(os.path.exists(os.path.join(theme_output, file))) for file in ['wow!', 'boom!', 'bap!', 'zap!']: self.assertTrue(os.path.exists(os.path.join(extra_path, file)))
def test_code_in_list(self): def r(f): with open(f) as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) test_post = filter(lambda p: p[0].startswith("Code in List"), self.posts) with temporary_folder() as temp: md = [r(f) for f in silent_f2p(test_post, 'markdown', temp)][0] sample_line = re.search(r'- This is a code sample', md).group(0) code_line = re.search(r'\s+a = \[1, 2, 3\]', md).group(0) self.assertTrue(sample_line.rindex('This') < code_line.rindex('a'))
def test_download_attachments(self): real_file = os.path.join(CUR_DIR, 'content/article.rst') good_url = 'file://' + real_file bad_url = 'http://localhost:1/not_a_file.txt' silent_da = mute()(download_attachments) with temporary_folder() as temp: #locations = download_attachments(temp, [good_url, bad_url]) locations = list(silent_da(temp, [good_url, bad_url])) self.assertTrue(len(locations) == 1) directory = locations[0] self.assertTrue(directory.endswith('content/article.rst'))
def test_custom_locale_generation_works(self): """Test that generation with fr_FR.UTF-8 locale works""" old_locale = locale.setlocale(locale.LC_TIME) if sys.platform == "win32": our_locale = str("French") else: our_locale = str("fr_FR.UTF-8") settings = read_settings( path=SAMPLE_FR_CONFIG, override={ "PATH": INPUT_PATH, "OUTPUT_PATH": self.temp_path, "CACHE_PATH": self.temp_cache, "LOCALE": our_locale, }, ) pelican = Pelican(settings=settings) mute(True)(pelican.run)() self.assertDirsEqual(self.temp_path, os.path.join(OUTPUT_PATH, "custom_locale"))
def test_dont_use_smart_quotes(self): def r(f): with open(f, encoding='utf-8') as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) test_post = filter( lambda p: p[0].startswith("Post with raw data"), self.posts) with temporary_folder() as temp: md = [r(f) for f in silent_f2p(test_post, 'markdown', temp)][0] escaped_quotes = re.search(r'\\[\'"“”‘’]', md) self.assertFalse(escaped_quotes)
def test_code_in_list(self): def r(f): with open(f, encoding='utf-8') as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) test_post = filter( lambda p: p[0].startswith("Code in List"), self.posts) with temporary_folder() as temp: md = [r(f) for f in silent_f2p(test_post, 'markdown', temp)][0] sample_line = re.search(r'- This is a code sample', md).group(0) code_line = re.search(r'\s+a = \[1, 2, 3\]', md).group(0) self.assertTrue(sample_line.rindex('This') < code_line.rindex('a'))
def test_cyclic_intersite_links_no_warnings(self): settings = read_settings(path=None, override={ 'PATH': os.path.join(CURRENT_DIR, 'cyclic_intersite_links'), 'OUTPUT_PATH': self.temp_path, 'CACHE_PATH': self.temp_cache, }) pelican = Pelican(settings=settings) mute(True)(pelican.run)() # There are four different intersite links: # - one pointing to the second article from first and third # - one pointing to the first article from second and third # - one pointing to the third article from first and second # - one pointing to a nonexistent from each # If everything goes well, only the warning about the nonexistent # article should be printed. Only two articles are not sufficient, # since the first will always have _context['generated_content'] empty # (thus skipping the link resolving) and the second will always have it # non-empty, containing the first, thus always succeeding. self.assertLogCountEqual( count=1, msg="Unable to find '.*\\.rst', skipping url replacement.", level=logging.WARNING)
def test_write_only_selected(self): """Test that only the selected files are written""" settings = read_settings(path=None, override={ 'PATH': INPUT_PATH, 'OUTPUT_PATH': self.temp_path, 'CACHE_PATH': self.temp_cache, 'WRITE_SELECTED': [ os.path.join(self.temp_path, 'oh-yeah.html'), os.path.join(self.temp_path, 'categories.html'), ], 'LOCALE': locale.normalize('en_US'), }) pelican = Pelican(settings=settings) logger = logging.getLogger() orig_level = logger.getEffectiveLevel() logger.setLevel(logging.INFO) mute(True)(pelican.run)() logger.setLevel(orig_level) self.assertLogCountEqual( count=2, msg="Writing .*", level=logging.INFO)
def test_preserve_verbatim_formatting(self): def r(f): with open(f) as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) test_post = filter(lambda p: p[0].startswith("Code in List"), self.posts) with temporary_folder() as temp: md = [r(f) for f in silent_f2p(test_post, 'markdown', temp)][0] self.assertTrue(re.search(r'\s+a = \[1, 2, 3\]', md)) self.assertTrue(re.search(r'\s+b = \[4, 5, 6\]', md)) for_line = re.search(r'\s+for i in zip\(a, b\):', md).group(0) print_line = re.search(r'\s+print i', md).group(0) self.assertTrue(for_line.rindex('for') < print_line.rindex('print'))
def test_dircat(self): silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.posts: # check post kind if len(post[5]) > 0: # Has a category test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, dircat=True)) index = 0 for post in test_posts: name = post[2] category = slugify(post[5][0]) name += '.md' filename = os.path.join(category, name) out_name = fnames[index] self.assertTrue(out_name.endswith(filename)) index += 1
def test_wp_custpost_true_dirpage_false(self): # pages should only be put in their own directory when dirpage = True silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.custposts: # check post kind if post[8] == 'page': test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, wp_custpost=True, dirpage=False)) index = 0 for post in test_posts: name = post[2] name += '.md' filename = os.path.join('pages', name) out_name = fnames[index] self.assertFalse(out_name.endswith(filename))
def test_can_toggle_raw_html_code_parsing(self): def r(f): with open(f) as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) with temporary_folder() as temp: rst_files = (r(f) for f in silent_f2p(self.posts, "markdown", temp)) self.assertTrue(any("<iframe" in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(self.posts, "markdown", temp, strip_raw=True)) self.assertFalse(any("<iframe" in rst for rst in rst_files)) # no effect in rst rst_files = (r(f) for f in silent_f2p(self.posts, "rst", temp)) self.assertFalse(any("<iframe" in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(self.posts, "rst", temp, strip_raw=True)) self.assertFalse(any("<iframe" in rst for rst in rst_files))
def test_can_toggle_raw_html_code_parsing(self): posts = list(self.posts) r = lambda f: open(f).read() silent_f2p = mute(True)(fields2pelican) with temporary_folder() as temp: rst_files = (r(f) for f in silent_f2p(posts, 'markdown', temp)) self.assertTrue(any('<iframe' in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(posts, 'markdown', temp, strip_raw=True)) self.assertFalse(any('<iframe' in rst for rst in rst_files)) # no effect in rst rst_files = (r(f) for f in silent_f2p(posts, 'rst', temp)) self.assertFalse(any('<iframe' in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(posts, 'rst', temp, strip_raw=True)) self.assertFalse(any('<iframe' in rst for rst in rst_files))
def test_can_toggle_raw_html_code_parsing(self): def r(f): with open(f) as infile: return infile.read() silent_f2p = mute(True)(fields2pelican) with temporary_folder() as temp: rst_files = (r(f) for f in silent_f2p(self.posts, 'markdown', temp)) self.assertTrue(any('<iframe' in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(self.posts, 'markdown', temp, strip_raw=True)) self.assertFalse(any('<iframe' in rst for rst in rst_files)) # no effect in rst rst_files = (r(f) for f in silent_f2p(self.posts, 'rst', temp)) self.assertFalse(any('<iframe' in rst for rst in rst_files)) rst_files = (r(f) for f in silent_f2p(self.posts, 'rst', temp, strip_raw=True)) self.assertFalse(any('<iframe' in rst for rst in rst_files))
def test_custom_posts_put_in_own_dir(self): silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.custposts: # check post kind if post[7] == 'article' or post[7] == 'page': pass else: test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, wp_custpost = True)) index = 0 for post in test_posts: name = post[2] kind = post[7] name += '.md' filename = os.path.join(kind, name) out_name = fnames[index] self.assertTrue(out_name.endswith(filename)) index += 1
def test_dircat(self): silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.posts: # check post kind if len(post[5]) > 0: # Has a category test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, dircat=True)) subs = DEFAULT_CONFIG['SLUG_REGEX_SUBSTITUTIONS'] index = 0 for post in test_posts: name = post[2] category = slugify(post[5][0], regex_subs=subs, preserve_case=True) name += '.md' filename = os.path.join(category, name) out_name = fnames[index] self.assertTrue(out_name.endswith(filename)) index += 1
def test_custom_posts_put_in_own_dir(self): silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.custposts: # check post kind if post[8] == 'article' or post[8] == 'page': pass else: test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, wp_custpost = True)) index = 0 for post in test_posts: name = post[2] kind = post[8] name += '.md' filename = os.path.join(kind, name) out_name = fnames[index] self.assertTrue(out_name.endswith(filename)) index += 1
def test_custom_posts_put_in_own_dir_and_catagory_sub_dir(self): silent_f2p = mute(True)(fields2pelican) test_posts = [] for post in self.custposts: # check post kind if post[8] == 'article' or post[8] == 'page': pass else: test_posts.append(post) with temporary_folder() as temp: fnames = list(silent_f2p(test_posts, 'markdown', temp, wp_custpost=True, dircat=True)) subs = DEFAULT_CONFIG['SLUG_REGEX_SUBSTITUTIONS'] index = 0 for post in test_posts: name = post[2] kind = post[8] category = slugify(post[5][0], regex_subs=subs, preserve_case=True) name += '.md' filename = os.path.join(kind, category, name) out_name = fnames[index] self.assertTrue(out_name.endswith(filename)) index += 1