From 1f8d7648b5d771881f75325adc7e6df3462421ef Mon Sep 17 00:00:00 2001 From: Lukas Klenk Date: Wed, 23 Mar 2022 09:04:20 +0000 Subject: [PATCH 1/8] Add support for Ansible zipped source files --- pysnooper/tracer.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index 21dd1ed..c173302 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -20,6 +20,7 @@ ipython_filename_pattern = re.compile('^$') +ansible_filename_pattern = re.compile('^(/.+zip)/(ansible/modules/.+)$') def get_local_reprs(frame, watch=(), custom_repr=(), max_length=None, normalize=False): @@ -67,6 +68,7 @@ def get_path_and_source_from_frame(frame): source = source.splitlines() if source is None: ipython_filename_match = ipython_filename_pattern.match(file_name) + ansible_filename_match = ansible_filename_pattern.match(file_name) if ipython_filename_match: entry_number = int(ipython_filename_match.group(1)) try: @@ -77,6 +79,13 @@ def get_path_and_source_from_frame(frame): source = source_chunk.splitlines() except Exception: pass + elif ansible_filename_match: + try: + import zipfile + archive_file = zipfile.ZipFile(ansible_filename_match.group(1), 'r') + source = archive_file.read(ansible_filename_match.group(2)).splitlines() + except Exception: + pass else: try: with open(file_name, 'rb') as fp: From c6c705e0021d931fc0fa43f7989e863d2df7b30a Mon Sep 17 00:00:00 2001 From: Lukas Klenk Date: Wed, 23 Mar 2022 12:43:48 +0000 Subject: [PATCH 2/8] Change to raw string and add file extension to get a better match --- pysnooper/tracer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index c173302..2eefaf9 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -20,7 +20,7 @@ ipython_filename_pattern = re.compile('^$') -ansible_filename_pattern = re.compile('^(/.+zip)/(ansible/modules/.+)$') +ansible_filename_pattern = re.compile(r'^(/.+\.zip)/(ansible/modules/.+\.py)$') def get_local_reprs(frame, watch=(), custom_repr=(), max_length=None, normalize=False): From 5aa10df8481e5c305c7d789269f3243171860b7c Mon Sep 17 00:00:00 2001 From: Lukas Klenk Date: Wed, 23 Mar 2022 12:45:17 +0000 Subject: [PATCH 3/8] Test the Ansible regex --- tests/test_utils/test_regex.py | 72 ++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/test_utils/test_regex.py diff --git a/tests/test_utils/test_regex.py b/tests/test_utils/test_regex.py new file mode 100644 index 0000000..4b08d4e --- /dev/null +++ b/tests/test_utils/test_regex.py @@ -0,0 +1,72 @@ +# Copyright 2022 Ram Rachum and collaborators. +# This program is distributed under the MIT license. + +import pysnooper +from pysnooper.tracer import ansible_filename_pattern + +def test_ansible_filename_pattern(): + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ansible/modules/my_module.py' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_with.zip_name.zip' + source_code_file = 'ansible/modules/my_module.py' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = '/my/new/path/payload.zip' + source_code_file = 'ansible/modules/my_module.py' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ansible/modules/in/new/path/my_module.py' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ansible/modules/my_module_is_called_.py.py' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ANSIBLE/modules/my_module.py' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name) == None + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ansible/modules/my_module.PY' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name) == None + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.Zip' + source_code_file = 'ansible/modules/my_module.py' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name) == None + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ansible/my_module.py' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name) == None + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = '' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name) == None + + archive_file = '' + source_code_file = 'ansible/modules/my_module.py' + file_name = archive_file + '/' + source_code_file + assert ansible_filename_pattern.match(file_name) == None + + + + + + From 55bb6a35cf57e124e36fe681f523359f3695e0ab Mon Sep 17 00:00:00 2001 From: Lukas Klenk Date: Thu, 24 Mar 2022 09:12:46 +0000 Subject: [PATCH 4/8] Change to is None and create strings with %s --- tests/test_utils/test_regex.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/test_utils/test_regex.py b/tests/test_utils/test_regex.py index 4b08d4e..bce6ccb 100644 --- a/tests/test_utils/test_regex.py +++ b/tests/test_utils/test_regex.py @@ -7,63 +7,63 @@ def test_ansible_filename_pattern(): archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' source_code_file = 'ansible/modules/my_module.py' - file_name = archive_file + '/' + source_code_file + file_name = '%s/%s' % (archive_file, source_code_file) assert ansible_filename_pattern.match(file_name).group(1) == archive_file assert ansible_filename_pattern.match(file_name).group(2) == source_code_file archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_with.zip_name.zip' source_code_file = 'ansible/modules/my_module.py' - file_name = archive_file + '/' + source_code_file + file_name = '%s/%s' % (archive_file, source_code_file) assert ansible_filename_pattern.match(file_name).group(1) == archive_file assert ansible_filename_pattern.match(file_name).group(2) == source_code_file archive_file = '/my/new/path/payload.zip' source_code_file = 'ansible/modules/my_module.py' - file_name = archive_file + '/' + source_code_file + file_name = '%s/%s' % (archive_file, source_code_file) assert ansible_filename_pattern.match(file_name).group(1) == archive_file assert ansible_filename_pattern.match(file_name).group(2) == source_code_file archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' source_code_file = 'ansible/modules/in/new/path/my_module.py' - file_name = archive_file + '/' + source_code_file + file_name = '%s/%s' % (archive_file, source_code_file) assert ansible_filename_pattern.match(file_name).group(1) == archive_file assert ansible_filename_pattern.match(file_name).group(2) == source_code_file archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' source_code_file = 'ansible/modules/my_module_is_called_.py.py' - file_name = archive_file + '/' + source_code_file + file_name = '%s/%s' % (archive_file, source_code_file) assert ansible_filename_pattern.match(file_name).group(1) == archive_file assert ansible_filename_pattern.match(file_name).group(2) == source_code_file archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' source_code_file = 'ANSIBLE/modules/my_module.py' - file_name = archive_file + '/' + source_code_file - assert ansible_filename_pattern.match(file_name) == None + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' source_code_file = 'ansible/modules/my_module.PY' - file_name = archive_file + '/' + source_code_file - assert ansible_filename_pattern.match(file_name) == None + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.Zip' source_code_file = 'ansible/modules/my_module.py' - file_name = archive_file + '/' + source_code_file - assert ansible_filename_pattern.match(file_name) == None + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' source_code_file = 'ansible/my_module.py' - file_name = archive_file + '/' + source_code_file - assert ansible_filename_pattern.match(file_name) == None + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' source_code_file = '' - file_name = archive_file + '/' + source_code_file - assert ansible_filename_pattern.match(file_name) == None + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None archive_file = '' source_code_file = 'ansible/modules/my_module.py' - file_name = archive_file + '/' + source_code_file - assert ansible_filename_pattern.match(file_name) == None + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None From d313d9937613ba2867c73a4f4807c7aba804a2ae Mon Sep 17 00:00:00 2001 From: Lukas Klenk Date: Mon, 28 Mar 2022 12:52:24 +0000 Subject: [PATCH 5/8] Add test cases for zip file handling --- tests/test_pysnooper.py | 146 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index 78dbc80..3a80d52 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -8,6 +8,8 @@ import types import os import sys +import zipfile +import importlib from pysnooper.utils import truncate import pytest @@ -1914,3 +1916,147 @@ def f(x): with pytest.raises(TypeError): f() + + +def test_valid_zipfile(): + with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder, \ + mini_toolbox.TempSysPathAdder(str(folder)): + module_name = 'my_valid_zip_module' + zip_name = 'valid.zip' + zip_base_path = 'ansible/modules' + python_file_path = folder / ('%s/%s/%s.py' % (zip_name, zip_base_path, module_name)) + os.makedirs(folder / ('%s/%s' % (zip_name, zip_base_path))) + sys.path.insert(0, str(folder / (zip_name))) + content = textwrap.dedent(u''' + import pysnooper + @pysnooper.snoop(color=False) + def f(x): + return x + ''') + with python_file_path.open('w') as python_file: + python_file.write(content) + + module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), module_name)) + + with zipfile.ZipFile(folder / 'foo_bar.zip', 'w') as myZipFile: + myZipFile.write(folder.joinpath('%s/%s/%s.py' % (zip_name, zip_base_path, module_name)), '%s/%s.py' % (zip_base_path, module_name,), zipfile.ZIP_DEFLATED) + + python_file_path.unlink() + os.rename(folder.joinpath(zip_name), folder.joinpath('%s.delete' % (zip_name)),) + os.rename(folder / 'foo_bar.zip', folder.joinpath(zip_name),) + + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = getattr(module, 'f')(7) + assert result == 7 + output = output_capturer.output + + assert_output( + output, + ( + SourcePathEntry(), + VariableEntry(stage='starting'), + CallEntry('def f(x):'), + LineEntry('return x'), + ReturnEntry('return x'), + ReturnValueEntry('7'), + ElapsedTimeEntry(), + ) + ) + + +def test_invalid_zipfile(): + with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder, \ + mini_toolbox.TempSysPathAdder(str(folder)): + module_name = 'my_invalid_zip_module' + zip_name = 'invalid.zip' + zip_base_path = 'invalid/modules/path' + python_file_path = folder / ('%s/%s/%s.py' % (zip_name, zip_base_path, module_name)) + os.makedirs(folder / ('%s/%s' % (zip_name, zip_base_path))) + sys.path.insert(0, str(folder / (zip_name))) + content = textwrap.dedent(u''' + import pysnooper + @pysnooper.snoop(color=False) + def f(x): + return x + ''') + with python_file_path.open('w') as python_file: + python_file.write(content) + + module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), module_name)) + + with zipfile.ZipFile(folder / 'foo_bar.zip', 'w') as myZipFile: + myZipFile.write(folder.joinpath('%s/%s/%s.py' % (zip_name, zip_base_path, module_name)), '%s/%s.py' % (zip_base_path, module_name,), zipfile.ZIP_DEFLATED) + + python_file_path.unlink() + os.rename(folder.joinpath(zip_name), folder.joinpath('%s.delete' % (zip_name)),) + os.rename(folder / 'foo_bar.zip', folder.joinpath(zip_name),) + + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = getattr(module, 'f')(7) + assert result == 7 + output = output_capturer.output + + assert_output( + output, + ( + SourcePathEntry(), + VariableEntry(stage='starting'), + CallEntry('SOURCE IS UNAVAILABLE'), + LineEntry('SOURCE IS UNAVAILABLE'), + ReturnEntry('SOURCE IS UNAVAILABLE'), + ReturnValueEntry('7'), + ElapsedTimeEntry(), + ) + ) + + +def test_valid_damaged_zipfile(): + with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder, \ + mini_toolbox.TempSysPathAdder(str(folder)): + module_name = 'my_damaged_module' + zip_name = 'damaged.zip' + zip_base_path = 'ansible/modules' + python_file_path = folder / ('%s/%s/%s.py' % (zip_name, zip_base_path, module_name)) + os.makedirs(folder / ('%s/%s' % (zip_name, zip_base_path))) + sys.path.insert(0, str(folder / (zip_name))) + content = textwrap.dedent(u''' + import pysnooper + @pysnooper.snoop(color=False) + def f(x): + return x + ''') + with python_file_path.open('w') as python_file: + python_file.write(content) + + module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), module_name)) + + python_file_path.unlink() + os.rename(folder.joinpath(zip_name), folder.joinpath('%s.delete' % (zip_name)),) + + content_damaged_zip = textwrap.dedent(u''' + I am not a zip file + ''') + + with folder.joinpath(zip_name).open('w') as damaged_zip_file: + damaged_zip_file.write(content_damaged_zip) + + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = getattr(module, 'f')(7) + assert result == 7 + output = output_capturer.output + + assert_output( + output, + ( + SourcePathEntry(), + VariableEntry(stage='starting'), + CallEntry('SOURCE IS UNAVAILABLE'), + LineEntry('SOURCE IS UNAVAILABLE'), + ReturnEntry('SOURCE IS UNAVAILABLE'), + ReturnValueEntry('7'), + ElapsedTimeEntry(), + ) + ) From 8fd5416c753d4e10c1e2b7a7fa1815593f1ed84e Mon Sep 17 00:00:00 2001 From: Lukas Klenk Date: Tue, 29 Mar 2022 09:50:35 +0000 Subject: [PATCH 6/8] Change tests to a clearer style --- tests/test_pysnooper.py | 238 +++++++++++++++++++++------------------- 1 file changed, 123 insertions(+), 115 deletions(-) diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index 3a80d52..fe1eca3 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -1924,45 +1924,50 @@ def test_valid_zipfile(): module_name = 'my_valid_zip_module' zip_name = 'valid.zip' zip_base_path = 'ansible/modules' - python_file_path = folder / ('%s/%s/%s.py' % (zip_name, zip_base_path, module_name)) - os.makedirs(folder / ('%s/%s' % (zip_name, zip_base_path))) - sys.path.insert(0, str(folder / (zip_name))) - content = textwrap.dedent(u''' - import pysnooper - @pysnooper.snoop(color=False) - def f(x): - return x - ''') - with python_file_path.open('w') as python_file: - python_file.write(content) - - module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), module_name)) - - with zipfile.ZipFile(folder / 'foo_bar.zip', 'w') as myZipFile: - myZipFile.write(folder.joinpath('%s/%s/%s.py' % (zip_name, zip_base_path, module_name)), '%s/%s.py' % (zip_base_path, module_name,), zipfile.ZIP_DEFLATED) - - python_file_path.unlink() - os.rename(folder.joinpath(zip_name), folder.joinpath('%s.delete' % (zip_name)),) - os.rename(folder / 'foo_bar.zip', folder.joinpath(zip_name),) - - with mini_toolbox.OutputCapturer(stdout=False, - stderr=True) as output_capturer: - result = getattr(module, 'f')(7) - assert result == 7 - output = output_capturer.output - - assert_output( - output, - ( - SourcePathEntry(), - VariableEntry(stage='starting'), - CallEntry('def f(x):'), - LineEntry('return x'), - ReturnEntry('return x'), - ReturnValueEntry('7'), - ElapsedTimeEntry(), + python_file_path = folder / zip_name / zip_base_path / ('%s.py' % (module_name)) + os.makedirs(folder / zip_name / zip_base_path) + try: + sys.path.insert(0, str(folder / zip_name)) + content = textwrap.dedent(u''' + import pysnooper + @pysnooper.snoop(color=False) + def f(x): + return x + ''') + python_file_path.write_text(content) + + module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), \ + module_name)) + + with zipfile.ZipFile(folder / 'foo_bar.zip', 'w') as myZipFile: + myZipFile.write(folder / zip_name / zip_base_path / ('%s.py' % (module_name)), \ + '%s/%s.py' % (zip_base_path, module_name,), \ + zipfile.ZIP_DEFLATED) + + python_file_path.unlink() + folder.joinpath(zip_name).rename(folder.joinpath('%s.delete' % (zip_name))) + folder.joinpath('foo_bar.zip').rename(folder.joinpath(zip_name)) + breakpoint() + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = getattr(module, 'f')(7) + assert result == 7 + output = output_capturer.output + + assert_output( + output, + ( + SourcePathEntry(), + VariableEntry(stage='starting'), + CallEntry('def f(x):'), + LineEntry('return x'), + ReturnEntry('return x'), + ReturnValueEntry('7'), + ElapsedTimeEntry(), + ) ) - ) + finally: + sys.path.remove(str(folder / zip_name)) def test_invalid_zipfile(): @@ -1971,45 +1976,50 @@ def test_invalid_zipfile(): module_name = 'my_invalid_zip_module' zip_name = 'invalid.zip' zip_base_path = 'invalid/modules/path' - python_file_path = folder / ('%s/%s/%s.py' % (zip_name, zip_base_path, module_name)) - os.makedirs(folder / ('%s/%s' % (zip_name, zip_base_path))) - sys.path.insert(0, str(folder / (zip_name))) - content = textwrap.dedent(u''' - import pysnooper - @pysnooper.snoop(color=False) - def f(x): - return x - ''') - with python_file_path.open('w') as python_file: - python_file.write(content) - - module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), module_name)) - - with zipfile.ZipFile(folder / 'foo_bar.zip', 'w') as myZipFile: - myZipFile.write(folder.joinpath('%s/%s/%s.py' % (zip_name, zip_base_path, module_name)), '%s/%s.py' % (zip_base_path, module_name,), zipfile.ZIP_DEFLATED) - - python_file_path.unlink() - os.rename(folder.joinpath(zip_name), folder.joinpath('%s.delete' % (zip_name)),) - os.rename(folder / 'foo_bar.zip', folder.joinpath(zip_name),) - - with mini_toolbox.OutputCapturer(stdout=False, + python_file_path = folder / zip_name / zip_base_path / ('%s.py' % (module_name)) + os.makedirs(folder / zip_name / zip_base_path) + try: + sys.path.insert(0, str(folder / zip_name)) + content = textwrap.dedent(u''' + import pysnooper + @pysnooper.snoop(color=False) + def f(x): + return x + ''') + python_file_path.write_text(content) + + module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), \ + module_name)) + + with zipfile.ZipFile(folder / 'foo_bar.zip', 'w') as myZipFile: + myZipFile.write(folder / zip_name / zip_base_path / ('%s.py' % (module_name)), \ + '%s/%s.py' % (zip_base_path, module_name,), \ + zipfile.ZIP_DEFLATED) + + python_file_path.unlink() + folder.joinpath(zip_name).rename(folder.joinpath('%s.delete' % (zip_name))) + folder.joinpath('foo_bar.zip').rename(folder.joinpath(zip_name)) + breakpoint() + with mini_toolbox.OutputCapturer(stdout=False, stderr=True) as output_capturer: - result = getattr(module, 'f')(7) - assert result == 7 - output = output_capturer.output - - assert_output( - output, - ( - SourcePathEntry(), - VariableEntry(stage='starting'), - CallEntry('SOURCE IS UNAVAILABLE'), - LineEntry('SOURCE IS UNAVAILABLE'), - ReturnEntry('SOURCE IS UNAVAILABLE'), - ReturnValueEntry('7'), - ElapsedTimeEntry(), + result = getattr(module, 'f')(7) + assert result == 7 + output = output_capturer.output + + assert_output( + output, + ( + SourcePathEntry(), + VariableEntry(stage='starting'), + CallEntry('SOURCE IS UNAVAILABLE'), + LineEntry('SOURCE IS UNAVAILABLE'), + ReturnEntry('SOURCE IS UNAVAILABLE'), + ReturnValueEntry('7'), + ElapsedTimeEntry(), + ) ) - ) + finally: + sys.path.remove(str(folder / zip_name)) def test_valid_damaged_zipfile(): @@ -2018,45 +2028,43 @@ def test_valid_damaged_zipfile(): module_name = 'my_damaged_module' zip_name = 'damaged.zip' zip_base_path = 'ansible/modules' - python_file_path = folder / ('%s/%s/%s.py' % (zip_name, zip_base_path, module_name)) - os.makedirs(folder / ('%s/%s' % (zip_name, zip_base_path))) - sys.path.insert(0, str(folder / (zip_name))) - content = textwrap.dedent(u''' - import pysnooper - @pysnooper.snoop(color=False) - def f(x): - return x - ''') - with python_file_path.open('w') as python_file: - python_file.write(content) - - module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), module_name)) - - python_file_path.unlink() - os.rename(folder.joinpath(zip_name), folder.joinpath('%s.delete' % (zip_name)),) - - content_damaged_zip = textwrap.dedent(u''' - I am not a zip file - ''') - - with folder.joinpath(zip_name).open('w') as damaged_zip_file: - damaged_zip_file.write(content_damaged_zip) - - with mini_toolbox.OutputCapturer(stdout=False, + python_file_path = folder / zip_name / zip_base_path / ('%s.py' % (module_name)) + os.makedirs(folder / zip_name / zip_base_path) + try: + sys.path.insert(0, str(folder / zip_name)) + content = textwrap.dedent(u''' + import pysnooper + @pysnooper.snoop(color=False) + def f(x): + return x + ''') + python_file_path.write_text(content) + + module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), \ + module_name)) + + python_file_path.unlink() + folder.joinpath(zip_name).rename(folder.joinpath('%s.delete' % (zip_name))) + + folder.joinpath(zip_name).write_text('I am not a zip file') + breakpoint() + with mini_toolbox.OutputCapturer(stdout=False, stderr=True) as output_capturer: - result = getattr(module, 'f')(7) - assert result == 7 - output = output_capturer.output - - assert_output( - output, - ( - SourcePathEntry(), - VariableEntry(stage='starting'), - CallEntry('SOURCE IS UNAVAILABLE'), - LineEntry('SOURCE IS UNAVAILABLE'), - ReturnEntry('SOURCE IS UNAVAILABLE'), - ReturnValueEntry('7'), - ElapsedTimeEntry(), + result = getattr(module, 'f')(7) + assert result == 7 + output = output_capturer.output + + assert_output( + output, + ( + SourcePathEntry(), + VariableEntry(stage='starting'), + CallEntry('SOURCE IS UNAVAILABLE'), + LineEntry('SOURCE IS UNAVAILABLE'), + ReturnEntry('SOURCE IS UNAVAILABLE'), + ReturnValueEntry('7'), + ElapsedTimeEntry(), + ) ) - ) + finally: + sys.path.remove(str(folder / zip_name)) From 772d263276b111e584bc439a1212e16d97222800 Mon Sep 17 00:00:00 2001 From: Lukas Klenk Date: Tue, 29 Mar 2022 17:31:33 +0000 Subject: [PATCH 7/8] Remove breakpoints from testing --- tests/test_pysnooper.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index fe1eca3..09e0132 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -1947,7 +1947,7 @@ def f(x): python_file_path.unlink() folder.joinpath(zip_name).rename(folder.joinpath('%s.delete' % (zip_name))) folder.joinpath('foo_bar.zip').rename(folder.joinpath(zip_name)) - breakpoint() + with mini_toolbox.OutputCapturer(stdout=False, stderr=True) as output_capturer: result = getattr(module, 'f')(7) @@ -1999,7 +1999,7 @@ def f(x): python_file_path.unlink() folder.joinpath(zip_name).rename(folder.joinpath('%s.delete' % (zip_name))) folder.joinpath('foo_bar.zip').rename(folder.joinpath(zip_name)) - breakpoint() + with mini_toolbox.OutputCapturer(stdout=False, stderr=True) as output_capturer: result = getattr(module, 'f')(7) @@ -2047,7 +2047,7 @@ def f(x): folder.joinpath(zip_name).rename(folder.joinpath('%s.delete' % (zip_name))) folder.joinpath(zip_name).write_text('I am not a zip file') - breakpoint() + with mini_toolbox.OutputCapturer(stdout=False, stderr=True) as output_capturer: result = getattr(module, 'f')(7) From eda664aa9b1e2e285e97fbaf428267a91c8f2f67 Mon Sep 17 00:00:00 2001 From: Lukas Klenk Date: Sat, 2 Apr 2022 13:51:14 +0200 Subject: [PATCH 8/8] Make tests python2 compatible and add Windows support --- pysnooper/tracer.py | 4 +-- tests/test_pysnooper.py | 47 ++++++++++++++++------------------ tests/test_utils/test_regex.py | 12 ++++----- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index 2eefaf9..0f35500 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -20,7 +20,7 @@ ipython_filename_pattern = re.compile('^$') -ansible_filename_pattern = re.compile(r'^(/.+\.zip)/(ansible/modules/.+\.py)$') +ansible_filename_pattern = re.compile(r'^(.+\.zip)[/|\\](ansible[/|\\]modules[/|\\].+\.py)$') def get_local_reprs(frame, watch=(), custom_repr=(), max_length=None, normalize=False): @@ -83,7 +83,7 @@ def get_path_and_source_from_frame(frame): try: import zipfile archive_file = zipfile.ZipFile(ansible_filename_match.group(1), 'r') - source = archive_file.read(ansible_filename_match.group(2)).splitlines() + source = archive_file.read(ansible_filename_match.group(2).replace('\\', '/')).splitlines() except Exception: pass else: diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index 09e0132..530a2f7 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -9,7 +9,6 @@ import os import sys import zipfile -import importlib from pysnooper.utils import truncate import pytest @@ -1923,24 +1922,24 @@ def test_valid_zipfile(): mini_toolbox.TempSysPathAdder(str(folder)): module_name = 'my_valid_zip_module' zip_name = 'valid.zip' - zip_base_path = 'ansible/modules' + zip_base_path = mini_toolbox.pathlib.Path('ansible/modules') python_file_path = folder / zip_name / zip_base_path / ('%s.py' % (module_name)) - os.makedirs(folder / zip_name / zip_base_path) + os.makedirs(str(folder / zip_name / zip_base_path)) try: - sys.path.insert(0, str(folder / zip_name)) + sys.path.insert(0, str(folder / zip_name / zip_base_path)) content = textwrap.dedent(u''' import pysnooper @pysnooper.snoop(color=False) def f(x): return x ''') + python_file_path.write_text(content) - module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), \ - module_name)) + module = __import__(module_name) - with zipfile.ZipFile(folder / 'foo_bar.zip', 'w') as myZipFile: - myZipFile.write(folder / zip_name / zip_base_path / ('%s.py' % (module_name)), \ + with zipfile.ZipFile(str(folder / 'foo_bar.zip'), 'w') as myZipFile: + myZipFile.write(str(folder / zip_name / zip_base_path / ('%s.py' % (module_name))), \ '%s/%s.py' % (zip_base_path, module_name,), \ zipfile.ZIP_DEFLATED) @@ -1967,7 +1966,7 @@ def f(x): ) ) finally: - sys.path.remove(str(folder / zip_name)) + sys.path.remove(str(folder / zip_name / zip_base_path)) def test_invalid_zipfile(): @@ -1975,11 +1974,11 @@ def test_invalid_zipfile(): mini_toolbox.TempSysPathAdder(str(folder)): module_name = 'my_invalid_zip_module' zip_name = 'invalid.zip' - zip_base_path = 'invalid/modules/path' + zip_base_path = mini_toolbox.pathlib.Path('invalid/modules/path') python_file_path = folder / zip_name / zip_base_path / ('%s.py' % (module_name)) - os.makedirs(folder / zip_name / zip_base_path) + os.makedirs(str(folder / zip_name / zip_base_path)) try: - sys.path.insert(0, str(folder / zip_name)) + sys.path.insert(0, str(folder / zip_name / zip_base_path)) content = textwrap.dedent(u''' import pysnooper @pysnooper.snoop(color=False) @@ -1988,12 +1987,11 @@ def f(x): ''') python_file_path.write_text(content) - module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), \ - module_name)) + module = __import__(module_name) - with zipfile.ZipFile(folder / 'foo_bar.zip', 'w') as myZipFile: - myZipFile.write(folder / zip_name / zip_base_path / ('%s.py' % (module_name)), \ - '%s/%s.py' % (zip_base_path, module_name,), \ + with zipfile.ZipFile(str(folder / 'foo_bar.zip'), 'w') as myZipFile: + myZipFile.write(str(folder / zip_name / zip_base_path / ('%s.py' % (module_name))), \ + str(zip_base_path / ('%s.py' % (module_name,))), \ zipfile.ZIP_DEFLATED) python_file_path.unlink() @@ -2019,7 +2017,7 @@ def f(x): ) ) finally: - sys.path.remove(str(folder / zip_name)) + sys.path.remove(str(folder / zip_name / zip_base_path)) def test_valid_damaged_zipfile(): @@ -2027,11 +2025,11 @@ def test_valid_damaged_zipfile(): mini_toolbox.TempSysPathAdder(str(folder)): module_name = 'my_damaged_module' zip_name = 'damaged.zip' - zip_base_path = 'ansible/modules' + zip_base_path = mini_toolbox.pathlib.Path('ansible/modules') python_file_path = folder / zip_name / zip_base_path / ('%s.py' % (module_name)) - os.makedirs(folder / zip_name / zip_base_path) + os.makedirs(str(folder / zip_name / zip_base_path)) try: - sys.path.insert(0, str(folder / zip_name)) + sys.path.insert(0, str(folder / zip_name / zip_base_path)) content = textwrap.dedent(u''' import pysnooper @pysnooper.snoop(color=False) @@ -2040,13 +2038,12 @@ def f(x): ''') python_file_path.write_text(content) - module = importlib.import_module('%s.%s' % ('.'.join(zip_base_path.split('/')), \ - module_name)) + module = __import__(module_name) python_file_path.unlink() folder.joinpath(zip_name).rename(folder.joinpath('%s.delete' % (zip_name))) - folder.joinpath(zip_name).write_text('I am not a zip file') + folder.joinpath(zip_name).write_text(u'I am not a zip file') with mini_toolbox.OutputCapturer(stdout=False, stderr=True) as output_capturer: @@ -2067,4 +2064,4 @@ def f(x): ) ) finally: - sys.path.remove(str(folder / zip_name)) + sys.path.remove(str(folder / zip_name / zip_base_path)) diff --git a/tests/test_utils/test_regex.py b/tests/test_utils/test_regex.py index bce6ccb..17d8805 100644 --- a/tests/test_utils/test_regex.py +++ b/tests/test_utils/test_regex.py @@ -35,6 +35,12 @@ def test_ansible_filename_pattern(): assert ansible_filename_pattern.match(file_name).group(1) == archive_file assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + archive_file = 'C:\\Users\\vagrant\\AppData\\Local\\Temp\\pysnooperw5c2lg35\\valid.zip' + source_code_file = 'ansible\\modules\\my_valid_zip_module.py' + file_name = '%s\\%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' source_code_file = 'ANSIBLE/modules/my_module.py' file_name = '%s/%s' % (archive_file, source_code_file) @@ -64,9 +70,3 @@ def test_ansible_filename_pattern(): source_code_file = 'ansible/modules/my_module.py' file_name = '%s/%s' % (archive_file, source_code_file) assert ansible_filename_pattern.match(file_name) is None - - - - - -