aboutsummaryrefslogtreecommitdiff
path: root/refstore/shorten_test.go
blob: 1975ab3f0da7271516cd5d5afbac180c4b878c2e (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
64
65
66
67
68
package refstore_test

import (
	"testing"

	"codeberg.org/lindenii/furgit/refstore"
)

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

	t.Run("simple", func(t *testing.T) {
		got := refstore.ShortenName("refs/heads/main", []string{"refs/heads/main"})
		if got != "main" {
			t.Fatalf("ShortenName simple = %q, want %q", got, "main")
		}
	})

	t.Run("ambiguous with tags", func(t *testing.T) {
		got := refstore.ShortenName(
			"refs/heads/main",
			[]string{
				"refs/heads/main",
				"refs/tags/main",
			},
		)
		if got != "heads/main" {
			t.Fatalf("ShortenName tags ambiguity = %q, want %q", got, "heads/main")
		}
	})

	t.Run("strict remote head ambiguity", func(t *testing.T) {
		// In strict mode, refs/remotes/%s/HEAD blocks shortening to "%s".
		got := refstore.ShortenName(
			"refs/heads/main",
			[]string{
				"refs/heads/main",
				"refs/remotes/main/HEAD",
			},
		)
		if got != "heads/main" {
			t.Fatalf("ShortenName strict ambiguity = %q, want %q", got, "heads/main")
		}
	})

	t.Run("deep fallback still shortens", func(t *testing.T) {
		// refs/remotes/origin/main conflicts with refs/heads/origin/main for
		// "origin/main", so it should fall back to "remotes/origin/main".
		got := refstore.ShortenName(
			"refs/remotes/origin/main",
			[]string{
				"refs/remotes/origin/main",
				"refs/heads/origin/main",
			},
		)
		if got != "remotes/origin/main" {
			t.Fatalf("ShortenName deep fallback = %q, want %q", got, "remotes/origin/main")
		}
	})

	t.Run("refs-prefix fallback", func(t *testing.T) {
		name := "refs/notes/review/topic"
		got := refstore.ShortenName(name, []string{name})
		if got != "notes/review/topic" {
			t.Fatalf("ShortenName refs-prefix fallback = %q, want %q", got, "notes/review/topic")
		}
	})
}