code.oscarkilo.com/klex-git

Hash:
28d685e5c5a0ded0e243819ab891445c7276a843
Author:
Igor Naverniouk <[email protected]>
Date:
Wed Aug 28 12:45:32 2024 -0700
Message:
oops
diff --git a/util/commit_test.go b/util/commit_test.go
index f5997d9..a864cbb 100644
--- a/util/commit_test.go
+++ b/util/commit_test.go
@@ -79,7 +79,6 @@ func TestGetCommitInfo(t *testing.T) {
golden := &CommitInfo{
ChangedPaths: []string{"bar/baz.txt", "bar/qux.txt", "foo.txt"},
}
- RunOrDie("cp", "-r", repo, "/tmp/committest") // DEBUG
if !reflect.DeepEqual(info, golden) {
t.Errorf("\nexpected %v; got %v", golden.DebugString(), info.DebugString())
}
a/util/commit_test.go
b/util/commit_test.go
1
package util
1
package util
2
2
3
import (
3
import (
4
"os"
4
"os"
5
"path"
5
"path"
6
"reflect"
6
"reflect"
7
"runtime"
7
"runtime"
8
"testing"
8
"testing"
9
)
9
)
10
10
11
func check(t *testing.T, err error) {
11
func check(t *testing.T, err error) {
12
if err != nil {
12
if err != nil {
13
t.Fatal(err)
13
t.Fatal(err)
14
}
14
}
15
}
15
}
16
16
17
func makeGitRepo(t *testing.T) string {
17
func makeGitRepo(t *testing.T) string {
18
// Create an empty git repo in a temporary directory.
18
// Create an empty git repo in a temporary directory.
19
dir := t.TempDir()
19
dir := t.TempDir()
20
os.Chdir(dir)
20
os.Chdir(dir)
21
RunOrDie("git", "init")
21
RunOrDie("git", "init")
22
RunOrDie("git", "config", "user.email", "[email protected]")
22
RunOrDie("git", "config", "user.email", "[email protected]")
23
RunOrDie("git", "config", "user.name", "Good User")
23
RunOrDie("git", "config", "user.name", "Good User")
24
24
25
// Add a go.mod dependency on this repo (klex-git).
25
// Add a go.mod dependency on this repo (klex-git).
26
_, this_file, _, _ := runtime.Caller(0)
26
_, this_file, _, _ := runtime.Caller(0)
27
os.Chdir(path.Dir(this_file))
27
os.Chdir(path.Dir(this_file))
28
this_repo, _ := FindRoot()
28
this_repo, _ := FindRoot()
29
os.Chdir(dir)
29
os.Chdir(dir)
30
go_mod := path.Join(dir, "go.mod")
30
go_mod := path.Join(dir, "go.mod")
31
check(t, AddLineTo(go_mod, "module klextest"))
31
check(t, AddLineTo(go_mod, "module klextest"))
32
AddLineTo(go_mod, "go 1.21")
32
AddLineTo(go_mod, "go 1.21")
33
AddLineTo(go_mod, "replace oscarkilo.com/klex-git => " + this_repo)
33
AddLineTo(go_mod, "replace oscarkilo.com/klex-git => " + this_repo)
34
RunOrDie("go", "get", "oscarkilo.com/klex-git")
34
RunOrDie("go", "get", "oscarkilo.com/klex-git")
35
RunOrDie("git", "add", "go.mod")
35
RunOrDie("git", "add", "go.mod")
36
RunOrDie("git", "commit", "-m", "adds go.mod")
36
RunOrDie("git", "commit", "-m", "adds go.mod")
37
37
38
// Add a pre-commit git hook that runs the committest binary.
38
// Add a pre-commit git hook that runs the committest binary.
39
// It writes to .git/hooks/pre-commit.out.
39
// It writes to .git/hooks/pre-commit.out.
40
hook := path.Join(dir, ".git", "hooks", "pre-commit")
40
hook := path.Join(dir, ".git", "hooks", "pre-commit")
41
AddLineTo(hook, "#!/bin/sh")
41
AddLineTo(hook, "#!/bin/sh")
42
AddLineTo(hook, "OUT=" + path.Join(path.Dir(hook), "pre-commit.out"))
42
AddLineTo(hook, "OUT=" + path.Join(path.Dir(hook), "pre-commit.out"))
43
AddLineTo(hook, "go run oscarkilo.com/klex-git/util/committest >$OUT 2>&1")
43
AddLineTo(hook, "go run oscarkilo.com/klex-git/util/committest >$OUT 2>&1")
44
os.Chmod(hook, 0755)
44
os.Chmod(hook, 0755)
45
45
46
return dir
46
return dir
47
}
47
}
48
48
49
func parseCommitInfoFrom(dir string, t *testing.T) *CommitInfo {
49
func parseCommitInfoFrom(dir string, t *testing.T) *CommitInfo {
50
out_file := path.Join(dir, ".git", "hooks", "pre-commit.out")
50
out_file := path.Join(dir, ".git", "hooks", "pre-commit.out")
51
var info CommitInfo
51
var info CommitInfo
52
err := ReadJsonFile(out_file, &info)
52
err := ReadJsonFile(out_file, &info)
53
if err != nil {
53
if err != nil {
54
t.Errorf("error parsing JSON from %s: %v", out_file, err)
54
t.Errorf("error parsing JSON from %s: %v", out_file, err)
55
return nil
55
return nil
56
}
56
}
57
return &info
57
return &info
58
}
58
}
59
59
60
func TestGetCommitInfo(t *testing.T) {
60
func TestGetCommitInfo(t *testing.T) {
61
repo := makeGitRepo(t)
61
repo := makeGitRepo(t)
62
t.Logf("created Git repo at %s", repo)
62
t.Logf("created Git repo at %s", repo)
63
63
64
commit := func(touched_files ...string) *CommitInfo {
64
commit := func(touched_files ...string) *CommitInfo {
65
// make a commit that touches the given files
65
// make a commit that touches the given files
66
for _, file := range touched_files {
66
for _, file := range touched_files {
67
err := AppendStringTo(path.Join(repo, file), "bubble and squeak\n")
67
err := AppendStringTo(path.Join(repo, file), "bubble and squeak\n")
68
if err != nil {
68
if err != nil {
69
t.Fatalf("couldn't write to %s: %v", file, err)
69
t.Fatalf("couldn't write to %s: %v", file, err)
70
}
70
}
71
}
71
}
72
RunOrDie("git", "add", ".")
72
RunOrDie("git", "add", ".")
73
RunOrDie("git", "commit", "-m", "test")
73
RunOrDie("git", "commit", "-m", "test")
74
return parseCommitInfoFrom(repo, t)
74
return parseCommitInfoFrom(repo, t)
75
}
75
}
76
76
77
// makea a 3-file commit
77
// makea a 3-file commit
78
info := commit("foo.txt", "bar/baz.txt", "bar/qux.txt")
78
info := commit("foo.txt", "bar/baz.txt", "bar/qux.txt")
79
golden := &CommitInfo{
79
golden := &CommitInfo{
80
ChangedPaths: []string{"bar/baz.txt", "bar/qux.txt", "foo.txt"},
80
ChangedPaths: []string{"bar/baz.txt", "bar/qux.txt", "foo.txt"},
81
}
81
}
82
RunOrDie("cp", "-r", repo, "/tmp/committest") // DEBUG
83
if !reflect.DeepEqual(info, golden) {
82
if !reflect.DeepEqual(info, golden) {
84
t.Errorf("\nexpected %v; got %v", golden.DebugString(), info.DebugString())
83
t.Errorf("\nexpected %v; got %v", golden.DebugString(), info.DebugString())
85
}
84
}
86
}
85
}