Back to index

python-cliapp  1.20120630
Public Member Functions | Public Attributes
cliapp.settings_tests.SettingsTests Class Reference

List of all members.

Public Member Functions

def setUp
def test_has_progname
def test_sets_progname
def test_has_version
def test_sets_usage_from_func
def test_adds_default_options_and_settings
def test_iterates_over_canonical_settings_names
def test_keys_returns_canonical_names
def test_parses_options
def test_does_not_have_foo_setting_by_default
def test_raises_keyerror_for_getting_unknown_setting
def test_raises_keyerror_for_setting_unknown_setting
def test_adds_string_setting
def test_adds_string_list_setting
def test_string_list_is_empty_list_by_default
def test_string_list_parses_one_item
def test_string_list_parses_two_items
def test_string_list_uses_nonempty_default_if_given
def test_string_list_uses_ignores_default_if_user_provides_values
def test_adds_choice_setting
def test_choice_defaults_to_first_one
def test_choice_accepts_any_valid_value
def test_choice_raises_error_for_unacceptable_value
def test_adds_boolean_setting
def test_boolean_setting_is_false_by_default
def test_sets_boolean_setting_to_true_for_many_true_values
def test_sets_boolean_setting_to_false_for_many_false_values
def test_sets_boolean_to_true_from_config_file
def test_sets_boolean_to_false_from_config_file
def test_adds_bytesize_setting
def test_parses_bytesize_option
def test_adds_integer_setting
def test_parses_integer_option
def test_has_list_of_default_config_files
def test_listconfs_returns_empty_list_for_nonexistent_directory
def test_listconfs_lists_config_files_only
def test_listconfs_sorts_names_in_C_locale
def test_has_config_files_attribute
def test_has_config_files_list_can_be_changed
def test_loads_config_files
def test_loads_string_list_from_config_files
def test_handles_defaults_with_config_files
def test_handles_overridden_defaults_with_config_files
def test_handles_values_from_config_files_overridden_on_command_line
def test_load_configs_ignore_errors_opening_a_file
def test_adds_config_file_with_dash_dash_config
def test_ignores_default_configs
def test_ignores_then_adds_configs_works
def test_require_raises_error_if_string_unset
def test_require_is_ok_with_set_string
def test_require_is_ok_with_default_string
def test_require_raises_error_if_string_list_unset
def test_require_is_ok_with_set_string_list
def test_require_is_ok_with_default_string_list
def test_require_is_ok_with_unset_choice
def test_require_is_ok_with_unset_boolean
def test_require_is_ok_with_unset_bytesize
def test_require_is_ok_with_unset_integer
def test_exports_configparser_with_settings
def test_exports_all_config_sections_via_as_cp

Public Attributes

 settings

Detailed Description

Definition at line 26 of file settings_tests.py.


Member Function Documentation

Definition at line 28 of file settings_tests.py.

00028 
00029     def setUp(self):
00030         self.settings = cliapp.Settings('appname', '1.0')
        

Definition at line 132 of file settings_tests.py.

00132 
00133     def test_adds_boolean_setting(self):
00134         self.settings.boolean(['foo'], 'foo help')
00135         self.assert_('foo' in self.settings)
        

Definition at line 174 of file settings_tests.py.

00174 
00175     def test_adds_bytesize_setting(self):
00176         self.settings.bytesize(['foo'], 'foo help')
00177         self.assert_('foo' in self.settings)

Definition at line 110 of file settings_tests.py.

00110 
00111     def test_adds_choice_setting(self):
00112         self.settings.choice(['foo'], ['foo', 'bar'], 'foo help')
00113         self.assert_('foo' in self.settings)

Definition at line 336 of file settings_tests.py.

00336 
00337     def test_adds_config_file_with_dash_dash_config(self):
00338         self.settings.parse_args(['--config=foo.conf'])
00339         self.assertEqual(self.settings.config_files,
00340                          self.settings._default_config_files + ['foo.conf'])

Definition at line 46 of file settings_tests.py.

00046 
00047     def test_adds_default_options_and_settings(self):
00048         self.assert_('output' in self.settings)
00049         self.assert_('log' in self.settings)
00050         self.assert_('log-level' in self.settings)

Definition at line 211 of file settings_tests.py.

00211 
00212     def test_adds_integer_setting(self):
00213         self.settings.integer(['foo'], 'foo help')
00214         self.assert_('foo' in self.settings)

Definition at line 81 of file settings_tests.py.

00081 
00082     def test_adds_string_list_setting(self):
00083         self.settings.string_list(['foo'], 'foo help')
00084         self.assert_('foo' in self.settings)

Definition at line 77 of file settings_tests.py.

00077 
00078     def test_adds_string_setting(self):
00079         self.settings.string(['foo'], 'foo help')
00080         self.assert_('foo' in self.settings)

Definition at line 136 of file settings_tests.py.

00136 
00137     def test_boolean_setting_is_false_by_default(self):
00138         self.settings.boolean(['foo'], 'foo help')
00139         self.assertFalse(self.settings['foo'])
        

Definition at line 119 of file settings_tests.py.

00119 
00120     def test_choice_accepts_any_valid_value(self):
00121         self.settings.choice(['foo'], ['foo', 'bar'], 'foo help')
00122         self.settings.parse_args(['--foo=foo'])
00123         self.assertEqual(self.settings['foo'], 'foo')
00124         self.settings.parse_args(['--foo=bar'])
00125         self.assertEqual(self.settings['foo'], 'bar')

Definition at line 114 of file settings_tests.py.

00114 
00115     def test_choice_defaults_to_first_one(self):
00116         self.settings.choice(['foo'], ['foo', 'bar'], 'foo help')
00117         self.settings.parse_args([])
00118         self.assertEqual(self.settings['foo'], 'foo')

Definition at line 126 of file settings_tests.py.

00126 
00127     def test_choice_raises_error_for_unacceptable_value(self):
00128         self.settings.choice(['foo'], ['foo', 'bar'], 'foo help')
00129         self.assertRaises(SystemExit,
00130                           self.settings.parse_args, ['--foo=xyzzy'],
00131                           suppress_errors=True)

Definition at line 68 of file settings_tests.py.

00068 
00069     def test_does_not_have_foo_setting_by_default(self):
00070         self.assertFalse('foo' in self.settings)

Definition at line 399 of file settings_tests.py.

00399 
00400     def test_exports_all_config_sections_via_as_cp(self):
00401     
00402         def mock_open(filename, mode=None):
00403             return StringIO.StringIO('''\
00404 [config]
00405 foo = yeehaa
00406 
00407 [other]
00408 bar = dodo
00409 ''')
00410     
00411         self.settings.string(['foo'], 'foo help', default='foo')
00412         self.settings.config_files = ['whatever.conf']
00413         self.settings.load_configs(open=mock_open)
00414         cp = self.settings.as_cp()
00415 
00416         self.assertEqual(sorted(cp.sections()), ['config', 'other'])
00417         self.assertEqual(cp.get('config', 'foo'), 'yeehaa')
00418         self.assertEqual(cp.options('other'), ['bar'])
00419         self.assertEqual(cp.get('other', 'bar'), 'dodo')
00420 

Definition at line 392 of file settings_tests.py.

00392 
00393     def test_exports_configparser_with_settings(self):
00394         self.settings.integer(['foo'], 'foo help', default=1)
00395         self.settings.string(['bar'], 'bar help', default='yo')
00396         cp = self.settings.as_cp()
00397         self.assertEqual(cp.get('config', 'foo'), '1')
00398         self.assertEqual(cp.get('config', 'bar'), 'yo')

Definition at line 282 of file settings_tests.py.

00282 
00283     def test_handles_defaults_with_config_files(self):
00284     
00285         def mock_open(filename, mode=None):
00286             return StringIO.StringIO('''\
00287 [config]
00288 ''')
00289     
00290         self.settings.string(['foo'], 'foo help', default='foo')
00291         self.settings.string_list(['bar'], 'bar help', default=['bar'])
00292         self.settings.config_files = ['whatever.conf']
00293         self.settings.load_configs(open=mock_open)
00294         self.assertEqual(self.settings['foo'], 'foo')
00295         self.assertEqual(self.settings['bar'], ['bar'])

Definition at line 296 of file settings_tests.py.

00296 
00297     def test_handles_overridden_defaults_with_config_files(self):
00298     
00299         def mock_open(filename, mode=None):
00300             return StringIO.StringIO('''\
00301 [config]
00302 foo = yeehaa
00303 bar = ping, pong
00304 ''')
00305     
00306         self.settings.string(['foo'], 'foo help', default='foo')
00307         self.settings.string_list(['bar'], 'bar help', default=['bar'])
00308         self.settings.config_files = ['whatever.conf']
00309         self.settings.load_configs(open=mock_open)
00310         self.assertEqual(self.settings['foo'], 'yeehaa')
00311         self.assertEqual(self.settings['bar'], ['ping', 'pong'])

Definition at line 312 of file settings_tests.py.

00312 
00313     def test_handles_values_from_config_files_overridden_on_command_line(self):
00314     
00315         def mock_open(filename, mode=None):
00316             return StringIO.StringIO('''\
00317 [config]
00318 foo = yeehaa
00319 bar = ping, pong
00320 ''')
00321     
00322         self.settings.string(['foo'], 'foo help', default='foo')
00323         self.settings.string_list(['bar'], 'bar help', default=['bar'])
00324         self.settings.config_files = ['whatever.conf']
00325         self.settings.load_configs(open=mock_open)
00326         self.settings.parse_args(['--foo=red', '--bar=blue', '--bar=white'])
00327         self.assertEqual(self.settings['foo'], 'red')
00328         self.assertEqual(self.settings['bar'], ['blue', 'white'])

Definition at line 244 of file settings_tests.py.

00244 
00245     def test_has_config_files_attribute(self):
00246         self.assertEqual(self.settings.config_files,
00247                          self.settings._default_config_files)

Definition at line 248 of file settings_tests.py.

00248 
00249     def test_has_config_files_list_can_be_changed(self):
00250         self.settings.config_files += ['./foo']
00251         self.assertEqual(self.settings.config_files,
00252                          self.settings._default_config_files + ['./foo'])

Definition at line 224 of file settings_tests.py.

00224 
00225     def test_has_list_of_default_config_files(self):
00226         defaults = self.settings._default_config_files
00227         self.assert_(isinstance(defaults, list))
00228         self.assert_(len(defaults) > 0)

Definition at line 31 of file settings_tests.py.

00031 
00032     def test_has_progname(self):
00033         self.assertEqual(self.settings.progname, 'appname')
        

Definition at line 38 of file settings_tests.py.

00038 
00039     def test_has_version(self):
00040         self.assertEqual(self.settings.version, '1.0')

Definition at line 341 of file settings_tests.py.

00341 
00342     def test_ignores_default_configs(self):
00343         self.settings.parse_args(['--no-default-configs'])
00344         self.assertEqual(self.settings.config_files, [])

Definition at line 345 of file settings_tests.py.

00345 
00346     def test_ignores_then_adds_configs_works(self):
00347         self.settings.parse_args(['--no-default-configs', '--config=foo.conf'])
00348         self.assertEqual(self.settings.config_files, ['foo.conf'])

Definition at line 51 of file settings_tests.py.

00051 
00052     def test_iterates_over_canonical_settings_names(self):
00053         known = ['output', 'log', 'log-level']
00054         self.assertEqual(sorted(x for x in self.settings if x in known),
00055                          sorted(known))

Definition at line 56 of file settings_tests.py.

00056 
00057     def test_keys_returns_canonical_names(self):
00058         known = ['output', 'log', 'log-level']
00059         self.assertEqual(sorted(x for x in self.settings.keys() if x in known),
00060                          sorted(known))

Definition at line 232 of file settings_tests.py.

00232 
00233     def test_listconfs_lists_config_files_only(self):
00234         def mock_listdir(dirname):
00235             return ['foo.conf', 'foo.notconf']
00236         names = self.settings._listconfs('.', listdir=mock_listdir)
00237         self.assertEqual(names, ['./foo.conf'])

Definition at line 229 of file settings_tests.py.

00229 
00230     def test_listconfs_returns_empty_list_for_nonexistent_directory(self):
00231         self.assertEqual(self.settings._listconfs('notexist'), [])

Definition at line 238 of file settings_tests.py.

00238 
00239     def test_listconfs_sorts_names_in_C_locale(self):
00240         def mock_listdir(dirname):
00241             return ['foo.conf', 'bar.conf']
00242         names = self.settings._listconfs('.', listdir=mock_listdir)
00243         self.assertEqual(names, ['./bar.conf', './foo.conf'])

Definition at line 329 of file settings_tests.py.

00329 
00330     def test_load_configs_ignore_errors_opening_a_file(self):
00331         
00332         def mock_open(filename, mode=None):
00333             raise IOError()
00334             
00335         self.assertEqual(self.settings.load_configs(open=mock_open), None)

Definition at line 253 of file settings_tests.py.

00253 
00254     def test_loads_config_files(self):
00255     
00256         def mock_open(filename, mode=None):
00257             return StringIO.StringIO('''\
00258 [config]
00259 foo = yeehaa
00260 ''')
00261     
00262         self.settings.string(['foo'], 'foo help')
00263         self.settings.config_files = ['whatever.conf']
00264         self.settings.load_configs(open=mock_open)
00265         self.assertEqual(self.settings['foo'], 'yeehaa')

Definition at line 266 of file settings_tests.py.

00266 
00267     def test_loads_string_list_from_config_files(self):
00268     
00269         def mock_open(filename, mode=None):
00270             return StringIO.StringIO('''\
00271 [config]
00272 foo = yeehaa
00273 bar = ping, pong
00274 ''')
00275     
00276         self.settings.string_list(['foo'], 'foo help')
00277         self.settings.string_list(['bar'], 'bar help')
00278         self.settings.config_files = ['whatever.conf']
00279         self.settings.load_configs(open=mock_open)
00280         self.assertEqual(self.settings['foo'], ['yeehaa'])
00281         self.assertEqual(self.settings['bar'], ['ping', 'pong'])

Definition at line 178 of file settings_tests.py.

00178 
00179     def test_parses_bytesize_option(self):
00180         self.settings.bytesize(['foo'], 'foo help')
00181 
00182         self.settings.parse_args(args=['--foo=xyzzy'])
00183         self.assertEqual(self.settings['foo'], 0)
00184 
00185         self.settings.parse_args(args=['--foo=123'])
00186         self.assertEqual(self.settings['foo'], 123)
00187 
00188         self.settings.parse_args(args=['--foo=123k'])
00189         self.assertEqual(self.settings['foo'], 123 * 1000)
00190 
00191         self.settings.parse_args(args=['--foo=123m'])
00192         self.assertEqual(self.settings['foo'], 123 * 1000**2)
00193 
00194         self.settings.parse_args(args=['--foo=123g'])
00195         self.assertEqual(self.settings['foo'], 123 * 1000**3)
00196 
00197         self.settings.parse_args(args=['--foo=123t'])
00198         self.assertEqual(self.settings['foo'], 123 * 1000**4)
00199 
00200         self.settings.parse_args(args=['--foo=123kib'])
00201         self.assertEqual(self.settings['foo'], 123 * 1024)
00202 
00203         self.settings.parse_args(args=['--foo=123mib'])
00204         self.assertEqual(self.settings['foo'], 123 * 1024**2)
00205 
00206         self.settings.parse_args(args=['--foo=123gib'])
00207         self.assertEqual(self.settings['foo'], 123 * 1024**3)
00208 
00209         self.settings.parse_args(args=['--foo=123tib'])
00210         self.assertEqual(self.settings['foo'], 123 * 1024**4)
        

Definition at line 215 of file settings_tests.py.

00215 
00216     def test_parses_integer_option(self):
00217         self.settings.integer(['foo'], 'foo help', default=123)
00218 
00219         self.settings.parse_args(args=[])
00220         self.assertEqual(self.settings['foo'], 123)
00221 
00222         self.settings.parse_args(args=['--foo=123'])
00223         self.assertEqual(self.settings['foo'], 123)

Definition at line 61 of file settings_tests.py.

00061 
00062     def test_parses_options(self):
00063         self.settings.string(['foo'], 'foo help', group='foo')
00064         self.settings.boolean(['bar'], 'bar help')
00065         self.settings.parse_args(['--foo=foovalue', '--bar'])
00066         self.assertEqual(self.settings['foo'], 'foovalue')
00067         self.assertEqual(self.settings['bar'], True)

Definition at line 71 of file settings_tests.py.

00071 
00072     def test_raises_keyerror_for_getting_unknown_setting(self):
00073         self.assertRaises(KeyError, self.settings.__getitem__, 'foo')

Definition at line 74 of file settings_tests.py.

00074 
00075     def test_raises_keyerror_for_setting_unknown_setting(self):
00076         self.assertRaises(KeyError, self.settings.__setitem__, 'foo', 'bar')

Definition at line 359 of file settings_tests.py.

00359 
00360     def test_require_is_ok_with_default_string(self):
00361         self.settings.string(['foo'], 'foo help', default='foo default')
00362         self.assertEqual(self.settings.require('foo'), None)

Definition at line 372 of file settings_tests.py.

00372 
00373     def test_require_is_ok_with_default_string_list(self):
00374         self.settings.string(['foo'], 'foo help', default=['foo'])
00375         self.assertEqual(self.settings.require('foo'), None)

Definition at line 354 of file settings_tests.py.

00354 
00355     def test_require_is_ok_with_set_string(self):
00356         self.settings.string(['foo'], 'foo help', default=None)
00357         self.settings['foo'] = 'bar'
00358         self.assertEqual(self.settings.require('foo'), None)

Definition at line 367 of file settings_tests.py.

00367 
00368     def test_require_is_ok_with_set_string_list(self):
00369         self.settings.string(['foo'], 'foo help')
00370         self.settings['foo'] = ['foo', 'bar']
00371         self.assertEqual(self.settings.require('foo'), None)

Definition at line 380 of file settings_tests.py.

00380 
00381     def test_require_is_ok_with_unset_boolean(self):
00382         self.settings.boolean(['foo'], 'foo help')
00383         self.assertEqual(self.settings.require('foo'), None)

Definition at line 384 of file settings_tests.py.

00384 
00385     def test_require_is_ok_with_unset_bytesize(self):
00386         self.settings.bytesize(['foo'], 'foo help')
00387         self.assertEqual(self.settings.require('foo'), None)

Definition at line 376 of file settings_tests.py.

00376 
00377     def test_require_is_ok_with_unset_choice(self):
00378         self.settings.choice(['foo'], ['foo', 'bar'], 'foo help')
00379         self.assertEqual(self.settings.require('foo'), None)

Definition at line 388 of file settings_tests.py.

00388 
00389     def test_require_is_ok_with_unset_integer(self):
00390         self.settings.integer(['foo'], 'foo help')
00391         self.assertEqual(self.settings.require('foo'), None)

Definition at line 363 of file settings_tests.py.

00363 
00364     def test_require_raises_error_if_string_list_unset(self):
00365         self.settings.string_list(['foo'], 'foo help')
00366         self.assertRaises(cliapp.AppException, self.settings.require, 'foo')

Definition at line 349 of file settings_tests.py.

00349 
00350     def test_require_raises_error_if_string_unset(self):
00351         self.settings.string(['foo'], 'foo help', default=None)
00352         self.assertRaises(cliapp.AppException, self.settings.require, 
00353                           'foo')

Definition at line 147 of file settings_tests.py.

00147 
00148     def test_sets_boolean_setting_to_false_for_many_false_values(self):
00149         self.settings.boolean(['foo'], 'foo help')
00150         self.settings['foo'] = False
00151         self.assertFalse(self.settings['foo'])
00152         self.settings['foo'] = 0
00153         self.assertFalse(self.settings['foo'])
00154         self.settings['foo'] = ()
00155         self.assertFalse(self.settings['foo'])
00156         self.settings['foo'] = []
00157         self.assertFalse(self.settings['foo'])
00158         self.settings['foo'] = ''
00159         self.assertFalse(self.settings['foo'])

Definition at line 140 of file settings_tests.py.

00140 
00141     def test_sets_boolean_setting_to_true_for_many_true_values(self):
00142         self.settings.boolean(['foo'], 'foo help')
00143         self.settings['foo'] = True
00144         self.assert_(self.settings['foo'])
00145         self.settings['foo'] = 1
00146         self.assert_(self.settings['foo'])
        

Definition at line 167 of file settings_tests.py.

00167 
00168     def test_sets_boolean_to_false_from_config_file(self):
00169         def fake_open(filename):
00170             return StringIO.StringIO('[config]\nfoo = False\n')
00171         self.settings.boolean(['foo'], 'foo help')
00172         self.settings.load_configs(open=fake_open)
00173         self.assertEqual(self.settings['foo'], False)
        

Definition at line 160 of file settings_tests.py.

00160 
00161     def test_sets_boolean_to_true_from_config_file(self):
00162         def fake_open(filename):
00163             return StringIO.StringIO('[config]\nfoo = yes\n')
00164         self.settings.boolean(['foo'], 'foo help')
00165         self.settings.load_configs(open=fake_open)
00166         self.assertEqual(self.settings['foo'], True)
        

Definition at line 34 of file settings_tests.py.

00034 
00035     def test_sets_progname(self):
00036         self.settings.progname = 'foo'
00037         self.assertEqual(self.settings.progname, 'foo')
        

Definition at line 41 of file settings_tests.py.

00041 
00042     def test_sets_usage_from_func(self):
00043         s = cliapp.Settings('appname', '1.0', usage=lambda: 'xyzzy')
00044         p = s.build_parser()
00045         self.assert_('xyzzy' in p.usage)

Definition at line 85 of file settings_tests.py.

00085 
00086     def test_string_list_is_empty_list_by_default(self):
00087         self.settings.string_list(['foo'], '')
00088         self.settings.parse_args([])
00089         self.assertEqual(self.settings['foo'], [])

Definition at line 90 of file settings_tests.py.

00090 
00091     def test_string_list_parses_one_item(self):
00092         self.settings.string_list(['foo'], '')
00093         self.settings.parse_args(['--foo=foo'])
00094         self.assertEqual(self.settings['foo'], ['foo'])

Definition at line 95 of file settings_tests.py.

00095 
00096     def test_string_list_parses_two_items(self):
00097         self.settings.string_list(['foo'], '')
00098         self.settings.parse_args(['--foo=foo', '--foo', 'bar'])
00099         self.assertEqual(self.settings['foo'], ['foo', 'bar'])

Definition at line 105 of file settings_tests.py.

00105 
00106     def test_string_list_uses_ignores_default_if_user_provides_values(self):
00107         self.settings.string_list(['foo'], '', default=['bar'])
00108         self.settings.parse_args(['--foo=pink', '--foo=punk'])
00109         self.assertEqual(self.settings['foo'], ['pink', 'punk'])

Definition at line 100 of file settings_tests.py.

00100 
00101     def test_string_list_uses_nonempty_default_if_given(self):
00102         self.settings.string_list(['foo'], '', default=['bar'])
00103         self.settings.parse_args([])
00104         self.assertEqual(self.settings['foo'], ['bar'])


Member Data Documentation

Definition at line 29 of file settings_tests.py.


The documentation for this class was generated from the following file: