aboutsummaryrefslogtreecommitdiff
path: root/ref/name/update_test.go
blob: 242626b80aa887acdf4ffee52f03a1219600f745 (about) (plain) (blame)
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 name_test

import (
	"testing"

	"lindenii.org/go/furgit/ref/name"
)

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

	tests := []struct {
		name        string
		hasNewValue bool
		wantErr     bool
	}{
		{name: "refs/heads/main", hasNewValue: true, wantErr: false},
		{name: "HEAD", hasNewValue: true, wantErr: false},
		{name: "PSEUDOREF", hasNewValue: true, wantErr: false},
		{name: "FETCH_HEAD", hasNewValue: true, wantErr: true},
		{name: "MERGE_HEAD", hasNewValue: true, wantErr: true},
		{name: "refs/heads/.bad", hasNewValue: true, wantErr: true},
		{name: "foo/bar", hasNewValue: true, wantErr: false},
		{name: "foo/bar", hasNewValue: false, wantErr: true},
		{name: "PSEUDOREF", hasNewValue: false, wantErr: false},
		{name: "HEAD", hasNewValue: false, wantErr: false},
	}

	for _, tt := range tests {
		err := name.ValidateUpdateName(tt.name, tt.hasNewValue)
		if (err != nil) != tt.wantErr {
			t.Fatalf("ValidateUpdateName(%q, %v) err=%v, wantErr=%v", tt.name, tt.hasNewValue, err, tt.wantErr)
		}
	}
}

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

	tests := []struct {
		ref     string
		target  string
		wantErr bool
	}{
		{ref: "HEAD", target: "refs/heads/main", wantErr: false},
		{ref: "HEAD", target: "foo", wantErr: true},
		{ref: "HEAD", target: "ORIG_HEAD", wantErr: true},
		{ref: "refs/heads/top", target: "ORIG_HEAD", wantErr: false},
		{ref: "refs/heads/top", target: "refs/heads/main", wantErr: false},
		{ref: "refs/heads/top", target: "worktrees/wt1/HEAD", wantErr: false},
		{ref: "refs/heads/top", target: "foo", wantErr: true},
		{ref: "refs/heads/top", target: "foo..bar", wantErr: true},
		{ref: "main-worktree/HEAD", target: "refs/heads/main", wantErr: false},
		{ref: "main-worktree/HEAD", target: "refs/tags/v1", wantErr: true},
	}

	for _, tt := range tests {
		err := name.ValidateSymbolicTarget(tt.ref, tt.target)
		if (err != nil) != tt.wantErr {
			t.Fatalf("ValidateSymbolicTarget(%q, %q) err=%v, wantErr=%v", tt.ref, tt.target, err, tt.wantErr)
		}
	}
}