Hambugsy:告诉你谁错了的 CLI——是你的测试还是你的代码

发布: (2026年2月16日 GMT+8 01:00)
8 分钟阅读
原文: Dev.to

Source: Dev.to

Hambugsy 是一个 CLI 工具,用来回答每个开发者在测试失败时都会问的问题:

“是我的测试有问题,还是我的代码有问题?”

与其花费 30 + 分钟进行排查,Hambugsy 能立即给出判定,并提供置信度分数和推荐的修复方案。

问题

每个开发者都见过这种情况:

❌ FAILED: testCalculateDiscount
   Expected: 90
   Actual: 85

现在开始调查:

  • 测试写得正确吗?
  • 有人更改了业务逻辑吗?
  • 这是一次回归吗?
  • 我需要修复哪个文件?

这项调查通常需要 30‑60 分钟 来处理每个失败的测试。

示例运行

$ hambugsy analyze ./src/OrderService.java

🍔 HAMBUGSY - Finding the bug in your stack...

┌─────────────────────────────────────────────────────────────────┐
  📍 calculateTotal() - line 47
  ├── Test FAILS: testCalculateTotal_WithDiscount
  ├── 🔬 Analysis:
 Test expects: 10% discount (written: 2025-03-15)         │
 Code applies: 15% discount (changed: 2026-01-22)         │
 Git blame: "Updated discount per new pricing policy"

  └── 🎯 VERDICT: Code CHANGED Test OUTDATED
      └── 💡 Fix: Update test assertion line 23: 90 85
└─────────────────────────────────────────────────────────────────┘

📊 Summary: 1 outdated test | Time saved: ~45 minutes

核心功能

功能描述
analyze诊断测试失败并决定是测试本身出错还是代码出错
--run-tests执行真实测试以准确检测失败
suggest找出缺失的测试并生成建议
fix自动修复检测到的问题(使用 --dry-run 预览)
Output formats控制台、JSON、Markdown、GitHub Actions 注释

安装

npm install -g hambugsy

快速入门

# Analyze your whole project
hambugsy analyze ./src

Hambugsy 如何使用 GitHub Copilot CLI

// 使用 Copilot 分析测试意图
const testAnalysis = await exec(`
  gh copilot explain "What behavior does this test verify: ${testCode}"
`);
// 使用 Copilot 分析代码行为
const codeAnalysis = await exec(`
  gh copilot explain "What does this function actually do: ${sourceCode}"
`);
// 生成修复建议
const fixSuggestion = await exec(`
  gh copilot suggest -t code "
    The test expects: ${testExpectation}
    The code does: ${actualBehavior}
    Generate a fix for the ${isTestWrong ? 'test' : 'code'}
  "
`);
// 判断更改是否是有意的
const intentAnalysis = await exec(`
  gh copilot explain "
    Was this change intentional or accidental based on the commit message: 
    '${commitMessage}'
  "
`);
// 为每个判决提供人类可读的解释
const explanation = await exec(`
  gh copilot explain "
    Explain why the test '${testName}' fails:
    - Test expects: ${expected}
    - Code returns: ${actual}
    - Test was written: ${testDate}
    - Code was changed: ${codeDate}
    Explain in plain English for a developer.
  "
`);

判定分类

判定图标含义
代码错误🐛测试正确,代码存在缺陷
过时的测试📜代码被有意更改,测试需要更新
不稳定的测试🎲测试结果不一致,时通过时失败
环境问题🌐外部依赖问题

决策流程图

                测试失败

        ┌────────────┴────────────┐
        │                         │
   代码已更改?            代码未更改
        │                         │
   ┌────┴────┐               ┌────┴────┐
   │         │               │         │
有意为之?  回归   测试有效?  测试无效?
   │         │               │         │
   ▼         ▼               ▼         ▼
过时的   代码            代码      测试
测试    错误             错误       错误

语言与框架支持

语言框架状态
JavaJUnit 4/5, TestNG✅ 完整
TypeScriptJest, Mocha, Vitest✅ 完整
Pythonpytest, unittest✅ 完整
Gogo test, testify✅ 完整
Rust#[test], tokio::test✅ 完整
C#NUnit, xUnit, MSTest✅ 完整

每种语言的示例命令

# Java / JUnit
hambugsy analyze ./src/main/java/
# TypeScript / Jest
hambugsy analyze ./src/ --framework=jest
# Python / pytest
hambugsy analyze ./tests/

查找缺失的测试覆盖

$ hambugsy suggest ./src/PaymentService.java

🍔 Finding gaps in your test coverage...

📍 processPayment() @ line 5
├── TESTED: Happy path
├── MISSING: null request handling
├── MISSING: negative amount validation
└── MISSING: large amount threshold

💡 SUGGESTED TESTS: (generates actual test code)

Hambugsy 不仅分析失败——它还能通过识别未测试的代码路径来防止未来的错误。


GitHub Actions 集成

- name: Analyze Tests
  run: hambugsy analyze ./src --format=github

该工具可以输出 GitHub Actions 注释,例如:

::error file=src/Service.java,line=47::CODE BUG: Missing null check

配置 (.hambugsy.yml)

patterns:
  source: ["src/**/*.java"]
  test:   ["test/**/*.java"]

analysis:
  git_history_days: 90
  confidence_threshold: 0.7

ci:
  fail_on_bugs: true
  fail_on_outdated_tests: false

Architecture Overview

┌─────────────────────────────────────┐
│            Hambugsy CLI              │
├─────────────────────────────────────┤
│  ┌─────────┐  ┌─────────┐  ┌─────────┐ │
│  │ Parser  │  │ Analyzer│  │ Reporter│ │
│  └────┬────┘  └────┬────┘  └────┬────┘ │
│       └─────────────┼───────────────┘ │
│                     │               │
│            ┌────────▼─────────┐     │
│            │ Copilot Bridge   │     │
└─────────────────────────────────────┘

Hambugsy 基本上是围绕 GitHub Copilot CLI 的功能构建的。它利用 Copilot 来理解意图、生成修复方案并提供清晰的解释——使调试更快、更可靠。

Hambugsy

CLI 工具,告诉你 WHO 出错:是你的测试还是代码。

 │                   │
 │           └────────┬────────┘                   │
 ├────────────────────┼────────────────────────────┤
 │                    │                            │
 │  ┌─────────┐  ┌────▼────┐  ┌─────────┐          │
 │  │   Git   │  │ Copilot │  │  File   │          │
 │  │ History │  │   CLI   │  │ System  │          │
 │  └─────────┘  └─────────┘  └─────────┘          │
 └─────────────────────────────────────────────────┘

🍔 Ham + 🐛 Bug + 🎩 Bugsy
就像层层叠叠的汉堡——bug 隐藏在测试层和代码层之间。

我们追踪 bug——找出罪魁祸首。
Bugsy Siegel —— 那个总能找出罪犯的黑帮老大。

“在你的技术栈中寻找 bug”

快速安装

npm install -g hambugsy
hambugsy analyze ./src

📦 在 npm 上查看🌐 网站⚡ 快速开始


问题

每个开发者都知道这种痛苦:

❌ FAILED: testCalculateDiscount
   Expected: 90
   Actual:   85

那么怎么办?

  • 测试错了吗?
  • 代码错了吗?
  • 有人更改了业务逻辑吗?
  • 测试已经过时了吗?

你花了大约30 分钟调查,结果发现该测试是针对旧的折扣逻辑编写的。


解决方案

$ hambugsy analyze ./src/OrderService.java
🍔 HAMBUGSY - Test Failure Diagnostics 🍔
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

┌────────────────────────────────────────────────────────────────────┐
  📍 方法: calculateDiscount() @ 32
├────────────────────────────────────────────────────────────────────┤
 失败测试: testPremiumDiscount

  🔬 分析:
  ├── 测试期望: 10% 折扣 (编写于: 2024‑03‑15)                  │
  └── 代码应用: 15% 折扣 (修改于: 2024‑11‑22)                  │

  🎯 裁决: 📜 过时的测试

└───────────────────────────────────────────────────────────────────────┘

在 GitHub 上查看📦 npm 包🌐 网站📖 文档🐛 问题跟踪

接下来

  • VS Code 扩展(已包含!)
  • 自动修复模式(hambugsy fix
  • IntelliJ 插件
  • 团队分析仪表板
  • Slack/Teams 通知

先决条件

gh extension install github/gh-copilot

安装 Hambugsy

npm install -g hambugsy

在你的项目中运行

hambugsy analyze ./src

看看到底是什么导致了你的测试失败。


我们期待你的反馈!

  • 这对你的工作流有用吗?
  • 你需要哪些语言/框架?
  • 还有哪些功能是你想要的?

在下方留下评论或在 GitHub 上打开 issue! 👇

为 GitHub Copilot CLI 挑战 ❤️ 构建

0 浏览
Back to Blog

相关文章

阅读更多 »