def test_basic(): with create_temp_folder() as tf1: assert isinstance(tf1, pathlib.Path) assert tf1.exists() assert tf1.is_dir() tf2 = create_temp_folder() with tf2 as tf2: assert isinstance(tf2, pathlib.Path) assert tf2.exists() assert tf2.is_dir() assert not tf2.exists() assert not tf2.is_dir() assert tf1.exists() assert tf1.is_dir() file_path = (tf1 / 'my_file') with file_path.open('w') as my_file: my_file.write('Woo hoo!') assert file_path.exists() assert file_path.is_file() with file_path.open('r') as my_file: assert my_file.read() == 'Woo hoo!' assert not tf1.exists() assert not tf1.is_dir() assert not file_path.exists() assert not file_path.is_file()
def test_without_pathlib(): with create_temp_folder() as tf1: assert os.path.exists(str(tf1)) assert os.path.isdir(str(tf1)) tf2 = create_temp_folder() with tf2 as tf2: assert os.path.exists(str(tf2)) assert os.path.isdir(str(tf2)) assert not os.path.exists(str(tf2)) assert not os.path.isdir(str(tf2)) assert os.path.exists(str(tf1)) assert os.path.isdir(str(tf1)) file_path = os.path.join(str(tf1), 'my_file') with open(file_path, 'w') as my_file: my_file.write('Woo hoo!') assert os.path.exists(file_path) assert os.path.isfile(file_path) with open(file_path, 'r') as my_file: assert my_file.read() == 'Woo hoo!' assert not os.path.exists(str(tf1)) assert not os.path.isdir(str(tf1)) assert not os.path.exists(file_path) assert not os.path.isdir(file_path)
def test_chmod(): with create_temp_folder(chmod=0o777) as liberal_temp_folder, \ create_temp_folder(chmod=0o000) as conservative_temp_folder: # Doing a very weak test of chmod because not everything is supported # on Windows. assert (liberal_temp_folder.stat().st_mode & 0o777) > \ (conservative_temp_folder.stat().st_mode & 0o777) # Making `conservative_temp_folder` writeable again so it could be # deleted in cleanup: conservative_temp_folder.chmod(0o777)
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 with path.open() as output_file: output = output_file.read() assert_output(output, ( VariableEntry('_foo', value_regex="u?'baba'"), CallEntry('def my_function(_foo):'), LineEntry('x = 7'), VariableEntry('x', '7'), LineEntry('y = 8'), VariableEntry('y', '8'), LineEntry('return y + x'), ReturnEntry('return y + x'), ReturnValueEntry('15'), ))
def test(): with temp_file_tools.create_temp_folder() as temp_folder: assert isinstance(temp_folder, pathlib.Path) folder_to_zip = (temp_folder / 'folder_to_zip') folder_to_zip.mkdir() assert isinstance(folder_to_zip, pathlib.Path) (folder_to_zip / 'some_file.txt').open('w').write(u'hello there!') (folder_to_zip / 'some_other_file.txt').open('w').write( u'hello there again!') import gc; gc.collect() # Making PyPy happy. zip_file_path = temp_folder / 'archive.zip' assert isinstance(zip_file_path, pathlib.Path) zip_tools.zip_folder(folder_to_zip, temp_folder / 'archive.zip') result = set( zip_tools.unzip_in_memory(zip_file_path.open('rb').read()) ) assert zip_file_path.is_file() # Got two options here because of PyPy shenanigans: assert result == { ('folder_to_zip/some_file.txt', b'hello there!'), ('folder_to_zip/some_other_file.txt', b'hello there again!'), } or result == { ('folder_to_zip/some_file.txt', 'hello there!'), ('folder_to_zip/some_other_file.txt', 'hello there again!'), } import gc; gc.collect() # Making PyPy happy.
def test_no_overwrite_by_default(): with temp_file_tools.create_temp_folder(prefix='pysnooper') as folder: path = folder / 'foo.log' with path.open('w') as output_file: output_file.write(u'lala') @pysnooper.snoop(str(path)) def my_function(foo): x = 7 y = 8 return y + x result = my_function('baba') assert result == 15 with path.open() as output_file: output = output_file.read() assert output.startswith('lala') shortened_output = output[4:] assert_output(shortened_output, ( VariableEntry('foo', value_regex="u?'baba'"), CallEntry('def my_function(foo):'), LineEntry('x = 7'), VariableEntry('x', '7'), LineEntry('y = 8'), VariableEntry('y', '8'), LineEntry('return y + x'), ReturnEntry('return y + x'), ReturnValueEntry('15'), ))
def test_unavailable_source(): with temp_file_tools.create_temp_folder(prefix='pysnooper') as folder, \ sys_tools.TempSysPathAdder(str(folder)): module_name = 'iaerojajsijf' python_file_path = folder / ('%s.py' % (module_name, )) content = textwrap.dedent(u''' import pysnooper @pysnooper.snoop() def f(x): return x ''') with python_file_path.open('w') as python_file: python_file.write(content) module = __import__(module_name) python_file_path.unlink() with sys_tools.OutputCapturer(stdout=False, stderr=True) as output_capturer: result = getattr(module, 'f')(7) assert result == 7 output = output_capturer.output assert_output(output, ( VariableEntry(stage='starting'), CallEntry('SOURCE IS UNAVAILABLE'), LineEntry('SOURCE IS UNAVAILABLE'), ReturnEntry('SOURCE IS UNAVAILABLE'), ReturnValueEntry('7'), ))
def test_folder_handler(): with temp_value_setting.TempValueSetter( (cute_profile.profile_handling, 'threading'), dummy_threading): with temp_file_tools.create_temp_folder( suffix='_python_toolbox_testing') as temp_folder: f = cute_profile.profile_ready(profile_handler=temp_folder)(func) f(1, 2) assert len(list(temp_folder.iterdir())) == 0 f(1, 2) assert len(list(temp_folder.iterdir())) == 0 f.profiling_on = True f(1, 2) assert len(list(temp_folder.iterdir())) == 1 f(1, 2) assert len(list(temp_folder.iterdir())) == 1 time.sleep(0.01) # To make for a different filename. f.profiling_on = True f(1, 2) assert len(list(temp_folder.iterdir())) == 2 f(1, 2) assert len(list(temp_folder.iterdir())) == 2
def test(): with temp_file_tools.create_temp_folder() as temp_folder: assert isinstance(temp_folder, pathlib.Path) folder_to_zip = (temp_folder / 'folder_to_zip') folder_to_zip.mkdir() assert isinstance(folder_to_zip, pathlib.Path) (folder_to_zip / 'some_file.txt').open('w').write(u'hello there!') (folder_to_zip / 'some_other_file.txt').open('w').write(u'hello there again!') import gc gc.collect() # Making PyPy happy. zip_file_path = temp_folder / 'archive.zip' assert isinstance(zip_file_path, pathlib.Path) zip_tools.zip_folder(folder_to_zip, temp_folder / 'archive.zip') result = set(zip_tools.unzip_in_memory( zip_file_path.open('rb').read())) assert zip_file_path.is_file() # Got two options here because of PyPy shenanigans: assert result == set(( ('folder_to_zip/some_file.txt', b'hello there!'), ('folder_to_zip/some_other_file.txt', b'hello there again!'), )) or result == set(( ('folder_to_zip/some_file.txt', 'hello there!'), ('folder_to_zip/some_other_file.txt', 'hello there again!'), )) import gc gc.collect() # Making PyPy happy.
def test_folder_handler(): with temp_value_setting.TempValueSetter((cute_profile.profile_handling, 'threading'), dummy_threading): with temp_file_tools.create_temp_folder( suffix='_python_toolbox_testing') as temp_folder: f = cute_profile.profile_ready(profile_handler=temp_folder)(func) f(1, 2) assert len(list(temp_folder.iterdir())) == 0 f(1, 2) assert len(list(temp_folder.iterdir())) == 0 f.profiling_on = True f(1, 2) assert len(list(temp_folder.iterdir())) == 1 f(1, 2) assert len(list(temp_folder.iterdir())) == 1 time.sleep(0.01) # To make for a different filename. f.profiling_on = True f(1, 2) assert len(list(temp_folder.iterdir())) == 2 f(1, 2) assert len(list(temp_folder.iterdir())) == 2
def test_as_decorator(): '''Test `TempWorkingDirectorySetter` used as a decorator.''' with temp_file_tools.create_temp_folder( prefix='test_python_toolbox_') as temp_folder: old_cwd = os.getcwd() @TempWorkingDirectorySetter(temp_folder) def f(): # Note that on Mac OS, the working dir will be phrased differently, # so we can't do `assert os.getcwd() == temp_folder`. Instead we'll # create a small file and check we can access it: with pathlib.Path('just_a_file').open('w') as my_file: my_file.write(u'One two three.') with pathlib.Path('just_a_file').open('r') as my_file: assert my_file.read() == 'One two three.' f() cute_testing.assert_polite_wrapper(f) with (temp_folder / 'just_a_file').open('r') as my_file: assert my_file.read() == 'One two three.' assert os.getcwd() == old_cwd
def test_exception(): '''Test `TempWorkingDirectorySetter` recovering from exception in suite.''' # Not using `assert_raises` here because getting the `with` suite in there # would be tricky. with temp_file_tools.create_temp_folder( prefix='test_python_toolbox_') as temp_folder: old_cwd = os.getcwd() try: with TempWorkingDirectorySetter(temp_folder): # Note that on Mac OS, the working dir will be phrased # differently, so we can't do `assert os.getcwd() == # temp_folder`. Instead we'll create a small file and check we # can access it: with pathlib.Path('just_a_file').open('w') as my_file: my_file.write(u'One two three.') with pathlib.Path('just_a_file').open('r') as my_file: assert my_file.read() == 'One two three.' raise MyException except MyException: with (temp_folder / 'just_a_file').open('r') as my_file: assert my_file.read() == 'One two three.' else: raise Exception with (temp_folder / 'just_a_file').open('r') as my_file: assert my_file.read() == 'One two three.'
def test_error_in_overwrite_argument(): with temp_file_tools.create_temp_folder(prefix='pysnooper') as folder: with pytest.raises(Exception, match='can only be used when writing'): @pysnooper.snoop(overwrite=True) def my_function(foo): x = 7 y = 8 return y + x
def test_file_output(): with temp_file_tools.create_temp_folder(prefix='snoop') as folder: path = folder / 'foo.log' config = Config(out=path) contents = u'stuff' config.write(contents) with path.open() as output_file: output = output_file.read() assert output == contents
def test_no_overwrite_by_default(): with temp_file_tools.create_temp_folder(prefix='snoop') as folder: path = folder / 'foo.log' with path.open('w') as output_file: output_file.write(u'lala') config = Config(str(path)) config.write(u' doo be doo') with path.open() as output_file: output = output_file.read() assert output == u'lala doo be doo'
def test_exception(): try: with create_temp_folder() as tf1: assert isinstance(tf1, pathlib.Path) assert tf1.exists() assert tf1.is_dir() file_path = (tf1 / 'my_file') with file_path.open('w') as my_file: my_file.write('Woo hoo!') assert file_path.exists() assert file_path.is_file() raise MyException except MyException: assert not tf1.exists() assert not tf1.is_dir() assert not file_path.exists() assert not file_path.is_file()
def test_chinese(): with temp_file_tools.create_temp_folder(prefix='pysnooper') as folder: path = folder / 'foo.log' @pysnooper.snoop(path) def foo(): a = 1 x = '失败' return 7 foo() with path.open(encoding='utf-8') as file: output = file.read() assert_output( output, (CallEntry(), LineEntry(), VariableEntry('a'), LineEntry(u"x = '失败'"), VariableEntry(u'x', (u"'失败'" if pycompat.PY3 else None)), LineEntry(), ReturnEntry(), ReturnValueEntry('7')), )
def test(): with temp_file_tools.create_temp_folder( prefix='test_python_toolbox_') as temp_folder: assert set(temp_folder.glob('*')) == set() file_1 = temp_folder / 'file_1.txt' assert not file_1.exists() file_tools.atomic_create_and_write(file_1, "Meow meow I'm a cat.") assert set(temp_folder.glob('*')) == {file_1} with file_1.open('r') as file: assert file.read() == "Meow meow I'm a cat." ####################################################################### file_2 = temp_folder / 'file_2.txt' with file_tools.atomic_create(file_2) as file: file.write('Hurr durr') assert not file_2.exists() assert len(set(temp_folder.glob('*'))) == 2 assert file_2.exists() assert len(set(temp_folder.glob('*'))) == 2 assert set(temp_folder.glob('*')) == {file_1, file_2} with file_2.open('r') as file: assert file.read() == 'Hurr durr' ####################################################################### file_3 = temp_folder / 'file_3.txt' with cute_testing.RaiseAssertor(ZeroDivisionError): with file_tools.atomic_create(file_3) as file: file.write('bloop bloop bloop') assert not file_3.exists() assert len(set(temp_folder.glob('*'))) == 3 1 / 0 assert not file_3.exists() assert len(set(temp_folder.glob('*'))) == 2 assert set(temp_folder.glob('*')) == {file_1, file_2}
def test_zip(): '''Test `exists` works on zip-imported modules.''' assert not exists('zip_imported_module_bla_bla') with temp_file_tools.create_temp_folder( prefix='test_python_toolbox_') as temp_folder: temp_zip_path = temp_folder / 'archive_with_module.zip' with temp_zip_path.open('wb') as temp_zip_file: temp_zip_file.write(zip_string) assert not exists('zip_imported_module_bla_bla') with sys_tools.TempSysPathAdder(temp_zip_path): assert exists('zip_imported_module_bla_bla') import zip_imported_module_bla_bla assert zip_imported_module_bla_bla.__doc__ == \ ('Module for testing `import_tools.exists` on zip-archived ' 'modules.')
def test(): '''Test basic workings of `TempWorkingDirectorySetter`.''' with temp_file_tools.create_temp_folder( prefix='test_python_toolbox_') as temp_folder: old_cwd = os.getcwd() with TempWorkingDirectorySetter(temp_folder): # Note that on Mac OS, the working dir will be phrased differently, # so we can't do `assert os.getcwd() == temp_dir`. Instead we'll # create a small file and check we can access it: with pathlib.Path('just_a_file').open('w') as my_file: my_file.write(u'One two three.') with pathlib.Path('just_a_file').open('r') as my_file: assert my_file.read() == 'One two three.' with (temp_folder / 'just_a_file').open('r') as my_file: assert my_file.read() == 'One two three.' assert os.getcwd() == old_cwd
def test(): with temp_file_tools.create_temp_folder() as temp_folder: assert isinstance(temp_folder, pathlib.Path) folder_to_zip = temp_folder / "folder_to_zip" folder_to_zip.mkdir() assert isinstance(folder_to_zip, pathlib.Path) (folder_to_zip / "some_file.txt").open("w").write(u"hello there!") (folder_to_zip / "some_other_file.txt").open("w").write(u"hello there again!") import gc gc.collect() # Making PyPy happy. zip_file_path = temp_folder / "archive.zip" assert isinstance(zip_file_path, pathlib.Path) zip_tools.zip_folder(folder_to_zip, temp_folder / "archive.zip") result = set(zip_tools.unzip_in_memory(zip_file_path.open("rb").read())) assert zip_file_path.is_file() # Got two options here because of PyPy shenanigans: assert result == set( ( ("folder_to_zip/some_file.txt", b"hello there!"), ("folder_to_zip/some_other_file.txt", b"hello there again!"), ) ) or result == set( ( ("folder_to_zip/some_file.txt", "hello there!"), ("folder_to_zip/some_other_file.txt", "hello there again!"), ) ) import gc gc.collect() # Making PyPy happy.
def test(): with temp_file_tools.create_temp_folder() as temp_folder: assert isinstance(temp_folder, pathlib.Path) folder_to_zip = (temp_folder / 'folder_to_zip') folder_to_zip.mkdir() assert isinstance(folder_to_zip, pathlib.Path) (folder_to_zip / 'some_file.txt').open('w').write('hello there!') (folder_to_zip / 'some_other_file.txt').open('w').write('hello there again!') zip_file_path = temp_folder / 'archive.zip' assert isinstance(zip_file_path, pathlib.Path) zip_tools.zip_folder(folder_to_zip, temp_folder / 'archive.zip') assert zip_file_path.is_file() assert set(zip_tools.unzip_in_memory( zip_file_path.open('rb').read())) == { ('folder_to_zip/some_file.txt', b'hello there!'), ('folder_to_zip/some_other_file.txt', b'hello there again!'), }
def test(): with temp_file_tools.create_temp_folder() as temp_folder: assert isinstance(temp_folder, pathlib.Path) folder_to_zip = (temp_folder / 'folder_to_zip') folder_to_zip.mkdir() assert isinstance(folder_to_zip, pathlib.Path) (folder_to_zip / 'some_file.txt').open('w').write('hello there!') (folder_to_zip / 'some_other_file.txt').open('w').write( 'hello there again!') zip_file_path = temp_folder / 'archive.zip' assert isinstance(zip_file_path, pathlib.Path) zip_tools.zip_folder(folder_to_zip, temp_folder / 'archive.zip') assert zip_file_path.is_file() assert set( zip_tools.unzip_in_memory(zip_file_path.open('rb').read()) ) == { ('folder_to_zip/some_file.txt', b'hello there!'), ('folder_to_zip/some_other_file.txt', b'hello there again!'), }
def test_zip(): '''Test `exists` works on zip-imported modules.''' assert not exists('zip_imported_module_bla_bla') zip_string = pkg_resources.resource_string(resources_package, 'archive_with_module.zip') with temp_file_tools.create_temp_folder( prefix='test_python_toolbox_') as temp_folder: temp_zip_path = temp_folder / 'archive_with_module.zip' with temp_zip_path.open('wb') as temp_zip_file: temp_zip_file.write(zip_string) assert not exists('zip_imported_module_bla_bla') with sys_tools.TempSysPathAdder(temp_zip_path): assert exists('zip_imported_module_bla_bla') import zip_imported_module_bla_bla assert zip_imported_module_bla_bla.__doc__ == \ ('Module for testing `import_tools.exists` on zip-archived ' 'modules.')
def test_parent_folder(): with create_temp_folder() as tf1: with create_temp_folder(parent_folder=str(tf1)) as tf2: assert isinstance(tf2, pathlib.Path) assert str(tf2).startswith(str(tf1))
def test_prefix_suffix(): with create_temp_folder(prefix='hocus', suffix='pocus') as tf1: assert tf1.name.startswith('hocus') assert tf1.name.endswith('pocus')
def test(): with temp_file_tools.create_temp_folder( prefix='test_python_toolbox_') as temp_folder: get_file_names_set = \ lambda: set(path.name for path in temp_folder.glob('*')) assert not get_file_names_set() file_path = temp_folder / 'meow.txt' string_to_write = "I'm a cat, hear me meow!" assert file_tools.write_to_file_renaming_if_taken( file_path, string_to_write) == len(string_to_write) with file_path.open('r') as file: assert file.read() == string_to_write assert get_file_names_set() == {'meow.txt'} assert file_tools.write_to_file_renaming_if_taken( file_path, string_to_write) == len(string_to_write) assert file_tools.write_to_file_renaming_if_taken( file_path, string_to_write) == len(string_to_write) assert file_tools.write_to_file_renaming_if_taken( file_path, string_to_write) == len(string_to_write) with (temp_folder / 'meow (2).txt').open('r') as last_file_input: assert last_file_input.read() == string_to_write assert get_file_names_set() == {'meow.txt', 'meow (1).txt', 'meow (2).txt', 'meow (3).txt'} with file_tools.create_file_renaming_if_taken(file_path) as last_file: assert not last_file.closed last_file.write(string_to_write[:5]) last_file.write(string_to_write[5:]) assert last_file.closed assert get_file_names_set() == {'meow.txt', 'meow (1).txt', 'meow (2).txt', 'meow (3).txt', 'meow (4).txt'} with pathlib.Path(last_file.name).open('r') as last_file_input: assert last_file_input.read() == string_to_write folder_1 = file_tools.create_folder_renaming_if_taken( temp_folder / 'woof' ) folder_2 = file_tools.create_folder_renaming_if_taken( temp_folder / 'woof' ) folder_3 = file_tools.create_folder_renaming_if_taken( temp_folder / 'woof' ) assert folder_1.name == 'woof' assert folder_2.name == 'woof (1)' assert folder_3.name == 'woof (2)' assert get_file_names_set() == {'meow.txt', 'meow (1).txt', 'meow (2).txt', 'meow (3).txt', 'meow (4).txt', 'woof', 'woof (1)', 'woof (2)'}