Skip to content

Commit 4dec89f

Browse files
committed
Fix compare_files missing file handling
1 parent cb6d09c commit 4dec89f

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

src/settings.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ def compare_project_file(filename):
114114

115115

116116
def compare_files(file1_path, file2_path):
117-
# if either of the files does not exist, return False
117+
# return False if either path is missing or the file does not exist
118+
if not file1_path or not file2_path:
119+
return False
118120
if not os.path.exists(file1_path) or not os.path.exists(file2_path):
119-
return True
121+
return False
120122

121123
try:
122124
with open(file1_path, "rb") as file1:
@@ -131,7 +133,7 @@ def compare_files(file1_path, file2_path):
131133

132134
except FileNotFoundError:
133135
logging.error("Error during file comparison!")
134-
return True
136+
return False
135137

136138

137139
def create_temporary_project_files():

test/compare_files_test.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os
2+
import sys
3+
import types
4+
import tempfile
5+
import unittest
6+
from unittest import mock
7+
8+
import qt_stubs # noqa: F401
9+
10+
# Provide a minimal vtk stub before importing settings
11+
vtk_stub = types.ModuleType("vtk")
12+
13+
14+
class DummyNamedColors:
15+
def GetColor3d(self, key):
16+
return (0.0, 0.0, 0.0)
17+
18+
19+
vtk_stub.vtkNamedColors = DummyNamedColors
20+
sys.modules["vtk"] = vtk_stub
21+
22+
from src.settings import compare_files
23+
24+
25+
class CompareFilesTest(unittest.TestCase):
26+
def test_missing_first_file_returns_false(self):
27+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
28+
temp_file.write(b"data")
29+
existing_path = temp_file.name
30+
missing_path = existing_path + "_missing"
31+
self.assertFalse(compare_files(missing_path, existing_path))
32+
os.remove(existing_path)
33+
34+
def test_missing_second_file_returns_false(self):
35+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
36+
temp_file.write(b"data")
37+
existing_path = temp_file.name
38+
missing_path = existing_path + "_missing"
39+
self.assertFalse(compare_files(existing_path, missing_path))
40+
os.remove(existing_path)
41+
42+
def test_both_files_missing_returns_false(self):
43+
import uuid
44+
45+
missing1 = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex)
46+
missing2 = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex)
47+
self.assertFalse(compare_files(missing1, missing2))
48+
49+
def test_file_not_found_during_read_returns_false(self):
50+
with tempfile.NamedTemporaryFile(delete=False) as f1:
51+
path1 = f1.name
52+
with tempfile.NamedTemporaryFile(delete=False) as f2:
53+
path2 = f2.name
54+
with mock.patch("builtins.open", side_effect=FileNotFoundError()):
55+
self.assertFalse(compare_files(path1, path2))
56+
os.remove(path1)
57+
os.remove(path2)
58+
59+
60+
if __name__ == "__main__":
61+
unittest.main()

0 commit comments

Comments
 (0)