def test_noargs_wizard(self): temp_stdout(self) temp_stdin(self, "\n") sys.argv[1:] = [] # EOFError is raised as the wizard stands with no input but a newline with self.assertRaises(EOFError): initialize(noargs_action="wizard")
def test_noargs_version(self): temp_stdout(self) sys.argv[1:] = [] # SystemExit is raised with code 0 as the version is displayed then it # exits with self.assertRaises(SystemExit): initialize(noargs_action="version")
def test_noargs_empty_config(self): temp_stdout(self) sys.argv[1:] = [] # SystemExit is raised as the default "config.ini" does not exist self.assertRaises(SystemExit, initialize, globals(), noargs_action="config")
def test_noargs_help(self): temp_stdout(self) sys.argv[1:] = [] # SystemExit is raised with code 0 as the help message is displayed then # it exits self.assertRaises(SystemExit, initialize, globals(), noargs_action="help")
def test_report_bad_function(self): if PYTHON3: temp_stdout(self) sys.argv[1:] = [] initialize(report_func="bad report function") self.assertFalse(hasattr(args, "output")) self.assertFalse(hasattr(args, "title")) self.assertFalse(hasattr(args, "css")) self.assertFalse(hasattr(args, "theme")) self.assertFalse(hasattr(args, "filename"))
def test_report_good_function(self): if PYTHON3: temp_stdout(self) sys.argv[1:] = [] initialize(report_func=lambda: (Title("Test"), )) self.assertEqual(args.output, "pdf") self.assertIs(args.title, None) self.assertIs(args.css, None) self.assertEqual(args.theme, "default") self.assertIs(args.filename, None)
def test_read_config(self): temp_stdout(self) with open(INI, 'w') as f: f.write(INI_CONF) sys.argv[1:] = ["-r", INI] parser.add_argument("-a", "--arg1") parser.add_argument("-b", "--arg2", action="store_true") initialize(add_config=True) self.assertEqual(args.arg1, "test") self.assertTrue(args.arg2) remove(INI)
def test_subparser(self): temp_stdout(self) sys.argv[1:] = ["subtest", "-b", "test"] test = parser.add_subparsers().add_parser("subtest", parents=[parser]) test.add_argument("-b", "--arg2") if PYTHON3: initialize() else: # with Python2, an error occurs with the overwritten sys.argv # and "subtest" is not parsed, hence throwing SystemExit with # code 2 as the subparser selection is missing self.assertRaises(SystemExit, initialize, globals())
def test_validation(self): temp_stdout(self) sys.argv[1:] = [] parser.add_argument("-a", "--arg1") parser.add_argument("-b", "--arg2", action="store_true") initialize() validate(('arg1', " ? is None", "Failed", "test")) self.assertRaises(ValueError, validate, globals(), ('bad/arg', "True")) self.assertRaises(SystemExit, validate, globals(), ('arg1', "bad condition")) validate(('arg1', "bad condition", "message", "default")) globals()['args'] = None validate()
def test_validation(self): temp_stdout(self) sys.argv[1:] = [] parser.add_argument("-a", "--arg1") parser.add_argument("-b", "--arg2", action="store_true") initialize() self.assertIsNone(validate(('arg1', " ? is None", "Failed", "test"))) with self.assertRaises(ValueError): validate(('bad/arg', "True")) with self.assertRaises(ValueError): validate(('os.system("id")', "")) with self.assertRaises(ValueError): validate(('import pty; pty.spawn("/bin/bash")', "")) with self.assertRaises(AttributeError): validate(('arg123', "bad condition")) with self.assertRaises(SystemExit): validate(('arg1', "bad condition")) self.assertIsNone(validate(('arg1', "False", "message", "default"))) globals()['args'] = None self.assertIsNone(validate())