• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 The Abseil Authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Auxiliary module for testing flags.py.
16
17The purpose of this module is to define a few flags.  We want to make
18sure the unit tests for flags.py involve more than one module.
19"""
20
21from absl import flags
22from absl.flags import _helpers
23
24FLAGS = flags.FLAGS
25
26
27def define_flags(flag_values=FLAGS):
28  """Defines some flags.
29
30  Args:
31    flag_values: The FlagValues object we want to register the flags
32      with.
33  """
34  # The 'tmod_bar_' prefix (short for 'test_module_bar') ensures there
35  # is no name clash with the existing flags.
36  flags.DEFINE_boolean('tmod_bar_x', True, 'Boolean flag.',
37                       flag_values=flag_values)
38  flags.DEFINE_string('tmod_bar_y', 'default', 'String flag.',
39                      flag_values=flag_values)
40  flags.DEFINE_boolean('tmod_bar_z', False,
41                       'Another boolean flag from module bar.',
42                       flag_values=flag_values)
43  flags.DEFINE_integer('tmod_bar_t', 4, 'Sample int flag.',
44                       flag_values=flag_values)
45  flags.DEFINE_integer('tmod_bar_u', 5, 'Sample int flag.',
46                       flag_values=flag_values)
47  flags.DEFINE_integer('tmod_bar_v', 6, 'Sample int flag.',
48                       flag_values=flag_values)
49
50
51def remove_one_flag(flag_name, flag_values=FLAGS):
52  """Removes the definition of one flag from flags.FLAGS.
53
54  Note: if the flag is not defined in flags.FLAGS, this function does
55  not do anything (in particular, it does not raise any exception).
56
57  Motivation: We use this function for cleanup *after* a test: if
58  there was a failure during a test and not all flags were declared,
59  we do not want the cleanup code to crash.
60
61  Args:
62    flag_name: A string, the name of the flag to delete.
63    flag_values: The FlagValues object we remove the flag from.
64  """
65  if flag_name in flag_values:
66    flag_values.__delattr__(flag_name)
67
68
69def names_of_defined_flags():
70  """Returns: List of names of the flags declared in this module."""
71  return ['tmod_bar_x',
72          'tmod_bar_y',
73          'tmod_bar_z',
74          'tmod_bar_t',
75          'tmod_bar_u',
76          'tmod_bar_v']
77
78
79def remove_flags(flag_values=FLAGS):
80  """Deletes the flag definitions done by the above define_flags().
81
82  Args:
83    flag_values: The FlagValues object we remove the flags from.
84  """
85  for flag_name in names_of_defined_flags():
86    remove_one_flag(flag_name, flag_values=flag_values)
87
88
89def get_module_name():
90  """Uses get_calling_module() to return the name of this module.
91
92  For checking that get_calling_module works as expected.
93
94  Returns:
95    A string, the name of this module.
96  """
97  return _helpers.get_calling_module()
98
99
100def execute_code(code, global_dict):
101  """Executes some code in a given global environment.
102
103  For testing of get_calling_module.
104
105  Args:
106    code: A string, the code to be executed.
107    global_dict: A dictionary, the global environment that code should
108      be executed in.
109  """
110  # Indeed, using exec generates a lint warning.  But some user code
111  # actually uses exec, and we have to test for it ...
112  exec(code, global_dict)  # pylint: disable=exec-used
113
114
115def disclaim_key_flags():
116  """Disclaims flags declared in this module."""
117  flags.disclaim_key_flags()
118