|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2008 the Melange authors. |
|
4 # |
|
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 |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
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. |
|
16 |
|
17 """Tests for the scripts.settings module. |
|
18 |
|
19 These tests can be run from the root of the SoC svn working copy with: |
|
20 |
|
21 nosetests trunk/scripts/tests |
|
22 |
|
23 To see specifically which tests are being run, add the -v (--verbosity) option. |
|
24 |
|
25 This test module is explicitly *not* an executable script so that it can use |
|
26 explicit relative references to "reach back" to the module to be tested from |
|
27 the tests/ sub-directory (which do not work if __name__ == '__main__'). |
|
28 """ |
|
29 |
|
30 __authors__ = [ |
|
31 # alphabetical order by last name, please |
|
32 '"Todd Larsen" <tlarsen@google.com>', |
|
33 ] |
|
34 |
|
35 |
|
36 import optparse |
|
37 import os |
|
38 import sys |
|
39 import unittest |
|
40 |
|
41 from .. import settings |
|
42 |
|
43 |
|
44 class SettingsTests(unittest.TestCase): |
|
45 """Python-format settings file tests for the settings.py module. |
|
46 """ |
|
47 |
|
48 def setUp(self): |
|
49 self.test_srcdir = os.path.dirname(__file__) |
|
50 self.settings_defaults = {'foo': 1, 'bif': 4} |
|
51 |
|
52 def testMissingPythonSettings(self): |
|
53 """Test that non-existent files work properly without errors. |
|
54 """ |
|
55 # non-existent settings file with no defaults produces empty dict |
|
56 self.assertEqual( |
|
57 {}, |
|
58 settings.readPythonSettings(settings_dir=self.test_srcdir, |
|
59 settings_file='nonexistent_file')) |
|
60 |
|
61 # non-existent settings file should just pass through the defaults |
|
62 self.assertEqual( |
|
63 self.settings_defaults, |
|
64 settings.readPythonSettings(defaults=self.settings_defaults, |
|
65 settings_dir=self.test_srcdir, |
|
66 settings_file='nonexistent_file')) |
|
67 |
|
68 def testGoodPythonSettings(self): |
|
69 """Test that settings file that is present overwrites defaults. |
|
70 """ |
|
71 # foo and bar are overwritten, but not bif (not in the settings file) |
|
72 self.assertEqual( |
|
73 {'foo': 3, 'bar': 3, 'bif': 4}, |
|
74 settings.readPythonSettings(defaults=self.settings_defaults, |
|
75 settings_dir=self.test_srcdir, |
|
76 settings_file='good_test_settings')) |
|
77 |
|
78 # but the original defaults will be untouched |
|
79 self.assertEqual({'foo': 1, 'bif': 4}, self.settings_defaults) |
|
80 |
|
81 def testBadPythonSettings(self): |
|
82 """Test that exception is raised when format of settings file is bad. |
|
83 """ |
|
84 self.assertRaises(settings.Error, settings.readPythonSettings, |
|
85 settings_dir=self.test_srcdir, |
|
86 settings_file='bad_test_settings') |
|
87 |
|
88 |
|
89 class OptionParserTests(unittest.TestCase): |
|
90 """Tests of custom optparse OptionParser with 'required' parameter support. |
|
91 """ |
|
92 |
|
93 def testRequiredPresent(self): |
|
94 """Test required=True raises nothing when value option is present. |
|
95 """ |
|
96 parser = settings.OptionParser( |
|
97 option_list=[ |
|
98 settings.Option( |
|
99 '-t', '--test', action='store', dest='test', required=True, |
|
100 help='(REQUIRED) test option'), |
|
101 ], |
|
102 ) |
|
103 |
|
104 options, args = parser.parse_args([sys.argv[0], '--test', '3']) |
|
105 |
|
106 def testRequiredMissing(self): |
|
107 """Test that Error exception is raised if required option not present. |
|
108 """ |
|
109 parser = settings.OptionParser( |
|
110 option_list=[ |
|
111 settings.Option( |
|
112 '-t', '--test', action='store', dest='test', required=True, |
|
113 help='(REQUIRED) test option'), |
|
114 ], |
|
115 ) |
|
116 |
|
117 self.assertRaises(settings.Error, parser.parse_args, []) |
|
118 |
|
119 def testBadRequiredAction(self): |
|
120 """Test that OptionError is raised if action does not support required=True. |
|
121 """ |
|
122 |
|
123 # store_true is not in Options.TYPED_VALUES, which means option cannot |
|
124 # take a value, so required=True is not permitted. |
|
125 self.assertRaises(optparse.OptionError, settings.Option, |
|
126 '-t', '--test', action='store_true', dest='test', required=True, |
|
127 help='(REQUIRED) test option') |