Skip to content

Commit 29deea6

Browse files
committed
Test running git-sizer from a subdirectory of the repository
1 parent f03e7ff commit 29deea6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

git_sizer_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"os"
88
"os/exec"
9+
"path/filepath"
910
"testing"
1011
"time"
1112

@@ -30,6 +31,24 @@ func gitCommand(t *testing.T, repo *git.Repository, args ...string) *exec.Cmd {
3031
return cmd
3132
}
3233

34+
func addFile(t *testing.T, repoPath string, repo *git.Repository, relativePath, contents string) {
35+
dirPath := filepath.Dir(relativePath)
36+
if dirPath != "." {
37+
require.NoError(t, os.MkdirAll(filepath.Join(repoPath, dirPath), 0777), "creating subdir")
38+
}
39+
40+
filename := filepath.Join(repoPath, relativePath)
41+
f, err := os.Create(filename)
42+
require.NoErrorf(t, err, "creating file %q", filename)
43+
_, err = f.WriteString(contents)
44+
require.NoErrorf(t, err, "writing to file %q", filename)
45+
require.NoErrorf(t, f.Close(), "closing file %q", filename)
46+
47+
cmd := gitCommand(t, repo, "add", relativePath)
48+
cmd.Dir = repoPath
49+
require.NoErrorf(t, cmd.Run(), "adding file %q", relativePath)
50+
}
51+
3352
func addAuthorInfo(cmd *exec.Cmd, timestamp *time.Time) {
3453
cmd.Env = append(cmd.Env,
3554
"GIT_AUTHOR_NAME=Arthur",
@@ -217,3 +236,33 @@ func TestTaggedTags(t *testing.T) {
217236
require.NoError(t, err, "scanning repository")
218237
assert.Equal(t, counts.Count32(3), h.MaxTagDepth, "tag depth")
219238
}
239+
240+
func TestFromSubdir(t *testing.T) {
241+
t.Parallel()
242+
path, err := ioutil.TempDir("", "subdir")
243+
require.NoError(t, err, "creating temporary directory")
244+
245+
defer func() {
246+
os.RemoveAll(path)
247+
}()
248+
249+
cmd := exec.Command("git", "init", path)
250+
require.NoError(t, cmd.Run(), "initializing repo")
251+
repo, err := git.NewRepository(path)
252+
require.NoError(t, err, "initializing Repository object")
253+
254+
timestamp := time.Unix(1112911993, 0)
255+
256+
addFile(t, path, repo, "subdir/file.txt", "Hello, world!\n")
257+
258+
cmd = gitCommand(t, repo, "commit", "-m", "initial")
259+
addAuthorInfo(cmd, &timestamp)
260+
require.NoError(t, cmd.Run(), "creating commit")
261+
262+
repo2, err := git.NewRepository(filepath.Join(path, "subdir"))
263+
require.NoError(t, err, "creating Repository object in subdirectory")
264+
_, err = sizes.ScanRepositoryUsingGraph(
265+
repo2, git.AllReferencesFilter, sizes.NameStyleNone, false,
266+
)
267+
require.NoError(t, err, "scanning repository")
268+
}

0 commit comments

Comments
 (0)