From 8fd5416c753d4e10c1e2b7a7fa1815593f1ed84e Mon Sep 17 00:00:00 2001 From: Lukas Klenk Date: Tue, 29 Mar 2022 09:50:35 +0000 Subject: [PATCH] Change tests to a clearer style --- tests/test_pysnooper.py | 214 +++++++++++++++++++++------------------- 1 file changed, 111 insertions(+), 103 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) + 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)) + 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) + 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() - os.rename(folder.joinpath(zip_name), folder.joinpath('%s.delete' % (zip_name)),) - os.rename(folder / 'foo_bar.zip', folder.joinpath(zip_name),) + 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 - 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(), + 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) + 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)) + 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) + 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() - 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.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 + 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(), + 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) + 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)) + 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)),) + python_file_path.unlink() + folder.joinpath(zip_name).rename(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, + 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 + 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(), + 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))