Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 1aa1c8c

Browse files
ethantkoeniglafriks
authored andcommitted
Tests for GetComimitsInfo (#99)
1 parent 4573c63 commit 1aa1c8c

36 files changed

+602
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ _testmain.go
2626
coverage.out
2727

2828
benchmark/
29+
tests/repos/repo1/

commit_info_test.go

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55
"path/filepath"
66
"testing"
77
"time"
8+
9+
"github.com/stretchr/testify/assert"
810
)
911

12+
const testReposDir = "tests/repos/"
1013
const benchmarkReposDir = "benchmark/repos/"
1114

12-
func setupGitRepo(url string, name string) (string, error) {
13-
repoDir := filepath.Join(benchmarkReposDir, name)
15+
func cloneRepo(url, dir, name string) (string, error) {
16+
repoDir := filepath.Join(dir, name)
1417
if _, err := os.Stat(repoDir); err == nil {
1518
return repoDir, nil
1619
}
@@ -22,6 +25,60 @@ func setupGitRepo(url string, name string) (string, error) {
2225
})
2326
}
2427

28+
func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
29+
// these test case are specific to the repo1 test repo
30+
testCases := []struct {
31+
CommitID string
32+
Path string
33+
ExpectedIDs map[string]string
34+
}{
35+
{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", "", map[string]string{
36+
"file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
37+
"file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
38+
}},
39+
{"2839944139e0de9737a044f78b0e4b40d989a9e3", "", map[string]string{
40+
"file1.txt": "2839944139e0de9737a044f78b0e4b40d989a9e3",
41+
"branch1.txt": "9c9aef8dd84e02bc7ec12641deb4c930a7c30185",
42+
}},
43+
{"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", "branch2", map[string]string{
44+
"branch2.txt": "5c80b0245c1c6f8343fa418ec374b13b5d4ee658",
45+
}},
46+
}
47+
for _, testCase := range testCases {
48+
commit, err := repo1.GetCommit(testCase.CommitID)
49+
assert.NoError(t, err)
50+
tree, err := commit.Tree.SubTree(testCase.Path)
51+
assert.NoError(t, err)
52+
entries, err := tree.ListEntries()
53+
assert.NoError(t, err)
54+
commitsInfo, err := entries.GetCommitsInfo(commit, testCase.Path)
55+
assert.NoError(t, err)
56+
assert.Len(t, commitsInfo, len(testCase.ExpectedIDs))
57+
for _, commitInfo := range commitsInfo {
58+
entry := commitInfo[0].(*TreeEntry)
59+
commit := commitInfo[1].(*Commit)
60+
expectedID, ok := testCase.ExpectedIDs[entry.Name()]
61+
if !assert.True(t, ok) {
62+
continue
63+
}
64+
assert.Equal(t, expectedID, commit.ID.String())
65+
}
66+
}
67+
}
68+
69+
func TestEntries_GetCommitsInfo(t *testing.T) {
70+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
71+
bareRepo1, err := OpenRepository(bareRepo1Path)
72+
assert.NoError(t, err)
73+
testGetCommitsInfo(t, bareRepo1)
74+
75+
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1")
76+
assert.NoError(t, err)
77+
clonedRepo1, err := OpenRepository(clonedPath)
78+
assert.NoError(t, err)
79+
testGetCommitsInfo(t, clonedRepo1)
80+
}
81+
2582
func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
2683
benchmarks := []struct {
2784
url string
@@ -36,7 +93,7 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
3693
for _, benchmark := range benchmarks {
3794
var commit *Commit
3895
var entries Entries
39-
if repoPath, err := setupGitRepo(benchmark.url, benchmark.name); err != nil {
96+
if repoPath, err := cloneRepo(benchmark.url, benchmarkReposDir, benchmark.name); err != nil {
4097
b.Fatal(err)
4198
} else if repo, err := OpenRepository(repoPath); err != nil {
4299
b.Fatal(err)

tests/repos/repo1_bare/HEAD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master

tests/repos/repo1_bare/config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = true

tests/repos/repo1_bare/description

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message taken by
4+
# applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit. The hook is
8+
# allowed to edit the commit message file.
9+
#
10+
# To enable this hook, rename this file to "applypatch-msg".
11+
12+
. git-sh-setup
13+
commitmsg="$(git rev-parse --git-path hooks/commit-msg)"
14+
test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"}
15+
:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message.
4+
# Called by "git commit" with one argument, the name of the file
5+
# that has the commit message. The hook should exit with non-zero
6+
# status after issuing an appropriate message if it wants to stop the
7+
# commit. The hook is allowed to edit the commit message file.
8+
#
9+
# To enable this hook, rename this file to "commit-msg".
10+
11+
# Uncomment the below to add a Signed-off-by line to the message.
12+
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
13+
# hook is more suited to it.
14+
#
15+
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
16+
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
17+
18+
# This example catches duplicate Signed-off-by lines.
19+
20+
test "" = "$(grep '^Signed-off-by: ' "$1" |
21+
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
22+
echo >&2 Duplicate Signed-off-by lines.
23+
exit 1
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to prepare a packed repository for use over
4+
# dumb transports.
5+
#
6+
# To enable this hook, rename this file to "post-update".
7+
8+
exec git update-server-info
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify what is about to be committed
4+
# by applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit.
8+
#
9+
# To enable this hook, rename this file to "pre-applypatch".
10+
11+
. git-sh-setup
12+
precommit="$(git rev-parse --git-path hooks/pre-commit)"
13+
test -x "$precommit" && exec "$precommit" ${1+"$@"}
14+
:
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify what is about to be committed.
4+
# Called by "git commit" with no arguments. The hook should
5+
# exit with non-zero status after issuing an appropriate message if
6+
# it wants to stop the commit.
7+
#
8+
# To enable this hook, rename this file to "pre-commit".
9+
10+
if git rev-parse --verify HEAD >/dev/null 2>&1
11+
then
12+
against=HEAD
13+
else
14+
# Initial commit: diff against an empty tree object
15+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
16+
fi
17+
18+
# If you want to allow non-ASCII filenames set this variable to true.
19+
allownonascii=$(git config --bool hooks.allownonascii)
20+
21+
# Redirect output to stderr.
22+
exec 1>&2
23+
24+
# Cross platform projects tend to avoid non-ASCII filenames; prevent
25+
# them from being added to the repository. We exploit the fact that the
26+
# printable range starts at the space character and ends with tilde.
27+
if [ "$allownonascii" != "true" ] &&
28+
# Note that the use of brackets around a tr range is ok here, (it's
29+
# even required, for portability to Solaris 10's /usr/bin/tr), since
30+
# the square bracket bytes happen to fall in the designated range.
31+
test $(git diff --cached --name-only --diff-filter=A -z $against |
32+
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
33+
then
34+
cat <<\EOF
35+
Error: Attempt to add a non-ASCII file name.
36+
37+
This can cause problems if you want to work with people on other platforms.
38+
39+
To be portable it is advisable to rename the file.
40+
41+
If you know what you are doing you can disable this check using:
42+
43+
git config hooks.allownonascii true
44+
EOF
45+
exit 1
46+
fi
47+
48+
# If there are whitespace errors, print the offending file names and fail.
49+
exec git diff-index --check --cached $against --

0 commit comments

Comments
 (0)