| 1 |
package otbuiltin |
| 2 |
|
| 3 |
import ( |
| 4 |
"io/ioutil" |
| 5 |
"os" |
| 6 |
"path" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestInitSuccess(t *testing.T) { |
| 11 |
// Make a base directory in which all of our test data resides |
| 12 |
baseDir, err := ioutil.TempDir("", "otbuiltin-test-") |
| 13 |
if err != nil { |
| 14 |
t.Errorf("%s", err) |
| 15 |
return |
| 16 |
} |
| 17 |
defer os.RemoveAll(baseDir) |
| 18 |
// Make a directory in which the repo should exist |
| 19 |
repoDir := path.Join(baseDir, "repo") |
| 20 |
err = os.Mkdir(repoDir, 0777) |
| 21 |
if err != nil { |
| 22 |
t.Errorf("%s", err) |
| 23 |
return |
| 24 |
} |
| 25 |
|
| 26 |
// Initialize the repo |
| 27 |
inited, err := Init(repoDir, NewInitOptions()) |
| 28 |
if err != nil { |
| 29 |
t.Errorf("%s", err) |
| 30 |
return |
| 31 |
} else if !inited { |
| 32 |
t.Errorf("Cannot test commit: failed to initialize repo") |
| 33 |
return |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func TestInitBareUser(t *testing.T) { |
| 38 |
// Make a base directory in which all of our test data resides |
| 39 |
baseDir, err := ioutil.TempDir("", "otbuiltin-test-") |
| 40 |
if err != nil { |
| 41 |
t.Errorf("%s", err) |
| 42 |
return |
| 43 |
} |
| 44 |
defer os.RemoveAll(baseDir) |
| 45 |
// Make a directory in which the repo should exist |
| 46 |
repoDir := path.Join(baseDir, "repo") |
| 47 |
err = os.Mkdir(repoDir, 0777) |
| 48 |
if err != nil { |
| 49 |
t.Errorf("%s", err) |
| 50 |
return |
| 51 |
} |
| 52 |
|
| 53 |
// Initialize the repo |
| 54 |
initOpts := NewInitOptions() |
| 55 |
initOpts.Mode = "bare-user" |
| 56 |
inited, err := Init(repoDir, initOpts) |
| 57 |
if err != nil { |
| 58 |
t.Errorf("%s", err) |
| 59 |
return |
| 60 |
} else if !inited { |
| 61 |
t.Errorf("Cannot test commit: failed to initialize repo") |
| 62 |
return |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestParseRepoMode(t *testing.T) { |
| 67 |
tests := []struct { |
| 68 |
in string |
| 69 |
isErr bool |
| 70 |
}{ |
| 71 |
{ |
| 72 |
"archive-z2", |
| 73 |
false, |
| 74 |
}, |
| 75 |
{ |
| 76 |
"bare", |
| 77 |
false, |
| 78 |
}, |
| 79 |
{ |
| 80 |
"bare-user", |
| 81 |
false, |
| 82 |
}, |
| 83 |
{ |
| 84 |
"fooMode", |
| 85 |
true, |
| 86 |
}, |
| 87 |
} |
| 88 |
|
| 89 |
for _, tt := range tests { |
| 90 |
_, err := parseRepoMode(tt.in) |
| 91 |
if tt.isErr && err == nil { |
| 92 |
t.Fatal("got unexpected nil error") |
| 93 |
} else if !tt.isErr && err != nil { |
| 94 |
t.Fatalf("got unexpected error %q", err) |
| 95 |
} |
| 96 |
} |
| 97 |
} |