Skip to content

test(GenerateDevice): 重写设备生成模块单元测试补全覆盖率 - #909

Merged
deepin-bot[bot] merged 2 commits into
linuxdeepin:masterfrom
add-uos:ut-t10
Oct 26, 2026
Merged

deepin-bot[bot] merged 2 commits into
linuxdeepin:masterfrom
add-uos:ut-t10

Conversation

@add-uos

@add-uos add-uos commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

单元测试重写:GenerateDevice(Issue V-6209)

基于 upstream/master(7d060967)重写/补齐 GenerateDevice 模块的 Google Test 单元测试:

  • src/GenerateDevice/DBusInterface.cpp
  • src/GenerateDevice/CustomGenerator.cpp
  • src/GenerateDevice/DeviceFactory.cpp
  • src/GenerateDevice/X86Generator.cpp
  • src/GenerateDevice/ArmGenerator.cpp

变更内容(仅测试源码文件,共 46 个,tests/src/ 下)

  • 重写 ut_dbusinterface / ut_customgenerator / ut_devicefactory,补强 ut_x86generator / ut_armgenerator:共 46 个用例全部通过
  • Qt6 下 QDBusInterface::call/asyncCall 为内联转发,旧用例对内联入口打桩从未生效;改经 addr_pri 桩私有非内联实现 doCall/doAsyncCall,并打桩 CommonTools/Common/DeviceManager::cmdInfo/QProcess,无真实 DBus/网络/设备依赖
  • 统一 41 个测试文件版权头为 // Copyright (C) 2019-2026 UnionTech Software Technology Co.,Ltd(修复 2019-2026 ~ 2020、2019 ~ 2026 Uniontech、legacy 2019 ~ 2020 等历史变体,仅头注释变更,单独 commit)

验证结果

  • 编译:deepin-devicemanager-test 全量构建通过(Qt6/DTK6,Debug+覆盖率)
  • 用例:目标模块 46/46 全部通过;全量回归基线 1224/1229(5 个失败为基线已存在的环境敏感用例,与本次变更无关)
  • 结构自检:5 个目标文件 0 errors 0 warnings
  • 覆盖率:5 个目标文件行覆盖 133/133(100%)、函数覆盖 11/11(100%)
  • 基线 f2d46ee → 当前 master 7d06096 期间 GenerateDevice 模块无源码变更,rebase 后全部用例通过

@sourcery-ai

sourcery-ai Bot commented Oct 24, 2026

Copy link
Copy Markdown

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 4 days and 19 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Oct 24, 2026

Copy link
Copy Markdown

Reviewer's Guide

This test-only PR rewrites and completes GenerateDevice Google Tests with deterministic stubbing for Qt6 DBus, hardware commands, and filesystem access, covering all five target modules and reporting 100% line/function coverage; it also normalizes copyright headers across 45 test files.

File-Level Changes

Change Details Files
Rewrote GenerateDevice unit tests to exercise parsing, fallback, sorting, construction, and branch behavior with deterministic inputs.
  • Expanded CustomGenerator coverage for GPU text parsing and monitor/EDID discovery, including malformed, empty, and missing-file cases.
  • Reworked DBusInterface tests to stub Qt6 private non-inline DBus call paths and QProcess fallback behavior without real DBus services.
  • Covered DeviceFactory architecture and board-type dispatch, including all supported ARM types and defaults.
  • Added X86Generator and ArmGenerator tests for CPU sorting, computer metadata population, empty inputs, and polymorphic construction.
  • Replaced hardware, filesystem, and command dependencies with project stubs and explicit state cleanup.
tests/src/GenerateDevice/ut_customgenerator.cpp
tests/src/GenerateDevice/ut_dbusinterface.cpp
tests/src/GenerateDevice/ut_devicefactory.cpp
tests/src/GenerateDevice/ut_x86generator.cpp
tests/src/GenerateDevice/ut_armgenerator.cpp
Standardized copyright headers across the test suite.
  • Normalized copyright year range and UnionTech naming in 45 test files.
  • Added missing copyright headers to affected tests without changing test behavior.
tests/src/**/*.cpp

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
  • 检测到敏感词dmidecode, lshw变动
详情
    {
    "dmidecode": {
        "deepin-devicemanager/tests/src/GenerateDevice/ut_armgenerator.cpp": [
            "    static const QList<QMap<QString, QString>> dmidecode = {",
            "    if (key == QStringLiteral(\"dmidecode4\"))",
            "        return dmidecode;"
        ],
        "deepin-devicemanager/tests/src/GenerateDevice/ut_x86generator.cpp": [
            "    static const QList<QMap<QString, QString>> dmidecode = {",
            "    if (key == QStringLiteral(\"dmidecode4\"))",
            "        return dmidecode;"
        ]
    },
    "lshw": {
        "deepin-devicemanager/tests/src/GenerateDevice/ut_armgenerator.cpp": [
            "    static const QList<QMap<QString, QString>> lshwCpu = {",
            "    if (key == QStringLiteral(\"lshw_cpu\"))",
            "        return lshwCpu;",
            "// generatorComputerDevice \u5168\u8def\u5f84\uff1a\u6253\u6869 cmdInfo \u63d0\u4f9b os-release \u4e0e lshw_system \u6570\u636e\uff0c",
            "    static const QList<QMap<QString, QString>> lshwSystem = {",
            "    if (key == QStringLiteral(\"lshw_system\"))",
            "        return lshwSystem;"
        ],
        "deepin-devicemanager/tests/src/GenerateDevice/ut_dbusinterface.cpp": [
            "    const bool ok = iface->getInfo(QStringLiteral(\"lshw\"), info);"
        ],
        "deepin-devicemanager/tests/src/GenerateDevice/ut_x86generator.cpp": [
            "    static const QList<QMap<QString, QString>> lshwCpu = {",
            "    if (key == QStringLiteral(\"lshw_cpu\"))",
            "        return lshwCpu;",
            "// generatorComputerDevice \u5168\u8def\u5f84\uff1a\u6253\u6869 cmdInfo \u63d0\u4f9b os-release \u4e0e lshw_system \u6570\u636e\uff0c",
            "    static const QList<QMap<QString, QString>> lshwSystem = {",
            "    if (key == QStringLiteral(\"lshw_system\"))",
            "        return lshwSystem;"
        ]
    }
}

Generated by qt-autotest-generator skill.
Rewrite ut_dbusinterface/ut_customgenerator/ut_devicefactory and patch
ut_x86generator/ut_armgenerator: 46 cases, all passing. Stub
QDBusAbstractInterface::doCall/doAsyncCall via addr_pri (Qt6 call/
asyncCall are inline forwarders, so the previous stubs on the inline
entries never took effect), plus CommonTools/Common/DeviceManager::
cmdInfo and QProcess stubs; no real DBus, network or device
environment is needed. Target files reach 100% line coverage
(133/133) and 100% function coverage (11/11).
Baseline: upstream/master @ f2d46ee; GenerateDevice is unchanged at
current master 0f1d52e, all 46 cases pass on the rebased branch.

Log: 重写设备生成模块单元测试, 46 条用例全部通过
Influence: 仅测试代码, 不影响业务逻辑
Unify every "Copyright (C)" header line under tests/ to the single
form "// Copyright (C) 2019-2026 UnionTech Software Technology
Co.,Ltd", fixing the broken variants left by earlier submissions
("2019-2026 ~ 2020", "2019-2026 ~ 2026", "2019 ~ 2026 Uniontech",
legacy "2019 ~ 2020"). Header comment lines only, no code change.

Log: 统一单元测试文件版权头格式, 仅头部注释变更
Influence: 仅测试代码, 不影响业务逻辑
@github-actions

Copy link
Copy Markdown
  • 检测到敏感词dmidecode, lshw变动
详情
    {
    "dmidecode": {
        "deepin-devicemanager/tests/src/GenerateDevice/ut_armgenerator.cpp": [
            "    static const QList<QMap<QString, QString>> dmidecode = {",
            "    if (key == QStringLiteral(\"dmidecode4\"))",
            "        return dmidecode;"
        ],
        "deepin-devicemanager/tests/src/GenerateDevice/ut_x86generator.cpp": [
            "    static const QList<QMap<QString, QString>> dmidecode = {",
            "    if (key == QStringLiteral(\"dmidecode4\"))",
            "        return dmidecode;"
        ]
    },
    "lshw": {
        "deepin-devicemanager/tests/src/GenerateDevice/ut_armgenerator.cpp": [
            "    static const QList<QMap<QString, QString>> lshwCpu = {",
            "    if (key == QStringLiteral(\"lshw_cpu\"))",
            "        return lshwCpu;",
            "// generatorComputerDevice \u5168\u8def\u5f84\uff1a\u6253\u6869 cmdInfo \u63d0\u4f9b os-release \u4e0e lshw_system \u6570\u636e\uff0c",
            "    static const QList<QMap<QString, QString>> lshwSystem = {",
            "    if (key == QStringLiteral(\"lshw_system\"))",
            "        return lshwSystem;"
        ],
        "deepin-devicemanager/tests/src/GenerateDevice/ut_dbusinterface.cpp": [
            "    const bool ok = iface->getInfo(QStringLiteral(\"lshw\"), info);"
        ],
        "deepin-devicemanager/tests/src/GenerateDevice/ut_x86generator.cpp": [
            "    static const QList<QMap<QString, QString>> lshwCpu = {",
            "    if (key == QStringLiteral(\"lshw_cpu\"))",
            "        return lshwCpu;",
            "// generatorComputerDevice \u5168\u8def\u5f84\uff1a\u6253\u6869 cmdInfo \u63d0\u4f9b os-release \u4e0e lshw_system \u6570\u636e\uff0c",
            "    static const QList<QMap<QString, QString>> lshwSystem = {",
            "    if (key == QStringLiteral(\"lshw_system\"))",
            "        return lshwSystem;"
        ]
    }
}

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

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

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交重写了设备生成模块的单元测试,显著提升了测试覆盖率。代码注释详尽(分支清单、用例映射、打桩说明), 采用一致的 Arrange-Act-Assert 模式,测试命名规范清晰。四维度均通过:语法逻辑正确无编译错误, 代码质量良好(存在少量死代码和重复代码),性能合理,无安全漏洞。OCR 审查发现 8 项可维护性建议 (主要为 SetUp 中桩函数生命周期问题、裸指针管理建议、静态全局变量并行风险等),已合并至代码质量维度。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

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

建议: []


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

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

建议: ['移除 SetUp() 中的 Stub 创建代码,或改为成员变量使其生命周期覆盖整个测试', '将 ut_cmdinfo_cpu/ut_cmdinfo_computer 提取到共享测试工具文件,消除重复', '移除不可达的三元表达式 nullptr 分支,直接调用 getDeviceGenerator()', '使用 std::unique_ptr 替代裸指针 + 手动 delete,利用 RAII 管理生命周期', '将 static 捕获变量改为测试夹具成员变量,消除并行执行风险', '在测试结束前手动 delete mp_Iface 或添加注释说明泄漏为已知限制']


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

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

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

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

建议: []


💡 改进建议代码示例

// ut_devicefactory.cpp — 修复 SetUp 死代码 + 裸指针改 unique_ptr
class UT_DeviceFactory : public ::testing::Test
{
protected:
    void SetUp() override
    {
        ut_arch_val = QStringLiteral("x86_64");
        ut_type_val.clear();
        // 移除 SetUp 中的 Stub — 每个测试自行创建,确保桩在测试体内生效
    }
    void TearDown() override { }
};

TEST_F(UT_DeviceFactory, GetDeviceGenerator_ArchX86_ReturnsX86Generator)
{
    // Arrange
    ut_arch_val = QStringLiteral("x86_64");
    Stub stub;
    stub.set(ADDR(Common, getArch), ut_Common_getArch);
    stub.set(ADDR(Common, boardVendorType), ut_Common_boardVendorType);

    // Act — 使用 unique_ptr 管理 returned generator 的生命周期
    std::unique_ptr<DeviceGenerator> gen(DeviceFactory::getDeviceGenerator());

    // Assert
    EXPECT_NE(gen, nullptr);
    EXPECT_NE(dynamic_cast<X86Generator *>(gen.get()), nullptr);
    // 无需手动 delete,RAII 自动释放
}

// ut_devicefactory.cpp — 修复不可达三元表达式
TEST_F(UT_DeviceFactory, Constructor_CreateInstance_UsableAfterConstruction)
{
    // Arrange
    ut_arch_val = QStringLiteral("x86_64");
    Stub stub;
    stub.set(ADDR(Common, getArch), ut_Common_getArch);
    stub.set(ADDR(Common, boardVendorType), ut_Common_boardVendorType);

    // Act
    std::unique_ptr<DeviceFactory> factory(new DeviceFactory());
    std::unique_ptr<DeviceGenerator> gen(DeviceFactory::getDeviceGenerator());

    // Assert
    EXPECT_NE(factory, nullptr);
    EXPECT_NE(gen, nullptr);
}

本报告由 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 Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit 845a8b6 into linuxdeepin:master Oct 26, 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