Skip to content

Commit 22b5d93

Browse files
authored
Fix audit stack -v (#94)
* Fix error with cf audit-stack -v Check for an empty string json a problem occurred: error unmarshaling spaces json: unexpected end of JSON input * Use err for error * Fix audit stack -v Update unit test for empty Json case * Remove comment * Update spec
1 parent 49489c5 commit 22b5d93

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

cf/cf.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ func (cf *CF) getAllSpaces() (resources.Spaces, error) {
126126
}
127127

128128
var spaces resources.SpacesJSON
129+
if strings.Join(spacesJSON, "") == "" {
130+
break
131+
}
129132
if err := json.Unmarshal([]byte(strings.Join(spacesJSON, "")), &spaces); err != nil {
130133
return nil, fmt.Errorf("error unmarshaling spaces json: %v", err)
131134
}
@@ -146,9 +149,12 @@ func (cf *CF) GetAllApps() ([]resources.V3AppsJSON, error) {
146149
}
147150

148151
var apps resources.V3AppsJSON
152+
if strings.Join(appJSON, "") == "" {
153+
break
154+
}
149155

150156
if err := json.Unmarshal([]byte(strings.Join(appJSON, "")), &apps); err != nil {
151-
return nil, fmt.Errorf("error unmarshaling apps json: %v", err)
157+
return nil, fmt.Errorf("error unmarshaling apps json: %v", appJSON)
152158
}
153159
nextURL = apps.Pagination.Next.Href
154160
allApps = append(allApps, apps)

cf/cf_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/cloudfoundry/stack-auditor/cf"
77

88
"github.com/cloudfoundry/stack-auditor/mocks"
9+
"github.com/cloudfoundry/stack-auditor/resources"
910
"github.com/golang/mock/gomock"
1011
. "github.com/onsi/ginkgo/v2"
1112
. "github.com/onsi/gomega"
@@ -24,6 +25,18 @@ var _ = Describe("CF", func() {
2425
c = cf.CF{Conn: mockConnection}
2526
})
2627

28+
When("getAllApps", func() {
29+
It("performs a successful getAllApps with empty Json", func() {
30+
mockOutput := make([]string, 3)
31+
var allApps []resources.V3AppsJSON
32+
cf.V3ResultsPerPage = "1"
33+
mockConnection.EXPECT().CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/apps?per_page=1")).Return(mockOutput, nil).AnyTimes()
34+
output, err := c.GetAllApps()
35+
Expect(err).NotTo(HaveOccurred())
36+
Expect(output).To(Equal(allApps))
37+
})
38+
})
39+
2740
When("CFCurl", func() {
2841
It("performs a successful CF curl", func() {
2942
mockOutput, err := mocks.FileToString("apps.json")

0 commit comments

Comments
 (0)