def test_main_add_page_with_title(self): args = MockArguments(sub_command='add', add_item_type='page', title="RealPage") Hyde.handle_sub_command(args) path = args.title + '/' page_file = path + 'index.md' self.assertTrue(os.path.isfile(page_file)) TestUtility.remove_file(page_file) self.assertFalse(os.path.isfile(page_file)) TestUtility.remove_directory(path) self.assertFalse(os.path.exists(path))
def test_main_draft_post_with_title(self): args = MockArguments(sub_command='draft', draft_item_type='post', title="real title") Hyde.handle_sub_command(args) path = '_drafts/posts/' expected_title = TestUtility.build_jekyll_post_title('real-title') + '.md' self.assertTrue(os.path.isfile(path + expected_title)) os.remove(path + expected_title) shutil.rmtree(path) self.assertFalse(os.path.isfile(path + expected_title)) self.assertFalse(os.path.exists(path))
def test_draft_not_found(self): TestUtility.clear_sys_args() os.mkdir(Config.drafts_dir) os.mkdir(Config.posts_drafts_dir) with self.assertRaises(CommandArgumentError) as err: TestUtility.append_sys_args(['publish', 'A Draft Post To Publish']) Hyde.main() self.assertEqual(err.exception.msg, "No draft found with 'A-Draft-Post-To-Publish' in the title.") TestUtility.remove_directory(Config.drafts_dir) self.assertFalse(os.path.exists(Config.drafts_dir)) TestUtility.clear_sys_args()
def test_handle_add_post(self): post_title = 'a test title' path = '_posts/' Hyde._handle_add_post(post_title, path) actual_title = TestUtility.build_jekyll_post_title('a-test-title') + '.md' actual_file = path + actual_title self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.isfile(actual_file)) expected_post_contents = JekyllPostTest.get_expected_post_contents(post_title) actual_post_contents = JekyllPostTest.get_actual_post_contents(actual_file) self.assertEqual(expected_post_contents, actual_post_contents) TestUtility.remove_file(actual_file) self.assertFalse(os.path.isfile(actual_file)) TestUtility.remove_directory(path) self.assertFalse(os.path.exists(path))
def test_handle_add_existing_page(self): """ Tests adding a page that already exists. Ensures a sub-page is created correctly and that the existing page remains unchanged. """ page_name = 'TestPage' actual_file_name = 'index.md' Hyde._handle_add_page(page_name) actual_file = page_name + '/' + actual_file_name self.assertTrue(os.path.exists(page_name)) self.assertTrue(os.path.isfile(actual_file)) Hyde._handle_add_page(page_name) self.assertTrue(os.path.exists(page_name)) self.assertTrue(os.path.isfile(actual_file))
def test_create_jekyll_post_title(self): """ Tests the creation of Jekyll post title using the Jekyll format. """ actual_title = Hyde.create_jekyll_post_title('title for this unit test') expected_title = TestUtility.build_jekyll_post_title('title-for-this-unit-test') self.assertEquals(expected_title, actual_title)
def test_handle_add_duplicate_post(self): post_title = 'a test title' Hyde._handle_add_post(post_title) path = '_posts/' actual_title = TestUtility.build_jekyll_post_title('a-test-title') + '.md' actual_file = path + actual_title self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.isfile(actual_file)) with self.assertRaises(DuplicatePostError) as err: Hyde._handle_add_post(post_title) self.assertEqual("The file " + path + actual_title + " already exists. Nothing Created.", err.exception.msg) TestUtility.remove_file(actual_file) self.assertFalse(os.path.isfile(actual_file)) TestUtility.remove_directory(path) self.assertFalse(os.path.exists(path))
def test_handle_add_new_page(self): """ Tests adding a page and ensures the file is created. Then cleans up the file and the directory. """ page_name = 'TestPage' actual_file_name = 'index.md' Hyde._handle_add_page(page_name) actual_file = page_name + '/' + actual_file_name self.assertTrue(os.path.exists(page_name)) self.assertTrue(os.path.isfile(actual_file)) expected_page_contents = JekyllPageTest.get_expected_page_contents(page_name) actual_page_contents = JekyllPageTest.get_actual_page_contents(actual_file) self.assertEqual(expected_page_contents, actual_page_contents) TestUtility.remove_file(actual_file) self.assertFalse(os.path.isfile(actual_file)) TestUtility.remove_directory(page_name) self.assertFalse(os.path.exists(page_name))
def test_publish_draft_post(self): TestUtility.clear_sys_args() TestUtility.append_sys_args(['draft', 'post', 'A Draft Post To Publish']) Hyde.main() actual_title = TestUtility.build_jekyll_post_title('a-draft-post-to-publish') + '.md' actual_file = Config.posts_drafts_dir + actual_title self.assertTrue(os.path.exists(Config.posts_drafts_dir)) self.assertTrue(os.path.isfile(actual_file)) os.mkdir(Config.posts_dir) TestUtility.clear_sys_args() TestUtility.append_sys_args(['publish', 'A Draft Post To Publish']) Hyde.main() self.assertTrue(os.path.exists(Config.posts_dir)) self.assertTrue(os.path.isfile(Config.posts_dir+actual_title)) TestUtility.remove_directory(Config.posts_dir) TestUtility.remove_directory(Config.posts_drafts_dir) TestUtility.remove_directory(Config.drafts_dir) self.assertFalse(os.path.exists(Config.posts_dir)) self.assertFalse(os.path.exists(Config.posts_drafts_dir)) self.assertFalse(os.path.exists(Config.drafts_dir)) TestUtility.clear_sys_args()
def test_main_unknown_unknown_no_title(self): missing_args = MockArguments(sub_command='unknown', draft_item_type='unknown') with self.assertRaises(CommandArgumentError) as commandError: Hyde.handle_sub_command(missing_args) self.assertEqual(HydeMainTests.NO_VALID_SUB_COMMAND, commandError.exception.msg)
def test_main_draft_unknown_no_title(self): missing_args = MockArguments(sub_command='draft', draft_item_type='unknown') with self.assertRaises(CommandArgumentError) as commandError: Hyde.handle_sub_command(missing_args) self.assertEqual(HydeMainTests.DRAFT_MISSING_ARGUMENT, commandError.exception.msg)
def test_main_draft_page_no_title(self): missing_args = MockArguments(sub_command='draft', draft_item_type='page') with self.assertRaises(CommandArgumentError) as commandError: Hyde.handle_sub_command(missing_args) self.assertEqual(HydeMainTests.MISSING_NAME, commandError.exception.msg)
def test_main_add_unknown_no_title(self): incorrect_args = MockArguments(sub_command="add", add_item_type="unknown") with self.assertRaises(CommandArgumentError) as commandError: Hyde.handle_sub_command(incorrect_args) self.assertEqual(HydeMainTests.ADD_MISSING_ARGUMENT, commandError.exception.msg)
def test_main_empty_args(self): with self.assertRaises(CommandArgumentError) as commandError: arguments = MockArguments() Hyde.handle_sub_command(arguments) self.assertEqual(HydeMainTests.NO_VALID_SUB_COMMAND, commandError.exception.msg)
def test_process_args_with_args(self): input_args = ['add', 'page', "RealPage"] args = Hyde.process_args(input_args) self.assertEqual(args.sub_command, 'add') self.assertEqual(args.add_item_type, 'page') self.assertEqual(args.title, "RealPage")
def test_draft_directory_not_found(self): with self.assertRaises(CommandArgumentError) as err: TestUtility.append_sys_args(['publish', 'A Draft Post To Publish']) Hyde.main() self.assertEqual(err.exception.msg, "The drafts directory _drafts/posts/ does not exist.")
def test_main_add_only(self): missing_args = MockArguments(sub_command="add") with self.assertRaises(CommandArgumentError) as commandError: Hyde.handle_sub_command(missing_args) self.assertEqual(HydeMainTests.ADD_MISSING_ARGUMENT, commandError.exception.msg)
def test_handle_argument_error(self): with self.assertRaises(CommandArgumentError) as err: args = MockArguments(sub_command='unknown') Hyde._handle_publish(args) self.assertEqual(err.exception.msg, "The 'publish' sub-command requires a title.")
def test_main_unknown_sub_command_only(self): incorrect_args = MockArguments(sub_command="unknown") with self.assertRaises(CommandArgumentError) as commandError: Hyde.handle_sub_command(incorrect_args) self.assertEqual(HydeMainTests.NO_VALID_SUB_COMMAND, commandError.exception.msg)
def verify_system_exit(self, exit_code): with self.assertRaises(SystemExit) as cm: monkey = Hyde() monkey.process_args() self.assertEqual(cm.exception.code, exit_code)
def test_main_add_post_no_title(self): missing_args = MockArguments(sub_command='add', add_item_type='post') with self.assertRaises(CommandArgumentError) as commandError: Hyde.handle_sub_command(missing_args) self.assertEqual(HydeMainTests.MISSING_TITLE, commandError.exception.msg)