Skip to content

[test] Add tests for writeGatewayConfig, loadEnvFile, and tracing helpers#4182

Merged
lpcox merged 1 commit intomainfrom
test/coverage-improve-writegatewayconfig-loadenvfile-tracing-6ba7ad86d7bf4812
Apr 20, 2026
Merged

[test] Add tests for writeGatewayConfig, loadEnvFile, and tracing helpers#4182
lpcox merged 1 commit intomainfrom
test/coverage-improve-writegatewayconfig-loadenvfile-tracing-6ba7ad86d7bf4812

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

Test Coverage Improvement: cmd package

Functions Analyzed

  • Package: internal/cmd
  • Functions: writeGatewayConfig, loadEnvFile, initTracingProviderWithFallback, shutdownTracingProviderWithTimeout
  • Previous Coverage: Multiple branches untested
  • Complexity: Medium–High (wildcard address logic, file I/O, tracing lifecycle)

Why These Functions?

writeGatewayConfig has a critical correctness requirement: wildcard bind addresses (0.0.0.0, [::]) must be remapped to 127.0.0.1 in output client URLs, since clients cannot connect to wildcard addresses directly. These branches had no test coverage, meaning a regression would go undetected.

loadEnvFile had no tests for malformed lines (no = separator), files containing only comments, or values with embedded = characters (e.g. base64-encoded secrets).

initTracingProviderWithFallback and shutdownTracingProviderWithTimeout had zero tests despite being called on every gateway startup and shutdown.

Tests Added

internal/cmd/root_test.go

  • TestWriteGatewayConfig_WildcardAddresses — 4 sub-cases:
    • IPv4 wildcard 0.0.0.0:3000 → domain remapped to 127.0.0.1
    • IPv6 wildcard [::]:4000 → domain remapped to 127.0.0.1
    • Empty host (:8080) → falls back to default 127.0.0.1
    • Non-wildcard 192.168.1.10 → preserved as-is
  • TestWriteGatewayConfig_EmptyServerList — valid JSON with empty mcpServers
  • TestWriteGatewayConfig_FileSync — exercises *os.File sync code path
  • TestLoadEnvFile_SkipMalformedLines — lines without = are silently skipped
  • TestLoadEnvFile_OnlyComments — all-comment file processes without error
  • TestLoadEnvFile_EqualsInValue — values with embedded = (e.g. dGVzdA==) preserved

internal/cmd/tracing_helpers_test.go

  • TestInitTracingProviderWithFallback — 3 sub-cases:
    • Nil config → noop provider, no warning
    • Config without endpoint → noop provider, no warning
    • Unreachable endpoint → always returns non-nil provider (fallback guarantee)
  • TestShutdownTracingProviderWithTimeout — noop provider shuts down cleanly

Notes

The make agent-finished pipeline (format/build/lint/test) could not be executed locally due to network restrictions in the agent environment blocking Go module downloads (go proxy.golang.org is firewalled). All tests are structurally correct and follow existing patterns. The CI pipeline performs go mod download before testing and will verify correctness.


Generated by Test Coverage Improver
Next run candidates: internal/proxy/handler.go, internal/server/http_helpers.go, preRun verbosity levels

Warning

⚠️ Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • proxy.golang.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "proxy.golang.org"

See Network Configuration for more information.

Generated by Test Coverage Improver · ● 7.8M ·

Cover previously untested branches in cmd package:

- writeGatewayConfig: wildcard address remapping (0.0.0.0 and [::] → 127.0.0.1),
  empty-host listen address, empty server list, and os.File sync path
- loadEnvFile: malformed lines without '=', files with only comments, and
  values that contain embedded '=' characters (e.g. base64 secrets)
- initTracingProviderWithFallback: nil config (noop), config without endpoint,
  and unreachable endpoint (error-fallback path)
- shutdownTracingProviderWithTimeout: clean shutdown of noop provider

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review April 20, 2026 13:32
Copilot AI review requested due to automatic review settings April 20, 2026 13:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds test coverage in internal/cmd for gateway config output rewriting, .env parsing edge-cases, and tracing provider lifecycle helpers to reduce regression risk in gateway startup/shutdown and generated client configs.

Changes:

  • Add tests for writeGatewayConfig covering wildcard/empty hosts, empty server list, and *os.File sync path.
  • Add tests for loadEnvFile covering malformed lines, comment-only files, and values containing =.
  • Add tests for tracing helper functions to validate noop paths and fallback/shutdown behavior.
Show a summary per file
File Description
internal/cmd/root_test.go Adds tests for gateway config rewriting and .env parsing edge-cases.
internal/cmd/tracing_helpers_test.go Adds tests for tracing init fallback and shutdown helper behavior.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comments suppressed due to low confidence (1)

internal/cmd/tracing_helpers_test.go:110

  • The assertion here is conditional (if warnMsg != "" { ... }), so the test will still pass even when no warning is emitted and the fallback branch was never executed. Once the error case is made deterministic, assert that the warning callback was invoked (and optionally that it includes the expected prefix) so the test actually validates the fallback guarantee.
		// Regardless of whether the provider initialisation fails,
		// the fallback must always return a non-nil provider.
		require.NotNil(t, provider, "Fallback provider must not be nil")
		// If the init failed, the warning callback must have been called.
		if warnMsg != "" {
			assert.Contains(t, warnMsg, "tracing init failed")
		}
	})
  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Comment on lines +87 to +94
t.Run("unreachable endpoint falls back to noop provider and emits warning", func(t *testing.T) {
var warnMsg string
cfg := &config.TracingConfig{
// Use a bogus address that will immediately fail during startup.
// InitProvider performs a dial during provider creation when an
// endpoint is set, so this exercises the error-fallback branch.
Endpoint: "https://127.0.0.1:1/does-not-exist",
}
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

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

This subtest tries to trigger the error/fallback branch by using an "unreachable" OTLP endpoint, but tracing.InitProvider only constructs an OTLP/HTTP exporter and does not necessarily dial the endpoint during initialization. That means this may not error (and could become flaky/slow if the exporter library changes). Use an intentionally invalid endpoint URL (one that fails parsing/validation) and/or a context with a short timeout so the test deterministically exercises the err != nil fallback path without relying on network behavior.

This issue also appears on line 103 of the same file.

Copilot uses AI. Check for mistakes.
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented Apr 20, 2026

@copilot address the review feedback #4182 (review)

@lpcox lpcox merged commit 0417cc8 into main Apr 20, 2026
22 of 23 checks passed
@lpcox lpcox deleted the test/coverage-improve-writegatewayconfig-loadenvfile-tracing-6ba7ad86d7bf4812 branch April 20, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants