Skip to content

[test] Add tests for envutil.deriveAPIFromServerURL — http scheme and edge cases#6708

Merged
lpcox merged 2 commits into
mainfrom
add-envutil-deriveapi-coverage-tests-247f4db590d5ae56
May 29, 2026
Merged

[test] Add tests for envutil.deriveAPIFromServerURL — http scheme and edge cases#6708
lpcox merged 2 commits into
mainfrom
add-envutil-deriveapi-coverage-tests-247f4db590d5ae56

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

Test Coverage Improvement: deriveAPIFromServerURL

Function Analyzed

  • Package: internal/envutil
  • Function: deriveAPIFromServerURL (in github.go)
  • File: internal/envutil/github.go (line 83)
  • Complexity: Medium (URL parsing, 3-way switch on hostname pattern, port extraction)

Why This Function?

deriveAPIFromServerURL contains several branches not exercised by the existing TestDeriveAPIFromServerURL (which only used https:// URLs):

  1. `(redacted) scheme — explicitly supported (the guard at lines 91-94 allows both), but no test covered the GHEC or GHES code paths with plain HTTP.
  2. Empty URLurl.Parse("") succeeds but leaves Host empty, triggering the parsed.Host == "" early-return guard. This path was not covered.
  3. Trailing slashstrings.TrimRight(serverURL, "/") strips trailing slashes before parsing. This normalisation was not verified.

Tests Added

New file: internal/envutil/github_coverage_test.go

  • (redacted) scheme with GHEC .ghe.comhostname →copilot-api.` subdomain
  • ✅ `(redacted) scheme with GHEC + explicit port
  • (redacted) scheme with GHES custom hostname → /api/v3` path
  • ✅ `(redacted) scheme with GHES + explicit port
  • ✅ Empty URL → "" (empty-host guard)
  • github.com with trailing slash → same result as without (TrimRight branch)
  • ✅ GHES URL with multiple trailing slashes (all stripped by TrimRight)

Coverage Report

Note: The network firewall in the CI/sandbox environment blocks proxy.golang.org, preventing go test execution. The tests are analytically verified: each case traces deriveAPIFromServerURL deterministically (pure URL parsing and string operations — no I/O, no randomness, no external dependencies).

Correctness Reasoning

Each new test case was traced manually through the function:

Input Expected output Path exercised
`(mycompany.ghe.com/redacted) `(copilotapi.mycompany.ghe.com/redacted) http scheme; .ghe.com → copilot-api, no port
`(mycompany.ghe.com/redacted) `(copilotapi.mycompany.ghe.com/redacted) Same with port branch
`(github.example.com/redacted) `(github.example.com/redacted) http scheme; GHES default
`(github.example.com/redacted) `(github.example.com/redacted) GHES with port
"" "" url.Parse("") → Host="" → early-return
https://github.com/ DefaultGitHubAPIBaseURL TrimRight strips /
`(github.example.com/redacted) `(github.example.com/redacted) Multiple slashes stripped

Generated by Test Coverage Improver

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 · sonnet46 17.2M ·

Cover the branches in internal/envutil/github.go that were not exercised
by the existing TestDeriveAPIFromServerURL test suite:

- http:// scheme with GHEC (.ghe.com) hostname — expects copilot-api subdomain
- http:// scheme with GHEC + port
- http:// scheme with GHES custom hostname — expects /api/v3 path
- http:// scheme with GHES + port
- Empty URL — triggers the 'parsed.Host == ""' guard, returns ""
- github.com with trailing slash — exercises strings.TrimRight, same result
- GHES URL with multiple trailing slashes — all stripped by TrimRight

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review May 29, 2026 21:01
Copilot AI review requested due to automatic review settings May 29, 2026 21:01
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

This PR improves unit coverage for internal/envutil URL derivation behavior without changing production code.

Changes:

  • Adds table-driven tests for http:// GHEC and GHES server URL derivation.
  • Adds edge-case coverage for empty input and trailing slash normalization.
  • Verifies existing constants and helper behavior through direct tests of deriveAPIFromServerURL.
Show a summary per file
File Description
internal/envutil/github_coverage_test.go Adds focused coverage for deriveAPIFromServerURL scheme handling and edge cases.

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: 1

Comment thread internal/envutil/github_coverage_test.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@lpcox lpcox merged commit 9306e84 into main May 29, 2026
16 checks passed
@lpcox lpcox deleted the add-envutil-deriveapi-coverage-tests-247f4db590d5ae56 branch May 29, 2026 21:25
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