pm/db/remote_test.go

158 lines
2.9 KiB
Go
Raw Normal View History

2018-03-02 23:23:13 -08:00
package db
2018-02-28 23:20:34 -08:00
import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
)
// TODO (sm): add more tests, including
// - empty add
// - removing db to empty
// - bad uris
func dirMe(t *testing.T) (string, func()) {
root, err := ioutil.TempDir("", "pm-tests-")
if err != nil {
t.Fatalf("tmpdir: %v", err)
}
if err := mkdirs(root); err != nil {
t.Fatalf("making pm dirs: %v", err)
}
return root, func() {
if err := os.RemoveAll(root); err != nil {
t.Fatalf("cleanup: %v", err)
}
}
}
func TestAdd(t *testing.T) {
root, del := dirMe(t)
defer del()
{
db, err := load(root)
if err != nil {
t.Fatalf("load: %v", err)
}
if got, want := len(db), 0; got != want {
t.Fatalf("empty db not empty: got %v, want %v", got, want)
}
}
bad := []string{
"http\ns://\nFoo|n",
}
2018-03-02 23:23:13 -08:00
if err := AddRemotes(root, bad); err == nil {
2018-02-28 23:20:34 -08:00
t.Fatalf("didn't detect bad url")
}
uris := []string{
"https://pm.mcquay.me/darwin/amd64",
}
2018-03-02 23:23:13 -08:00
if err := AddRemotes(root, uris); err != nil {
2018-02-28 23:20:34 -08:00
t.Fatalf("add: %v", err)
}
db, err := load(root)
if err != nil {
t.Fatalf("load: %v", err)
}
if got, want := len(db), len(uris); got != want {
t.Fatalf("unepected number of uris; got %v, want %v", got, want)
}
for _, u := range uris {
found := false
for _, d := range db {
if d.String() == u {
found = true
}
}
if !found {
t.Fatalf("did not find %v in the db", u)
}
}
2018-03-02 23:23:19 -08:00
if err := AddRemotes(root, uris); err == nil {
2018-02-28 23:20:34 -08:00
t.Fatalf("did not detect duplicate, and should have")
}
}
func TestRemove(t *testing.T) {
root, del := dirMe(t)
defer del()
2018-03-02 23:23:19 -08:00
if err := RemoveRemotes(root, nil); err == nil {
2018-02-28 23:20:34 -08:00
t.Fatalf("should have returned error on empty db")
}
uris := []string{
"https://pm.mcquay.me/foo",
"https://pm.mcquay.me/bar",
"https://pm.mcquay.me/baz",
}
2018-03-02 23:23:19 -08:00
if err := RemoveRemotes(root, uris); err == nil {
2018-02-28 23:20:34 -08:00
t.Fatalf("should have returned error asking to remove many uri on empty db")
}
2018-03-02 23:23:19 -08:00
if err := AddRemotes(root, uris); err != nil {
2018-02-28 23:20:34 -08:00
t.Fatalf("add: %v", err)
}
2018-03-02 23:23:19 -08:00
if err := RemoveRemotes(root, uris[1:2]); err != nil {
2018-02-28 23:20:34 -08:00
t.Fatalf("remove: %v", err)
}
db, err := load(root)
if err != nil {
t.Fatalf("load: %v", err)
}
if got, want := len(db), len(uris)-1; got != want {
t.Fatalf("unepected number of uris; got %v, want %v", got, want)
}
found := false
for _, d := range db {
if d.String() == uris[1] {
found = true
}
}
if found {
for _, v := range db {
t.Logf("%v", v.String())
}
t.Fatalf("failed to remove %v", uris[1:2])
}
}
func TestList(t *testing.T) {
root, del := dirMe(t)
defer del()
uris := []string{
"https://pm.mcquay.me/foo",
"https://pm.mcquay.me/bar",
"https://pm.mcquay.me/baz",
}
2018-03-02 23:23:19 -08:00
if err := AddRemotes(root, uris); err != nil {
2018-02-28 23:20:34 -08:00
t.Fatalf("add: %v", err)
}
buf := &bytes.Buffer{}
2018-03-02 23:23:19 -08:00
if err := ListRemotes(root, buf); err != nil {
2018-02-28 23:20:34 -08:00
t.Fatalf("list: %v", err)
}
for _, u := range uris {
if !strings.Contains(buf.String(), u) {
t.Fatalf("could not find %q in output\n%v", u, buf.String())
}
}
}