aboutsummaryrefslogtreecommitdiffstats
path: root/commands/select/select_test.go
blob: 5a34d71e6a3eb29d35f044f567a3f44421c0c354 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package _select

import (
	"fmt"
	"io/ioutil"
	"log"
	"testing"

	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/repository"
)

func TestSelect(t *testing.T) {
	repo, err := cache.NewRepoCache(createRepo())
	checkErr(t, err)

	_, _, err = ResolveBug(repo, []string{})
	if err != ErrNoValidId {
		t.Fatal("expected no valid id error, got", err)
	}

	err = Select(repo, "invalid")
	checkErr(t, err)

	_, _, err = ResolveBug(repo, []string{})
	if err == nil {
		t.Fatal("expected invalid bug error")
	}

	// generate a bunch of bugs
	for i := 0; i < 10; i++ {
		_, err := repo.NewBug("title", "message")
		checkErr(t, err)
	}

	// two more for testing
	b1, err := repo.NewBug("title", "message")
	checkErr(t, err)
	b2, err := repo.NewBug("title", "message")
	checkErr(t, err)

	err = Select(repo, b1.Id())
	checkErr(t, err)

	// normal select without args
	b3, _, err := ResolveBug(repo, []string{})
	checkErr(t, err)
	if b3.Id() != b1.Id() {
		t.Fatal("incorrect bug returned")
	}

	// override selection with same id
	b4, _, err := ResolveBug(repo, []string{b1.Id()})
	checkErr(t, err)
	if b4.Id() != b1.Id() {
		t.Fatal("incorrect bug returned")
	}

	// override selection with a prefix
	b5, _, err := ResolveBug(repo, []string{b1.HumanId()})
	checkErr(t, err)
	if b5.Id() != b1.Id() {
		t.Fatal("incorrect bug returned")
	}

	// args that shouldn't override
	b6, _, err := ResolveBug(repo, []string{"arg"})
	checkErr(t, err)
	if b6.Id() != b1.Id() {
		t.Fatal("incorrect bug returned")
	}

	// override with a different id
	b7, _, err := ResolveBug(repo, []string{b2.Id()})
	checkErr(t, err)
	if b7.Id() != b2.Id() {
		t.Fatal("incorrect bug returned")
	}

	err = Clear(repo)
	checkErr(t, err)

	_, _, err = ResolveBug(repo, []string{})
	if err == nil {
		t.Fatal("expected invalid bug error")
	}
}

func createRepo() *repository.GitRepo {
	dir, err := ioutil.TempDir("", "")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Creating repo:", dir)

	repo, err := repository.InitGitRepo(dir)
	if err != nil {
		log.Fatal(err)
	}

	return repo
}

func checkErr(t testing.TB, err error) {
	if err != nil {
		t.Fatal(err)
	}
}