diff --git a/pysnooper/pysnooper.py b/pysnooper/pysnooper.py index 8d0c6a7..19139d3 100644 --- a/pysnooper/pysnooper.py +++ b/pysnooper/pysnooper.py @@ -23,7 +23,7 @@ def get_write_function(output): stderr.write(s) elif isinstance(output, (pycompat.PathLike, str)): def write(s): - with open(output_path, 'a') as output_file: + with open(output, 'a') as output_file: output_file.write(s) else: assert isinstance(output, utils.WritableStream) diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index 13f0401..27af77d 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -7,6 +7,7 @@ import abc from python_toolbox import caching from python_toolbox import sys_tools +from python_toolbox import temp_file_tools import pysnooper @@ -169,3 +170,30 @@ def test_method_and_prefix(): ), prefix='ZZZ' ) + +def test_file_output(): + + with temp_file_tools.create_temp_folder(prefix='pysnooper') as folder: + path = folder / 'foo.log' + @pysnooper.snoop(str(path)) + def my_function(foo): + x = 7 + y = 8 + return y + x + result = my_function('baba') + assert result == 15 + output = path.open().read() + assert_output( + output, + ( + VariableEntry('foo', value_regex="u?'baba'"), + CallEntry(), + LineEntry('x = 7'), + VariableEntry('x', '7'), + LineEntry('y = 8'), + VariableEntry('y', '8'), + LineEntry('return y + x'), + ReturnEntry('return y + x'), + ) + ) +