class ParametersParseTest(unittest.TestCase): def setUp(self): self.p = Parameters() def test_parse(self): '''Parameters().parse()''' self.p.parse() self.assertTrue(self.p.parsed) def test_parse_with_help(self): '''Parameters().parse()—with --help''' sys.argv.append('--help') self.addCleanup(functools.partial(sys.argv.remove, '--help')) with self.assertRaises(SystemExit): self.p.parse() def test_parse_only_known(self): '''Parameters().parse(only_known = True)''' self.p.parse(only_known = True) self.assertFalse(self.p.parsed) def test_parse_only_known_with_help(self): '''Parameters().parse(only_known = True)—with --help''' sys.argv.append('--help') self.addCleanup(functools.partial(sys.argv.remove, '--help')) self.p.parse(only_known = True) self.assertFalse(self.p.parsed)
class ParametersReadTest(unittest.TestCase): def setUp(self): self.original_argv0 = sys.argv[0] def _(): sys.argv[0] = self.original_argv0 self.addCleanup(_) sys.argv[0] = 'crumbs' self.p = Parameters() def populateMulti(self, group = False): self.p.add_parameter(options = [ '--multi', ]) def populateEnvironment(self): os.environ['CRUMBS_ENVIRONMENT_ONLY'] = 'environment_only' os.environ['CRUMBS_MULTI'] = 'environment_multi' self.addCleanup(functools.partial(os.unsetenv, 'CRUMBS_ENVIRONMENT_ONLY')) self.addCleanup(functools.partial(os.unsetenv, 'CRUMBS_MULTI')) self.p.add_parameter(options = ( '--environment-only', ), only = ( 'environment', )) def populateArgumentVector(self): sys.argv.extend([ '--argument-only', 'argument_only' ]) sys.argv.extend([ '--multi', 'argument_multi' ]) self.addCleanup(functools.partial(sys.argv.remove, '--argument-only')) self.addCleanup(functools.partial(sys.argv.remove, 'argument_only')) self.addCleanup(functools.partial(sys.argv.remove, '--multi')) self.addCleanup(functools.partial(sys.argv.remove, 'argument_multi')) self.p.add_parameter(options = [ '--argument-only', ], only = ( 'argument', )) def populateConfiguration(self): tmp_fh = tempfile.NamedTemporaryFile(mode = 'w') tmp_fh.write( '[default]\n' 'configuration_only = configuration_only\n' 'multi = configuration_multi\n' '\n' 'type_int = 15\n' ) tmp_fh.seek(0) self.addCleanup(tmp_fh.close) self.p.add_parameter(options = ( '--configuration-only', ), only = ( 'configuration', )) self.p.add_configuration_file(tmp_fh.name) def populateTypes(self): sys.argv.extend([ '--type-int', '15' ]) self.addCleanup(functools.partial(sys.argv.remove, '--type-int')) self.addCleanup(functools.partial(sys.argv.remove, '15')) self.p.add_parameter(options = [ '--type-int', ], type = int, default = 0) def test_read_implicit_default_group(self): '''Parameters()[key]—implicit default group''' self.populateEnvironment() self.p.parse() self.assertEqual('environment_only', self.p['environment_only']) self.assertEqual('environment_only', self.p['default.environment_only']) def test_read_underscores_or_hyphens(self): '''Parameters()[key]—indistinguishable characters: -, _''' self.populateEnvironment() self.p.parse() self.assertEqual('environment_only', self.p['environment-only']) self.assertEqual('environment_only', self.p['environment_only']) def test_read_types(self): '''Parameters()[key]—type cast''' self.populateTypes() self.p.parse() self.assertIsInstance(self.p['type_int'], int) self.assertEqual(15, self.p['type_int']) def test_read_environment(self): '''Parameters()[key]—environment''' self.populateEnvironment() self.populateMulti() self.p.parse() self.assertEqual('environment_only', self.p['environment_only']) self.assertEqual('environment_multi', self.p['multi']) def test_read_configuration(self): '''Parameters()[key]—configuration''' self.populateConfiguration() self.populateMulti() self.p.parse() self.assertEqual('configuration_only', self.p['configuration_only']) self.assertEqual('configuration_multi', self.p['multi']) def test_read_argument(self): '''Parameters()[key]—argument''' self.populateArgumentVector() self.populateMulti() self.p.parse() self.assertEqual('argument_only', self.p['argument_only']) self.assertEqual('argument_multi', self.p['multi']) def test_read_environment_configuration(self): '''Parameters()[key]—environment,configuration''' self.populateEnvironment() self.populateConfiguration() self.populateMulti() self.p.parse() self.assertEqual('environment_only', self.p['environment_only']) self.assertEqual('configuration_only', self.p['configuration_only']) self.assertEqual('configuration_multi', self.p['multi']) def test_read_environment_argument(self): '''Parameters()[key]—environment,argument''' self.populateEnvironment() self.populateArgumentVector() self.populateMulti() self.p.parse() self.assertEqual('environment_only', self.p['environment_only']) self.assertEqual('argument_only', self.p['argument_only']) self.assertEqual('argument_multi', self.p['multi']) def test_read_configuration_argument(self): '''Parameters()[key]—configuration,argument''' self.populateConfiguration() self.populateArgumentVector() self.populateMulti() self.p.parse() self.assertEqual('configuration_only', self.p['configuration_only']) self.assertEqual('argument_only', self.p['argument_only']) self.assertEqual('argument_multi', self.p['multi']) def test_read_environment_configuration_argument(self): '''Parameters()[key]—environment,configuration,argument''' self.populateEnvironment() self.populateConfiguration() self.populateArgumentVector() self.populateMulti() self.p.parse() self.assertEqual('environment_only', self.p['environment_only']) self.assertEqual('configuration_only', self.p['configuration_only']) self.assertEqual('argument_only', self.p['argument_only']) self.assertEqual('argument_multi', self.p['multi'])
group = 'pmort', metavar = 'DIR', default = os.path.join(os.path.sep, 'var', 'spool', sys.argv[0]), help = \ 'Specifies the output directory that the collection output ' \ 'is collected into.' ) PARAMETERS.add_parameter( options = [ '--cache-directory' ], group = 'pmort', metavar = 'DIR', default = os.path.join(os.path.sep, 'var', 'cache', sys.argv[0]), help = \ 'Specifies the output directory for any learned ' \ 'information. Default: %(default)s' ) PARAMETERS.add_parameter( options = [ '--configuration-file-path' ], group = 'logging', metavar = 'FILE', default = os.path.join(CONFIGURATION_DIRECTORY, 'logging.ini'), help = \ 'Specifies the file path for the logging configuration file. ' \ ) PARAMETERS.parse(only_known = True) PARAMETERS.add_configuration_file(PARAMETERS['pmort.configuration_file_path'])
class ParametersReadTest(unittest.TestCase): def setUp(self): self.original_argv0 = sys.argv[0] def _(): sys.argv[0] = self.original_argv0 self.addCleanup(_) sys.argv[0] = 'crumbs' self.p = Parameters() def populateMulti(self, group=False): self.p.add_parameter(options=[ '--multi', ]) def populateEnvironment(self): os.environ['CRUMBS_ENVIRONMENT_ONLY'] = 'environment_only' os.environ['CRUMBS_MULTI'] = 'environment_multi' self.addCleanup( functools.partial(os.unsetenv, 'CRUMBS_ENVIRONMENT_ONLY')) self.addCleanup(functools.partial(os.unsetenv, 'CRUMBS_MULTI')) self.p.add_parameter(options=('--environment-only', ), only=('environment', )) def populateArgumentVector(self): sys.argv.extend(['--argument-only', 'argument_only']) sys.argv.extend(['--multi', 'argument_multi']) self.addCleanup(functools.partial(sys.argv.remove, '--argument-only')) self.addCleanup(functools.partial(sys.argv.remove, 'argument_only')) self.addCleanup(functools.partial(sys.argv.remove, '--multi')) self.addCleanup(functools.partial(sys.argv.remove, 'argument_multi')) self.p.add_parameter(options=[ '--argument-only', ], only=('argument', )) def populateConfiguration(self): tmp_fh = tempfile.NamedTemporaryFile(mode='w') tmp_fh.write('[default]\n' 'configuration_only = configuration_only\n' 'multi = configuration_multi\n' '\n' 'type_int = 15\n') tmp_fh.seek(0) self.addCleanup(tmp_fh.close) self.p.add_parameter(options=('--configuration-only', ), only=('configuration', )) self.p.add_configuration_file(tmp_fh.name) def populateTypes(self): sys.argv.extend(['--type-int', '15']) self.addCleanup(functools.partial(sys.argv.remove, '--type-int')) self.addCleanup(functools.partial(sys.argv.remove, '15')) self.p.add_parameter(options=[ '--type-int', ], type=int, default=0) def test_read_implicit_default_group(self): '''Parameters()[key]—implicit default group''' self.populateEnvironment() self.p.parse() self.assertEqual('environment_only', self.p['environment_only']) self.assertEqual('environment_only', self.p['default.environment_only']) def test_read_underscores_or_hyphens(self): '''Parameters()[key]—indistinguishable characters: -, _''' self.populateEnvironment() self.p.parse() self.assertEqual('environment_only', self.p['environment-only']) self.assertEqual('environment_only', self.p['environment_only']) def test_read_types(self): '''Parameters()[key]—type cast''' self.populateTypes() self.p.parse() self.assertIsInstance(self.p['type_int'], int) self.assertEqual(15, self.p['type_int']) def test_read_custom_environment_prefix(self): '''Parameters()[key]—environment_prefix=custom''' os.environ['CUSTOM_CUSTOM_ENVIRONMENT'] = 'custom_environment' self.addCleanup( functools.partial(os.unsetenv, 'CUSTOM_CUSTOM_ENVIRONMENT')) self.p.add_parameter(options=('--custom-environment', ), only=('environment', ), environment_prefix='custom') self.p.parse() self.assertEqual('custom_environment', self.p['custom_environment']) def test_read_empty_environment_prefix(self): '''Parameters()[key]—environment_prefix=None''' os.environ['CUSTOM_ENVIRONMENT'] = 'custom_environment' self.addCleanup(functools.partial(os.unsetenv, 'CUSTOM_ENVIRONMENT')) self.p.add_parameter(options=('--custom-environment', ), only=('environment', ), environment_prefix=None) self.p.parse() self.assertEqual('custom_environment', self.p['custom_environment']) def test_read_environment_with_expansion(self): '''Parameters()[key]—with expansion''' os.environ['FOO'] = 'foo' self.addCleanup(functools.partial(os.unsetenv, 'FOO')) os.environ['EXPAND'] = '${FOO}' self.addCleanup(functools.partial(os.unsetenv, 'EXPAND')) self.p.add_parameter(options=('--expand', ), only=('environment', ), environment_prefix=None) self.p.parse() self.assertEqual('foo', self.p['expand']) def test_read_environment(self): '''Parameters()[key]—environment''' self.populateEnvironment() self.populateMulti() self.p.parse() self.assertEqual('environment_only', self.p['environment_only']) self.assertEqual('environment_multi', self.p['multi']) def test_read_configuration(self): '''Parameters()[key]—configuration''' self.populateConfiguration() self.populateMulti() self.p.parse() self.assertEqual('configuration_only', self.p['configuration_only']) self.assertEqual('configuration_multi', self.p['multi']) def test_read_argument(self): '''Parameters()[key]—argument''' self.populateArgumentVector() self.populateMulti() self.p.parse() self.assertEqual('argument_only', self.p['argument_only']) self.assertEqual('argument_multi', self.p['multi']) def test_read_environment_configuration(self): '''Parameters()[key]—environment,configuration''' self.populateEnvironment() self.populateConfiguration() self.populateMulti() self.p.parse() self.assertEqual('environment_only', self.p['environment_only']) self.assertEqual('configuration_only', self.p['configuration_only']) self.assertEqual('configuration_multi', self.p['multi']) def test_read_environment_argument(self): '''Parameters()[key]—environment,argument''' self.populateEnvironment() self.populateArgumentVector() self.populateMulti() self.p.parse() self.assertEqual('environment_only', self.p['environment_only']) self.assertEqual('argument_only', self.p['argument_only']) self.assertEqual('argument_multi', self.p['multi']) def test_read_configuration_argument(self): '''Parameters()[key]—configuration,argument''' self.populateConfiguration() self.populateArgumentVector() self.populateMulti() self.p.parse() self.assertEqual('configuration_only', self.p['configuration_only']) self.assertEqual('argument_only', self.p['argument_only']) self.assertEqual('argument_multi', self.p['multi']) def test_read_environment_configuration_argument(self): '''Parameters()[key]—environment,configuration,argument''' self.populateEnvironment() self.populateConfiguration() self.populateArgumentVector() self.populateMulti() self.p.parse() self.assertEqual('environment_only', self.p['environment_only']) self.assertEqual('configuration_only', self.p['configuration_only']) self.assertEqual('argument_only', self.p['argument_only']) self.assertEqual('argument_multi', self.p['multi'])