Skip to content

Commit b6954a7

Browse files
committed
added test
1 parent f384863 commit b6954a7

File tree

8 files changed

+160
-89
lines changed

8 files changed

+160
-89
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11

2+
.PHONY: test
3+
24
git-log-exec:
35
go build -o git-log-exec *.go
6+
7+
test:
8+
go test ./...

dumper.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import (
4+
"encoding/csv"
5+
"fmt"
6+
"io"
7+
"os"
8+
"time"
9+
10+
"github.com/schollz/progressbar/v2"
11+
)
12+
13+
type entry struct {
14+
commit string
15+
timestamp int
16+
data string
17+
}
18+
19+
func DumpHistory(directory string, command string, output io.Writer, limit int, after string, before string) error {
20+
var err error
21+
22+
if command == "" {
23+
return fmt.Errorf("No command given\n")
24+
}
25+
26+
if directory != "" {
27+
err = os.Chdir(directory)
28+
if err != nil {
29+
return fmt.Errorf("Invalid directory: %s\n", directory)
30+
}
31+
}
32+
33+
// make sure we leave a clean state afterwards
34+
branch, err := execute("git", "rev-parse", "--abbrev-ref", "HEAD")
35+
if err != nil {
36+
return err
37+
}
38+
defer execute("git", "checkout", branch)
39+
40+
commits, err := getCommits(limit, after, before)
41+
if err != nil {
42+
return err
43+
}
44+
45+
bar := progressbar.New(len(commits))
46+
result := make([]entry, 0, len(commits))
47+
48+
for _, commit := range commits {
49+
bar.Add(1)
50+
result = append(result, evaluate(commit, command))
51+
}
52+
53+
return writeCsv(result, output)
54+
}
55+
56+
func writeCsv(logs []entry, file io.Writer) error {
57+
writer := csv.NewWriter(file)
58+
err := writer.Write([]string{
59+
"time",
60+
"result",
61+
"commit",
62+
})
63+
64+
if err != nil {
65+
return err
66+
}
67+
68+
for _, row := range logs {
69+
err := writer.Write([]string{
70+
time.Unix(int64(row.timestamp), 0).Format(time.RFC3339),
71+
row.data,
72+
row.commit,
73+
})
74+
if err != nil {
75+
return err
76+
}
77+
}
78+
79+
writer.Flush()
80+
81+
return nil
82+
}

git.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
"strings"
77
)
88

9-
func getCommits(branch string, limit int, after string, before string) ([]entry, error) {
10-
checkout(branch)
11-
9+
func getCommits(limit int, after string, before string) ([]entry, error) {
1210
args := []string{
1311
"log",
1412
"--pretty=\"%h %ct\"",
@@ -53,9 +51,3 @@ func getCommits(branch string, limit int, after string, before string) ([]entry,
5351

5452
return commits, nil
5553
}
56-
57-
func checkout(branch string) error {
58-
_, err := execute("git", "checkout", branch)
59-
60-
return err
61-
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/brainexe/git-log-exec
22

33
go 1.13
44

5-
require github.com/schollz/progressbar/v2 v2.15.0
5+
require (
6+
github.com/schollz/progressbar/v2 v2.15.0
7+
github.com/stretchr/testify v1.3.0
8+
)

integration_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"io/ioutil"
7+
"os"
8+
"os/exec"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
const GitDir = "git_directory"
15+
16+
func TestRemoteRepo(t *testing.T) {
17+
t.Run("slack-bot-1", func(t *testing.T) {
18+
before := ""
19+
after := "2019-09-04T17:14:43+02:00"
20+
limit := 20
21+
22+
test(t, "../tests/test1.csv", "ls * | wc -l", "https://github.com/innogames/slack-bot.git", limit, before, after)
23+
})
24+
}
25+
26+
func test(t *testing.T, expectedFile string, command string, gitUrl string, limit int, after string, before string) {
27+
os.RemoveAll(GitDir)
28+
cmd := exec.Command("git", "clone", gitUrl, GitDir)
29+
out, err := cmd.CombinedOutput()
30+
assert.NoError(t, err, string(out))
31+
defer os.RemoveAll(GitDir)
32+
33+
var actual bytes.Buffer
34+
err = DumpHistory(GitDir, command, bufio.NewWriter(&actual), limit, after, before)
35+
assert.NoError(t, err)
36+
37+
if expectedFile != "" {
38+
expected, err := ioutil.ReadFile(expectedFile)
39+
assert.NoError(t, err, expected)
40+
41+
assert.Equal(t, string(expected), actual.String())
42+
}
43+
}

main.go

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,18 @@ import (
66
"os"
77
"os/exec"
88
"strings"
9-
10-
"github.com/schollz/progressbar/v2"
119
)
1210

13-
type entry struct {
14-
commit string
15-
timestamp int
16-
data string
17-
}
18-
19-
var directory string
20-
var command string
21-
var branch string
22-
var outputFile string
23-
var limit int
24-
var after string
25-
var before string
26-
2711
func main() {
12+
var directory string
13+
var command string
14+
var outputFile string
15+
var limit int
16+
var after string
17+
var before string
18+
2819
flag.StringVar(&directory, "directory", "", "git workspace (by default current directory)")
2920
flag.StringVar(&command, "command", "", "command to execute")
30-
flag.StringVar(&branch, "branch", "master", "git branch to check")
3121
flag.StringVar(&outputFile, "output", "out.csv", "output csv file")
3222
flag.StringVar(&after, "after", "", "optional begin date of history search")
3323
flag.StringVar(&before, "before", "", "optional end date of history search")
@@ -36,37 +26,14 @@ func main() {
3626

3727
// if no parameter given, use history of current history
3828
var err error
39-
if directory == "" {
40-
directory, err = os.Getwd()
41-
checkError(err)
42-
}
43-
44-
if stat, err := os.Stat(directory); err != nil || !stat.IsDir() {
45-
fmt.Printf("Invalid directory: %s\n", directory)
46-
flag.PrintDefaults()
47-
os.Exit(1)
48-
}
49-
50-
commits, err := getCommits(branch, limit, after, before)
51-
checkError(err)
52-
53-
bar := progressbar.New(len(commits))
54-
result := make([]entry, 0, len(commits))
55-
56-
for _, commit := range commits {
57-
bar.Add(1)
58-
result = append(result, evaluate(commit))
59-
}
60-
61-
// checkout to initial version
62-
checkout(branch)
6329

6430
out, err := os.Create(outputFile)
6531
checkError(err)
6632
defer out.Close()
6733

68-
err = writeCsv(result, out)
34+
err = DumpHistory(directory, command, out, limit, after, before)
6935
checkError(err)
36+
7037
fmt.Printf("\nWrote file %s\n", outputFile)
7138
}
7239

@@ -77,25 +44,25 @@ func checkError(err error) {
7744
}
7845
}
7946

80-
func evaluate(commit entry) entry {
81-
checkout(commit.commit)
47+
func evaluate(commit entry, command string) entry {
48+
execute("git", "checkout", commit.commit)
8249

8350
stdout, _ := execute("sh", "-c", command)
8451

85-
commit.data = strings.TrimSpace(stdout)
52+
data := strings.SplitN(stdout, "\n", 2)
53+
commit.data = data[0]
8654

8755
return commit
8856
}
8957

9058
func execute(name string, args ...string) (string, error) {
9159
cmd := exec.Command(name, args...)
92-
cmd.Dir = directory
9360
out, err := cmd.CombinedOutput()
9461

9562
if err != nil {
9663
fmt.Fprintf(os.Stderr, "Command '%s' failed: %s: %s", cmd.String(), err, string(out))
9764
os.Exit(1)
9865
}
9966

100-
return string(out), err
67+
return strings.TrimSpace(string(out)), err
10168
}

tests/test1.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
time,result,commit
2+
2019-09-04T17:14:43+02:00,94,47529f9
3+
2019-09-04T11:03:08+02:00,94,18dc0e8
4+
2019-08-31T18:39:27+02:00,94,9c3d6ea
5+
2019-08-14T21:49:34+02:00,94,b1c0848
6+
2019-08-12T11:07:55+02:00,94,c210243
7+
2019-07-30T21:27:03+02:00,94,c9934c9
8+
2019-07-25T19:12:51+02:00,90,9824e7f
9+
2019-07-24T20:47:41+02:00,88,8c56c5d
10+
2019-07-23T19:04:47+02:00,88,73a7583
11+
2019-07-21T21:18:39+02:00,88,ddeb55b

writer.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)