-
Notifications
You must be signed in to change notification settings - Fork 78
/
version_test.go
105 lines (90 loc) · 3.02 KB
/
version_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
package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func gitCmdOrFatal(t *testing.T, tempdir string, arg ...string) {
cmd := exec.Command("git", arg...)
cmd.Dir = tempdir
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatalf("Could not run %v: %v", cmd.Args, err)
}
}
func TestSnapshotVersion(t *testing.T) {
os.Setenv("TZ", "UTC")
defer os.Unsetenv("TZ")
tempdir, err := os.MkdirTemp("", "dh-make-golang")
if err != nil {
t.Fatalf("Could not create temp dir: %v", err)
}
defer os.RemoveAll(tempdir)
tempfile := filepath.Join(tempdir, "test")
if err := os.WriteFile(tempfile, []byte("testcase"), 0644); err != nil {
t.Fatalf("Could not write temp file %q: %v", tempfile, err)
}
gitCmdOrFatal(t, tempdir, "init")
gitCmdOrFatal(t, tempdir, "config", "user.email", "[email protected]")
gitCmdOrFatal(t, tempdir, "config", "user.name", "Unit Test")
gitCmdOrFatal(t, tempdir, "add", "test")
cmd := exec.Command("git", "commit", "-a", "-m", "initial commit")
cmd.Env = append(os.Environ(), "GIT_COMMITTER_DATE=2015-04-20T11:22:33")
cmd.Dir = tempdir
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatalf("Could not run %v: %v", cmd.Args, err)
}
var u upstream
got, err := pkgVersionFromGit(tempdir, &u, false)
if err != nil {
t.Fatalf("Determining package version from git failed: %v", err)
}
if want := "0.0~git20150420."; !strings.HasPrefix(got, want) {
t.Errorf("got %q, want %q", got, want)
}
gitCmdOrFatal(t, tempdir, "tag", "-a", "v1", "-m", "release v1")
got, err = pkgVersionFromGit(tempdir, &u, false)
if err != nil {
t.Fatalf("Determining package version from git failed: %v", err)
}
if want := "1"; got != want {
t.Logf("got %q, want %q", got, want)
}
if err := os.WriteFile(tempfile, []byte("testcase 2"), 0644); err != nil {
t.Fatalf("Could not write temp file %q: %v", tempfile, err)
}
cmd = exec.Command("git", "commit", "-a", "-m", "first change")
cmd.Env = append(os.Environ(), "GIT_COMMITTER_DATE=2015-05-07T11:22:33")
cmd.Dir = tempdir
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatalf("Could not run %v: %v", cmd.Args, err)
}
got, err = pkgVersionFromGit(tempdir, &u, false)
if err != nil {
t.Fatalf("Determining package version from git failed: %v", err)
}
if want := "1+git20150507.1."; !strings.HasPrefix(got, want) {
t.Logf("got %q, want %q", got, want)
}
if err := os.WriteFile(tempfile, []byte("testcase 3"), 0644); err != nil {
t.Fatalf("Could not write temp file %q: %v", tempfile, err)
}
cmd = exec.Command("git", "commit", "-a", "-m", "second change")
cmd.Env = append(os.Environ(), "GIT_COMMITTER_DATE=2015-05-08T11:22:33")
cmd.Dir = tempdir
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatalf("Could not run %v: %v", cmd.Args, err)
}
got, err = pkgVersionFromGit(tempdir, &u, false)
if err != nil {
t.Fatalf("Determining package version from git failed: %v", err)
}
if want := "1+git20150508.2."; !strings.HasPrefix(got, want) {
t.Logf("got %q, want %q", got, want)
}
}