87 lines
2.7 KiB
Go
87 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestParseConflictFiles(t *testing.T) {
|
|
output := []byte("0123456789abcdef\x00src/a.go\x00docs/name with spaces.md\x00src/a.go\x00")
|
|
got, err := parseConflictFiles(output)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []string{"docs/name with spaces.md", "src/a.go"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("conflict files = %#v, want %#v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestParseConflictFilesRejectsMalformedOutput(t *testing.T) {
|
|
if _, err := parseConflictFiles([]byte("not-delimited")); err == nil {
|
|
t.Fatal("malformed merge-tree output was accepted")
|
|
}
|
|
}
|
|
|
|
func TestConflictFileCacheUsesCommitPairAndRetriesErrors(t *testing.T) {
|
|
client := NewGitHubClient("https://api.github.com/graphql", "secret")
|
|
calls := 0
|
|
client.conflicts = func(
|
|
_ context.Context, _ string, _ int, _ string, _, _, _ string,
|
|
) ([]string, error) {
|
|
calls++
|
|
return []string{"main.go"}, nil
|
|
}
|
|
first, err := client.loadConflictFiles(
|
|
context.Background(), "https://github.com/o/r", 1, "main", "base", "head",
|
|
)
|
|
if err != nil || len(first) != 1 {
|
|
t.Fatalf("first load = %#v, %v", first, err)
|
|
}
|
|
first[0] = "mutated"
|
|
second, err := client.loadConflictFiles(
|
|
context.Background(), "https://github.com/o/r", 1, "main", "base", "head",
|
|
)
|
|
if err != nil || !reflect.DeepEqual(second, []string{"main.go"}) || calls != 1 {
|
|
t.Fatalf("cached load = %#v, %v, calls=%d", second, err, calls)
|
|
}
|
|
|
|
failing := NewGitHubClient("https://api.github.com/graphql", "secret")
|
|
failedCalls := 0
|
|
failing.conflicts = func(
|
|
_ context.Context, _ string, _ int, _ string, _, _, _ string,
|
|
) ([]string, error) {
|
|
failedCalls++
|
|
return nil, errors.New("temporary")
|
|
}
|
|
const repositoryURL = "https://github.com/o/r"
|
|
_, _ = failing.loadConflictFiles(
|
|
context.Background(), repositoryURL, 1, "main", "base", "head",
|
|
)
|
|
key := strings.Join([]string{repositoryURL, "base", "head"}, "\x00")
|
|
entry := failing.conflictCache[key]
|
|
entry.checkedAt = time.Now().Add(-2 * time.Minute)
|
|
failing.conflictCache[key] = entry
|
|
_, _ = failing.loadConflictFiles(
|
|
context.Background(), repositoryURL, 1, "main", "base", "head",
|
|
)
|
|
if failedCalls != 2 {
|
|
t.Fatalf("expired conflict error was not retried; calls=%d", failedCalls)
|
|
}
|
|
}
|
|
|
|
func TestGitAuthenticationEnvironmentDoesNotExposeTokenInArguments(t *testing.T) {
|
|
got := gitAuthenticationEnvironment([]string{"PATH=/bin"}, "token value")
|
|
joined := strings.Join(got, "\n")
|
|
credentials := base64.StdEncoding.EncodeToString([]byte("x-access-token:token value"))
|
|
if !strings.Contains(joined, "GIT_TERMINAL_PROMPT=0") ||
|
|
!strings.Contains(joined, "Authorization: Basic "+credentials) {
|
|
t.Fatalf("authentication environment = %#v", got)
|
|
}
|
|
}
|