| 1 |
package otbuiltin |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io/ioutil" |
| 6 |
"os" |
| 7 |
"path" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/14rcole/gopopulate" |
| 11 |
) |
| 12 |
|
| 13 |
func TestCommitSuccess(t *testing.T) { |
| 14 |
// Make a base directory in which all of our test data resides |
| 15 |
baseDir, err := ioutil.TempDir("", "otbuiltin-test-") |
| 16 |
if err != nil { |
| 17 |
t.Errorf("%s", err) |
| 18 |
return |
| 19 |
} |
| 20 |
defer os.RemoveAll(baseDir) |
| 21 |
|
| 22 |
// Make a directory in which the repo should exist |
| 23 |
repoDir := path.Join(baseDir, "repo") |
| 24 |
err = os.Mkdir(repoDir, 0777) |
| 25 |
if err != nil { |
| 26 |
t.Errorf("%s", err) |
| 27 |
return |
| 28 |
} |
| 29 |
|
| 30 |
// Initialize the repo |
| 31 |
inited, err := Init(repoDir, NewInitOptions()) |
| 32 |
if !inited || err != nil { |
| 33 |
fmt.Println("Cannot test commit: failed to initialize repo") |
| 34 |
return |
| 35 |
} |
| 36 |
|
| 37 |
// Make a new directory full of random data to commit |
| 38 |
commitDir := path.Join(baseDir, "commit1") |
| 39 |
err = os.Mkdir(commitDir, 0777) |
| 40 |
if err != nil { |
| 41 |
t.Errorf("%s", err) |
| 42 |
return |
| 43 |
} |
| 44 |
err = gopopulate.PopulateDir(commitDir, "rd", 4, 4) |
| 45 |
if err != nil { |
| 46 |
t.Errorf("%s", err) |
| 47 |
return |
| 48 |
} |
| 49 |
|
| 50 |
// Test commit |
| 51 |
repo, err := OpenRepo(repoDir) |
| 52 |
if err != nil { |
| 53 |
t.Errorf("%s", err) |
| 54 |
} |
| 55 |
opts := NewCommitOptions() |
| 56 |
branch := "test-branch" |
| 57 |
_, err = repo.PrepareTransaction() |
| 58 |
if err != nil { |
| 59 |
t.Errorf("%s", err) |
| 60 |
} |
| 61 |
ret, err := repo.Commit(commitDir, branch, opts) |
| 62 |
if err != nil { |
| 63 |
t.Errorf("%s", err) |
| 64 |
} else { |
| 65 |
fmt.Println(ret) |
| 66 |
} |
| 67 |
_, err = repo.CommitTransaction() |
| 68 |
if err != nil { |
| 69 |
t.Errorf("%s", err) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestCommitTreeSuccess(t *testing.T) { |
| 74 |
// Make a base directory in which all of our test data resides |
| 75 |
baseDir, err := ioutil.TempDir("", "otbuiltin-test-") |
| 76 |
if err != nil { |
| 77 |
t.Fatalf("failed to create tempdir: %s", err) |
| 78 |
} |
| 79 |
defer os.RemoveAll(baseDir) |
| 80 |
|
| 81 |
// Make a directory in which the repo should exist |
| 82 |
repoDir := path.Join(baseDir, "repo") |
| 83 |
if err := os.MkdirAll(repoDir, 0777); err != nil { |
| 84 |
t.Fatalf("failed to create repodir at %q: %s", repoDir, err) |
| 85 |
} |
| 86 |
|
| 87 |
// Initialize the repo |
| 88 |
repoOpts := NewInitOptions() |
| 89 |
repoOpts.Mode = "archive" |
| 90 |
initOk, err := Init(repoDir, repoOpts) |
| 91 |
if err != nil { |
| 92 |
t.Fatalf("failed to initialize the repo: %s", err) |
| 93 |
} |
| 94 |
if !initOk { |
| 95 |
t.Fatal("failed to initialize repo") |
| 96 |
} |
| 97 |
|
| 98 |
// Make a new directory full of random data to commit |
| 99 |
commitDir := path.Join(baseDir, "commit1") |
| 100 |
tarPath := path.Join(baseDir, "tree.tar") |
| 101 |
if err := os.Mkdir(commitDir, 0777); err != nil { |
| 102 |
t.Fatalf("failed to make random data dir at %q: %s", commitDir, err) |
| 103 |
} |
| 104 |
if err := gopopulate.PopulateDir(commitDir, "rd", 4, 4); err != nil { |
| 105 |
t.Fatalf("failed to populate dir: %s", err) |
| 106 |
} |
| 107 |
if err := gopopulate.Tar(commitDir, tarPath); err != nil { |
| 108 |
t.Fatalf("failed to tar populated dir: %s", err) |
| 109 |
} |
| 110 |
|
| 111 |
// Test commit |
| 112 |
repo, err := OpenRepo(repoDir) |
| 113 |
if err != nil { |
| 114 |
t.Fatalf("failed to open repo at %q: %s", repoDir, err) |
| 115 |
} |
| 116 |
branch := "test-branch" |
| 117 |
opts := NewCommitOptions() |
| 118 |
opts.Subject = "blob" |
| 119 |
opts.Tree = []string{"tar=" + tarPath} |
| 120 |
opts.TarAutoCreateParents = true |
| 121 |
if _, err := repo.PrepareTransaction(); err != nil { |
| 122 |
t.Fatalf("failed to prepare transaction: %s", err) |
| 123 |
} |
| 124 |
|
| 125 |
if _, err := repo.Commit("", branch, opts); err != nil { |
| 126 |
t.Fatalf("failed to commit: %s", err) |
| 127 |
} |
| 128 |
|
| 129 |
if _, err = repo.CommitTransaction(); err != nil { |
| 130 |
t.Fatalf("failed to commit transaction: %s", err) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestCommitTreeParentSuccess(t *testing.T) { |
| 135 |
// Make a base directory in which all of our test data resides |
| 136 |
baseDir, err := ioutil.TempDir("", "otbuiltin-test-") |
| 137 |
if err != nil { |
| 138 |
t.Fatalf("failed to create tempdir: %s", err) |
| 139 |
} |
| 140 |
defer os.RemoveAll(baseDir) |
| 141 |
|
| 142 |
// Make a directory in which the repo should exist |
| 143 |
repoDir := path.Join(baseDir, "repo") |
| 144 |
if err = os.MkdirAll(repoDir, 0777); err != nil { |
| 145 |
t.Fatalf("failed to create repodir at %q: %s", repoDir, err) |
| 146 |
} |
| 147 |
|
| 148 |
// Initialize the repo |
| 149 |
repoOpts := NewInitOptions() |
| 150 |
repoOpts.Mode = "archive" |
| 151 |
initOk, err := Init(repoDir, repoOpts) |
| 152 |
if err != nil { |
| 153 |
t.Fatalf("failed to initialize the repo: %s", err) |
| 154 |
} |
| 155 |
if !initOk { |
| 156 |
t.Fatal("failed to initialize repo") |
| 157 |
} |
| 158 |
|
| 159 |
// Make a new directory full of random data to commit |
| 160 |
commitDir := path.Join(baseDir, "commit1") |
| 161 |
tarPath := path.Join(baseDir, "tree.tar") |
| 162 |
if err := os.Mkdir(commitDir, 0777); err != nil { |
| 163 |
t.Fatalf("failed to make random data dir at %q: %s", commitDir, err) |
| 164 |
} |
| 165 |
if err := gopopulate.PopulateDir(commitDir, "rd", 4, 4); err != nil { |
| 166 |
t.Fatalf("failed to populate dir: %s", err) |
| 167 |
} |
| 168 |
if err := gopopulate.Tar(commitDir, tarPath); err != nil { |
| 169 |
t.Fatalf("failed to tar populated dir: %s", err) |
| 170 |
} |
| 171 |
|
| 172 |
// Create a test commit |
| 173 |
repo, err := OpenRepo(repoDir) |
| 174 |
if err != nil { |
| 175 |
t.Fatalf("failed to open repo at %q: %s", repoDir, err) |
| 176 |
} |
| 177 |
opts := NewCommitOptions() |
| 178 |
opts.Subject = "blob" |
| 179 |
opts.Tree = []string{"tar=" + tarPath} |
| 180 |
opts.TarAutoCreateParents = true |
| 181 |
branch := "test-branch" |
| 182 |
|
| 183 |
// Commit a first time |
| 184 |
if _, err := repo.PrepareTransaction(); err != nil { |
| 185 |
t.Fatalf("failed to prepare transaction: %s", err) |
| 186 |
} |
| 187 |
parentChecksum, err := repo.Commit("", branch, opts) |
| 188 |
if err != nil { |
| 189 |
t.Fatalf("failed to commit: %s", err) |
| 190 |
} |
| 191 |
if _, err := repo.CommitTransaction(); err != nil { |
| 192 |
t.Fatalf("failed to commit transaction: %s", err) |
| 193 |
} |
| 194 |
|
| 195 |
// Commit again, this time with a parent checksum |
| 196 |
if _, err := repo.PrepareTransaction(); err != nil { |
| 197 |
t.Fatalf("failed to prepare transaction: %s", err) |
| 198 |
} |
| 199 |
opts.Parent = parentChecksum |
| 200 |
if _, err := repo.Commit("", branch, opts); err != nil { |
| 201 |
t.Fatalf("failed to commit with parent: %s", err) |
| 202 |
} |
| 203 |
if _, err := repo.CommitTransaction(); err != nil { |
| 204 |
t.Fatalf("failed to commit transaction: %s", err) |
| 205 |
} |
| 206 |
} |