-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitdir_test.go
More file actions
91 lines (84 loc) · 2.13 KB
/
gitdir_test.go
File metadata and controls
91 lines (84 loc) · 2.13 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
package gittools_test
import (
"os"
"strconv"
"testing"
"time"
"github.com/denormal/go-gittools"
)
func TestGitDir(t *testing.T) {
// we should be in a working copy
_cwd, _err := os.Getwd()
if _err != nil {
t.Fatalf(
"unable to determine current working directory: %s",
_err.Error(),
)
}
// ensure the environment isn't set
_env := os.Getenv("GIT_DIR")
_err = os.Setenv("GIT_DIR", "")
if _err != nil {
t.Fatalf("unable to set GIT_DIR environment variable: %s", _err.Error())
}
defer func() {
if _env == "" {
os.Unsetenv("GIT_DIR")
} else {
os.Setenv("GIT_DIR", _env)
}
}()
// using the current working directory or "" should give the same results
for _, _path := range []string{_cwd, ""} {
// are we in a working copy?
_is, _err := gittools.IsWorkingCopy(_path)
if _err != nil {
if _err != gittools.MissingWorkingCopyError {
t.Fatalf("unexpected error: %s", _err.Error())
}
}
if _is {
// we're in a working copy so ensure we get a non-empty response
_dir, _err := gittools.GitDir(_path)
if _err != nil {
t.Errorf("%q: unexpected error: %s", _path, _err.Error())
}
if _dir == "" {
t.Errorf(
"%q: expected to be in working copy; none found",
_path,
)
}
} else {
// we're not in a working copy so ensure we get an empty response
_dir, _err := gittools.GitDir(_path)
if _err != nil {
if _err != gittools.MissingWorkingCopyError {
t.Errorf("%q: unexpected error: %s", _path, _err.Error())
}
}
if _dir != "" {
t.Errorf(
"%q: expected not to be in working copy; found %q",
_path, _dir,
)
}
}
}
// if we manually set the GIT_DIR environment variable, do we get the
// expected result from GitDir()?
_value := strconv.FormatInt(time.Now().UnixNano(), 16)
_err = os.Setenv("GIT_DIR", _value)
if _err != nil {
t.Fatalf("unable to set GIT_DIR environment variable: %s", _err.Error())
}
_dir, _err := gittools.GitDir("")
if _err != nil {
t.Fatalf("unexpected error: %s", _err.Error())
} else if _dir != _value {
t.Fatalf(
"git directory mismatch; expected %q, got %q",
_value, _dir,
)
}
} // TestGitDir()