-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathTestLoadProps.py
More file actions
608 lines (475 loc) · 24.8 KB
/
TestLoadProps.py
File metadata and controls
608 lines (475 loc) · 24.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
"""
Comprehensive tests for LoadProps method in DelphiFMX module.
Testing can be run with:
`python -m unittest discover -s tests -p 'TestLoadProps.py'`
or with pytest (for nicer output, though this requires pytest to be installed):
`pytest -v TestLoadProps.py`
Tests cover:
- Valid inputs (str, bytes, PathLike objects)
- Invalid inputs (wrong types, non-existent files)
- UTF-8 path handling
- Edge cases and error conditions
- PathLike objects with unusual behavior
Cross-platform support: Windows, Linux, macOS, Android
"""
import unittest
import os
import sys
import tempfile
import shutil
import platform
from pathlib import Path
# Ensure DelphiFMX module can be found
_test_dir = os.path.dirname(os.path.abspath(__file__))
_module_dir = os.path.dirname(_test_dir)
# Detect platform and architecture for cross-platform support
_system = platform.system()
_is_64bit = sys.maxsize > 2**32
# Detect Android (check for Android-specific indicators)
_is_android = False
if hasattr(sys, 'getandroidapilevel'):
_is_android = True
elif 'ANDROID_ROOT' in os.environ or 'ANDROID_DATA' in os.environ:
_is_android = True
elif _system == 'Linux' and os.path.exists('/system/build.prop'):
_is_android = True
# Determine platform-specific paths and module extensions
if _is_android:
_platform_dir = 'Android64' if _is_64bit else 'Android'
elif _system == 'Windows':
_platform_dir = 'Win64' if _is_64bit else 'Win32'
elif _system == 'Linux':
_platform_dir = 'Linux64'
elif _system == 'Darwin': # macOS
_platform_dir = 'OSX64' if _is_64bit else 'OSX32'
else:
raise NotImplementedError(f"Unsupported platform: {_system}")
# Try to find the module in the pyd directory
_pyd_dir = os.path.join(_module_dir, 'pyd', 'Release', _platform_dir)
# Find and add the directory with the module
import importlib
for _module_ext in importlib.machinery.EXTENSION_SUFFIXES:
_module_file = os.path.join(_pyd_dir, f'DelphiFMX{_module_ext}')
if os.path.exists(_module_file):
if _pyd_dir not in sys.path:
sys.path.insert(0, _pyd_dir)
print(f"Module will be loaded from: {_module_file}")
break
# Import DelphiFMX module - fail loudly if not available
try:
from DelphiFMX import Form
except ImportError as e:
raise ImportError(
f"Failed to import DelphiFMX module.\n"
f"Tried to load from: {_pyd_dir}\n"
f"Platform: {_system}, Android: {_is_android}, Architecture: {_platform_dir}, Extension: {_module_ext}\n"
f"Make sure DelphiFMX{_module_ext} is built and available at:\n"
f" {_module_file}\n"
f"Original error: {e}"
) from e
class FormForTest(Form):
"""Test form class - allows for adding subcomponents at LoadProps."""
pass
class TestLoadProps(unittest.TestCase):
"""Test suite for LoadProps method."""
# Path to the reference .fmx file in tests directory
_TEST_FMX_SOURCE = os.path.join(os.path.dirname(__file__), 'test_form.fmx')
@classmethod
def setUpClass(cls):
"""Set up test fixtures before all tests."""
if not os.path.exists(cls._TEST_FMX_SOURCE):
raise FileNotFoundError(
f"Test .fmx file not found: {cls._TEST_FMX_SOURCE}\n"
"This file must exist for tests to run."
)
# Create a temporary directory for test files
cls.test_dir = tempfile.mkdtemp(prefix='p4d_test_')
# Copy the reference .fmx file to test directory
cls.valid_fmx = os.path.join(cls.test_dir, 'test_form.fmx')
shutil.copy2(cls._TEST_FMX_SOURCE, cls.valid_fmx)
# Create UTF-8 path test directory and copy .fmx file there
utf8_dir = os.path.join(cls.test_dir, '测试_тест_🎉')
os.makedirs(utf8_dir, exist_ok=True)
cls.utf8_fmx = os.path.join(utf8_dir, 'form_测试.fmx')
shutil.copy2(cls._TEST_FMX_SOURCE, cls.utf8_fmx)
@classmethod
def tearDownClass(cls):
"""Clean up test fixtures after all tests."""
try:
shutil.rmtree(cls.test_dir)
except:
pass
def setUp(self):
"""Set up before each test."""
# Create a fresh form for each test
self.form = FormForTest(None)
def tearDown(self):
"""Clean up after each test."""
try:
if hasattr(self, 'form') and self.form:
self.form.Release()
except:
pass
def _copy_fmx_to_path(self, target_path):
"""Helper to copy the test .fmx file to a specific path."""
target_dir = os.path.dirname(target_path)
if target_dir and not os.path.exists(target_dir):
os.makedirs(target_dir, exist_ok=True)
shutil.copy2(self._TEST_FMX_SOURCE, target_path)
return target_path
def _deny_read_access(self, path):
"""Deny read access to a file or directory using platform-specific methods.
Returns a context manager that restores permissions on exit.
Cross-platform: Windows uses win32security, Unix uses os.chmod().
Raises:
ImportError: If win32security is not available (Windows only)
Exception: If setting permissions fails
"""
is_windows = platform.system() == 'Windows'
is_directory = os.path.isdir(path)
if is_windows:
try:
import win32security
import win32api
import ntsecuritycon as con
except ImportError as e:
raise ImportError(
f"win32security module (pywin32) is required for permission testing on Windows. "
f"Install it with: pip install pywin32. Original error: {e}"
) from e
class PermissionRestorer:
def __init__(self, path):
self.path = path
self.user_sid = win32security.LookupAccountName(None, win32api.GetUserName())[0]
def __enter__(self):
dacl = win32security.ACL()
dacl.AddAccessDeniedAce(win32security.ACL_REVISION, con.GENERIC_READ, self.user_sid)
# Use SetNamedSecurityInfo with PROTECTED_DACL to disable inheritance
win32security.SetNamedSecurityInfo(self.path, win32security.SE_FILE_OBJECT,
win32security.DACL_SECURITY_INFORMATION | win32security.PROTECTED_DACL_SECURITY_INFORMATION,
None, None, dacl, None)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
# Restore default permissions
dacl = win32security.ACL()
dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.GENERIC_ALL, self.user_sid)
win32security.SetNamedSecurityInfo(self.path, win32security.SE_FILE_OBJECT,
win32security.DACL_SECURITY_INFORMATION,
None, None, dacl, None)
return False # Don't suppress exceptions
return PermissionRestorer(path)
else:
import stat
class PermissionRestorer:
def __init__(self, path, is_directory):
self.path = path
self.is_directory = is_directory
self.original_mode = os.stat(path).st_mode
def __enter__(self):
# Remove read and execute permissions (execute needed to access files in directory)
os.chmod(self.path, stat.S_IWRITE) # Write-only
return self
def __exit__(self, exc_type, exc_val, exc_tb):
os.chmod(self.path, self.original_mode)
return False # Don't suppress exceptions
return PermissionRestorer(path, is_directory)
def _lock_file(self, file_path):
"""Lock a file exclusively using Windows file locking.
Returns a context manager that unlocks the file on exit.
Windows only - Unix file locking is advisory and not reliable for testing.
Raises:
ImportError: If msvcrt is not available
Exception: If locking the file fails
"""
if platform.system() != 'Windows':
raise NotImplementedError("File locking test only available on Windows - Unix uses advisory locking which is not reliable")
try:
import msvcrt
except ImportError as e:
raise ImportError(
f"msvcrt module is required for file locking on Windows. "
f"Original error: {e}"
) from e
class FileLocker:
def __init__(self, path):
self.path = path
self.handle = None
self.file_size = None
def __enter__(self):
self.handle = open(self.path, 'r+b')
self.file_size = os.path.getsize(self.path)
# Lock the file exclusively (lock entire file: 0 to file size)
msvcrt.locking(self.handle.fileno(), msvcrt.LK_LOCK, self.file_size)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self.handle:
try:
msvcrt.locking(self.handle.fileno(), msvcrt.LK_UNLCK, self.file_size)
finally:
self.handle.close()
return False # Don't suppress exceptions
return FileLocker(file_path)
def _verify_basic_properties_loaded(self, form, msg_prefix=""):
"""Helper to verify that basic form properties were actually loaded from .fmx file.
This ensures LoadProps didn't just return True without doing anything.
"""
self.assertEqual(form.Caption, 'Form1',
f"{msg_prefix}Caption should be 'Form1' after LoadProps")
self.assertEqual(form.ClientWidth, 624,
f"{msg_prefix}ClientWidth should be 624 after LoadProps")
self.assertEqual(form.ClientHeight, 441,
f"{msg_prefix}ClientHeight should be 441 after LoadProps")
# ========== Valid Input Tests ==========
def test_loadprops_with_string_path(self):
"""Test LoadProps with a regular string path."""
result = self.form.LoadProps(self.valid_fmx)
self.assertTrue(result, "LoadProps should return True for valid .fmx file")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_pathlib_path(self):
"""Test LoadProps with pathlib.Path object."""
path_obj = Path(self.valid_fmx)
result = self.form.LoadProps(path_obj)
self.assertTrue(result, "LoadProps should return True for valid Path object")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_utf8_string_path(self):
"""Test LoadProps with UTF-8 characters in string path."""
result = self.form.LoadProps(self.utf8_fmx)
self.assertTrue(result, "LoadProps should return True for UTF-8 path")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_utf8_pathlib_path(self):
"""Test LoadProps with UTF-8 characters in Path object."""
path_obj = Path(self.utf8_fmx)
result = self.form.LoadProps(path_obj)
self.assertTrue(result, "LoadProps should return True for UTF-8 Path object")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_absolute_path(self):
"""Test LoadProps with absolute path."""
abs_path = os.path.abspath(self.valid_fmx)
result = self.form.LoadProps(abs_path)
self.assertTrue(result, "LoadProps should work with absolute path")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_relative_path(self):
"""Test LoadProps with relative path."""
old_cwd = os.getcwd()
try:
os.chdir(self.test_dir)
rel_path = os.path.basename(self.valid_fmx)
result = self.form.LoadProps(rel_path)
self.assertTrue(result, "LoadProps should work with relative path")
self._verify_basic_properties_loaded(self.form)
finally:
os.chdir(old_cwd)
def test_loadprops_with_path_containing_spaces(self):
"""Test LoadProps with path containing spaces."""
space_dir = os.path.join(self.test_dir, 'path with spaces')
space_file = os.path.join(space_dir, 'test file.fmx')
self._copy_fmx_to_path(space_file)
result = self.form.LoadProps(space_file)
self.assertTrue(result, "LoadProps should work with path containing spaces")
self._verify_basic_properties_loaded(self.form)
# ========== Invalid Input Tests ==========
def test_loadprops_with_nonexistent_file(self):
"""Test LoadProps with non-existent file path."""
nonexistent = os.path.join(self.test_dir, 'nonexistent.fmx')
with self.assertRaises(OSError) as context:
self.form.LoadProps(nonexistent)
self.assertIn(nonexistent, str(context.exception))
self.assertIn('not found', str(context.exception))
def test_loadprops_with_none(self):
"""Test LoadProps with None (should raise TypeError)."""
with self.assertRaises(TypeError):
self.form.LoadProps(None)
def test_loadprops_with_empty_filename(self):
"""Test LoadProps with empty string as filename."""
with self.assertRaises(OSError) as context:
self.form.LoadProps('')
self.assertIn('not found', str(context.exception))
def test_loadprops_with_integer(self):
"""Test LoadProps with integer (wrong type)."""
with self.assertRaises(TypeError):
self.form.LoadProps(123)
def test_loadprops_with_wrong_file_content(self):
"""Test LoadProps with file that exists but wrong content."""
txt_file = os.path.join(self.test_dir, 'test_wrong_content.fmx')
with open(txt_file, 'w', encoding='utf-8') as f:
f.write('not a fmx file')
with self.assertRaises(RuntimeError) as context:
self.form.LoadProps(txt_file)
self.assertIn('EParserError', str(context.exception))
def test_loadprops_with_empty_file(self):
"""Test LoadProps with empty file."""
empty_file = os.path.join(self.test_dir, 'empty.fmx')
with open(empty_file, 'w', encoding='utf-8'):
pass
with self.assertRaises(RuntimeError) as context:
self.form.LoadProps(empty_file)
self.assertIn('EReadError', str(context.exception))
# ========== PathLike Object Edge Cases ==========
def test_loadprops_with_custom_pathlike(self):
"""Test LoadProps with custom PathLike object."""
class CustomPathLike:
def __init__(self, path):
self.path = path
def __fspath__(self):
return self.path
result = self.form.LoadProps(CustomPathLike(self.valid_fmx))
self.assertTrue(result, "LoadProps should work with custom PathLike")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_pathlike_raising_exception(self):
"""Test LoadProps with PathLike that raises exception in __fspath__."""
class ExceptionPathLike:
def __fspath__(self):
raise ValueError("Custom exception from __fspath__")
# The exception from __fspath__ should propagate
with self.assertRaises(ValueError) as context:
self.form.LoadProps(ExceptionPathLike())
self.assertIn("Custom exception from __fspath__", str(context.exception))
def test_loadprops_with_pathlike_returning_none(self):
"""Test LoadProps with PathLike that returns None from __fspath__."""
class NonePathLike:
def __fspath__(self):
return None
with self.assertRaises(TypeError) as context:
self.form.LoadProps(NonePathLike())
self.assertIn('Python function `__fspath__` should return value of following type(s): str or bytes. Instead type `NoneType` was returned.', str(context.exception))
def test_loadprops_with_pathlike_returning_integer(self):
"""Test LoadProps with PathLike that returns integer from __fspath__."""
class IntPathLike:
def __fspath__(self):
return 42
with self.assertRaises(TypeError) as context:
self.form.LoadProps(IntPathLike())
self.assertIn('Python function `__fspath__` should return value of following type(s): str or bytes. Instead type `int` was returned.', str(context.exception))
def test_loadprops_with_pathlike_being_not_callable(self):
"""Test LoadProps with PathLike that returns integer from __fspath__."""
class NonCallablePathLike:
def __init__(self, path):
self.__fspath__ = path
with self.assertRaises(TypeError) as context:
self.form.LoadProps(NonCallablePathLike(self.valid_fmx))
self.assertIn('Expected argument type(s): str, bytes or os.PathLike', str(context.exception))
def test_loadprops_with_pathlike_utf8(self):
"""Test LoadProps with custom PathLike returning UTF-8 path."""
class UTF8PathLike:
def __init__(self, path):
self.path = path
def __fspath__(self):
return self.path
result = self.form.LoadProps(UTF8PathLike(self.utf8_fmx))
self.assertTrue(result, "LoadProps should work with UTF-8 PathLike")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_bytes_path(self):
"""Test LoadProps with bytes object as path."""
bytes_path = self.valid_fmx.encode('utf-8')
result = self.form.LoadProps(bytes_path)
self.assertTrue(result, "LoadProps should work with bytes path")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_bytes_path_utf8(self):
"""Test LoadProps with UTF-8 bytes path."""
bytes_path = self.utf8_fmx.encode('utf-8')
result = self.form.LoadProps(bytes_path)
self.assertTrue(result, "LoadProps should work with UTF-8 bytes path")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_pathlike_returning_bytes(self):
"""Test LoadProps with PathLike that returns bytes from __fspath__."""
class BytesPathLike:
def __init__(self, path):
self.path = path
def __fspath__(self):
return self.path.encode('utf-8')
bytes_pathlike = BytesPathLike(self.valid_fmx)
result = self.form.LoadProps(bytes_pathlike)
self.assertTrue(result, "LoadProps should work with PathLike returning bytes")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_pathlike_returning_bytes_utf8(self):
"""Test LoadProps with PathLike returning UTF-8 bytes."""
class UTF8BytesPathLike:
"""PathLike that returns UTF-8 bytes."""
def __init__(self, path):
self.path = path
def __fspath__(self):
return self.path.encode('utf-8')
utf8_bytes_pathlike = UTF8BytesPathLike(self.utf8_fmx)
result = self.form.LoadProps(utf8_bytes_pathlike)
self.assertTrue(result, "LoadProps should work with PathLike returning UTF-8 bytes")
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_pathlike_returning_bytes_invalid_encoding(self):
"""Test LoadProps with PathLike returning bytes with invalid encoding."""
class NonUTF8BytesPathLike:
def __fspath__(self):
return b'\xff\xfe\x00\x01'
if platform.system() == 'Windows':
with self.assertRaises(UnicodeDecodeError) as context:
self.form.LoadProps(NonUTF8BytesPathLike())
self.assertEqual("'utf-8' codec can't decode byte 0xff in position 0: invalid start byte", str(context.exception))
else: # On Linux this is actually valid path, so we actually dont find the file
with self.assertRaises(OSError) as context:
self.form.LoadProps(NonUTF8BytesPathLike())
self.assertIn('not found', str(context.exception))
self.assertIn(os.fsdecode(NonUTF8BytesPathLike().__fspath__()), str(context.exception))
def test_loadprops_overwrites_existing_properties(self):
"""Test that LoadProps overwrites existing form properties."""
self.form.Caption = 'Initial Caption'
self.form.ClientWidth = 100
self.form.ClientHeight = 100
result = self.form.LoadProps(self.valid_fmx)
self.assertTrue(result)
self._verify_basic_properties_loaded(self.form)
def test_loadprops_with_file_no_read_permission(self):
"""Test LoadProps with file that has no read permissions."""
no_read_file = os.path.join(self.test_dir, 'no_read.fmx')
self._copy_fmx_to_path(no_read_file)
with self._deny_read_access(no_read_file):
with self.assertRaises(OSError) as context:
self.form.LoadProps(no_read_file)
self.assertIn('denied', str(context.exception))
self.assertIn('EFOpenError', str(context.exception))
self.assertIn(no_read_file, str(context.exception))
def test_loadprops_with_directory_no_read_permission(self):
"""Test LoadProps with file in directory that has no read permissions."""
no_read_dir = os.path.join(self.test_dir, 'no_read_dir')
os.makedirs(no_read_dir, exist_ok=True)
file_in_no_read_dir = os.path.join(no_read_dir, 'test.fmx')
self._copy_fmx_to_path(file_in_no_read_dir)
with self._deny_read_access(no_read_dir):
with self.assertRaises(OSError) as context:
self.form.LoadProps(file_in_no_read_dir)
# Not readable directory should lead to file not found or permission error
self.assertIn(file_in_no_read_dir, str(context.exception))
self.assertIn('not found', str(context.exception))
def test_loadprops_with_locked_file(self):
"""Test LoadProps with file that is locked by another process.
Windows only - Unix file locking is advisory and not reliable for testing.
"""
if platform.system() != 'Windows':
self.skipTest("File locking test only available on Windows - Unix uses advisory locking")
locked_file = os.path.join(self.test_dir, 'locked.fmx')
self._copy_fmx_to_path(locked_file)
with self._lock_file(locked_file):
with self.assertRaises(OSError) as context:
self.form.LoadProps(locked_file)
self.assertIn(locked_file, str(context.exception))
self.assertIn('EFOpenError', str(context.exception))
def test_loadprops_with_corrupted_binary_file(self):
"""Test LoadProps with file that looks like binary but is corrupted."""
corrupted_file = os.path.join(self.test_dir, 'corrupted.fmx')
# Write some binary data that might look like a FMX but is corrupted
with open(corrupted_file, 'wb') as f:
f.write(b'TPF0') # Valid signature
f.write(b'a' * 100)
with self.assertRaises(RuntimeError) as context:
self.form.LoadProps(corrupted_file)
self.assertTrue(str(context.exception).startswith('EReadError'), f"Expected EReadError, got: {context.exception}")
def run_tests():
"""Run all tests."""
loader = unittest.TestLoader()
suite = unittest.TestSuite()
# Add all test classes
suite.addTests(loader.loadTestsFromTestCase(TestLoadProps))
# Run tests with default unittest runner
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
# Return exit code
return 0 if result.wasSuccessful() else 1
if __name__ == '__main__':
sys.exit(run_tests())