Change tests to a clearer style

This commit is contained in:
Lukas Klenk 2022-03-29 09:50:35 +00:00
parent d313d99376
commit 8fd5416c75

View file

@ -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))