import os
import tempfile
import unittest
from olc.utils.file_utils import FileUtils
class TestFileUtils(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.properties_file = os.path.join(self.temp_dir, "test.properties")
with open(self.properties_file, "w", encoding="utf-8") as f:
f.write("# Test properties file\n")
f.write("key1=value1\n")
f.write("key2=value2\n")
self.json_file = os.path.join(self.temp_dir, "test.json")
with open(self.json_file, "w", encoding="utf-8") as f:
f.write('{"key1": "value1", "key2": "value2"}\n')
def tearDown(self):
for file_path in [self.properties_file, self.json_file]:
if os.path.exists(file_path):
os.remove(file_path)
os.rmdir(self.temp_dir)
def test_is_valid_path(self):
self.assertTrue(FileUtils.is_valid_path("/valid/path/file.txt"))
self.assertTrue(FileUtils.is_valid_path("C:\\valid\\path\\file.txt"))
self.assertFalse(FileUtils.is_valid_path(""))
self.assertFalse(FileUtils.is_valid_path("/invalid<path/file.txt"))
self.assertFalse(FileUtils.is_valid_path("/invalid>path/file.txt"))
self.assertFalse(FileUtils.is_valid_path("/invalid\"path/file.txt"))
self.assertFalse(FileUtils.is_valid_path("/invalid|path/file.txt"))
self.assertFalse(FileUtils.is_valid_path("/invalid?path/file.txt"))
self.assertFalse(FileUtils.is_valid_path("/invalid*path/file.txt"))
def test_exists(self):
self.assertTrue(FileUtils.exists(self.properties_file))
non_existent_file = os.path.join(self.temp_dir, "non_existent.txt")
self.assertFalse(FileUtils.exists(non_existent_file))
self.assertFalse(FileUtils.exists(self.temp_dir))
def test_read_properties_file(self):
props = FileUtils.read_properties_file(self.properties_file)
self.assertEqual(props["key1"], "value1")
self.assertEqual(props["key2"], "value2")
non_existent_file = os.path.join(self.temp_dir, "non_existent.properties")
props = FileUtils.read_properties_file(non_existent_file)
self.assertEqual(props, {})
if __name__ == "__main__":
unittest.main()