summaryrefslogtreecommitdiff
path: root/internal/sync/backup_test.go
blob: 9bdbff3f13fa1cc14de43698476a4851c844379f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package sync

import (
	"errors"
	"testing"

	"codeberg.org/snonux/gitsyncer/internal/config"
)

func TestHandlePushError_DisablesBackupForSession(t *testing.T) {
	resetBackupSessionState()
	t.Cleanup(resetBackupSessionState)

	syncer := &Syncer{}
	syncer.SetBackupEnabled(true)

	err := syncer.handlePushError("backup", &config.Organization{BackupLocation: true}, errors.New("dial tcp: connection refused"))
	if err != nil {
		t.Fatalf("expected backup push failure to be downgraded, got %v", err)
	}
	if syncer.backupActive() {
		t.Fatal("expected backup sync to be disabled for the remainder of the session")
	}
}

func TestHandlePushError_PropagatesPrimaryRemoteFailure(t *testing.T) {
	resetBackupSessionState()
	t.Cleanup(resetBackupSessionState)

	syncer := &Syncer{}
	syncer.SetBackupEnabled(true)

	pushErr := errors.New("push rejected")
	err := syncer.handlePushError("origin", &config.Organization{}, pushErr)
	if !errors.Is(err, pushErr) {
		t.Fatalf("expected primary remote error to be returned, got %v", err)
	}
}

func TestParseSSHLocation_SupportsSSHURLWithPort(t *testing.T) {
	t.Parallel()

	userHost, sshArgs, basePath, err := parseSSHLocation("ssh://git@r0:30022/repos")
	if err != nil {
		t.Fatalf("parseSSHLocation() error = %v", err)
	}
	if userHost != "git@r0" {
		t.Fatalf("userHost = %q, want %q", userHost, "git@r0")
	}
	if basePath != "/repos" {
		t.Fatalf("basePath = %q, want %q", basePath, "/repos")
	}

	wantArgs := []string{"-p", "30022", "git@r0"}
	if len(sshArgs) != len(wantArgs) {
		t.Fatalf("sshArgs = %#v, want %#v", sshArgs, wantArgs)
	}
	for i := range wantArgs {
		if sshArgs[i] != wantArgs[i] {
			t.Fatalf("sshArgs = %#v, want %#v", sshArgs, wantArgs)
		}
	}
}