tests/settings_test.py
author David Anderson <david.jc.anderson@gmail.com>
Fri, 13 Mar 2009 01:44:18 +0000
changeset 1822 c6bb25fa7f7b
parent 593 01f8c7aabb7e
permissions -rw-r--r--
Create an error module containing the top-level error class.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Tests for the scripts.settings module.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
35
07e9dc69074a Replace verbose text in __doc__ string with link to wiki topic about using
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    20
For details on running the tests, see:
07e9dc69074a Replace verbose text in __doc__ string with link to wiki topic about using
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    21
  http://code.google.com/p/soc/wiki/TestingGuidelines#Running_the_smoke_tests
29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
"""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
__authors__ = [
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
  # alphabetical order by last name, please
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
  '"Todd Larsen" <tlarsen@google.com>',
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
]
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
import optparse
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
import os
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
import sys
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
import unittest
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
593
01f8c7aabb7e Removed cruft from the tests directory
Sverre Rabbelier <srabbelier@gmail.com>
parents: 63
diff changeset
    35
from scripts import settings
29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
class SettingsTests(unittest.TestCase):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
  """Python-format settings file tests for the settings.py module.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
  """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
  def setUp(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
    self.test_srcdir = os.path.dirname(__file__)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
    self.settings_defaults = {'foo': 1, 'bif': 4}
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
  def testMissingPythonSettings(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
    """Test that non-existent files work properly without errors.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
    """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
    # non-existent settings file with no defaults produces empty dict
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
    self.assertEqual(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
        {},
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
        settings.readPythonSettings(settings_dir=self.test_srcdir,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
                                    settings_file='nonexistent_file'))
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
    # non-existent settings file should just pass through the defaults
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
    self.assertEqual(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
        self.settings_defaults,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
        settings.readPythonSettings(defaults=self.settings_defaults,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
                                    settings_dir=self.test_srcdir,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
                                    settings_file='nonexistent_file'))
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
  def testGoodPythonSettings(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
    """Test that settings file that is present overwrites defaults.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
    """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
    # foo and bar are overwritten, but not bif (not in the settings file)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
    self.assertEqual(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
        {'foo': 3, 'bar': 3, 'bif': 4},
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
        settings.readPythonSettings(defaults=self.settings_defaults,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
                                    settings_dir=self.test_srcdir,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
                                    settings_file='good_test_settings'))
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
    # but the original defaults will be untouched
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
    self.assertEqual({'foo': 1, 'bif': 4}, self.settings_defaults)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
  def testBadPythonSettings(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
    """Test that exception is raised when format of settings file is bad.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
    """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
    self.assertRaises(settings.Error, settings.readPythonSettings,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
                      settings_dir=self.test_srcdir,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
                      settings_file='bad_test_settings')
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
class OptionParserTests(unittest.TestCase):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
  """Tests of custom optparse OptionParser with 'required' parameter support.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
  """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
  def testRequiredPresent(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    88
    """Test required=True raises nothing when value option is present.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
    """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
    parser = settings.OptionParser(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    91
      option_list=[
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    92
        settings.Option(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    93
            '-t', '--test', action='store', dest='test', required=True,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    94
            help='(REQUIRED) test option'),
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    95
        ],
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    96
      )
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    97
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    98
    options, args = parser.parse_args([sys.argv[0], '--test', '3'])
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    99
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   100
  def testRequiredMissing(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
    """Test that Error exception is raised if required option not present.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
    """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   103
    parser = settings.OptionParser(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   104
      option_list=[
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
        settings.Option(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
            '-t', '--test', action='store', dest='test', required=True,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   107
            help='(REQUIRED) test option'),
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   108
        ],
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   109
      )
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   110
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   111
    self.assertRaises(settings.Error, parser.parse_args, [])
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   112
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   113
  def testBadRequiredAction(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   114
    """Test that OptionError is raised if action does not support required=True.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   115
    """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   116
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   117
    # store_true is not in Options.TYPED_VALUES, which means option cannot
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   118
    # take a value, so required=True is not permitted.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   119
    self.assertRaises(optparse.OptionError, settings.Option,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   120
        '-t', '--test', action='store_true', dest='test', required=True,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   121
        help='(REQUIRED) test option')