Skip to content

test: add unit tests for MLogger and HeaderInfoTableDelegate - #759

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:agent/agent/b8cd6c5fae76
Sep 16, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:agent/agent/b8cd6c5fae76

Conversation

@add-uos

@add-uos add-uos commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Summary

为 deepin-devicemanager 项目补充两个模块的单元测试:

  • MLoggertests/src/LogConfigRead/ut_mlogger.cpp):覆盖构造函数环境变量读取与分号替换、setRules 分号→换行符替换、appendRules 追加/去重/空输入/混合输入、rules() getter、析构函数不崩溃,共 12 个用例
  • HeaderInfoTableDelegatetests/src/Widget/ut_headerinfotabledelegate.cpp):覆盖构造函数、paint 非 selected 状态、paint selected 状态、paint 无活跃窗口(Inactive palette 路径),共 4 个用例

同时将 .ut/ 工作目录加入 .gitignore

Test

  • 编译通过 ✅
  • 16/16 测试全绿 ✅

Summary by Sourcery

Add unit tests for logging rule management and header table delegate rendering, and exclude test artifacts from version control.

Tests:

  • Add unit coverage for MLogger environment-rule parsing, rule updates, deduplication, accessors, and destruction.
  • Add unit coverage for HeaderInfoTableDelegate construction and painting across selected, non-selected, and inactive-window states.

Chores:

  • Ignore the .ut/ test work directory in Git.

@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 @add-uos, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 1 day and 8 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Sep 16, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds 16 unit tests covering MLogger rule handling and HeaderInfoDelegate painting branches, using DConfig/DApplication stubs and Qt widget fixtures, and ignores the .ut/ test workspace.

File-Level Changes

Change Details Files
Adds focused unit coverage for MLogger’s rule parsing, mutation, access, and destruction behavior.
  • Stubs DConfig creation and value lookup to isolate constructor behavior.
  • Verifies environment-variable loading, semicolon normalization, and empty/non-empty inputs.
  • Covers rule appending, duplicate filtering, mixed inputs, getter behavior, and destructor safety.
deepin-devicemanager/tests/src/LogConfigRead/ut_mlogger.cpp
Adds GUI-oriented unit coverage for HeaderInfoDelegate construction and paint-state branches.
  • Creates a table model and delegate fixture with controlled DStyle lookup.
  • Exercises painting for unselected, selected, and inactive-window palette paths.
  • Uses rendered widget capture as a smoke assertion that painting completes successfully.
deepin-devicemanager/tests/src/Widget/ut_headerinfotabledelegate.cpp
Excludes the unit-test working directory from version control.
  • Adds .ut/ to the ignore list.
.gitignore

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions

Copy link
Copy Markdown
  • 检测到敏感词unset变动
详情
    {
    "unset": {
        "deepin-devicemanager/tests/src/LogConfigRead/ut_mlogger.cpp": {
            "b": [
                "        // MLogger ctor reads QT_LOGGING_RULES then unsets it.",
                "        qunsetenv(\"QT_LOGGING_RULES\");"
            ]
        }
    }
}

- Add ut_mlogger.cpp covering MLogger constructor env-var reading, semicolon-to-newline replacement, setRules/appendRules/rules() and destructor safety
- Add ut_headerinfotabledelegate.cpp covering HeaderInfoTableDelegate constructor and paint in normal/selected/inactive states
- Fix memory leak in UT_MLogger::TearDown by deleting the DConfig object created in SetUp

测试(单元测试): 为 MLogger 和 HeaderInfoTableDelegate 添加单元测试

- 新增 ut_mlogger.cpp,覆盖 MLogger 构造函数环境变量读取、分号到换行符替换、setRules/appendRules/rules() 及析构函数安全性
- 新增 ut_headerinfotabledelegate.cpp,覆盖 HeaderInfoTableDelegate 构造函数及正常/选中/非激活状态下的 paint 绘制
- 修复 UT_MLogger::TearDown 中未释放 DConfig 对象的内存泄漏

Log: 为 MLogger 日志配置类和 HeaderInfoTableDelegate 添加单元测试,修复 TearDown 内存泄漏
@add-uos
add-uos force-pushed the agent/agent/b8cd6c5fae76 branch from 08e0cf1 to b3d9e28 Compare September 16, 2026 08:34
@github-actions

Copy link
Copy Markdown
  • 检测到敏感词unset变动
详情
    {
    "unset": {
        "deepin-devicemanager/tests/src/LogConfigRead/ut_mlogger.cpp": {
            "b": [
                "        // MLogger ctor reads QT_LOGGING_RULES then unsets it.",
                "        qunsetenv(\"QT_LOGGING_RULES\");"
            ]
        }
    }
}

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 95 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 95 分,大于 70 分通过阈值,代码质量符合要求。本次提交为 MLogger 和 HeaderInfoTableDelegate 添加单元测试,代码结构清晰,测试覆盖全面,无安全漏洞。

🔍 详细分析

1. 语法逻辑 ✅

评价: 良好 ✅ 通过

潜在问题:

  1. deepin-devicemanager/tests/src/Widget/ut_headerinfotabledelegate.cpp:89 - 测试 UT_HeaderInfoDelegate_paint_noActiveWindow 声称测试非激活调色板分支,但未通过 stub 显式设置 DApplication::activeWindow() 返回 nullptr,测试可能无法可靠覆盖目标分支

建议: 在 UT_HeaderInfoDelegate_paint_noActiveWindow 测试中添加 stub.set(ADDR(DApplication, activeWindow), { return (QWidget *)nullptr; }); 以确保可靠测试非激活状态分支


2. 代码质量 ✅

评价: 优秀 ✅ 通过

潜在问题:

  1. deepin-devicemanager/tests/src/LogConfigRead/ut_mlogger.cpp:20 - 全局指针 ut_mlogger_config 在 SetUp(line 45) 中 new 分配,在 TearDown 中手动 delete,若 SetUp 在分配后抛出异常则可能导致内存泄漏,建议使用智能指针
  2. deepin-devicemanager/tests/src/Widget/ut_headerinfotabledelegate.cpp:21 - 全局可变指针 ut_HeaderInfoDelegate_style 在测试夹具和 stub 函数间共享,对测试隔离性较为脆弱,建议作为夹具成员使用 lambda 捕获

建议: 考虑使用 std::unique_ptrDtk::Core::DConfig 管理生命周期,或将 DStyle 作为测试夹具成员并使用 lambda 捕获,提升测试隔离性和异常安全性


3. 代码性能 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 性能良好,资源使用合理,测试代码无性能瓶颈


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: 存在0个安全漏洞,安全合规


💡 改进建议代码示例

// ut_headerinfotabledelegate.cpp - 改进建议
// 1. 将 DStyle 作为夹具成员
class UT_HeaderInfoDelegate : public UT_HEAD
{
    // ...
    DStyle m_style;  // 作为成员变量
};

void SetUp() override
{
    // ...
    // 使用 lambda 捕获 this->m_style
    stub.set(ADDR(DApplication, style), [this]() { return &m_style; });
}

// 2. 在 noActiveWindow 测试中显式 stub activeWindow
TEST_F(UT_HeaderInfoDelegate, UT_HeaderInfoDelegate_paint_noActiveWindow)
{
    QStyleOptionViewItem option;
    QPainter painter(m_tableWidget);
    QModelIndex index = m_tableWidget->model()->index(0, 0);

    Stub stub;
    stub.set(ADDR(DApplication, style), [this]() { return &m_style; });
    stub.set(ADDR(DApplication, activeWindow), []() { return (QWidget *)nullptr; });

    m_delegate->paint(&painter, option, index);
    EXPECT_FALSE(m_tableWidget->grab().isNull());
}

// ut_mlogger.cpp - 改进建议
// 使用智能指针管理 DConfig 生命周期
static std::unique_ptr<Dtk::Core::DConfig> ut_mlogger_config;

void SetUp() override
{
    qputenv("QT_LOGGING_RULES", "a.debug=true");
    ut_mlogger_config = std::make_unique<Dtk::Core::DConfig>("org.deepin.devicemanager");
    // ...
}

void TearDown() override
{
    ut_mlogger_config.reset();
    qunsetenv("QT_LOGGING_RULES");
}

本报告由 AI 代码审查工具自动生成

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

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

@add-uos

add-uos commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit 3e43305 into linuxdeepin:master Sep 16, 2026
16 of 17 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