summaryrefslogtreecommitdiff
path: root/internal/sync/branch_analyzer_test.go
blob: 2a51bc74d3b3b49039f35436f50bc97b3b0925d3 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package sync

import (
	"strings"
	"testing"
	"time"
)

func TestFilterProtectedAbandonedBranchReport_SkipsProtectedBranches(t *testing.T) {
	report := &AbandonedBranchReport{
		AbandonedBranches: []BranchInfo{
			{Name: "hosts"},
			{Name: "feature/still-delete"},
		},
		AbandonedIgnoredBranches: []BranchInfo{
			{Name: "hosts"},
			{Name: "ignored/still-delete"},
		},
	}

	filtered := filterProtectedAbandonedBranchReport("xerl", report)

	if len(filtered.AbandonedBranches) != 1 || filtered.AbandonedBranches[0].Name != "feature/still-delete" {
		t.Fatalf("expected protected abandoned branch to be filtered, got %#v", filtered.AbandonedBranches)
	}

	if len(filtered.AbandonedIgnoredBranches) != 1 || filtered.AbandonedIgnoredBranches[0].Name != "ignored/still-delete" {
		t.Fatalf("expected protected ignored branch to be filtered, got %#v", filtered.AbandonedIgnoredBranches)
	}

	if len(report.AbandonedBranches) != 2 || len(report.AbandonedIgnoredBranches) != 2 {
		t.Fatalf("expected original report to remain unchanged, got %#v", report)
	}
}

func TestGenerateDeleteCommands_SkipsProtectedXerlHostsBranchOnly(t *testing.T) {
	syncer := &Syncer{}
	report := &AbandonedBranchReport{
		AbandonedBranches: []BranchInfo{
			{
				Name:              "hosts",
				LastCommit:        time.Date(2024, time.January, 2, 0, 0, 0, 0, time.UTC),
				RemotesWithBranch: []string{"origin"},
			},
			{
				Name:              "feature/still-delete",
				LastCommit:        time.Date(2024, time.January, 3, 0, 0, 0, 0, time.UTC),
				RemotesWithBranch: []string{"origin"},
			},
		},
	}

	commands := syncer.GenerateDeleteCommands(report, "xerl")

	if strings.Contains(commands, "hosts") {
		t.Fatalf("expected protected branch to be omitted from delete commands, got %q", commands)
	}

	if !strings.Contains(commands, "feature/still-delete") {
		t.Fatalf("expected non-protected branch to remain in delete commands, got %q", commands)
	}
}

func TestGenerateDeleteScript_ReturnsEmptyWhenOnlyProtectedBranchesRemain(t *testing.T) {
	syncer := &Syncer{
		workDir: t.TempDir(),
		abandonedReports: map[string]*AbandonedBranchReport{
			"xerl": {
				MainBranchUpdated: true,
				AbandonedBranches: []BranchInfo{
					{
						Name:              "hosts",
						LastCommit:        time.Date(2024, time.January, 2, 0, 0, 0, 0, time.UTC),
						RemotesWithBranch: []string{"origin"},
					},
				},
			},
		},
	}

	scriptPath, err := syncer.GenerateDeleteScript()
	if err != nil {
		t.Fatalf("expected no error, got %v", err)
	}

	if scriptPath != "" {
		t.Fatalf("expected no delete script for protected branches, got %q", scriptPath)
	}
}