Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions pkg/gitcredentials/gitcredentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Loading