def test_lazy_links(self): from engineer.conf import settings from engineer.models import Post settings.reload('config.yaml') post = Post('posts/lazy_markdown_links.md') self.assertEqual(post.content_preprocessed.strip(), self._expected_output.strip()) post = Post('posts/lazy_markdown_links2.md') self.assertEqual(post.content_preprocessed.strip(), self._expected_output2.strip())
def single_tag_test(self): """Single tag""" file = self.post_tests_dir / 'tag_single.md' post = Post(file) self.assertEqual(post.tags, ['single']) self.assertEqual(len(post.tags), 1)
def multiple_tag_test(self): """Multiple tags""" file = self.post_tests_dir / 'tag_multiple.md' post = Post(file) self.assertEqual(len(post.tags), 3) self.assertIsInstance(post.tags, list)
def draft_default_test(self): """Draft default status""" file = self.post_tests_dir / 'fenced_metadata.md' post = Post(file) self.assertTrue(post.is_draft) self.assertFalse(post.is_published)
def published_test(self): """Published status""" file = self.post_tests_dir / 'published.md' post = Post(file) self.assertTrue(post.is_published) self.assertFalse(post.is_draft)
def load_all(input): new_posts = PostCollection() cached_posts = PostCollection() file_list = [] for directory in input: logger.info("Getting posts from %s." % directory) file_list.extend(path(directory).listdir('*.md') + path(directory).listdir('*.markdown')) for f in file_list: try: if f not in settings.POST_CACHE: logger.debug("'%s': Beginning to parse." % f.basename()) post = Post(f) logger.debug("'%s': Parsed successfully." % f.basename()) new_posts.append(post) logger.info("'%s': LOADED" % f.basename()) else: logger.info("'%s': FROM CACHE" % f.basename()) cached_posts.append(settings.POST_CACHE[f]) except PostMetadataError as e: logger.warning("SKIPPING '%s': metadata is invalid. %s" % (f.basename(), e.message)) continue logger.console("Found %d new posts and loaded %s from the cache." % (len(new_posts), len(cached_posts))) settings.CACHE.sync() return new_posts, cached_posts
def custom_properties_test(self): """Custom properties""" file = self.post_tests_dir / 'custom_properties.md' post = Post(file) self.assertEqual(len(post.custom_properties), 2) self.assertIsInstance(post.custom_properties['custom_list'], list) self.assertIsInstance(post.custom_properties['custom_dict'], dict)
def cased_metadata_test(self): """Cased metadata""" file = self.post_tests_dir / 'cased_metadata.md' post = Post(file) self.assertEqual(post.title, 'Cased Metadata') self.assertEqual(post.custom_properties['customproperty'], 'custom') self.assertEqual(len(post.tags), 2)
def default_metadata_test(self): """Metadata defaults""" file = self.post_tests_dir / 'fenced_metadata.md' post = Post(file) self.assertNotEqual(post.slug, None) self.assertNotEqual(post.timestamp, None) self.assertNotEqual(post.status, None)
def unicode_content_test(self): """Unicode post content.""" file = self.post_dir / 'unicode_content.md' # Just loading the post will throw an exception if the unicode handling is broken - no need to specifically # assert anything for that case. post = Post(file) self.assertIn(u"also a “unicode character” in the metadata!", post.tags)
def test_pretty(self): from engineer.conf import settings settings.reload(self.config_dir / 'permalinks_pretty.yaml') file = self.post_dir / 'tag_multiple.md' post = Post(file) self.assertEqual(post.url, '/test/2012/09/tag-multiple/') self.assertEqual(post.output_file_name, 'index.html')
def test_timestamp_custom(self): from engineer.conf import settings settings.reload(self.config_dir / 'permalinks_timestamp_custom.yaml') file = self.post_dir / 'tag_multiple.md' post = Post(file) self.assertEqual(post.url, '/test/2012-09-04/tag-multiple.html') self.assertEqual(post.output_file_name, 'tag-multiple.html')
def test_no_leading_zeroes(self): from engineer.conf import settings settings.reload(self.config_dir / 'permalinks_no_leading_zeroes.yaml') file = self.post_dir / 'tag_multiple.md' post = Post(file) self.assertEqual(post.url, '/test/2012/9/4/tag-multiple.html') self.assertEqual(post.output_file_name, 'tag-multiple.html')
def test_end_slash(self): from engineer.conf import settings settings.reload(self.post_tests_dir / 'permalinks_end_slash.yaml') file = self.post_tests_dir / 'tag_multiple.md' post = Post(file) self.assertEqual(post.url, '/test/test/tag-multiple/') self.assertEqual(post.output_file_name, 'index.html')
def test_slug(self): from engineer.conf import settings settings.reload(self.post_tests_dir / 'permalinks_slug.yaml') file = self.post_tests_dir / 'tag_multiple.md' post = Post(file) self.assertEqual(post.url, '/test/2012/09/04/tag-multiple.html') self.assertEqual(post.output_file_name, 'tag-multiple.html')
def pending_test(self): """Pending status""" file = self.post_tests_dir / 'published.md' post = Post(file) post.timestamp = times.now() + timedelta(days=1) self.assertTrue(post.is_pending) self.assertFalse(post.is_published) self.assertFalse(post.is_draft)
def test_post_renamer_custom_config(self): from engineer.conf import settings from engineer.models import Post settings.reload('custom_renames.yaml') post = Post('posts/draft_post.md') self.assertEqual(post.source.name, 'draft_post.md') self.assertTrue(post.source.exists()) post = Post('posts/review_post.md') self.assertEqual(post.source.name, '2012-11-02-a-post-in-review.md') self.assertTrue(post.source.exists()) self.assertFalse(path('posts/review_post.md').exists()) post = Post('posts/published_post_with_timestamp.md') self.assertEqual(post.source.name, '(p) 2012-11-02 a-published-post.md') self.assertTrue(post.source.exists()) self.assertFalse(path('posts/published_post_with_timestamp.md').exists())
def regular_loading_test(self): from engineer.conf import settings from engineer.loaders import LocalLoader from engineer.models import Post, PostCollection post_file = self.post_dir / 'subdir/post_in_subdir.md' expected_post = Post(post_file) new_posts, cached_posts = LocalLoader.load_all(settings.POST_DIR) all_posts = PostCollection(new_posts + cached_posts) self.assertNotIn(expected_post, all_posts)
def test_force_fenced_metadata(self): from engineer.conf import settings from engineer.models import Post settings.reload('finalization_fenced.yaml') settings.create_required_directories() post = Post('posts/finalization_draft.md') self.assertEqual(post.title, "Finalization Draft") expected_output = '---\n\n' + finalization_draft_output with open(post.source, mode='rb') as post_file: content = post_file.read() self.assertEqual(content, expected_output)
def global_links_test(self): """Global links test.""" from engineer.conf import settings settings.reload(self.config_dir / 'global_links_settings.yaml') post = Post(self.post_dir / 'global_links_post.md') expected_content = """ <p><a href="http://tylerbutler.com">Tyler Butler</a> is the author of Engineer.</p> <p>He does not like to be called <a href="http://tylerbutler.com">Ty</a>.</p> """ actual_content = unicode(post.convert_to_html(post.content_preprocessed)) self.assertEqual(actual_content.strip(), expected_content.strip())
def load_all(input): new_posts = PostCollection() cached_posts = PostCollection() # parse input directories into a dict. The key is the path, the value is a bool indicating # whether the path should be walked when looking for posts or not directories = {} for dir in input: if unicode(dir).endswith('*'): directories[path( dir[:-1]).expand().abspath().normpath()] = True else: directories[path(dir).expand().abspath().normpath()] = False file_list = [] for directory, should_walk in directories.iteritems(): if directory.exists(): logger.info("Getting posts from %s." % directory) if should_walk: file_list.extend( [f for f in directory.walkfiles('*.md')] + [f for f in directory.files('*.markdown')]) else: file_list.extend( directory.files('*.md') + directory.files('*.markdown')) else: logger.warning("Can't find source post directory %s." % directory) for f in file_list: try: if f not in settings.POST_CACHE: logger.debug("'%s': Beginning to parse." % f.basename()) post = Post(f) logger.debug("'%s': Parsed successfully." % f.basename()) new_posts.append(post) logger.info("'%s': LOADED" % f.basename()) else: logger.info("'%s': FROM CACHE" % f.basename()) cached_posts.append(settings.POST_CACHE[f]) except PostMetadataError as e: logger.warning("SKIPPING '%s': metadata is invalid. %s" % (f.basename(), e.message)) continue logger.console("Found %d new posts and loaded %s from the cache." % (len(new_posts), len(cached_posts))) settings.CACHE.sync() return new_posts, cached_posts
def test_finalization_unfenced_post(self): from engineer.conf import settings from engineer.models import Post settings.reload('finalization_unfenced.yaml') settings.create_required_directories() post = Post('posts/finalization_fenced.md') self.assertEqual(post.title, "Finalization Fenced") expected_output = ''.join( finalization_fenced_output.splitlines(True)[2:]) with open(post.source, mode='rb') as post_file: content = post_file.read() self.assertEqual(content, expected_output)
def test_lazy_links_persist(self): from engineer.conf import settings from engineer.models import Post settings.reload('lazy_links_persist.yaml') post = Post('posts/lazy_markdown_links.md') self.assertEqual(post.content_preprocessed.strip(), self._expected_output.strip()) with open(post.source, mode='rb') as post_file: content = post_file.read() self.assertEqual( content.strip(), self._expected_metadata + self._expected_output.strip())
def test_finalization_draft(self): from engineer.conf import settings from engineer.models import Post settings.reload('finalization.yaml') settings.create_required_directories() post = Post('posts/finalization_draft.md') self.assertEqual(post.title, "Finalization Draft") expected_output = """title: Finalization Draft status: draft url: /{year:02d}/{month:02d}/{day:02d}/finalization-draft/ slug: finalization-draft tags: - tag --- This is a finalization test post. """.format(year=post.timestamp_local.year, month=post.timestamp_local.month, day=post.timestamp_local.day) with open(post.source, mode='rb') as post_file: content = post_file.read() self.assertEqual(content, expected_output)
def fenced_metadata_test(self): """Fenced metadata""" file = self.post_tests_dir / 'fenced_metadata.md' post = Post(file) self.assertEqual(post._fence, True)
def post_breaks_simple_test(self): """Post breaks of the form: -- more --""" file = self.post_tests_dir / 'post_breaks_simple.md' post = Post(file) self.assertNotEqual(getattr(post, 'content_teaser', None), None)
def post_breaks_octopress_test(self): """Post breaks of the form: <!-- more -->""" file = self.post_tests_dir / 'post_breaks_octopress.md' post = Post(file) self.assertNotEqual(getattr(post, 'content_teaser', None), None)
def missing_metadata_test(self): """Missing metadata""" file = self.post_tests_dir / 'missing_metadata.md' with self.assertRaises(PostMetadataError): Post(file)
def numeric_tags_test(self): """Numeric tags""" file = self.post_dir / 'numeric_tags.md' post = Post(file) self.assertSequenceEqual(post.tags, ['2013', '2014'])
def no_custom_properties_test(self): """No custom properties""" file = self.post_tests_dir / 'fenced_metadata.md' post = Post(file) self.assertEqual(post.custom_properties, {})