|
1 | 1 | package main_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
4 | 8 | "os/exec"
|
5 | 9 | "testing"
|
| 10 | + |
| 11 | + "github.com/github/git-sizer/counts" |
| 12 | + "github.com/github/git-sizer/git" |
| 13 | + "github.com/github/git-sizer/sizes" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
6 | 16 | )
|
7 | 17 |
|
8 | 18 | // Smoke test that the program runs.
|
9 | 19 | func TestExec(t *testing.T) {
|
10 |
| - command := exec.Command("bin/git-sizer") |
11 |
| - output, err := command.CombinedOutput() |
| 20 | + cmd := exec.Command("bin/git-sizer") |
| 21 | + output, err := cmd.CombinedOutput() |
12 | 22 | if err != nil {
|
13 | 23 | t.Errorf("command failed (%s); output: %#v", err, string(output))
|
14 | 24 | }
|
15 | 25 | }
|
| 26 | + |
| 27 | +func newGitBomb( |
| 28 | + repoName string, depth, breadth int, body string, |
| 29 | +) (repo *git.Repository, err error) { |
| 30 | + path, err := ioutil.TempDir("", repoName) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + |
| 35 | + defer func() { |
| 36 | + if err != nil { |
| 37 | + os.RemoveAll(path) |
| 38 | + } |
| 39 | + }() |
| 40 | + |
| 41 | + cmd := exec.Command("git", "init", "--bare", path) |
| 42 | + err = cmd.Run() |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + repo, err = git.NewRepository(path) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + |
| 52 | + oid, err := repo.CreateObject("blob", func(w io.Writer) error { |
| 53 | + _, err := io.WriteString(w, body) |
| 54 | + return err |
| 55 | + }) |
| 56 | + |
| 57 | + digits := len(fmt.Sprintf("%d", breadth-1)) |
| 58 | + |
| 59 | + mode := "100644" |
| 60 | + prefix := "f" |
| 61 | + |
| 62 | + for ; depth > 0; depth-- { |
| 63 | + oid, err = repo.CreateObject("tree", func(w io.Writer) error { |
| 64 | + for i := 0; i < breadth; i++ { |
| 65 | + _, err = fmt.Fprintf( |
| 66 | + w, "%s %s%0*d\x00%s", |
| 67 | + mode, prefix, digits, i, oid.Bytes(), |
| 68 | + ) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + } |
| 73 | + return nil |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + mode = "40000" |
| 80 | + prefix = "d" |
| 81 | + } |
| 82 | + |
| 83 | + oid, err = repo.CreateObject("commit", func(w io.Writer) error { |
| 84 | + _, err := fmt.Fprintf( |
| 85 | + w, |
| 86 | + "tree %s\n"+ |
| 87 | + "author Example <[email protected]> 1112911993 -0700\n"+ |
| 88 | + "committer Example <[email protected]> 1112911993 -0700\n"+ |
| 89 | + "\n"+ |
| 90 | + "Mwahahaha!\n", |
| 91 | + oid, |
| 92 | + ) |
| 93 | + return err |
| 94 | + }) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + err = repo.UpdateRef("refs/heads/master", oid) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + return repo, nil |
| 105 | +} |
| 106 | + |
| 107 | +func pow(x uint64, n int) uint64 { |
| 108 | + p := uint64(1) |
| 109 | + for ; n > 0; n-- { |
| 110 | + p *= x |
| 111 | + } |
| 112 | + return p |
| 113 | +} |
| 114 | + |
| 115 | +func TestBomb(t *testing.T) { |
| 116 | + assert := assert.New(t) |
| 117 | + |
| 118 | + repo, err := newGitBomb("bomb", 10, 10, "boom!\n") |
| 119 | + if err != nil { |
| 120 | + t.Errorf("failed to create bomb: %s", err) |
| 121 | + } |
| 122 | + defer os.RemoveAll(repo.Path()) |
| 123 | + |
| 124 | + h, err := sizes.ScanRepositoryUsingGraph( |
| 125 | + repo, git.AllReferencesFilter, sizes.NameStyleNone, false, |
| 126 | + ) |
| 127 | + if !assert.NoError(err) { |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + assert.Equal(counts.Count32(1), h.UniqueCommitCount, "unique commit count") |
| 132 | + assert.Equal(counts.Count64(169), h.UniqueCommitSize, "unique commit size") |
| 133 | + assert.Equal(counts.Count32(169), h.MaxCommitSize, "max commit size") |
| 134 | + assert.Equal(counts.Count32(1), h.MaxHistoryDepth, "max history depth") |
| 135 | + assert.Equal(counts.Count32(0), h.MaxParentCount, "max parent count") |
| 136 | + |
| 137 | + assert.Equal(counts.Count32(10), h.UniqueTreeCount, "unique tree count") |
| 138 | + assert.Equal(counts.Count64(2910), h.UniqueTreeSize, "unique tree size") |
| 139 | + assert.Equal(counts.Count64(100), h.UniqueTreeEntries, "unique tree entries") |
| 140 | + assert.Equal(counts.Count32(10), h.MaxTreeEntries, "max tree entries") |
| 141 | + |
| 142 | + assert.Equal(counts.Count32(1), h.UniqueBlobCount, "unique blob count") |
| 143 | + assert.Equal(counts.Count64(6), h.UniqueBlobSize, "unique blob size") |
| 144 | + assert.Equal(counts.Count32(6), h.MaxBlobSize, "max blob size") |
| 145 | + |
| 146 | + assert.Equal(counts.Count32(0), h.UniqueTagCount, "unique tag count") |
| 147 | + assert.Equal(counts.Count32(0), h.MaxTagDepth, "max tag depth") |
| 148 | + |
| 149 | + assert.Equal(counts.Count32(1), h.ReferenceCount, "reference count") |
| 150 | + |
| 151 | + assert.Equal(counts.Count32(11), h.MaxPathDepth, "max path depth") |
| 152 | + assert.Equal(counts.Count32(29), h.MaxPathLength, "max path length") |
| 153 | + |
| 154 | + assert.Equal(counts.Count32((pow(10, 10)-1)/(10-1)), h.MaxExpandedTreeCount, "max expanded tree count") |
| 155 | + assert.Equal(counts.Count32(0xffffffff), h.MaxExpandedBlobCount, "max expanded blob count") |
| 156 | + assert.Equal(counts.Count64(6*pow(10, 10)), h.MaxExpandedBlobSize, "max expanded blob size") |
| 157 | + assert.Equal(counts.Count32(0), h.MaxExpandedLinkCount, "max expanded link count") |
| 158 | + assert.Equal(counts.Count32(0), h.MaxExpandedSubmoduleCount, "max expanded submodule count") |
| 159 | +} |
0 commit comments