Skip to content

chore(tests): adapt unit tests for Qt6 compatibility and fix lcov#425

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/qt6-compatibility-and-test-fix
Jun 8, 2026
Merged

chore(tests): adapt unit tests for Qt6 compatibility and fix lcov#425
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/qt6-compatibility-and-test-fix

Conversation

@pengfeixx
Copy link
Copy Markdown
Contributor

  • Add QMouseEvent Qt5/Qt6 constructor compatibility in 5 test files
  • Replace Stub with subclass in test_slotQuery to avoid ASan conflict
  • Fix lcov coverage extraction path from /src/ to /3rdparty/
  • Add LD_LIBRARY_PATH in cmake-lcov-test.sh for runtime library loading
  • Use ${QT_DESIRED_VERSION} instead of hardcoded Qt5 in CMake

适配Qt6兼容性:5个测试文件添加QMouseEvent Qt5/Qt6构造函数兼容性,
使用子类替代Stub避免ASan冲突,修复lcov覆盖率提取路径,
添加LD_LIBRARY_PATH环境变量。

Log: 适配Qt6兼容性并修复单元测试覆盖率问题
Influence: 修复后单元测试可在Qt6环境下正常编译运行,代码覆盖率报告正确生成。

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Sorry @pengfeixx, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

- Add QMouseEvent Qt5/Qt6 constructor compatibility in 5 test files
- Replace Stub with subclass in test_slotQuery to avoid ASan conflict
- Fix lcov coverage extraction path from */src/* to */3rdparty/*
- Add LD_LIBRARY_PATH in cmake-lcov-test.sh for runtime library loading
- Use ${QT_DESIRED_VERSION} instead of hardcoded Qt5 in CMake

适配Qt6兼容性:5个测试文件添加QMouseEvent Qt5/Qt6构造函数兼容性,
使用子类替代Stub避免ASan冲突,修复lcov覆盖率提取路径,
添加LD_LIBRARY_PATH环境变量。

Log: 适配Qt6兼容性并修复单元测试覆盖率问题
Influence: 修复后单元测试可在Qt6环境下正常编译运行,代码覆盖率报告正确生成。
@pengfeixx pengfeixx force-pushed the fix/qt6-compatibility-and-test-fix branch from d0e08a7 to 3e2431f Compare June 8, 2026 08:39
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

根据提供的git diff,我将从语法逻辑、代码质量、代码性能和代码安全四个方面进行分析,并提出改进建议:

1. CMakeLists.txt 的修改

语法逻辑和代码质量:

  • find_package(Qt5Core) 改为 find_package(Qt${QT_DESIRED_VERSION} REQUIRED COMPONENTS Core) 是一个改进,增加了灵活性,允许指定Qt版本。
  • 相应地将 target_link_libraries(${PROJECT_NAME} Qt5::Core) 也改为 target_link_libraries(${PROJECT_NAME} Qt${QT_DESIRED_VERSION}::Core) 是一致的改进。

改进建议:

  • 考虑添加版本检查,确保 QT_DESIRED_VERSION 已定义:
    if(NOT DEFINED QT_DESIRED_VERSION)
        set(QT_DESIRED_VERSION 5)
    endif()

2. Qt版本相关的测试代码修改

语法逻辑和代码质量:

  • 在多个测试文件中添加了Qt 6.0的兼容性代码,使用 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 条件编译。
  • 这些修改确保了代码在Qt 5和Qt 6上的兼容性。

改进建议:

  • 考虑将重复的版本检查代码提取为宏或辅助函数,减少代码重复:
    #ifdef CREATE_QT6_EVENT
        #define CREATE_MOUSE_EVENT(eventType, pos) \
            new QMouseEvent(eventType, QPointF(pos), QPointF(pos), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)
    #else
        #define CREATE_MOUSE_EVENT(eventType, pos) \
            new QMouseEvent(eventType, QPointF(pos), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)
    #endif

3. 测试代码中的事件处理

代码安全和性能:

  • 在多个测试中创建了 QMouseEvent 对象并在测试后删除,这是正确的内存管理。
  • 使用了 QPointF 而不是 QPoint,这可能是为了Qt 6的兼容性。

改进建议:

  • 考虑使用 Qt 的测试框架提供的工具函数来创建事件,而不是手动创建和删除:
    QTest::mousePress(m_tester, Qt::LeftButton, Qt::NoModifier, QPoint(50, 50));

4. 测试代码中的桩(Stub)替换

代码安全和性能:

  • ut_mainwindow.cpp 中,将二进制桩替换为子类实现,避免了ASan与桩框架冲突导致的DEADLYSIGNAL问题。

改进建议:

  • 这种方法很好,但可以考虑进一步封装测试辅助类,使测试代码更简洁:
    class TestQueryHelper {
    public:
        static void executeNoOpQuery(MainWindow* tester) {
            TestOverwriteQuery query("", tester);
            tester->slotQuery(&query);
        }
    };
    
    TEST_F(UT_MainWindow, test_slotQuery) {
        TestQueryHelper::executeNoOpQuery(m_tester);
    }

5. 脚本文件的修改

代码安全和性能:

  • cmake-lcov-test.sh 中添加了库路径设置,确保测试能找到必要的库。
  • test-prj-running.sh 中修改了覆盖率统计的提取路径。

改进建议:

  • 脚本中的路径硬编码可能导致在不同环境下的兼容性问题,建议使用变量或配置文件:
    # 配置文件示例 (config.sh)
    export LD_LIBRARY_PATH="./3rdparty/interface:$LD_LIBRARY_PATH"
    export COVERAGE_SOURCE_PATH="*/src/*"

6. 版权信息更新

代码质量:

  • 更新了版权年份从2022到2022-2026,这是必要的法律合规性更新。

改进建议:

  • 考虑使用自动化工具或CI/CD流程来管理版权信息,确保一致性。

总结

  1. 优点:

    • 版本兼容性处理得当
    • 解决了ASan与桩框架的冲突
    • 版权信息更新及时
  2. 改进建议:

    • 减少重复代码,提高可维护性
    • 使用更高级的测试框架工具
    • 增强配置的灵活性,减少硬编码
    • 考虑自动化工具辅助管理
  3. 安全性考虑:

    • 确保所有动态分配的对象都被正确释放
    • 避免在测试中使用可能导致内存泄漏的模式
    • 使用更安全的测试替代方案(如Qt测试框架的工具函数)

这些修改总体上是积极的,提高了代码的兼容性和可维护性,但仍有进一步优化的空间。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, pengfeixx

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

@pengfeixx
Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot deepin-bot Bot merged commit 6986692 into linuxdeepin:master Jun 8, 2026
16 checks passed
@pengfeixx pengfeixx deleted the fix/qt6-compatibility-and-test-fix branch June 8, 2026 08:58
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