aboutsummaryrefslogtreecommitdiffstats
path: root/lib/pama/remove_test.go
blob: c9ce6c65c8440b1f19090e67d2658ad90083fe05 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package pama_test

import (
	"reflect"
	"testing"

	"git.sr.ht/~rjarry/aerc/lib/pama"
	"git.sr.ht/~rjarry/aerc/lib/pama/models"
)

func TestPatchmgmt_Remove(t *testing.T) {
	setup := func(p models.Project) (pama.PatchManager, models.RevisionController, models.PersistentStorer) {
		return newTestManager(
			[]string{"0", "1", "2", "3", "4", "5"},
			[]string{"0", "a", "b", "c", "d", "f"},
			map[string]models.Project{p.Name: p}, p.Name,
		)
	}

	tests := []struct {
		name    string
		remove  string
		commits []models.Commit
		want    []models.Commit
	}{
		{
			name:   "remove only patch",
			remove: "patch1",
			commits: []models.Commit{
				newCommit("1", "a", "patch1"),
			},
			want: []models.Commit{},
		},
		{
			name:   "remove second one of two patch",
			remove: "patch2",
			commits: []models.Commit{
				newCommit("1", "a", "patch1"),
				newCommit("2", "b", "patch2"),
			},
			want: []models.Commit{
				newCommit("1", "a", "patch1"),
			},
		},
		{
			name:   "remove first one of two patch",
			remove: "patch1",
			commits: []models.Commit{
				newCommit("1", "a", "patch1"),
				newCommit("2", "b", "patch2"),
			},
			want: []models.Commit{
				newCommit("2_new", "b", "patch2"),
			},
		},
	}

	for _, test := range tests {
		p := models.Project{
			Name:    "project1",
			Commits: test.commits,
			Base:    newCommit("0", "0", ""),
		}
		mgr, rc, _ := setup(p)

		err := mgr.RemovePatch(test.remove)
		if err != nil {
			t.Errorf("test '%s' failed. %v", test.name, err)
		}

		q, _ := mgr.CurrentProject()
		if !reflect.DeepEqual(q.Commits, test.want) {
			t.Errorf("test '%s' failed. Commits don't match: "+
				"got %v, but wanted %v", test.name, q.Commits,
				test.want)
		}

		if len(test.want) > 0 {
			last := test.want[len(test.want)-1]
			if !rc.Exists(last.ID) {
				t.Errorf("test '%s' failed. Could not find last commits: %v", test.name, last)
			}
		}
	}
}