~cbehrens/nova/lp844160-build-works-with-zones

« back to all changes in this revision

Viewing changes to vendor/python-gflags/test_module_bar.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Copyright (c) 2009, Google Inc.
 
4
# All rights reserved.
 
5
#
 
6
# Redistribution and use in source and binary forms, with or without
 
7
# modification, are permitted provided that the following conditions are
 
8
# met:
 
9
#
 
10
#     * Redistributions of source code must retain the above copyright
 
11
# notice, this list of conditions and the following disclaimer.
 
12
#     * Redistributions in binary form must reproduce the above
 
13
# copyright notice, this list of conditions and the following disclaimer
 
14
# in the documentation and/or other materials provided with the
 
15
# distribution.
 
16
#     * Neither the name of Google Inc. nor the names of its
 
17
# contributors may be used to endorse or promote products derived from
 
18
# this software without specific prior written permission.
 
19
#
 
20
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
23
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
24
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
25
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
26
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
27
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
28
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
29
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
30
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
31
 
 
32
"""Auxiliary module for testing flags.py.
 
33
 
 
34
The purpose of this module is to define a few flags.  We want to make
 
35
sure the unit tests for flags.py involve more than one module.
 
36
"""
 
37
 
 
38
__author__ = 'Alex Salcianu'
 
39
 
 
40
__pychecker__ = "no-local" # for unittest
 
41
 
 
42
# We use the name 'flags' internally in this test, for historical reasons.
 
43
# Don't do this yourself! :-) Just do 'import gflags; FLAGS=gflags.FLAGS; etc'
 
44
import gflags as flags
 
45
FLAGS = flags.FLAGS
 
46
 
 
47
 
 
48
def DefineFlags(flag_values=FLAGS):
 
49
  """Defines some flags.
 
50
 
 
51
  Args:
 
52
    flag_values: The FlagValues object we want to register the flags
 
53
      with.
 
54
  """
 
55
  # The 'tmod_bar_' prefix (short for 'test_module_bar') ensures there
 
56
  # is no name clash with the existing flags.
 
57
  flags.DEFINE_boolean('tmod_bar_x', True, 'Boolean flag.',
 
58
                       flag_values=flag_values)
 
59
  flags.DEFINE_string('tmod_bar_y', 'default', 'String flag.',
 
60
                      flag_values=flag_values)
 
61
  flags.DEFINE_boolean('tmod_bar_z', False,
 
62
                       'Another boolean flag from module bar.',
 
63
                       flag_values=flag_values)
 
64
  flags.DEFINE_integer('tmod_bar_t', 4, 'Sample int flag.',
 
65
                       flag_values=flag_values)
 
66
  flags.DEFINE_integer('tmod_bar_u', 5, 'Sample int flag.',
 
67
                       flag_values=flag_values)
 
68
  flags.DEFINE_integer('tmod_bar_v', 6, 'Sample int flag.',
 
69
                       flag_values=flag_values)
 
70
 
 
71
 
 
72
def RemoveOneFlag(flag_name, flag_values=FLAGS):
 
73
  """Removes the definition of one flag from flags.FLAGS.
 
74
 
 
75
  Note: if the flag is not defined in flags.FLAGS, this function does
 
76
  not do anything (in particular, it does not raise any exception).
 
77
 
 
78
  Motivation: We use this function for cleanup *after* a test: if
 
79
  there was a failure during a test and not all flags were declared,
 
80
  we do not want the cleanup code to crash.
 
81
 
 
82
  Args:
 
83
    flag_name: A string, the name of the flag to delete.
 
84
    flag_values: The FlagValues object we remove the flag from.
 
85
  """
 
86
  if flag_name in flag_values.FlagDict():
 
87
    flag_values.__delattr__(flag_name)
 
88
 
 
89
 
 
90
def NamesOfDefinedFlags():
 
91
  """Returns: List of names of the flags declared in this module."""
 
92
  return ['tmod_bar_x',
 
93
          'tmod_bar_y',
 
94
          'tmod_bar_z',
 
95
          'tmod_bar_t',
 
96
          'tmod_bar_u',
 
97
          'tmod_bar_v']
 
98
 
 
99
 
 
100
def RemoveFlags(flag_values=FLAGS):
 
101
  """Deletes the flag definitions done by the above DefineFlags().
 
102
 
 
103
  Args:
 
104
    flag_values: The FlagValues object we remove the flags from.
 
105
  """
 
106
  for flag_name in NamesOfDefinedFlags():
 
107
    RemoveOneFlag(flag_name, flag_values=flag_values)
 
108
 
 
109
 
 
110
def GetModuleName():
 
111
  """Uses flags._GetCallingModule() to return the name of this module.
 
112
 
 
113
  For checking that _GetCallingModule works as expected.
 
114
 
 
115
  Returns:
 
116
    A string, the name of this module.
 
117
  """
 
118
  # Calling the protected _GetCallingModule generates a lint warning,
 
119
  # but we do not have any other alternative to test that function.
 
120
  return flags._GetCallingModule()
 
121
 
 
122
 
 
123
def ExecuteCode(code, global_dict):
 
124
  """Executes some code in a given global environment.
 
125
 
 
126
  For testing of _GetCallingModule.
 
127
 
 
128
  Args:
 
129
    code: A string, the code to be executed.
 
130
    global_dict: A dictionary, the global environment that code should
 
131
      be executed in.
 
132
  """
 
133
  # Indeed, using exec generates a lint warning.  But some user code
 
134
  # actually uses exec, and we have to test for it ...
 
135
  exec code in global_dict