"""Test whether file utils works right.""" import os import shutil from flexmock import flexmock import pytest from transcoding.utils import file_utils TEST_FILE_PATH = 'test.txt' TEST_DIR_PATH = '/test_tmp_dir' @pytest.fixture def temp_directory_fixture(tmpdir): """Fixture with test directory which contain two files and subdirectory.""" test_directory = tmpdir.mkdir(TEST_DIR_PATH) test_directory.mkdir('test_dir') file_1 = test_directory.join('f1.txt') file_2 = test_directory.join('f2.txt') file_1.write('content_1') file_2.write('content_2') return test_directory def test_remove_file(): """Test remove_file removes existing file.""" with open(TEST_FILE_PATH, 'w') as file: file.write('test_value') if os.path.isfile(TEST_FILE_PATH): file_utils.remove_file(TEST_FILE_PATH) result = os.path.isfile(TEST_FILE_PATH) assert not result def test_remove_no_existing_file(): """Test remove_file runs with non-existing file without exception.""" try: file_utils.remove_file('some/unreal/path/to/file.py') except FileNotFoundError: pytest.fail('FileNotFound Exception should not raise') def test_clean_directory_failure(temp_directory_fixture): """Test for failure call of clean_directory method.""" # data preparation # mocking (flexmock(os) .should_receive('unlink') .and_raise(OSError())) (flexmock(shutil) .should_receive('rmtree') .and_raise(OSError())) # test functional call file_utils.clean_directory(temp_directory_fixture.strpath) assert len(temp_directory_fixture.listdir()) == 3 def test_clean_directory_success(temp_directory_fixture): """Test for successful call of clean_directory method.""" # checking directory content before cleaning assert len(temp_directory_fixture.listdir()) == 3 # test functional call file_utils.clean_directory(temp_directory_fixture.strpath) # checking directory content after cleaning assert len(temp_directory_fixture.listdir()) == 0