def _test(self, args, expect): """Test the command line args and compare the prefix of the output.""" with self.assertRaises(SystemExit): with mock_stdout() as stdout: khard.main(args) text = stdout.getvalue() self.assertTrue(text.startswith(expect))
def test_edit_source_file_without_modifications(self): with mock.patch('subprocess.Popen') as popen: # just hide stdout with mock.patch('sys.stdout'): khard.main(["source", "uid1"]) popen.assert_called_once_with(['editor', 'test/fixture/foo.abook/contact1.vcf'])
def test_simple_phone_without_options(self): with mock_stdout() as stdout: khard.main(['phone']) text = [line.strip() for line in stdout.getvalue().splitlines()] expect = ["Name Type Phone", "second contact voice 0123456789"] self.assertListEqual(text, expect)
def test_simple_email_without_options(self): with mock_stdout() as stdout: khard.main(['email']) text = [line.strip() for line in stdout.getvalue().splitlines()] expect = ["Name Type E-Mail", "second contact home [email protected]"] self.assertListEqual(text, expect)
def test_simple_details_without_options(self): with mock_stdout() as stdout: khard.main(['details', 'uid1']) text = stdout.getvalue() # Currently the FN field is not shown with "details". self.assertIn('Address book: foo', text) self.assertIn('UID: testuid1', text)
def test_edit_source_file_without_modifications(self): with mock.patch('subprocess.Popen') as popen: # just hide stdout with mock.patch('sys.stdout'): khard.main(["source", "uid1"]) popen.assert_called_once_with( ['editor', 'test/fixture/foo.abook/contact1.vcf'])
def test_simple_bdays_without_options(self): with mock_stdout() as stdout: khard.main(['birthdays']) text = [line.strip() for line in stdout.getvalue().splitlines()] expect = ["Name Birthday", "text birthday circa 1800", "second contact 01/20/18"] self.assertListEqual(text, expect)
def test_simple_file_without_options(self): with mock_stdout() as stdout: khard.main(['filename']) text = [line.strip() for line in stdout.getvalue().splitlines()] expect = ["test/fixture/test.abook/contact1.vcf", "test/fixture/test.abook/text-bday.vcf", "test/fixture/test.abook/contact2.vcf"] self.assertListEqual(text, expect)
def test_simple_remove_with_force_option(self): # just hide stdout with mock.patch('sys.stdout'): # Without the --force this asks for confirmation. khard.main(['remove', '--force', '-a', 'abook1', 'testuid1']) results = list(self.abook2.glob('*.vcf')) self.assertFalse(self.contact.exists()) self.assertEqual(len(results), 0)
def test_simple_edit_without_modification(self): with mock.patch('subprocess.Popen') as popen: # just hide stdout with mock.patch('sys.stdout'): khard.main(["modify", "uid1"]) # The editor is called with a temp file so how to we check this more # precisely? popen.assert_called_once()
def test_simple_file_without_options(self): with mock_stdout() as stdout: khard.main(['filename']) text = [line.strip() for line in stdout.getvalue().splitlines()] expect = ["test/fixture/foo.abook/contact1.vcf", "test/fixture/foo.abook/text-bday.vcf", "test/fixture/foo.abook/contact2.vcf"] self.assertListEqual(text, expect)
def test_simple_copy(self): # just hide stdout with mock.patch('sys.stdout'): khard.main(['copy', '-a', 'abook1', '-A', 'abook2', 'testuid1']) # The contact is copied to a filename based on a new uid. results = list(self.abook2.glob('*.vcf')) self.assertTrue(self.contact.exists()) self.assertEqual(len(results), 1)
def test_simple_move(self): # just hide stdout with mock.patch('sys.stdout'): khard.main(['move', '-a', 'abook1', '-A', 'abook2', 'testuid1']) # The contact is moved to a filename based on the uid. target = self.abook2 / 'testuid1.vcf' # We currently only assert that the target file exists, nothing about # its contents. self.assertFalse(self.contact.exists()) self.assertTrue(target.exists())
def test_list_bug_195(self): with with_vcards(['test/fixture/vcards/tel-value-uri.vcf']): with mock_stdout() as stdout: khard.main(['list']) text = [line.strip() for line in stdout.getvalue().splitlines()] expect = [ "Address book: tmp", "Index Name Phone E-Mail UID", "1 bug 195 cell: 67545678 b"] self.assertListEqual(text, expect)
def test_ls_fields_like_email(self): with mock_stdout() as stdout: khard.main(['ls', '-p', '-F', 'emails.home.0,name']) text = stdout.getvalue().splitlines() expected = [ "[email protected]\tsecond contact", "\ttext birthday", "\tthird contact", ] self.assertListEqual(text, expected)
def test_new_contact_with_simple_user_input(self): old = len(list(self.abook1.glob('*.vcf'))) # Mock user input on stdin (yaml format). with mock.patch('sys.stdin.isatty', return_value=False): with mock.patch('sys.stdin.read', return_value='First name: foo\nLast name: bar'): # just hide stdout with mock.patch('sys.stdout'): khard.main(['new', '-a', 'abook1']) new = len(list(self.abook1.glob('*.vcf'))) self.assertEqual(new, old + 1)
def test_list_bug_243_part_2(self): """Search for a category with the email command""" with with_vcards(['test/fixture/vcards/category.vcf']): with mock_stdout() as stdout: khard.main(['email', 'bar']) text = [line.strip() for line in stdout.getvalue().splitlines()] expect = [ "Name Type E-Mail", "contact with category internet [email protected]", ] self.assertListEqual(text, expect)
def test_list_bug_249(self): with with_vcards(['test/fixture/vcards/issue249.vcf']): with mock_stdout() as stdout: # If all spaces are removed this should match "Foo Bar" khard.main(['list', 'oba']) text = [line.strip() for line in stdout.getvalue().splitlines()] expect = [ 'Address book: tmp', 'Index Name Phone Email Uid', '1 Foo Bar i' ] self.assertListEqual(text, expect)
def test_simple_show_with_yaml_format(self): with mock_stdout() as stdout: khard.main(["show", "--format=yaml", "uid1"]) # This implicitly tests if the output is valid yaml. yaml = YAML(typ="base").load(stdout.getvalue()) # Just test some keys. self.assertIn('Address', yaml) self.assertIn('Birthday', yaml) self.assertIn('Email', yaml) self.assertIn('First name', yaml) self.assertIn('Last name', yaml) self.assertIn('Nickname', yaml)
def test_simple_export_without_options(self): with mock_stdout() as stdout: khard.main(["export", "uid1"]) # This implicitly tests if the output is valid yaml. yaml = YAML(typ="base").load(stdout.getvalue()) # Just test some keys. self.assertIn('Address', yaml) self.assertIn('Birthday', yaml) self.assertIn('Email', yaml) self.assertIn('First name', yaml) self.assertIn('Last name', yaml) self.assertIn('Nickname', yaml)
def test_display_post_address(self): with mock_stdout() as stdout: with with_vcards(["test/fixture/vcards/post.vcf"]): khard.main(['postaddress']) text = [line.rstrip() for line in stdout.getvalue().splitlines()] expected = [ 'Name Type Post address', 'With post address home Main Street 1', ' PostBox Ext', ' 00000 The City', ' SomeState, HomeCountry' ] self.assertListEqual(expected, text)
def test_list_bug_243_part_1(self): """Search for a category with the ls command""" with with_vcards(['test/fixture/vcards/category.vcf']): with mock_stdout() as stdout: khard.main(['list', 'bar']) text = [line.strip() for line in stdout.getvalue().splitlines()] expect = [ "Address book: tmp", "Index Name Phone " "Email Uid", "1 contact with category " "internet: [email protected] c", ] self.assertListEqual(text, expect)
def test_simple_ls_without_options(self): with mock_stdout() as stdout: khard.main(['list']) text = [l.strip() for l in stdout.getvalue().splitlines()] expected = [ "Address book: foo", "Index Name Phone " "E-Mail UID", "1 second contact voice: 0123456789 " "home: [email protected] testuid1", "2 text birthday " " testuid3", "3 third contact " " testuid2"] self.assertListEqual(text, expected)
def test_case_of_search_terms_does_not_matter(self): with mock_stdout() as stdout1: khard.main(['list', 'second', 'contact']) with mock_stdout() as stdout2: khard.main(['list', 'SECOND', 'CONTACT']) text1 = [l.strip() for l in stdout1.getvalue().splitlines()] text2 = [l.strip() for l in stdout2.getvalue().splitlines()] expected = [ "Address book: foo", "Index Name Phone " "Email Uid", "1 second contact voice: 0123456789 " "home: [email protected] testuid1" ] self.assertListEqual(text1, expected) self.assertListEqual(text2, expected)
def test_regex_special_chars_are_not_special(self): with self.assertRaises(SystemExit): with mock_stdout() as stdout: khard.main(['list', 'uid.']) self.assertEqual(stdout.getvalue(), "Found no contacts\n")
def test_parsable_bdays(self): with mock_stdout() as stdout: khard.main(['birthdays', '--parsable']) text = stdout.getvalue().splitlines() expect = ["circa 1800\ttext birthday", "2018.01.20\tsecond contact"] self.assertListEqual(text, expect)
def test_simple_abooks_without_options(self): with mock_stdout() as stdout: khard.main(['addressbooks']) text = stdout.getvalue().strip() expect = "foo" self.assertEqual(text, expect)
def run_main(*args): """Run the khard.main() method with mocked stdout""" with mock_stream() as stdout: khard.main(args) return stdout
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from khard.khard import main if __name__ == '__main__': main()