From 4b79c0c765b4ec2d76abfcda227f66ca02524b48 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Wed, 28 Feb 2018 23:20:34 -0800 Subject: [PATCH] Adds remote tests --- remote/remote.go | 10 +++ remote/remote_test.go | 157 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 remote/remote_test.go diff --git a/remote/remote.go b/remote/remote.go index 12064f9..9e283ed 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -138,3 +138,13 @@ func strip(u url.URL) url.URL { Path: u.Path, } } + +func mkdirs(root string) error { + d, _ := filepath.Split(filepath.Join(root, fn)) + if !fs.Exists(d) { + if err := os.MkdirAll(d, 0700); err != nil { + return errors.Wrap(err, "mk pm dir") + } + } + return nil +} diff --git a/remote/remote_test.go b/remote/remote_test.go new file mode 100644 index 0000000..c2d55e6 --- /dev/null +++ b/remote/remote_test.go @@ -0,0 +1,157 @@ +package remote + +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", + } + + if err := Add(root, bad); err == nil { + t.Fatalf("didn't detect bad url") + } + + uris := []string{ + "https://pm.mcquay.me/darwin/amd64", + } + + if err := Add(root, uris); err != nil { + 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) + } + } + + if err := Add(root, uris); err == nil { + t.Fatalf("did not detect duplicate, and should have") + } +} + +func TestRemove(t *testing.T) { + root, del := dirMe(t) + defer del() + + if err := Remove(root, nil); err == nil { + 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", + } + + if err := Remove(root, uris); err == nil { + t.Fatalf("should have returned error asking to remove many uri on empty db") + } + + if err := Add(root, uris); err != nil { + t.Fatalf("add: %v", err) + } + if err := Remove(root, uris[1:2]); err != nil { + 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", + } + + if err := Add(root, uris); err != nil { + t.Fatalf("add: %v", err) + } + + buf := &bytes.Buffer{} + if err := List(root, buf); err != nil { + 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()) + } + } + +}