diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index 49ab4db..88cf625 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -5,6 +5,7 @@ import io import textwrap import threading import types +import os import sys from pysnooper.utils import truncate @@ -1172,3 +1173,57 @@ def test_custom_repr(): ReturnValueEntry('49995000'), ) ) + + +def test_activate_deactivate_snoop(): + string_io = io.StringIO() + + def my_function(foo): + x = 7 + y = 8 + return x + y + + os.environ['PYSNOOPER_DISABLED'] = '1' + with pysnooper.snoop(): + result = my_function('baba') + output = string_io.getvalue() + assert output == "" + + os.environ['PYSNOOPER_DISABLED'] = '' + test_string_io() + + +def test_setup_snoop_global(): + my_snoop = pysnooper.snoop.setup(disable=False) + os.environ['PYSNOOPER_DISABLED'] = '1' + + @my_snoop() + def my_function(foo): + x = 7 + y = 8 + return y + x + + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = my_function('baba') + output = output_capturer.string_io.getvalue() + assert_output( + output, + ( + VariableEntry('foo', value_regex="u?'baba'"), + CallEntry('def my_function(foo):'), + LineEntry('x = 7'), + VariableEntry('x', '7'), + LineEntry('y = 8'), + VariableEntry('y', '8'), + LineEntry('return y + x'), + ReturnEntry('return y + x'), + ReturnValueEntry('15'), + ) + ) + + +def test_mismatch_parameters_in_setup(): + with pytest.raises(Exception, match='contain non-snoop parameters') as excinfo: + my_snoop = pysnooper.snoop.setup(disabled=False) +