/
georgiy
/
work
Обзор
Документация
Войти
/
georgiy
/
work
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
task1/tests/test_memoryfs.py
79 строк
2 KB
Georgiy Yankovskiy
Task1
17 ноя 2024, 01:47
17 ноя 2024, 01:47
bdefd3a
Код
Авторство
О чём код?
import os import shutil import subprocess import tempfile import time import unittest class TestOSInteraction(unittest.TestCase): @classmethod def setUpClass(cls): cls.tmp_dir = tempfile.mkdtemp(prefix="memfs_") cls.mount_point = os.path.join(cls.tmp_dir, "memfs") os.makedirs(cls.mount_point) cls.fuse_process = subprocess.Popen( ["python", "-m", "src.memoryfs", cls.mount_point], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True ) # Ждём немного, чтобы FUSE успел смонтировать файловую систему time.sleep(1) @classmethod def tearDownClass(cls): # Завершаем процесс FUSE if cls.fuse_process.poll() is None: cls.fuse_process.terminate() cls.fuse_process.wait(timeout=2) try: subprocess.run(["fusermount", "-u", cls.mount_point], check=True) shutil.rmtree(cls.tmp_dir) except Exception: pass def test_create_file(self): filepath = os.path.join(self.mount_point, "test.txt") with open(filepath, "w") as f: f.write("Hello, World!") self.assertTrue(os.path.exists(filepath)) def test_read_file(self): filepath = os.path.join(self.mount_point, "another_test.txt") with open(filepath, "w+") as f: f.write("Hello, World!") with open(filepath, "r") as f: content = f.read() self.assertEqual(content, "Hello, World!") def test_delete_file(self): filepath = os.path.join(self.mount_point, "delete_Test.txt") with open(filepath, "w"): pass os.remove(filepath) self.assertFalse(os.path.exists(filepath)) def test_create_directory(self): dirpath = os.path.join(self.mount_point, "hello_world") os.mkdir(dirpath) self.assertTrue(os.path.isdir(dirpath)) def test_list_directory(self): dirpath = os.path.join(self.mount_point, "test_dir") os.mkdir(dirpath) files = os.listdir(self.mount_point) self.assertIn("test_dir", files) def test_delete_directory(self): dirpath = os.path.join(self.mount_point, "test_dir") os.mkdir(dirpath) shutil.rmtree(dirpath) self.assertFalse(os.path.exists(dirpath)) if __name__ == "__main__": unittest.main()