Skip to content

refactor: replace manual temp file handling with TempDir utility#1565

Merged
dengbo11 merged 1 commit into
OpenAtom-Linyaps:masterfrom
myml:fix-test
Jan 22, 2026
Merged

refactor: replace manual temp file handling with TempDir utility#1565
dengbo11 merged 1 commit into
OpenAtom-Linyaps:masterfrom
myml:fix-test

Conversation

@myml

@myml myml commented Jan 22, 2026

Copy link
Copy Markdown
Collaborator

Removed custom temporary file management code from test fixtures and replaced it with the existing TempDir utility class. This change eliminates duplicate code for creating and cleaning up temporary files in multiple test files. The TempDir utility provides better resource management through RAII pattern, ensuring proper cleanup even if tests fail. This improves code maintainability and reduces the risk of resource leaks.

Influence:

  1. Verify all driver detection tests still pass
  2. Confirm file lock functionality tests work correctly
  3. Check that temporary files are properly cleaned up after tests
  4. Ensure no file permission issues with new temp directory approach
  5. Test inter-process locking behavior remains consistent

refactor: 使用 TempDir 工具类替换手动临时文件处理

从测试夹具中移除自定义的临时文件管理代码,改用现有的 TempDir 工具类。此
更改消除了多个测试文件中创建和清理临时文件的重复代码。TempDir 工具通过
RAII 模式提供更好的资源管理,确保即使测试失败也能正确清理。这提高了代码
的可维护性并降低了资源泄漏的风险。

Influence:

  1. 验证所有驱动检测测试仍然通过
  2. 确认文件锁功能测试正常工作
  3. 检查临时文件在测试后是否正确清理
  4. 确保新的临时目录方法没有文件权限问题
  5. 测试进程间锁定行为保持一致

Removed custom temporary file management code from test fixtures
and replaced it with the existing TempDir utility class. This change
eliminates duplicate code for creating and cleaning up temporary files
in multiple test files. The TempDir utility provides better resource
management through RAII pattern, ensuring proper cleanup even if
tests fail. This improves code maintainability and reduces the risk of
resource leaks.

Influence:
1. Verify all driver detection tests still pass
2. Confirm file lock functionality tests work correctly
3. Check that temporary files are properly cleaned up after tests
4. Ensure no file permission issues with new temp directory approach
5. Test inter-process locking behavior remains consistent

refactor: 使用 TempDir 工具类替换手动临时文件处理

从测试夹具中移除自定义的临时文件管理代码,改用现有的 TempDir 工具类。此
更改消除了多个测试文件中创建和清理临时文件的重复代码。TempDir 工具通过
RAII 模式提供更好的资源管理,确保即使测试失败也能正确清理。这提高了代码
的可维护性并降低了资源泄漏的风险。

Influence:
1. 验证所有驱动检测测试仍然通过
2. 确认文件锁功能测试正常工作
3. 检查临时文件在测试后是否正确清理
4. 确保新的临时目录方法没有文件权限问题
5. 测试进程间锁定行为保持一致
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @myml, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the way temporary files are managed within various test suites. By migrating from custom, manual setup and teardown procedures to the TempDir utility, the change centralizes temporary resource handling. This not only streamlines test code by removing repetitive logic but also enhances reliability by guaranteeing proper cleanup through RAII, thereby preventing potential resource leaks and improving overall test stability.

Highlights

  • Refactoring Temporary File Handling: Replaced custom, manual temporary file creation and cleanup logic in test fixtures with the TempDir utility class across multiple test files.
  • Improved Resource Management: Leveraged the TempDir utility's RAII (Resource Acquisition Is Initialization) pattern to ensure temporary files and directories are properly cleaned up automatically, even if tests fail.
  • Code Maintainability and Duplication Reduction: Eliminated duplicate code for temporary file management, leading to cleaner, more maintainable test code and reducing the risk of resource leaks.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@deepin-ci-robot

Copy link
Copy Markdown
Collaborator

deepin pr auto review

这段代码的改动主要是将测试代码中的临时文件管理方式从手动管理(使用 std::filesystem::temp_directory_pathSetUp/TearDown)迁移到了使用一个名为 TempDir 的辅助类。

以下是详细的代码审查和改进建议:

1. 语法与逻辑审查

  • 改动逻辑正确性

    • 代码移除了测试固件(Test Fixture)中的 SetUpTearDown 方法,以及成员变量 configFilePath / versionFilePath / temp_path。这是合理的,因为原本的逻辑是在每个测试用例开始时删除可能存在的临时文件,并在结束时清理。
    • 新引入的 TempDir temp_dir; 在每个测试用例(TEST_F)内部定义。根据 RAII(资源获取即初始化)原则,TempDir 的构造函数应该负责创建一个唯一的临时目录,析构函数负责删除它。这比手动管理更安全,且避免了测试用例之间的状态污染。
    • 文件路径拼接逻辑从 GetTempFilePath() 改为了 temp_dir.path() / "filename",逻辑上等价,但隔离性更好。
  • 潜在逻辑风险(未展示部分)

    • 代码审查中未包含 TempDir 类的定义。必须确保 TempDir 的实现满足以下条件:
      1. 原子性/唯一性:构造函数生成的目录路径必须是唯一的(例如包含进程ID或随机字符串),防止多线程/多进程测试并发运行时发生冲突。
      2. 自动清理:析构函数必须能够递归删除目录及其内容,即使测试失败或抛出异常。
      3. 异常安全:如果创建目录失败,构造函数应抛出异常或提供明确的检查机制。

2. 代码质量

  • 改进点
    • 代码简洁性:移除了大量的样板代码(SetUp, TearDown, GetTempFilePath),测试代码更加专注于测试逻辑本身。
    • 可读性TempDir temp_dir; 的语义非常清晰,读者一眼就能看出这是一个临时目录的生命周期范围。
    • 消除重复:之前每个测试文件都重复定义了类似的 GetTempFilePathSetUp/TearDown 逻辑,现在复用了 common/tempdir.h,符合 DRY (Don't Repeat Yourself) 原则。

3. 代码性能

  • 影响
    • 性能影响极小。std::filesystem::temp_directory_path 的调用次数减少了(从每个测试用例调用一次变为每个测试用例构造 TempDir 时调用一次)。
    • 如果 TempDir 实现中使用了随机数生成器来创建唯一目录名,可能会比简单的字符串拼接有极微小的开销,但这对于测试环境通常可以忽略不计。

4. 代码安全

  • 安全性提升
    • 避免竞态条件:旧代码使用固定的文件名(如 test_config.json),如果两个测试进程同时运行,可能会互相干扰。新的实现假设 TempDir 会生成唯一的目录名,从而解决了这个问题。
    • 资源泄漏防护:旧代码依赖 TearDown 清理文件。如果测试用例中间 ASSERT_* 失败导致提前退出,或者测试代码抛出异常,TearDown 可能会被跳过(取决于 gtest 的实现和异常设置),导致临时文件残留。RAII 风格的 TempDir 利用栈对象析构函数保证清理,更加安全。

5. 改进建议

虽然代码改动本身是很好的重构,但为了确保万无一失,建议对 TempDir 类或其使用方式做以下检查:

  1. 检查 TempDir 的实现

    • 确保它使用了 std::filesystem::create_directories
    • 确保析构函数中使用了 std::filesystem::remove_all 来清理目录。
    • 确保路径生成包含足够的随机性(例如使用 std::tmpnam 或 UUID)。
  2. 路径处理细节

    • 当前代码中 auto configFilePath = temp_dir.path() / "test_config.json"; 是正确的。但要注意,如果 temp_dir.path() 返回的路径末尾带有分隔符(虽然 std::filesystem::path 通常处理得很好),或者路径中有特殊字符,operator/ 应该能正确处理。
  3. 跨平台兼容性

    • std::filesystem 在 C++17 中引入。确保项目编译标准至少为 C++17。
    • 确保在 Windows 和 Linux/macOS 上,TempDir 生成的路径格式都是合法的。
  4. 测试用例中的变量命名

    • TEST_F 内部,auto configFilePath = ... 定义的变量遮蔽了旧代码中类成员变量的位置。这是好事,因为作用域更小了。但要注意不要在同一个测试用例的不同作用域(如 lambda 或 if 块)中意外重用该变量名导致混淆。

总结
这是一个高质量的代码重构,通过引入 TempDir 辅助类,显著提升了测试代码的健壮性、可维护性和安全性。只要 TempDir 类本身的实现是健壮的(特别是唯一目录生成和异常安全的析构),这个改动就是完全正向的。

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次重构成功地将手动临时文件处理替换为 TempDir 工具类,这是一个很好的改进,有助于资源管理。然而,这也导致在多个测试文件中引入了大量重复代码。我的审查意见主要集中在如何通过重新引入利用 TempDir 的测试夹具来消除这些重复,从而提高代码的可维护性。

Comment on lines 23 to 25
class FileLockTest : public ::testing::Test
{
protected:
std::filesystem::path temp_path;

void SetUp() override
{
temp_path = GetTempFilePath();
// Ensure the file does not exist initially
std::filesystem::remove(temp_path);
}

void TearDown() override { std::filesystem::remove(temp_path); }
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

此文件中的所有测试用例都重复了 TempDirtemp_path 的创建逻辑。为了消除这种重复并提高代码的可维护性,建议将此逻辑移回 FileLockTest 夹具中。

class FileLockTest : public ::testing::Test
{
protected:
    TempDir temp_dir;
    std::filesystem::path temp_path = temp_dir.path() / "test_filelock.lock";
};

Comment on lines +87 to +88
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

Comment on lines +106 to +107
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

Comment on lines +51 to +52
TempDir temp_dir;
auto configFilePath = temp_dir.path() / "test_config.json";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将 TempDirconfigFilePath 移至测试夹具后,可以删除此处的重复代码。

Comment on lines 17 to 19
class DriverDetectorTest : public ::testing::Test
{
protected:
void SetUp() override
{
versionFilePath = GetTempFilePath();
// Ensure the file does not exist initially
std::filesystem::remove(versionFilePath);
ASSERT_FALSE(versionFilePath.empty()) << "Failed to create temporary directory";
}

void TearDown() override { std::filesystem::remove(versionFilePath); }

std::filesystem::path versionFilePath;
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

此文件中的一些测试需要临时文件,而另一些则不需要。为了避免在不需要的测试中创建不必要的 TempDir,同时减少需要它的测试中的代码重复,建议创建一个新的测试夹具,例如 DriverDetectorTempFileTest。 然后,需要临时文件的测试可以从这个新的夹具继承。

class DriverDetectorTest : public ::testing::Test
{
};

class DriverDetectorTempFileTest : public ::testing::Test
{
protected:
    TempDir temp_dir;
    std::filesystem::path versionFilePath = temp_dir.path() / "test_version";
};

Comment on lines +372 to +373
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

Comment on lines +386 to +387
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

Comment on lines +401 to +402
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

Comment on lines +423 to +424
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

Comment on lines +437 to +438
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

@codecov

codecov Bot commented Jan 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@deepin-ci-robot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengbo11, myml

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@dengbo11 dengbo11 merged commit 4994466 into OpenAtom-Linyaps:master Jan 22, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants