renamed SimpleStore receiver.

Fixes #12

Change-Id: If22752568e38843be48f25d9126dd9fd4d81c076
This commit is contained in:
Stephen McQuay 2016-03-01 23:41:44 -08:00
parent 2e4add8a1c
commit 20da49f37f
2 changed files with 44 additions and 27 deletions

View File

@ -224,6 +224,23 @@ func TestBadJson(t *testing.T) {
} }
} }
func TestBadVcs(t *testing.T) {
ms := NewSimpleStore("")
s := &Server{
storage: ms,
}
ts := httptest.NewServer(s)
url := fmt.Sprintf("%s/foo", ts.URL)
resp, err := http.Post(url, "application/json", strings.NewReader(`{"vcs": "bitbucket", "repo": "https://s.mcquay.me/sm/vain"}`))
if err != nil {
t.Errorf("couldn't POST: %v", err)
}
resp.Body.Close()
if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
t.Errorf("should have reported bad vcs specified; got %v, want %v", http.StatusText(got), http.StatusText(want))
}
}
func TestUnsupportedMethod(t *testing.T) { func TestUnsupportedMethod(t *testing.T) {
ms := NewSimpleStore("") ms := NewSimpleStore("")
s := &Server{ s := &Server{

View File

@ -50,14 +50,14 @@ func (ms *SimpleStore) Contains(name string) bool {
} }
// Add adds p to the SimpleStore. // Add adds p to the SimpleStore.
func (ms *SimpleStore) Add(p Package) error { func (ss *SimpleStore) Add(p Package) error {
ms.l.Lock() ss.l.Lock()
ms.p[p.path] = p ss.p[p.path] = p
ms.l.Unlock() ss.l.Unlock()
m := "" m := ""
if err := ms.Save(); err != nil { if err := ss.Save(); err != nil {
m = fmt.Sprintf("unable to store db: %v", err) m = fmt.Sprintf("unable to store db: %v", err)
if err := ms.Remove(p.path); err != nil { if err := ss.Remove(p.path); err != nil {
m = fmt.Sprintf("%s\nto add insult to injury, could not delete package: %v\n", m, err) m = fmt.Sprintf("%s\nto add insult to injury, could not delete package: %v\n", m, err)
} }
return errors.New(m) return errors.New(m)
@ -66,49 +66,49 @@ func (ms *SimpleStore) Add(p Package) error {
} }
// Remove removes p from the SimpleStore. // Remove removes p from the SimpleStore.
func (ms *SimpleStore) Remove(path string) error { func (ss *SimpleStore) Remove(path string) error {
ms.l.Lock() ss.l.Lock()
delete(ms.p, path) delete(ss.p, path)
ms.l.Unlock() ss.l.Unlock()
return nil return nil
} }
// All returns all current packages. // All returns all current packages.
func (ms *SimpleStore) All() []Package { func (ss *SimpleStore) All() []Package {
r := []Package{} r := []Package{}
ms.l.RLock() ss.l.RLock()
for _, p := range ms.p { for _, p := range ss.p {
r = append(r, p) r = append(r, p)
} }
ms.l.RUnlock() ss.l.RUnlock()
return r return r
} }
// Save writes the db to disk. // Save writes the db to disk.
func (ms *SimpleStore) Save() error { func (ss *SimpleStore) Save() error {
// running in-memory only // running in-memory only
if ms.path == "" { if ss.path == "" {
return nil return nil
} }
ms.dbl.Lock() ss.dbl.Lock()
defer ms.dbl.Unlock() defer ss.dbl.Unlock()
f, err := os.Create(ms.path) f, err := os.Create(ss.path)
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer f.Close()
return json.NewEncoder(f).Encode(ms.p) return json.NewEncoder(f).Encode(ss.p)
} }
// Load reads the db from disk and populates ms. // Load reads the db from disk and populates ss.
func (ms *SimpleStore) Load() error { func (ss *SimpleStore) Load() error {
// running in-memory only // running in-memory only
if ms.path == "" { if ss.path == "" {
return nil return nil
} }
ms.dbl.Lock() ss.dbl.Lock()
defer ms.dbl.Unlock() defer ss.dbl.Unlock()
f, err := os.Open(ms.path) f, err := os.Open(ss.path)
if err != nil { if err != nil {
return err return err
} }
@ -120,7 +120,7 @@ func (ms *SimpleStore) Load() error {
} }
for k, v := range in { for k, v := range in {
v.path = k v.path = k
ms.p[k] = v ss.p[k] = v
} }
return nil return nil
} }