internal/gitutil/archive_test.go
36 lines · 1047 bytes
1 symbol in this file
1package gitutil
2
3import (
4 "bytes"
5 "errors"
6 "testing"
7)
8
9// An archive is bounded: past MaxArchiveBytes git is stopped and the
10// caller gets ErrArchiveTooLarge rather than a truncated stream (#124).
11func TestArchiveCap(t *testing.T) {
12 dir := t.TempDir()
13 git(t, dir, "init", "-q", "-b", "main")
14 write(t, dir, "big.txt", string(bytes.Repeat([]byte("gitbay archive cap test line\n"), 4000)))
15 git(t, dir, "add", ".")
16 git(t, dir, "commit", "-q", "-m", "base")
17
18 var out bytes.Buffer
19 if err := Archive(dir, "main", "x", &out); err != nil {
20 t.Fatalf("archive: %v", err)
21 }
22 if out.Len() < 2 || out.Bytes()[0] != 0x1f || out.Bytes()[1] != 0x8b {
23 t.Fatalf("not gzip output: %d bytes", out.Len())
24 }
25
26 defer func(v int64) { MaxArchiveBytes = v }(MaxArchiveBytes)
27 MaxArchiveBytes = 64
28 out.Reset()
29 err := Archive(dir, "main", "x", &out)
30 if !errors.Is(err, ErrArchiveTooLarge) {
31 t.Fatalf("over the cap: err=%v, %d bytes written", err, out.Len())
32 }
33 if int64(out.Len()) > 64 {
34 t.Fatalf("wrote %d bytes past a 64-byte cap", out.Len())
35 }
36}