This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_ledger_test.go
161 lines (136 loc) · 3.69 KB
/
git_ledger_test.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package ledger_test
import (
. "github.com/git-hook/git-ledger"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"os"
"fmt"
"path"
)
var (
r Record
s Record
)
// Move the current git-ledger to a new location so testing can create
// a temporary one
func pushGitLedger() {
os.Rename(Path(), fmt.Sprintf("%s.backup", Path()))
f, _ := os.Create(Path())
defer f.Close()
}
// Remove the temporary git-ledger and replace it with the backup
func popGitLedger() {
os.Remove(Path())
os.Rename(fmt.Sprintf("%s.backup", Path()), Path())
}
// Create a dummy ledger-file to act as the device-under-test.
func createDummyLedger() {
r = Record{Path: "/tmp", Slug: "pony/fill"}
r.AddToLedger()
s = Record{Path: "/home", Slug: "badger/bear"}
s.AddToLedger()
}
var _ = Describe("GitLedger", func() {
BeforeEach(func() {
pushGitLedger()
createDummyLedger()
})
AfterEach(popGitLedger)
// TODO: test that git-ledger silently creates the ledger if asked to read it
Describe("CLI", func() {
Context("add", func() {
// TODO: test about duplicates
Specify("should accept fully-initialized records", func() {
Record{Path: "/proc", Slug: "tron/man"}.AddToLedger()
records, _ := GetRecords()
Expect(len(records)).To(Equal(3))
Record{Path: "/dev", Slug: "cron/man"}.AddToLedger()
records, _ = GetRecords()
Expect(len(records)).To(Equal(4))
})
})
Context("remove", func() {
Specify("should accept fully-initialized records", func() {
s.RemoveFromLedger()
records, _ := GetRecords()
Expect(len(records)).To(Equal(1))
r.RemoveFromLedger()
records, _ = GetRecords()
Expect(len(records)).To(Equal(0))
})
})
// Context("find", func() {
// var (
// r Record
// s Record
// )
// Specify("")
// })
})
Context("The ledger", func() {
Specify("should reside in the user's home directory", func() {
Expect(path.Join(os.Getenv("HOME"), ".git-ledger")).To(Equal(Path()))
})
Specify("should be created if it is accessed and it does not exist", func() {
os.Remove(Path())
records, err := GetRecords()
Expect(err).To(BeNil())
Expect(len(records)).To(Equal(0))
})
})
Describe("Records", func() {
Context("When the ledger is empty", func() {
It("should return 0 records", func() {
os.Remove(Path())
records, _ := GetRecords()
Expect(len(records)).To(Equal(0))
})
})
// fixme: make this format more awesome
Specify("should print in an expected format", func() {
path := "/path/to/nowhere"
slug := "sclopio/peepio"
record := Record{Path: path, Slug: slug}
expected := fmt.Sprintf("[[Record]]\npath = \"%s\"\nslug = \"%s\"\n\n", record.Path, record.Slug)
Expect(record.String()).To(Equal(expected))
})
Specify("should be index-able by slug", func() {
rec, _ := GetBySlug("fill")
Expect(rec).To(Equal(r))
rec, _ = GetBySlug("pony/fill")
Expect(rec).To(Equal(r))
rec, _ = GetBySlug("badger")
Expect(rec).To(Equal(s))
})
Specify("should be index-able by path", func() {
rec, _ := GetByPath("/tmp")
Expect(rec).To(Equal(r))
rec, _ = GetByPath("/home")
Expect(rec).To(Equal(s))
_, err := GetByPath("DNE")
Ω(err).ShouldNot(BeNil())
})
})
Describe("Detecting git meta-data", func() {
Context("Git repositories", func() {
var (
dir string
gitdir string
)
BeforeEach(func() {
dir = "/tmp"
gitdir = path.Join(dir, ".git")
})
AfterEach(func() {
os.Remove(gitdir)
})
Specify("should identify as such", func() {
os.MkdirAll(gitdir, os.ModePerm)
Ω(IsGitProject(dir)).Should(BeTrue())
})
Specify("should be differentiable from non-git repositories", func() {
Ω(IsGitProject(dir)).Should(BeFalse())
})
})
})
})