Add .DS_Store Mac OS X specific files to .hgignore.
[Melange.git] / tests / settings_test.py
blobaf68be0c0d436eebedc807598b59f8c02df2ec1e
1 #!/usr/bin/python2.5
3 # Copyright 2008 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Tests for the scripts.settings module.
20 For details on running the tests, see:
21 http://code.google.com/p/soc/wiki/TestingGuidelines#Running_the_smoke_tests
22 """
24 __authors__ = [
25 # alphabetical order by last name, please
26 '"Todd Larsen" <tlarsen@google.com>',
30 import optparse
31 import os
32 import sys
33 import unittest
35 from scripts import settings
38 class SettingsTests(unittest.TestCase):
39 """Python-format settings file tests for the settings.py module.
40 """
42 def setUp(self):
43 self.test_srcdir = os.path.dirname(__file__)
44 self.settings_defaults = {'foo': 1, 'bif': 4}
46 def testMissingPythonSettings(self):
47 """Test that non-existent files work properly without errors.
48 """
49 # non-existent settings file with no defaults produces empty dict
50 self.assertEqual(
51 {},
52 settings.readPythonSettings(settings_dir=self.test_srcdir,
53 settings_file='nonexistent_file'))
55 # non-existent settings file should just pass through the defaults
56 self.assertEqual(
57 self.settings_defaults,
58 settings.readPythonSettings(defaults=self.settings_defaults,
59 settings_dir=self.test_srcdir,
60 settings_file='nonexistent_file'))
62 def testGoodPythonSettings(self):
63 """Test that settings file that is present overwrites defaults.
64 """
65 # foo and bar are overwritten, but not bif (not in the settings file)
66 self.assertEqual(
67 {'foo': 3, 'bar': 3, 'bif': 4},
68 settings.readPythonSettings(defaults=self.settings_defaults,
69 settings_dir=self.test_srcdir,
70 settings_file='good_test_settings'))
72 # but the original defaults will be untouched
73 self.assertEqual({'foo': 1, 'bif': 4}, self.settings_defaults)
75 def testBadPythonSettings(self):
76 """Test that exception is raised when format of settings file is bad.
77 """
78 self.assertRaises(settings.Error, settings.readPythonSettings,
79 settings_dir=self.test_srcdir,
80 settings_file='bad_test_settings')
83 class OptionParserTests(unittest.TestCase):
84 """Tests of custom optparse OptionParser with 'required' parameter support.
85 """
87 def testRequiredPresent(self):
88 """Test required=True raises nothing when value option is present.
89 """
90 parser = settings.OptionParser(
91 option_list=[
92 settings.Option(
93 '-t', '--test', action='store', dest='test', required=True,
94 help='(REQUIRED) test option'),
98 options, args = parser.parse_args([sys.argv[0], '--test', '3'])
100 def testRequiredMissing(self):
101 """Test that Error exception is raised if required option not present.
103 parser = settings.OptionParser(
104 option_list=[
105 settings.Option(
106 '-t', '--test', action='store', dest='test', required=True,
107 help='(REQUIRED) test option'),
111 self.assertRaises(settings.Error, parser.parse_args, [])
113 def testBadRequiredAction(self):
114 """Test that OptionError is raised if action does not support required=True.
117 # store_true is not in Options.TYPED_VALUES, which means option cannot
118 # take a value, so required=True is not permitted.
119 self.assertRaises(optparse.OptionError, settings.Option,
120 '-t', '--test', action='store_true', dest='test', required=True,
121 help='(REQUIRED) test option')