/usr/share/gocode/src/github.com/bep/gitmap/gitmap_test.go is in golang-github-bep-gitmap-dev 0.0~git20170613.0.de8030e-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | // Copyright © 2016-present Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gitmap
import (
"encoding/json"
"os"
"strings"
"testing"
)
var (
revision = "4d9ad733fa40310607ebe9f700d59dcac93ace89"
repository string
)
func init() {
var err error
if repository, err = os.Getwd(); err != nil {
panic(err)
}
}
func TestMap(t *testing.T) {
var (
gm GitMap
gr *GitRepo
err error
)
if gr, err = Map(repository, revision); err != nil {
t.Fatal(err)
}
gm = gr.Files
if len(gm) != 9 {
t.Fatalf("Wrong number of files, got %d, expected %d", len(gm), 9)
}
assertFile(t, gm,
"testfiles/d1/d1.txt",
"4d9ad73",
"4d9ad733fa40310607ebe9f700d59dcac93ace89",
"2016-07-20",
)
assertFile(t, gm,
"testfiles/d2/d2.txt",
"9d1dc47",
"9d1dc478eef267829831226d913a3ca249c489d4",
"2016-07-19",
)
assertFile(t, gm,
"README.md",
"866cbcc",
"866cbccdab588b9908887ffd3b4f2667e94090c3",
"2016-07-20",
)
}
func assertFile(
t *testing.T,
gm GitMap,
filename,
expectedAbbreviatedHash,
expectedHash,
expectedDate string) {
var (
gi *GitInfo
ok bool
)
if gi, ok = gm[filename]; !ok {
t.Fatal(filename)
}
if gi.AbbreviatedHash != expectedAbbreviatedHash || gi.Hash != expectedHash {
t.Error("Invalid tree hash, file", filename, "abbreviated:", gi.AbbreviatedHash, "full:", gi.Hash, gi.Subject)
}
if gi.AuthorName != "Bjørn Erik Pedersen" || gi.AuthorEmail != "bjorn.erik.pedersen@gmail.com" {
t.Error("These commits are mine! Got", gi.AuthorName, "and", gi.AuthorEmail)
}
if gi.AuthorDate.Format("2006-01-02") != expectedDate {
t.Error("Invalid date:", gi.AuthorDate)
}
}
func TestActiveRevision(t *testing.T) {
var (
gm GitMap
gr *GitRepo
err error
)
if gr, err = Map(repository, "HEAD"); err != nil {
t.Fatal(err)
}
gm = gr.Files
if len(gm) < 10 {
t.Fatalf("Wrong number of files, got %d, expected at least %d", len(gm), 10)
}
if len(gm) < 10 {
t.Fatalf("Wrong number of files, got %d, expected at least %d", len(gm), 10)
}
}
func TestGitExecutableNotFound(t *testing.T) {
defer initDefaults()
gitExec = "thisShouldHopefullyNotExistOnPath"
gi, err := Map(repository, revision)
if err != GitNotFound || gi != nil {
t.Fatal("Invalid error handling")
}
}
func TestEncodeJSON(t *testing.T) {
var (
gm GitMap
gr *GitRepo
gi *GitInfo
err error
ok bool
filename = "README.md"
)
if gr, err = Map(repository, revision); err != nil {
t.Fatal(err)
}
gm = gr.Files
if gi, ok = gm[filename]; !ok {
t.Fatal(filename)
}
b, err := json.Marshal(&gi)
if err != nil {
t.Fatal(err)
}
s := string(b)
if s != `{"hash":"866cbccdab588b9908887ffd3b4f2667e94090c3","abbreviatedHash":"866cbcc","subject":"Add codecov to Travis config","authorName":"Bjørn Erik Pedersen","authorEmail":"bjorn.erik.pedersen@gmail.com","authorDate":"2016-07-20T02:22:23+02:00"}` {
t.Errorf("JSON marshal error: \n%s", s)
}
}
func TestGitRevisionNotFound(t *testing.T) {
gi, err := Map(repository, "adfasdfasdf")
// TODO(bep) improve error handling.
if err == nil || gi != nil {
t.Fatal("Invalid error handling", err)
}
}
func TestGitRepoNotFound(t *testing.T) {
gi, err := Map("adfasdfasdf", revision)
// TODO(bep) improve error handling.
if err == nil || gi != nil {
t.Fatal("Invalid error handling", err)
}
}
func TestTopLevelAbsPath(t *testing.T) {
var (
gr *GitRepo
err error
)
if gr, err = Map(repository, revision); err != nil {
t.Fatal(err)
}
expected := "github.com/bep/gitmap"
if !strings.HasSuffix(gr.TopLevelAbsPath, expected) {
t.Fatalf("Expected to end with %q got %q", expected, gr.TopLevelAbsPath)
}
}
func BenchmarkMap(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := Map(repository, revision)
if err != nil {
b.Fatalf("Got error: %s", err)
}
}
}
|