fix: make getProcessStartTime mockable for unit tests - #202
Conversation
1. Add processStartTime function field to allowCallerRegistry struct. 2. Default to getProcessStartTime in newAllowCallerRegistryWithConfig. 3. Replace direct getProcessStartTime calls with r.processStartTime. 4. Set mock processStartTime in tests that use non-existent PIDs. Log: Test-only fix; no functional change Influence: 1. Verify all authorizeRegistrar and addCaller tests pass without /proc access. fix: 将 getProcessStartTime 改为可 mock 结构体字段,修复测试 1. 在 allowCallerRegistry 中添加 processStartTime 函数字段。 2. newAllowCallerRegistryWithConfig 默认赋值为 getProcessStartTime。 3. authorizeRegistrar 中两处调用改为 r.processStartTime。 4. 测试用例设置 mock 返回固定值,避免依赖 /proc。 Log: 仅测试修复,无功能变更 Influence: 1. 验证所有 authorizeRegistrar 和 addCaller 测试无需 /proc 即可通过。
Reviewer's guide (collapsed on small PRs)Reviewer's GuideThis PR makes the process start time lookup in allowCallerRegistry injectable so tests can run without accessing /proc, by adding a processStartTime function field to the registry, wiring it to getProcessStartTime in production, and overriding it with a fixed mock in unit tests for authorizeRegistrar and addCaller. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review★ 总体评分:95分■ 【总体评价】
■ 【详细分析】
■ 【改进建议代码示例】 // locale-helper/allow_caller_test.go
// mockProcessStartTime 提取公共的 Mock 函数,避免测试代码重复
func mockProcessStartTime(pid uint32) (uint64, error) {
return 12345, nil
}
func TestAddCallerAuthorizedSender(t *testing.T) {
// ... 前置代码 ...
r := newRegistryForTest(t, bus)
r.privilegedGroupID = 42
r.processStartTime = mockProcessStartTime // 使用提取后的公共函数
r.processGroups = func(pid uint32) ([]uint32, error) {
return []uint32{42}, nil
}
// ... 后续逻辑 ...
}
func TestAuthorizeRegistrar(t *testing.T) {
// ... 在各个子测试中同样使用 r.processStartTime = mockProcessStartTime 替换原有的内联匿名函数 ...
} |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: fly602, mhduiy The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Log: Test-only fix; no functional change
Influence:
fix: 将 getProcessStartTime 改为可 mock 结构体字段,修复测试
Log: 仅测试修复,无功能变更
Influence:
Summary by Sourcery
Decouple process start-time checks from direct system access to make unit tests reliable without changing production behavior.
Bug Fixes:
Enhancements:
Tests: