Skip to content

fix(common): fix Qt6 regex porting bug and improve UT coverage - #554

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/qt6-unittest-stub-and-regex
Jul 20, 2026
Merged

fix(common): fix Qt6 regex porting bug and improve UT coverage#554
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/qt6-unittest-stub-and-regex

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Fix QRegularExpression regression in parseNestedQString where spStr was wrongly assigned a QString, causing split by literal "\s+". Also adapt 5 stub-based tests to Qt6 ABI and raise line coverage from 79.8% to 81.3% by adding DBusManager/NewDspinBox test cases.

修复 parseNestedQString 中 Qt6 正则移植漏改问题,spStr 误赋为
QString 导致按字面字符串 "\s+" 分割。同时适配 5 个 stub 测试以
兼容 Qt6 ABI,并新增 DBusManager/NewDspinBox 测试用例,将行覆盖
率从 79.8% 提升至 81.3%。

Log: 修复Qt6正则分割bug并提升单元测试覆盖率至80%以上
Influence: 修复后 -e 参数解析恢复正常,427 个单元测试全部通过,
无新增失败,主程序编译正常。

Summary by Sourcery

Fix argument parsing whitespace splitting regression on Qt6 and update unit tests to improve coverage and Qt6 compatibility.

Bug Fixes:

  • Correct Qt6 nested QString parsing to use a regular expression split with empty parts skipped, restoring expected argument parsing behavior.

Enhancements:

  • Adjust Qt6-specific stubbing in settings, term widget page, and service dialog tests to hook the appropriate functions under the Qt6 ABI.
  • Clarify remote management panel list expectations by accounting for header items in list count checks and preventing stale server configs from affecting focus state.

Tests:

  • Add DBusManager signal, DBus call, and console settings getter/setter tests.
  • Extend NewDspinBox tests to cover range handling, smart completion on key and focus events, invalid input fallback, and wheel behavior without focus.
  • Update settings translation and term widget cursor blinking tests to use Qt6-compatible stubs and assertions.
  • Refine remote management panel tests to validate search state item counts and avoid side effects from pre-existing server configurations.

Fix QRegularExpression regression in parseNestedQString where spStr
was wrongly assigned a QString, causing split by literal "\\s+".
Also adapt 5 stub-based tests to Qt6 ABI and raise line coverage
from 79.8% to 81.3% by adding DBusManager/NewDspinBox test cases.

修复 parseNestedQString 中 Qt6 正则移植漏改问题,spStr 误赋为
QString 导致按字面字符串 "\\s+" 分割。同时适配 5 个 stub 测试以
兼容 Qt6 ABI,并新增 DBusManager/NewDspinBox 测试用例,将行覆盖
率从 79.8% 提升至 81.3%。

Log: 修复Qt6正则分割bug并提升单元测试覆盖率至80%以上
Influence: 修复后 -e 参数解析恢复正常,427 个单元测试全部通过,
    无新增失败,主程序编译正常。
@sourcery-ai

sourcery-ai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Reviewer's Guide

Fixes Qt6-specific regex handling in Utils::parseNestedQString to restore -e argument parsing behavior and updates several unit tests and stubs to align with Qt6 ABI while adding new coverage for DBusManager, NewDspinBox, Settings, TermWidgetPage, Service, and RemoteManagementPanel behaviors.

File-Level Changes

Change Details Files
Correct Qt6 whitespace splitting in parseNestedQString to use QRegularExpression and skip empty parts, matching Qt5 behavior and fixing -e argument parsing.
  • Replace QString-based split pattern in Qt6 branch with QRegularExpression("\s+").
  • Update all split() calls in parseNestedQString to pass the regex and SkipEmptyParts to avoid empty elements from trailing whitespace.
src/common/utils.cpp
Add DBusManager unit tests to validate entry signal emission, DBus call stubs, and Settings-backed console configuration getters/setters.
  • Include Settings and QSignalSpy headers for DBusManager tests.
  • Add tests for entry() emitting entryArgs(QStringList).
  • Add tests for callKDECurrentDesktop() and callSystemSound() using QDBus call stubs.
  • Add getter/setter tests for font size, font family, opacity, cursor shape/blink, color scheme, and shell path using Settings::instance().
  • Add test for callAppearanceFont(QStringList, QString) overload with empty list to ensure stubbed DBus call returns an empty result.
tests/src/main/ut_dbusmanager_test.cpp
Extend NewDspinBox unit tests to cover range logic, smart completion on key/focus events, invalid input handling, and wheel behavior without focus.
  • Add tests for setRange in font, history, and generic ranges verifying boundary and internal minimum adjustments.
  • Add keyPressEvent tests for Enter-triggered smart completion vs non-Enter keys and non-history ranges.
  • Add focusOutEvent tests for smart completion in history range, non-completion in font range, and invalid text fallback to current value.
  • Add wheelEvent test ensuring value is unchanged when the spinbox has no focus.
tests/src/settings/ut_newdspinbox_test.cpp
Adapt Settings translation stub to Qt6 by intercepting QCoreApplication::translate instead of QTranslator::translate, and assert that the stubbed static function runs.
  • Define a static ut_QCoreApplication_translate function that toggles a flag when invoked.
  • Use Stub::set to replace QCoreApplication::translate with the custom function via a function pointer typedef.
  • Update GenerateSettingTranslate test to rely on the flag rather than QTRANSLATE stub macros.
tests/src/settings/ut_settings_test.cpp
Update TermWidgetPage blinking cursor test to stub QWidget::update(const QRect&) overload used in Qt6, instead of the parameterless update() stub macros.
  • Document the setBlinkingCursor call chain leading to TerminalDisplay::update(QRect) in Qt6.
  • Use Stub::set to redirect QWidget::update(const QRect&) to ut_terminalDisplay_update.
  • Replace macro-based QWIDGET_UPDATES stubs with explicit flag-based checks before and after setBlinkingCursor(false/true).
tests/src/views/ut_termwidgetpage_test.cpp
Adjust Service hideSettingDialog test to stub QWidget::hide() directly, matching Qt6 vtable behavior instead of setVisible().
  • Explain that QWidget::hide() now jumps to setVisible(false) via vtable and that patching setVisible is ineffective.
  • Use Stub::set to replace QWidget::hide() with ut_QWidget_update and reset the run flag.
  • Update expectations to assert ut_QWidget_update_hasRuned after hideSettingDialog() instead of setVisible stub macros.
tests/src/main/ut_service_test.cpp
Fix RemoteManagementPanel tests to account for extra list items and clear shared ServerConfigManager state between tests to avoid focus side effects.
  • Update refreshSearchState expectation so listWidget count equals server item count plus two header labels appended in fillManagePanel.
  • Clear ServerConfigManager::m_serverConfigs in lambda test before emitting refreshList to avoid leftover configs setting m_currentIndex inadvertently.
tests/src/remotemanage/ut_remotemanagementpanel_test.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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码实现了Qt6兼容性适配并完善了相关单元测试,逻辑严谨且无安全漏洞
代码质量良好,修复了Qt6下字符串分割行为不一致的问题,测试用例覆盖全面,但因部分注释可进一步精简扣5分

■ 【详细分析】

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

src/common/utils.cpp 中的 parseNestedQString 函数正确将 Qt6 分支的 spStr 类型修改为 QRegularExpression,并在三处 split 调用中补充了 SKIP_EMPTY_PARTS 参数,确保了跨版本行为一致性。测试代码中针对 Qt6 虚函数表和静态函数的 stub 修改均符合 C++ 语法与 Qt6 机制。
潜在问题:无
建议:保持现有逻辑,确保 SKIP_EMPTY_PARTS 在所有需要过滤空字符串的场景中一致使用

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

新增的单元测试命名规范,覆盖了 NewDspinBox 和 DBusManager 的多种边界条件。ut_remotemanagementpanel_test.cpp 中补充了对列表项计数的详细注释,提高了可读性。ut_service_test.cpp 和 ut_settings_test.cpp 中对 stub 机制的修改附带了清晰的原理说明。
潜在问题:部分测试函数如 setConsoleFontFamily 仅验证不崩溃,断言强度较弱
建议:在条件允许的情况下,增加对设置结果的断言验证,提升测试有效性

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

parseNestedQString 函数仅修改了正则表达式对象的类型和分割参数,未引入额外的循环或复杂的正则表达式,对性能无负面影响。测试代码中的对象创建和销毁均使用了 deleteLater,符合 Qt 内存管理规范。
潜在问题:无
建议:无需额外优化

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

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次修改仅涉及字符串分割逻辑调整和单元测试代码增加,未引入外部输入处理、文件操作或网络通信,不存在可利用的攻击面。

  • 建议:无需修复,保持现有安全编码规范

■ 【改进建议代码示例】

// src/common/utils.cpp 无需修改,当前实现已为最佳实践。
// 对于 tests/src/main/ut_dbusmanager_test.cpp 中的弱断言测试,建议增强验证逻辑:

TEST_F(UT_Dbusmanager_Test, setConsoleFontFamily)
{
    const QString testFamily = QStringLiteral("Noto Sans Mono");
    m_pDbusManager->setConsoleFontFamily(testFamily);
    // 若环境支持该字体,可验证是否设置成功;若不支持,验证默认值未被破坏
    QString currentFamily = m_pDbusManager->consoleFontFamily();
    EXPECT_FALSE(currentFamily.isEmpty());
}

@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.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="tests/src/main/ut_dbusmanager_test.cpp" line_range="111-120" />
<code_context>
+TEST_F(UT_Dbusmanager_Test, consoleFontSize)
</code_context>
<issue_to_address>
**suggestion (testing):** Consider isolating and restoring `Settings` singleton state to avoid inter-test coupling.

These DBusManager tests mutate `Settings::instance()` (font size, opacity, cursor shape/blink, color scheme, etc.) and assume its state. Without resetting these fields, tests can become order-dependent and leak state to other suites.

Please either save and restore the affected `Settings` values per test (or in SetUp/TearDown / an RAII helper) so all modified fields are consistently reset after each test.

Suggested implementation:

```cpp
class SettingsGuard
{
public:
    SettingsGuard()
        : m_originalFontSize(Settings::instance()->fontSize())
    {
    }

    ~SettingsGuard()
    {
        Settings::instance()->setFontSize(m_originalFontSize);
    }

private:
    int m_originalFontSize;
};

TEST_F(UT_Dbusmanager_Test, callKDECurrentDesktop)

```

```cpp
TEST_F(UT_Dbusmanager_Test, consoleFontSize)
{
    SettingsGuard settingsGuard;

    const int expectSize = 18;
    Settings::instance()->setFontSize(expectSize);
    EXPECT_EQ(m_pDbusManager->consoleFontSize(), expectSize);
}

```

```cpp
TEST_F(UT_Dbusmanager_Test, setConsoleFontSize)
{
    SettingsGuard settingsGuard;

    const int newSize = 22;
    m_pDbusManager->setConsoleFontSize(newSize);
}

```

The `SettingsGuard` currently only preserves and restores `fontSize`. To fully address inter-test coupling for other `Settings` fields (opacity, cursor shape/blink, color scheme, etc.), extend `SettingsGuard` to capture and reset all fields that the DBusManager tests touch, using the corresponding getters/setters (e.g. `opacity()`, `setOpacity()`, etc.), and instantiate `SettingsGuard` in each test that mutates those fields.
If your test fixture (`UT_Dbusmanager_Test`) already has `SetUp`/`TearDown`, you may alternatively move `SettingsGuard` into a member field and construct it in `SetUp` so every test in this suite benefits from automatic state restoration.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +111 to +120
TEST_F(UT_Dbusmanager_Test, consoleFontSize)
{
const int expectSize = 18;
Settings::instance()->setFontSize(expectSize);
EXPECT_EQ(m_pDbusManager->consoleFontSize(), expectSize);
}

TEST_F(UT_Dbusmanager_Test, setConsoleFontSize)
{
const int newSize = 22;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Consider isolating and restoring Settings singleton state to avoid inter-test coupling.

These DBusManager tests mutate Settings::instance() (font size, opacity, cursor shape/blink, color scheme, etc.) and assume its state. Without resetting these fields, tests can become order-dependent and leak state to other suites.

Please either save and restore the affected Settings values per test (or in SetUp/TearDown / an RAII helper) so all modified fields are consistently reset after each test.

Suggested implementation:

class SettingsGuard
{
public:
    SettingsGuard()
        : m_originalFontSize(Settings::instance()->fontSize())
    {
    }

    ~SettingsGuard()
    {
        Settings::instance()->setFontSize(m_originalFontSize);
    }

private:
    int m_originalFontSize;
};

TEST_F(UT_Dbusmanager_Test, callKDECurrentDesktop)
TEST_F(UT_Dbusmanager_Test, consoleFontSize)
{
    SettingsGuard settingsGuard;

    const int expectSize = 18;
    Settings::instance()->setFontSize(expectSize);
    EXPECT_EQ(m_pDbusManager->consoleFontSize(), expectSize);
}
TEST_F(UT_Dbusmanager_Test, setConsoleFontSize)
{
    SettingsGuard settingsGuard;

    const int newSize = 22;
    m_pDbusManager->setConsoleFontSize(newSize);
}

The SettingsGuard currently only preserves and restores fontSize. To fully address inter-test coupling for other Settings fields (opacity, cursor shape/blink, color scheme, etc.), extend SettingsGuard to capture and reset all fields that the DBusManager tests touch, using the corresponding getters/setters (e.g. opacity(), setOpacity(), etc.), and instantiate SettingsGuard in each test that mutates those fields.
If your test fixture (UT_Dbusmanager_Test) already has SetUp/TearDown, you may alternatively move SettingsGuard into a member field and construct it in SetUp so every test in this suite benefits from automatic state restoration.

@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 7d776d4 into linuxdeepin:master Jul 20, 2026
18 checks passed
@pengfeixx
pengfeixx deleted the fix/qt6-unittest-stub-and-regex branch July 20, 2026 08:15
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