Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Added test for claimstore and ensured the use of empty slices instead…
Browse files Browse the repository at this point in the history
… of nil ones (#311)
  • Loading branch information
tariq1890 authored and technosophos committed Oct 30, 2018
1 parent fd86c5a commit 77901a4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/claim/claimstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s Store) Read(name string) (Claim, error) {

// ReadAll retrieves all the claims
func (s Store) ReadAll() ([]Claim, error) {
claims := []Claim{}
claims := make([]Claim, 0)

list, err := s.backingStore.List()
if err != nil {
Expand Down
39 changes: 39 additions & 0 deletions pkg/claim/claimstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,42 @@ func TestCanUpdate(t *testing.T) {
t.Errorf("Expected to read back new revision, got old revision %s", rev)
}
}

func TestReadAll(t *testing.T) {
is := assert.New(t)

tempDir, err := ioutil.TempDir("", "duffletest")
if err != nil {
t.Fatalf("Failed to create temp dir: %s", err)
}
defer os.RemoveAll(tempDir)

storeDir := filepath.Join(tempDir, "claimstore")
store := NewClaimStore(crud.NewFileSystemStore(storeDir, "json"))

claim, err := New("foo")
is.NoError(err)
claim.Bundle = &bundle.Bundle{Name: "foobundle", Version: "0.1.0"}

is.NoError(store.Store(*claim), "Failed to store: %s", err)

claim2, err := New("bar")
is.NoError(err)
claim2.Bundle = &bundle.Bundle{Name: "barbundle", Version: "0.1.0"}

is.NoError(store.Store(*claim2), "Failed to store: %s", err)

claim3, err := New("baz")
is.NoError(err)
claim3.Bundle = &bundle.Bundle{Name: "bazbundle", Version: "0.1.0"}

is.NoError(store.Store(*claim3), "Failed to store: %s", err)

claims, err := store.ReadAll()
is.NoError(err, "Failed to read claims: %s", err)

is.Len(claims, 3)
is.Equal("foo", claim.Name)
is.Equal("bar", claim2.Name)
is.Equal("baz", claim3.Name)
}
4 changes: 2 additions & 2 deletions pkg/utils/crud/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (s fileSystemStore) ensure() error {
}

func (s fileSystemStore) storageFiles(files []os.FileInfo) []os.FileInfo {
var result []os.FileInfo
result := make([]os.FileInfo, 0)
ext := "." + s.fileExtension
for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ext {
Expand All @@ -103,7 +103,7 @@ func (s fileSystemStore) storageFiles(files []os.FileInfo) []os.FileInfo {
}

func names(files []os.FileInfo) []string {
var result []string
result := make([]string, 0)
for _, file := range files {
result = append(result, name(file.Name()))
}
Expand Down

0 comments on commit 77901a4

Please sign in to comment.