Skip to content

test: expand unit tests to lift src/ coverage from ~58% to ~60%#866

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:master
Jun 25, 2026
Merged

test: expand unit tests to lift src/ coverage from ~58% to ~60%#866
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:master

Conversation

@pengfeixx

Copy link
Copy Markdown
Contributor

Add tests for dbus_name/constant/delaytime/shapesutils/previewwidget/ screengrabber/toolbutton/maintoolwidget/event_monitor/main_window EF helpers, and guard forceToExitApp() with ENABLE_UNIT_TEST. Also extend lcov exclusions for untestable Wayland/X11/camera code.

新增多个源文件的单元测试(dbus_name/constant/delaytime/shapesutils/ previewwidget/screengrabber/toolbutton/maintoolwidget/event_monitor/ main_window 事件过滤助手等),并用 ENABLE_UNIT_TEST 宏守卫
forceToExitApp 避免测试进程退出;同时扩展 lcov 排除项,剔除无法
单测的 Wayland/X11/摄像头相关代码。

Log: 提升单元测试覆盖率
Influence: 扩充单元测试覆盖面,src/ 行覆盖率由约58%提升至约60%,为后续达到80%奠定基础;源码改动均以 ENABLE_UNIT_TEST 宏守卫,不影响正式构建行为。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

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 tests for dbus_name/constant/delaytime/shapesutils/previewwidget/
screengrabber/toolbutton/maintoolwidget/event_monitor/main_window EF
helpers, and guard forceToExitApp() with ENABLE_UNIT_TEST. Also extend
lcov exclusions for untestable Wayland/X11/camera code.

新增多个源文件的单元测试(dbus_name/constant/delaytime/shapesutils/
previewwidget/screengrabber/toolbutton/maintoolwidget/event_monitor/
main_window 事件过滤助手等),并用 ENABLE_UNIT_TEST 宏守卫
forceToExitApp 避免测试进程退出;同时扩展 lcov 排除项,剔除无法
单测的 Wayland/X11/摄像头相关代码。

Log: 提升单元测试覆盖率
Influence: 扩充单元测试覆盖面,src/ 行覆盖率由约58%提升至约60%,为后续达到80%奠定基础;源码改动均以 ENABLE_UNIT_TEST 宏守卫,不影响正式构建行为。
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码通过条件编译和测试基础设施优化有效解决了单测进程被杀死的问题,整体质量高
逻辑完全正确且无安全漏洞,因测试代码中存在部分重复的桩设置逻辑扣5分

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

核心修改点 src/utils/delaytime.cpp 中的 #ifdef ENABLE_UNIT_TEST ... #else ... #endif 预处理宏结构完整闭合,在单测编译时准确拦截 _Exit(0) 调用并替换为 emit doWork() 信号发射,与 .pro 文件中新增的 DEFINES += ENABLE_UNIT_TEST 形成闭环。tests/ut_screen_shot_recorder/ut_screenshot.h 中移除的 stub.reset(ADDR(ConfigSettings, value)); 与全局 stub 生命周期管理一致,不会引起悬空桩。ut_process_tree_ext.hstatic proc_t proc_info 声明在循环外,符合 readproc 的覆写语义。
潜在问题:无
建议:无

  • 2.代码质量(良好)✓

新增单测覆盖面广,注释极其详尽,精准解释了为何剔除特定用例(如 getTwoScreenIntersectPos 越界 SEGV、saveTopWindow 解引用 null QString 等)。test-prj-running.sh 中通过 lcov --remove 剔除 Wayland 生成代码和强依赖硬件的模块,使覆盖率指标真实反映可测代码。但 ut_main_window_ext.hut_main_window_ef.h 中的 SetUp 方法存在高度重复的 stub 设置和 MainWindow 安全构造逻辑(完全相同的 6 行 stub.set 和 3 行初始化)。
潜在问题:两个测试夹具类中存在完全相同的 stub 设置与对象初始化代码,若未来安全构造桩发生变更需同步修改多处
建议:将 MainWindowExtTestMainWindowEFTest 的公共 stub 设置与初始化逻辑提取到公共基类或测试辅助工具类中

  • 3.代码性能(无性能问题)✓

test-prj-running.shQT_QPA_PLATFORM 从空值切换为 offscreen,避免了 native 平台下大量 GUI 用例在无头环境中触发 X11/dxcb 初始化及 ASan abort,显著提升了单测执行稳定性与速度。ut_delaytime.h 中通过 stub.set(ADDR(QThread, msleep), delaytime_msleep_stub) 将真实线程睡眠替换为空操作,消除了倒计时等待开销。
潜在问题:无
建议:无

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次修改全部集中在测试框架与条件编译防御层面,未引入任何命令注入、内存越界或权限绕过风险。test-prj-running.sh 中的 DISPLAY="${DISPLAY:-:0}" 使用了安全的默认值扩展,且仅作用于隔离的子 shell 进程,不会污染或覆盖调用方环境。_Exit(0) 的条件替换严格受限于 ENABLE_UNIT_TEST 宏,生产环境构建不受影响。

  • 建议:无

■ 【改进建议代码示例】

// 提取 MainWindow 测试的公共基类,消除 ut_main_window_ext.h 与 ut_main_window_ef.h 中的重复代码
// 文件:tests/ut_screen_shot_recorder/ut_main_window_common.h
#pragma once
#include <gtest/gtest.h>
#include <QRect>
#include "stub.h"
#include "addr_pri.h"
#include "../../src/main_window.h"

class MainWindowTestBase : public testing::Test
{
public:
    Stub stub;
    MainWindow *m_w = nullptr;

    // 复刻 ut_main_window.h 的安全构造桩
    static QRect geometry_stub() { return QRect(0, 0, 1920, 1080); }
    static void passInputEvent_stub(void *, int, int, int, int) { return; }
    static qreal devicePixelRatio_stub() { return 1.0; }
    static int width_stub() { return 1920; }
    static int height_stub() { return 1080; }

    void SetUp() override
    {
        stub.set(ADDR(QScreen, geometry), geometry_stub);
        stub.set(ADDR(Utils, passInputEvent), passInputEvent_stub);
        stub.set(ADDR(QScreen, devicePixelRatio), devicePixelRatio_stub);
        stub.set(ADDR(QWidget, width), width_stub);
        stub.set(ADDR(QWidget, height), height_stub);
        m_w = new MainWindow;
        m_w->initAttributes();
        m_w->initResource();
    }

    // 故意泄漏 m_w,避免无显示 ASAN 环境下析构崩溃
    void TearDown() override { }
};

@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 9ce104d into linuxdeepin:master Jun 25, 2026
12 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