From eda664aa9b1e2e285e97fbaf428267a91c8f2f67 Mon Sep 17 00:00:00 2001 From: Lukas Klenk Date: Sat, 2 Apr 2022 13:51:14 +0200 Subject: [PATCH] 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 @@ if pycompat.PY2: 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 types 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 test_valid_zipfile(): ) ) 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 test_invalid_zipfile(): ''') 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 test_invalid_zipfile(): ) ) 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 test_valid_damaged_zipfile(): ''') 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 test_valid_damaged_zipfile(): ) ) 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 - - - - - -