"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.5.1/buildbot/test/unit/test_test_util_warnings.py" (24 Nov 2019, 5699 Bytes) of package /linux/misc/buildbot-2.5.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # This file is part of Buildbot.  Buildbot is free software: you can
    2 # redistribute it and/or modify it under the terms of the GNU General Public
    3 # License as published by the Free Software Foundation, version 2.
    4 #
    5 # This program is distributed in the hope that it will be useful, but WITHOUT
    6 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    7 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    8 # details.
    9 #
   10 # You should have received a copy of the GNU General Public License along with
   11 # this program; if not, write to the Free Software Foundation, Inc., 51
   12 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   13 #
   14 # Copyright Buildbot Team Members
   15 
   16 
   17 import warnings
   18 
   19 from twisted.trial import unittest
   20 
   21 from buildbot.test.util.warnings import assertNotProducesWarnings
   22 from buildbot.test.util.warnings import assertProducesWarning
   23 from buildbot.test.util.warnings import assertProducesWarnings
   24 from buildbot.test.util.warnings import ignoreWarning
   25 
   26 
   27 class SomeWarning(Warning):
   28     pass
   29 
   30 
   31 class OtherWarning(Warning):
   32     pass
   33 
   34 
   35 class TestWarningsFilter(unittest.TestCase):
   36 
   37     def test_warnigs_caught(self):
   38         # Assertion is correct.
   39         with assertProducesWarning(SomeWarning):
   40             warnings.warn("test", SomeWarning)
   41 
   42     def test_warnigs_caught_num_check(self):
   43         # Assertion is correct.
   44         with assertProducesWarnings(SomeWarning, num_warnings=3):
   45             warnings.warn("1", SomeWarning)
   46             warnings.warn("2", SomeWarning)
   47             warnings.warn("3", SomeWarning)
   48 
   49     def test_warnigs_caught_num_check_fail(self):
   50         def f1():
   51             with assertProducesWarnings(SomeWarning, num_warnings=2):
   52                 pass
   53         with self.assertRaises(AssertionError):
   54             f1()
   55 
   56         def f2():
   57             with assertProducesWarnings(SomeWarning, num_warnings=2):
   58                 warnings.warn("1", SomeWarning)
   59         with self.assertRaises(AssertionError):
   60             f2()
   61 
   62         def f3():
   63             with assertProducesWarnings(SomeWarning, num_warnings=2):
   64                 warnings.warn("1", SomeWarning)
   65                 warnings.warn("2", SomeWarning)
   66                 warnings.warn("3", SomeWarning)
   67         with self.assertRaises(AssertionError):
   68             f3()
   69 
   70     def test_warnigs_caught_pattern_check(self):
   71         # Assertion is correct.
   72         with assertProducesWarning(SomeWarning, message_pattern=r"t.st"):
   73             warnings.warn("The test", SomeWarning)
   74 
   75     def test_warnigs_caught_pattern_check_fail(self):
   76         def f():
   77             # Assertion fails.
   78             with assertProducesWarning(SomeWarning, message_pattern=r"other"):
   79                 warnings.warn("The test", SomeWarning)
   80 
   81         with self.assertRaises(AssertionError):
   82             f()
   83 
   84     def test_warnigs_caught_patterns_check(self):
   85         # Assertion is correct.
   86         with assertProducesWarnings(SomeWarning,
   87                                     messages_patterns=["1", "2", "3"]):
   88             warnings.warn("log 1 message", SomeWarning)
   89             warnings.warn("log 2 message", SomeWarning)
   90             warnings.warn("log 3 message", SomeWarning)
   91 
   92     def test_warnigs_caught_patterns_check_fails(self):
   93         def f1():
   94             # Assertion fails.
   95             with assertProducesWarnings(SomeWarning,
   96                                         messages_patterns=["1", "2"]):
   97                 warnings.warn("msg 1", SomeWarning)
   98 
   99         with self.assertRaises(AssertionError):
  100             f1()
  101 
  102         def f2():
  103             # Assertion fails.
  104             with assertProducesWarnings(SomeWarning,
  105                                         messages_patterns=["1", "2"]):
  106                 warnings.warn("msg 2", SomeWarning)
  107                 warnings.warn("msg 1", SomeWarning)
  108 
  109         with self.assertRaises(AssertionError):
  110             f2()
  111 
  112         def f3():
  113             # Assertion fails.
  114             with assertProducesWarnings(SomeWarning,
  115                                         messages_patterns=["1", "2"]):
  116                 warnings.warn("msg 1", SomeWarning)
  117                 warnings.warn("msg 2", SomeWarning)
  118                 warnings.warn("msg 3", SomeWarning)
  119 
  120         with self.assertRaises(AssertionError):
  121             f3()
  122 
  123     def test_no_warnigs_check(self):
  124         with assertNotProducesWarnings(SomeWarning):
  125             pass
  126 
  127         with ignoreWarning(OtherWarning):
  128             with assertNotProducesWarnings(SomeWarning):
  129                 warnings.warn("msg 3", OtherWarning)
  130 
  131     def test_warnigs_filter(self):
  132         with ignoreWarning(OtherWarning):
  133             with assertProducesWarnings(SomeWarning,
  134                                         messages_patterns=["1", "2", "3"]):
  135                 warnings.warn("other", OtherWarning)
  136                 warnings.warn("log 1 message", SomeWarning)
  137                 warnings.warn("other", OtherWarning)
  138                 warnings.warn("log 2 message", SomeWarning)
  139                 warnings.warn("other", OtherWarning)
  140                 warnings.warn("log 3 message", SomeWarning)
  141                 warnings.warn("other", OtherWarning)
  142 
  143     def test_nested_filters(self):
  144         with assertProducesWarnings(SomeWarning,
  145                                     messages_patterns=["some 1"]):
  146             with assertProducesWarnings(OtherWarning,
  147                                         messages_patterns=["other 1"]):
  148                 warnings.warn("other 1", OtherWarning)
  149                 warnings.warn("some 1", SomeWarning)
  150 
  151     def test_ignore_warnings(self):
  152         with assertNotProducesWarnings(SomeWarning):
  153             with ignoreWarning(SomeWarning):
  154                 warnings.warn("some 1", SomeWarning)