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"""Tests -v/--verbosity flag and logging.root level's sync behavior."""
16
17import logging
18
19assert logging.root.getEffectiveLevel() == logging.WARN, (
20    'default logging.root level should be WARN, but found {}'.format(
21        logging.root.getEffectiveLevel()))
22
23# This is here to test importing logging won't change the level.
24logging.root.setLevel(logging.ERROR)
25
26assert logging.root.getEffectiveLevel() == logging.ERROR, (
27    'logging.root level should be changed to ERROR, but found {}'.format(
28        logging.root.getEffectiveLevel()))
29
30# pylint: disable=g-import-not-at-top
31from absl import flags
32from absl import logging as _  # pylint: disable=unused-import
33from absl.testing import absltest
34# pylint: enable=g-import-not-at-top
35
36FLAGS = flags.FLAGS
37
38assert FLAGS['verbosity'].value == -1, (
39    '-v/--verbosity should be -1 before flags are parsed.')
40
41assert logging.root.getEffectiveLevel() == logging.ERROR, (
42    'logging.root level should be kept to ERROR, but found {}'.format(
43        logging.root.getEffectiveLevel()))
44
45
46class VerbosityFlagTest(absltest.TestCase):
47
48  def test_default_value_after_init(self):
49    self.assertEqual(0, FLAGS.verbosity)
50    self.assertEqual(logging.INFO, logging.root.getEffectiveLevel())
51
52
53if __name__ == '__main__':
54  absltest.main()
55