scripts/tests/settings_test.py
changeset 63 9b1909e46633
parent 62 1627b79c396a
child 64 b73eec62825a
equal deleted inserted replaced
62:1627b79c396a 63:9b1909e46633
     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 
       
    20 For details on running the tests, see:
       
    21   http://code.google.com/p/soc/wiki/TestingGuidelines#Running_the_smoke_tests
       
    22 """
       
    23 
       
    24 __authors__ = [
       
    25   # alphabetical order by last name, please
       
    26   '"Todd Larsen" <tlarsen@google.com>',
       
    27 ]
       
    28 
       
    29 
       
    30 import optparse
       
    31 import os
       
    32 import sys
       
    33 import unittest
       
    34 
       
    35 from .. import settings
       
    36 
       
    37 
       
    38 class SettingsTests(unittest.TestCase):
       
    39   """Python-format settings file tests for the settings.py module.
       
    40   """
       
    41 
       
    42   def setUp(self):
       
    43     self.test_srcdir = os.path.dirname(__file__)
       
    44     self.settings_defaults = {'foo': 1, 'bif': 4}
       
    45 
       
    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'))
       
    54 
       
    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'))
       
    61 
       
    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'))
       
    71 
       
    72     # but the original defaults will be untouched
       
    73     self.assertEqual({'foo': 1, 'bif': 4}, self.settings_defaults)
       
    74 
       
    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')
       
    81 
       
    82 
       
    83 class OptionParserTests(unittest.TestCase):
       
    84   """Tests of custom optparse OptionParser with 'required' parameter support.
       
    85   """
       
    86 
       
    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'),
       
    95         ],
       
    96       )
       
    97 
       
    98     options, args = parser.parse_args([sys.argv[0], '--test', '3'])
       
    99 
       
   100   def testRequiredMissing(self):
       
   101     """Test that Error exception is raised if required option not present.
       
   102     """
       
   103     parser = settings.OptionParser(
       
   104       option_list=[
       
   105         settings.Option(
       
   106             '-t', '--test', action='store', dest='test', required=True,
       
   107             help='(REQUIRED) test option'),
       
   108         ],
       
   109       )
       
   110 
       
   111     self.assertRaises(settings.Error, parser.parse_args, [])
       
   112 
       
   113   def testBadRequiredAction(self):
       
   114     """Test that OptionError is raised if action does not support required=True.
       
   115     """
       
   116 
       
   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')