3
# Copyright (c) 2009, Google Inc.
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions are
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
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.
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.
32
"""Auxiliary module for testing flags.py.
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.
38
__author__ = 'Alex Salcianu'
40
__pychecker__ = "no-local" # for unittest
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
48
def DefineFlags(flag_values=FLAGS):
49
"""Defines some flags.
52
flag_values: The FlagValues object we want to register the flags
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)
72
def RemoveOneFlag(flag_name, flag_values=FLAGS):
73
"""Removes the definition of one flag from flags.FLAGS.
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).
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.
83
flag_name: A string, the name of the flag to delete.
84
flag_values: The FlagValues object we remove the flag from.
86
if flag_name in flag_values.FlagDict():
87
flag_values.__delattr__(flag_name)
90
def NamesOfDefinedFlags():
91
"""Returns: List of names of the flags declared in this module."""
100
def RemoveFlags(flag_values=FLAGS):
101
"""Deletes the flag definitions done by the above DefineFlags().
104
flag_values: The FlagValues object we remove the flags from.
106
for flag_name in NamesOfDefinedFlags():
107
RemoveOneFlag(flag_name, flag_values=flag_values)
111
"""Uses flags._GetCallingModule() to return the name of this module.
113
For checking that _GetCallingModule works as expected.
116
A string, the name of this module.
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()
123
def ExecuteCode(code, global_dict):
124
"""Executes some code in a given global environment.
126
For testing of _GetCallingModule.
129
code: A string, the code to be executed.
130
global_dict: A dictionary, the global environment that code should
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