-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
134 lines (114 loc) · 2.74 KB
/
main_test.go
File metadata and controls
134 lines (114 loc) · 2.74 KB
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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
"time"
"github.com/Sirupsen/logrus"
"github.com/libgit2/git2go"
)
func init() {
gitPathEnv = "/tmp/git"
configPath = "/tmp/.gitwork"
configFile = "config"
err := createConfigFile()
if err != nil {
logrus.Fatal(err)
}
}
func createGitRepositories() {
signature := &git.Signature{
Name: "John Doe",
Email: "johndoe@nowhere.com",
When: time.Now(),
}
repositories := []struct {
Repo string
CommitMessage string
}{
{path.Join(gitPathEnv, "test1"), "First commit"},
{path.Join(gitPathEnv, "test2"), "Initial commit"},
{path.Join(gitPathEnv, "nongit"), ""},
}
for _, r := range repositories {
if r.CommitMessage == "" {
os.Mkdir(r.Repo, 0755)
continue
}
// git init
gitRepo, err := git.InitRepository(r.Repo, false)
if err != nil {
logrus.Fatal("0: ", err)
}
// create content for the file
filename := path.Join(r.Repo, "file")
data := []byte(`This is a file`)
err = ioutil.WriteFile(filename, data, 0755)
if err != nil {
logrus.Fatal("1: ", err)
}
// get repo's index
index, err := gitRepo.Index()
if err != nil {
logrus.Fatal("2: ", err)
}
// add content to the index/staging area, file
// must be relative to the repository.
err = index.AddByPath("file")
if err != nil {
logrus.Fatal("3: ", err)
}
treeId, err := index.WriteTree()
if err != nil {
logrus.Fatal("4: ", err)
}
err = index.Write()
if err != nil {
logrus.Fatal("5: ", err)
}
tree, err := gitRepo.LookupTree(treeId)
if err != nil {
logrus.Fatal("6: ", err)
}
// Create commit, as this is the first commit, there are no parents.
_, err = gitRepo.CreateCommit("refs/heads/master", signature, signature, r.CommitMessage, tree)
if err != nil {
logrus.Fatal("7: ", err)
}
}
}
func TestReadConfigFile(t *testing.T) {
// Default flag: activeWork defines how many days
// if not value is provided should be expected 90.
configExpected := &Config{Global: Global{DaysAgo: 90}}
config, err := readConfigFile()
if err != nil {
logrus.Fatal(err)
}
if configExpected.Global != config.Global {
fmt.Println("Expected: ", configExpected)
fmt.Println("Got: ", config)
t.Error("Default configuration does not match ")
}
defer func() {
os.RemoveAll(configPath)
}()
}
func TestGetRepositories(t *testing.T) {
// Create two git repositories and one non git repo. This
// gets all the directories
createGitRepositories()
reposExpected := []string{"/tmp/git/test1", "/tmp/git/test2", "/tmp/git/nongit"}
repos, err := getRepositories(gitPathEnv)
if err != nil {
logrus.Fatal(err)
}
if len(reposExpected) != len(repos) {
t.Error("Repositories not found")
}
defer func() {
os.RemoveAll(gitPathEnv)
}()
}