From cdc4839b2039e334f3c5513400add5b735256ba5 Mon Sep 17 00:00:00 2001 From: "devsy-app[bot]" <277138668+devsy-app[bot]@users.noreply.github.com> Date: Fri, 14 Aug 2026 10:44:12 +0000 Subject: [PATCH] test: cover GetHTTPPath branches Add tests for gitcredentials.GetHTTPPath, which had no coverage, exercising all branches: current-path override, useHttpPath disabled (empty), useHttpPath enabled (parsed repository path), host-only repository URL (no path), invalid repository URL error, and per-host config scoping. --- pkg/gitcredentials/gitcredentials_test.go | 100 ++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/pkg/gitcredentials/gitcredentials_test.go b/pkg/gitcredentials/gitcredentials_test.go index 86cddeb08..ef45ec04d 100644 --- a/pkg/gitcredentials/gitcredentials_test.go +++ b/pkg/gitcredentials/gitcredentials_test.go @@ -217,3 +217,103 @@ func TestRemoveHelperOnMissingConfigIsNoError(t *testing.T) { RemoveHelperFromPath(context.Background(), filepath.Join(t.TempDir(), "does-not-exist")), ) } + +const ( + httpPathTestHost = "github.com" + httpPathTestProtocol = "https" + httpPathTestRepository = "https://github.com/org/repo.git" + httpPathTestRepoPath = "/org/repo.git" +) + +func httpPathTestEnv(t *testing.T, credentialConfig string) { + t.Helper() + tmpHome := t.TempDir() + t.Setenv("HOME", tmpHome) + t.Setenv("XDG_CONFIG_HOME", "") + t.Setenv("GIT_CONFIG_NOSYSTEM", "1") + require.NoError( + t, + os.WriteFile(filepath.Join(tmpHome, ".gitconfig"), []byte(credentialConfig), 0o600), + ) +} + +func TestGetHTTPPath_ReturnsCurrentPathWhenProvided(t *testing.T) { + httpPathTestEnv(t, "") + + got, err := GetHTTPPath(context.Background(), GetHttpPathParameters{ + Host: httpPathTestHost, + Protocol: httpPathTestProtocol, + CurrentPath: "/existing/path", + Repository: httpPathTestRepository, + }) + require.NoError(t, err) + assert.Equal(t, "/existing/path", got) +} + +func TestGetHTTPPath_EmptyWhenUseHttpPathDisabled(t *testing.T) { + httpPathTestEnv(t, "") + + got, err := GetHTTPPath(context.Background(), GetHttpPathParameters{ + Host: httpPathTestHost, + Protocol: httpPathTestProtocol, + Repository: httpPathTestRepository, + }) + require.NoError(t, err) + assert.Empty(t, got) +} + +func TestGetHTTPPath_ReturnsRepositoryPathWhenUseHttpPathEnabled(t *testing.T) { + httpPathTestEnv(t, `[credential "https://github.com"] + useHttpPath = true +`) + + got, err := GetHTTPPath(context.Background(), GetHttpPathParameters{ + Host: httpPathTestHost, + Protocol: httpPathTestProtocol, + Repository: httpPathTestRepository, + }) + require.NoError(t, err) + assert.Equal(t, httpPathTestRepoPath, got) +} + +func TestGetHTTPPath_EmptyPathWhenRepositoryHasNoPath(t *testing.T) { + httpPathTestEnv(t, `[credential "https://github.com"] + useHttpPath = true +`) + + got, err := GetHTTPPath(context.Background(), GetHttpPathParameters{ + Host: httpPathTestHost, + Protocol: httpPathTestProtocol, + Repository: "https://github.com", + }) + require.NoError(t, err) + assert.Empty(t, got, "a host-only repository URL has no path component") +} + +func TestGetHTTPPath_ErrorOnInvalidRepositoryURL(t *testing.T) { + httpPathTestEnv(t, `[credential "https://github.com"] + useHttpPath = true +`) + + _, err := GetHTTPPath(context.Background(), GetHttpPathParameters{ + Host: httpPathTestHost, + Protocol: httpPathTestProtocol, + Repository: "://not-a-url", + }) + require.Error(t, err) + assert.Contains(t, err.Error(), "parse workspace repository") +} + +func TestGetHTTPPath_PerHostScopedConfigOnly(t *testing.T) { + httpPathTestEnv(t, `[credential "https://gitlab.com"] + useHttpPath = true +`) + + got, err := GetHTTPPath(context.Background(), GetHttpPathParameters{ + Host: httpPathTestHost, + Protocol: httpPathTestProtocol, + Repository: httpPathTestRepository, + }) + require.NoError(t, err) + assert.Empty(t, got, "useHttpPath set for a different host must not apply") +}