Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions office/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ def _check_first_run(self) -> bool:
Returns:
bool: 如果是首次运行返回True,否则返回False
"""
# 创建标记目录
self.mark_file.parent.mkdir(exist_ok=True)

# 如果标记文件不存在,则是首次运行
if not self.mark_file.exists():
# 创建标记文件
self.mark_file.write_text(f"First run on {platform.system()} at {platform.platform()}")
return True
try:
# 创建标记目录
self.mark_file.parent.mkdir(exist_ok=True)

# 如果标记文件不存在,则是首次运行
if not self.mark_file.exists():
# 创建标记文件
self.mark_file.write_text(f"First run on {platform.system()} at {platform.platform()}")
return True
except OSError:
# 兼容性提示不应影响主包导入;HOME 只读或不可写时跳过首次运行提示。
return False
return False

def get_compatibility_info(self) -> Dict[str, List[str]]:
Expand Down Expand Up @@ -235,10 +239,6 @@ def check_compatibility():
return checker


# 在模块导入时自动检查兼容性
compatibility_checker = check_compatibility()


if __name__ == "__main__":
# 测试代码
checker = CrossPlatformCompatibility()
Expand All @@ -247,4 +247,4 @@ def check_compatibility():
print(f"标记文件: {checker.mark_file}")

# 显示警告(仅在非Windows且首次运行时)
checker.display_warning()
checker.display_warning()
9 changes: 9 additions & 0 deletions tests/test_code/test_optional_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ def test_import_office_without_windows_only_dependencies(self):
with self._optional_import_test_environment():
importlib.import_module("office")

def test_compatibility_check_does_not_fail_when_mark_file_cannot_be_written(self):
with self._optional_import_test_environment():
compatibility = importlib.import_module("office.compatibility")

with mock.patch.object(compatibility.Path, "mkdir", side_effect=PermissionError("readonly")):
checker = compatibility.CrossPlatformCompatibility()

self.assertFalse(checker.is_first_run)

def test_loader_preserves_dependency_internal_import_errors(self):
with self._optional_import_test_environment():
importlib.import_module("office")
Expand Down