def test_load_articles(self): """ Make sure that all articles are loaded as expected. """ # Go to temporary directory os.chdir(self.test_dir) # Generate base site args = juliet.parse_arguments(['init']) juliet.init(args) # Generate a few articles args = juliet.parse_arguments(['new', '--', 'title', 'Hello, World!']) juliet.init_new_entry(args) args = juliet.parse_arguments(['new', '--', 'title', 'First article']) juliet.init_new_entry(args) args = juliet.parse_arguments(['new', '--', 'title', 'Trip to France']) juliet.init_new_entry(args) # Make sure are articles can be loaded as expected config = {"site": juliet.configurator.get_config(juliet.paths.CFG_FILE)} posts = juliet.loader.get_from_folder(juliet.paths.POSTS_PATH, config) self.assertEqual(len(posts), 3) # Go back to current directory os.chdir(self.cur_dir)
def test_generate_default_with_filenaming_pattern(self): """ Make sure new article files are generated with the right name when a filenaming pattern is specified. Doesn't check for content. """ # Generate base site self._init_juliet_structure_in_test_dir() # Add filenaming pattern with open(juliet.paths.CFG_FILE, "a") as cfgfile: cfgfile.write("filenaming_pattern: 'article-$number.md'") # Prepare args base_args = ['new', '--', 'number', '12'] args = juliet.parse_arguments(base_args) # Generate article juliet.init_new_entry(args) # Make sure article was created file_name = "article-12.md" article_path = os.path.join(juliet.paths.POSTS_BUILDDIR, file_name) self.assertTrue( os.path.exists(article_path), "Expected article to be generated at {} but couldn't find it". format(article_path))
def test_generate_default_with_dest_folder(self): """ Make sure new article files are generated when a destination folder is specified. Doesn't check for content. """ # Generate base site self._init_juliet_structure_in_test_dir() # Prepare args base_args = ['new', '--build-src', self.test_dir] args = juliet.parse_arguments(base_args) # Generate article juliet.init_new_entry(args) # Make sure article was created file_name = Template(defaults.DEFAULT_POST_NAMING_PATTERN).substitute( juliet._process_header_dict( defaults.DEFAULT_THEME_HEADERS["posts"], {})) article_path = os.path.join(args.src, juliet.paths.POSTS_BUILDDIR, file_name) self.assertTrue( os.path.exists(article_path), "Expected article to be generated at {} but couldn't find it". format(article_path)) # Make sure it was created at the right place self.assertEqual( os.path.dirname(article_path), os.path.join(self.test_dir, juliet.paths.POSTS_PATH), "Expected article to be generated in {} but in fact " + "it was generated at {}".format( os.path.join(self.test_dir, juliet.paths.POSTS_PATH), os.path.dirname(article_path)))
def test_missing_structure(self): """ Make sure Juliet behaves correctly when folder content is missing. """ # Do *not* generate base site base_args = ['new', '--build-src', self.test_dir] args = juliet.parse_arguments(base_args) # Try to generate article self.assertRaises(FileNotFoundError, juliet.init_new_entry, args)
def test_build_simple_no_permalinks(self): """ Make sure that juliet is able to build a simple source correctly. """ args = juliet.parse_arguments([ 'build', '--build-src', self.test_data_path2, '--build-dst', self.dest ]) juliet.build(args) self.assertTrue( self._dir_trees_identical(self.test_built_path2, self.dest))
def test_build_header_yml(self): """ Make sure that juliet is able to build a source correctly with a somewhat more complicated theme that features a headers.yml. """ args = juliet.parse_arguments([ 'build', '--build-src', self.test_data_path3, '--build-dst', self.dest ]) juliet.build(args) self.assertTrue( self._dir_trees_identical(self.test_built_path3, self.dest))
def test_pass_filename(self): """ Make sure new article is generated at the right place when file name is passed. Make sure that passing a file name does not modify the actual content.""" # Generate base site self._init_juliet_structure_in_test_dir() # Prepare args b_file_name = 'hello_world.md' base_args = ['new', '-f', b_file_name] b_args = juliet.parse_arguments(base_args) comp_args = ['new'] c_args = juliet.parse_arguments(comp_args) # Generate articles juliet.init_new_entry(b_args) juliet.init_new_entry(c_args) # Make sure article was created where it is expected to be created b_article_path = os.path.join(juliet.paths.POSTS_BUILDDIR, b_file_name) self.assertTrue( os.path.exists(b_article_path), "Expected article to be generated at {} but couldn't find it". format(b_article_path)) # Make sure it is the same as the default article (passing a file # name should only change the file name) c_file_name = Template( defaults.DEFAULT_POST_NAMING_PATTERN).substitute( juliet._process_header_dict( defaults.DEFAULT_THEME_HEADERS["posts"], {})) c_article_path = os.path.join(juliet.paths.POSTS_BUILDDIR, c_file_name) with open(b_article_path) as b_f: with open(c_article_path) as c_f: self.assertEqual(b_f.read(), c_f.read(), "passing file name modifies article content")
def _test_generate_with_remainder(remainder_dict): # Generate base site self._init_juliet_structure_in_test_dir() remainder = ["--"] for key, value in remainder_dict.items(): remainder.append(key) remainder.append(value) # Prepare args base_args = ['new', *remainder] args = juliet.parse_arguments(base_args) parsed_header_entries = juliet._parse_raw_header_entries(remainder) # Generate article juliet.init_new_entry(args) # Make sure article was created filename = Template( defaults.DEFAULT_POST_NAMING_PATTERN).substitute( juliet._process_header_dict( defaults.DEFAULT_THEME_HEADERS["posts"], remainder)) path = os.path.join(juliet.paths.POSTS_BUILDDIR, filename) self.assertTrue( os.path.exists(path), "Expected article to be generated at {} but couldn't find it". format(path)) # Retrieve and parse generated header raw_file = "" with open(path) as f: raw_file = f.read() parsed_header = PageProcessor._get_parsed_header( raw_file, filename, defaults.DEFAULT_FILE_NAMING_VARIABLE) for key, value in parsed_header_entries.items(): self.assertTrue(remainder_dict[key] == value) for key, value in remainder_dict.items(): self.assertTrue(parsed_header_entries[key] == value) # catches bugs in unicode support self.assertTrue(raw_file.count(key) == 1) self.assertTrue(raw_file.count(value) == 1) # Avoid interferences with future calls by deleting generated file os.remove(path)
def test_without_dir(self): """ Make sure new article files are well generated with minimal set of options. """ # Go to temporary directory os.chdir(self.test_dir) # Generate base site args = juliet.parse_arguments(['init']) juliet.init(args) # Test config file self.assertTrue( self._check_config(juliet.paths.CFG_FILE), "default config was installed but doesn't contain valid content") # Test site structure for directory in juliet.paths.SOURCE_DIRS: self.assertTrue(os.path.isdir(directory)) # Go back to current directory os.chdir(self.cur_dir)
def test_pass_filename_and_remainder(self): """ Make sure new article files are generated when a destination folder is specified together with a remainder (-- stuff). Doesn't check for content. """ # Generate base site self._init_juliet_structure_in_test_dir() # Prepare args file_name = 'my-new-article.md' base_args = ['new', '-f', file_name, '--', 'title', 'value'] args = juliet.parse_arguments(base_args) # Generate article juliet.init_new_entry(args) # Make sure article was created article_path = os.path.join(juliet.paths.POSTS_BUILDDIR, file_name) self.assertTrue( os.path.exists(article_path), "Expected article to be generated at {} but couldn't find it". format(article_path))
def test_with_dir(self): """ Make sure new article files are well generated when date option is passed. """ # Generate base site args = juliet.parse_arguments(['init', '--dir', self.test_dir]) juliet.init(args) # Test config file cfg_file = os.path.join(self.test_dir, juliet.paths.CFG_FILE) self.assertTrue( self._check_config(cfg_file), "default config was installed but doesn't contain valid content") # Test site structure for directory in juliet.paths.SOURCE_DIRS: dir_to_check = os.path.join(self.test_dir, directory) self.assertTrue(os.path.isdir(dir_to_check)) self.assertTrue( self._check_config( os.path.join(self.test_dir, juliet.paths.CFG_FILE)), "default config was installed but doesn't contain valid content")
def test_overwrite(self): """ Make sure new article files don't overwrite existing files. """ # Generate base site self._init_juliet_structure_in_test_dir() # Prepare args file_name = "file-name.md" base_args = ['new', '-f', file_name] args = juliet.parse_arguments(base_args) # Generate article juliet.init_new_entry(args) # Make sure article was created path = os.path.join(juliet.paths.POSTS_BUILDDIR, file_name) self.assertTrue( os.path.exists(path), "Expected article to be generated at {} but couldn't find it". format(path)) # Try to generate file a second time self.assertRaises(ValueError, juliet.init_new_entry, args)
def test_generate_default(self): """ Make sure new article files are generated. Check for default content. """ # Generate base site self._init_juliet_structure_in_test_dir() # Prepare args base_args = ['new'] args = juliet.parse_arguments(base_args) # Generate article juliet.init_new_entry(args) # Make sure article was created filename = Template(defaults.DEFAULT_POST_NAMING_PATTERN).substitute( juliet._process_header_dict( defaults.DEFAULT_THEME_HEADERS["posts"], {})) path = os.path.join(juliet.paths.POSTS_BUILDDIR, filename) self.assertTrue( os.path.exists(path), "Expected article to be generated at {} but couldn't find it". format(path)) # Retrieve and parse generated header raw_file = "" with open(path) as f: raw_file = f.read() parsed_header = PageProcessor._get_parsed_header( raw_file, filename, defaults.DEFAULT_FILE_NAMING_VARIABLE) for key, value in defaults.DEFAULT_THEME_HEADERS["posts"].items(): if (value != None): self.assertEqual(parsed_header[key], value) elif (key == "date"): # We did not pass date, so expect it to be the current day self.assertEqual(parsed_header[key], datetime.date.today())
def _init_juliet_structure_in_test_dir(self): args = juliet.parse_arguments(['init', '--dir', self.test_dir]) juliet.init(args)