[test] Add tests for config.unmarshalStringListOrExpression#7895
Merged
lpcox merged 1 commit intoJun 21, 2026
Conversation
Cover the two previously untested error paths in unmarshalStringListOrExpression (guard_policy.go), along with related error paths in GuardPolicy.UnmarshalJSON and GuardPolicyToMap: - Line 289: JSON value that is neither array nor string (number, bool, object) triggers 'expected array of strings or comma/newline-delimited expression' - Line 296: string containing only delimiter characters (comma/newline) produces zero FieldsFunc parts, triggering 'must include at least one label' - GuardPolicy.UnmarshalJSON line 100-102: non-object top-level JSON - GuardPolicyToMap line 175-177: typed nil pointer marshals to JSON null New test functions: - TestUnmarshalStringListOrExpression (15 table-driven cases) - TestGuardPolicyUnmarshalJSON_NonObjectJSON (3 cases) - TestGuardPolicyToMap_TypedNilPointer (1 case) - TestAllowOnlyPolicyUnmarshalJSON_RefusalLabelsInvalidType (5 cases) Coverage improvements: - unmarshalStringListOrExpression: 89.5% -> 100% - GuardPolicy.UnmarshalJSON: 0% -> 100% - GuardPolicyToMap: 0% -> 100% Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves test coverage in internal/config by adding a dedicated test file that exercises previously uncovered branches in unmarshalStringListOrExpression and related guard-policy JSON helpers.
Changes:
- Add table-driven unit tests that cover valid inputs and multiple invalid-type / delimiter-only error paths for
unmarshalStringListOrExpression. - Add tests for
GuardPolicy.UnmarshalJSONrejecting non-object JSON payloads. - Add a test covering
GuardPolicyToMapbehavior when passed a typed-nil pointer that marshals to JSONnull.
Show a summary per file
| File | Description |
|---|---|
internal/config/guard_policy_str_expr_test.go |
Adds new unit tests to reach previously uncovered error branches in guard policy parsing/mapping helpers. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 4
Comment on lines
+11
to
+14
| // TestUnmarshalStringListOrExpression directly tests all branches of the private | ||
| // unmarshalStringListOrExpression function, including the two uncovered error paths: | ||
| // - non-string/non-array JSON type (line 289) | ||
| // - string of only delimiter characters with no labels (line 296) |
Comment on lines
+134
to
+136
| // TestGuardPolicyUnmarshalJSON_NonObjectJSON covers the error path in | ||
| // GuardPolicy.UnmarshalJSON when the top-level JSON is not an object. | ||
| // This triggers the return at line 100-102 of guard_policy.go. |
Comment on lines
+170
to
+173
| // TestGuardPolicyToMap_TypedNilPointer covers the payload == nil guard in | ||
| // GuardPolicyToMap (guard_policy.go line 175-177). A typed nil pointer passes | ||
| // the interface nil check but marshals to JSON "null", which then unmarshals | ||
| // the map variable to nil. |
Comment on lines
+182
to
+184
| // TestAllowOnlyPolicyUnmarshalJSON_RefusalLabelsInvalidType covers the error | ||
| // path in unmarshalStringListOrExpression (line 289) when refusal-labels has | ||
| // an invalid JSON type (number, boolean) that is neither an array nor a string. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Coverage Improvement:
unmarshalStringListOrExpressionFunction Analyzed
internal/configunmarshalStringListOrExpression(and related helpers inguard_policy.go)unmarshalStringListOrExpression89.5%,GuardPolicy.UnmarshalJSON0%,GuardPolicyToMap0%Why This Function?
unmarshalStringListOrExpressionhad two untested error paths that could not be reached by existing tests:[]stringarray nor astring(e.g. a JSON number, boolean, or object). No existing test passed such a value throughrefusal-labels.strings.FieldsFuncreturns zero parts. The existing test for" "(spaces only) skipped this branch because spaces are not delimiters; it fell through to the second empty-result check instead.GuardPolicy.UnmarshalJSONandGuardPolicyToMapalso had uncovered error paths (non-object JSON input, typed-nil-pointer marshalling tonull) that are now covered.Tests Added
New file:
internal/config/guard_policy_str_expr_test.goTestUnmarshalStringListOrExpression(15 table-driven cases)TestGuardPolicyUnmarshalJSON_NonObjectJSON(3 cases) — covers line 100–102TestGuardPolicyToMap_TypedNilPointer(1 case) — covers line 175–177 via typed nil marshalling tonullTestAllowOnlyPolicyUnmarshalJSON_RefusalLabelsInvalidType(5 cases) — exercises both new error paths end-to-end throughAllowOnlyPolicy.UnmarshalJSONCoverage Report
Test Execution
All 24 new sub-tests pass:
Generated by Test Coverage Improver
Next run will target the next most complex under-tested function
Warning
Firewall blocked 7 domains
The following domains were blocked by the firewall during workflow execution:
go.opentelemetry.iogo.yaml.ingolang.orggoogle.golang.orggopkg.inproxy.golang.orgsum.golang.orgSee Network Configuration for more information.