def test_is_dir_empty(): work_dir = os.getcwd() test_dir_path = os.path.join(work_dir, 'dir') deleted_file_path = os.path.join(test_dir_path, 'deleted2.txt') deleted_file2_path = os.path.join(test_dir_path, 'deleted.txt') mkdir(test_dir_path) ntfile = NTFile(test_dir_path) ntfile2 = NTFile(deleted_file_path) try: assert ntfile.is_dir_empty touch(deleted_file_path) touch(deleted_file2_path) assert not ntfile.is_dir_empty ntfile2.open(Access.DELETE, Share.DELETE) ntfile2.dispose() assert not ntfile.is_dir_empty rm(deleted_file2_path) assert ntfile.is_dir_empty finally: ntfile.close() ntfile2.close()
def test_rename(): work_dir = os.getcwd() test_file_path = os.path.join(work_dir, 'test_rename.txt') touch(test_file_path) ntfile = NTFile(test_file_path) ntfile.open(Access.READ_DATA) try: with pytest.raises(NTException): ntfile.rename(os.path.join(work_dir, 'test_rename2.txt')) finally: ntfile.close() ntfile.rename(os.path.join(work_dir, 'test_rename2.txt'))
def test_dispose(): work_dir = os.getcwd() test_file_path = os.path.join(work_dir, "test_dispose.txt") touch(test_file_path) ntfile = NTFile(test_file_path) ntfile.open(Access.READ_DATA) try: with pytest.raises(NTException): ntfile.dispose() finally: ntfile.close() ntfile.dispose()
def test_open_file_in_dir(): work_dir = os.getcwd() test_dir_path = os.path.join(work_dir, 'dir') mkdir(test_dir_path) touch(os.path.join(test_dir_path, 'toto.txt')) try: ntfile = NTFile(test_dir_path) ntfile.open() ntfile2 = NTFile('toto.txt', parent=ntfile) ntfile2.open() finally: ntfile.close() ntfile2.close()
def test_move_to_trash(): work_dir = os.getcwd() test_file_path = os.path.join(work_dir, 'test_mv_to_trash.txt') touch(test_file_path) ntfile = NTFile(test_file_path) ntfile.open(Access.READ_DATA) try: with pytest.raises(NTException): ntfile.move_to_trash() finally: ntfile.close() trash_path = ntfile.trash_path ntfile.move_to_trash() rm(trash_path)
def test_unlink(): work_dir = os.getcwd() test_dir_path = os.path.join(work_dir, 'dir') deleted_file_path = os.path.join(test_dir_path, 'deleted2.txt') mkdir(test_dir_path) ntfile = NTFile(test_dir_path) ntfile3 = NTFile(test_dir_path) ntfile2 = NTFile(deleted_file_path) try: # delete inexisting file ntfile2.unlink() # delete file with readonly attribute touch(deleted_file_path) ntfile2.read_attributes() ntfile2.basic_info.file_attributes.attr |= FileAttribute.READONLY ntfile2.write_attributes() ntfile2.unlink() # delete file already pending deletion touch(deleted_file_path) ntfile2.open(Access.DELETE, Share.DELETE) ntfile2.dispose() ntfile2.unlink() # delete containing directory ntfile.unlink() ntfile.close() ntfile2.close() mkdir(test_dir_path) ntfile.open(Access.LIST_DIRECTORY, Share.ALL) ntfile3.unlink() finally: ntfile.close() ntfile2.close() ntfile2.close()
def test_write_attributes(): work_dir = os.getcwd() test_file_path = os.path.join(work_dir, 'test_write_attr_file.txt') touch(test_file_path) ntfile = NTFile(test_file_path) ntfile.read_attributes() ntfile.open(Access.READ_ATTRS) ntfile.basic_info.change_time = FileTime( datetime.now() - timedelta(seconds=3600)) try: with pytest.raises(NTException): ntfile.write_attributes() finally: ntfile.close() ntfile.basic_info.change_time = FileTime( datetime.now() - timedelta(days=3)) ntfile.write_attributes() assert datetime.now() - \ ntfile.basic_info.change_time.as_datetime > \ timedelta(seconds=3000)
def test_unlink(): work_dir = os.getcwd() test_dir_path = os.path.join(work_dir, 'dir') deleted_file_path = os.path.join(test_dir_path, 'deleted2.txt') mkdir(test_dir_path) ntfile = NTFile(test_dir_path) ntfile3 = NTFile(test_dir_path) ntfile2 = NTFile(deleted_file_path) try: # delete inexisting file ntfile2.unlink() # delete file with readonly attribute touch(deleted_file_path) ntfile2.read_attributes() ntfile2.basic_info.file_attributes.attr |= FileAttribute.READONLY assert 'READONLY' in str(ntfile2.basic_info.file_attributes) ntfile2.write_attributes() ntfile2.unlink() # delete file already pending deletion touch(deleted_file_path) ntfile2.open(Access.DELETE, Share.DELETE) ntfile2.dispose() ntfile2.unlink() # delete containing directory ntfile.unlink() ntfile.close() ntfile2.close() mkdir(test_dir_path) ntfile.open(Access.LIST_DIRECTORY, Share.ALL) ntfile3.unlink() finally: ntfile.close() ntfile2.close() ntfile2.close() ntfile = NTFile('nul') with pytest.raises(NTException) as err: ntfile.unlink() ntfile.close() assert 'NTFile.read_attributes:' in str(err) # A directory that is not empty cannot be deleted dir_to_delete = os.path.join(test_dir_path, 'dir_to_delete') mkdir(dir_to_delete) touch(os.path.join(dir_to_delete, 'afile.txt')) ntfile = NTFile(dir_to_delete) try: with pytest.raises(NTException) as err: ntfile.unlink() finally: ntfile.close() # A directory that is already opened and not empty cannot be # moved to trash dir_to_delete = os.path.join(test_dir_path, 'dir_to_delete') mkdir(dir_to_delete) touch(os.path.join(dir_to_delete, 'afile.txt')) ntfile = NTFile(dir_to_delete) ntfile2 = NTFile(dir_to_delete) try: ntfile.open(Access.LIST_DIRECTORY, Share.ALL) with pytest.raises(NTException) as err: ntfile2.unlink() finally: ntfile.close() ntfile2.close() # Try to delete a file that we cannot open ntfile = NTFile(deleted_file_path) ntfile2 = NTFile(deleted_file_path) try: touch(deleted_file_path) ntfile.open(Access.READ_DATA, Share.NOTHING) with pytest.raises(NTException) as err: ntfile2.unlink() finally: ntfile.close() ntfile2.close()