Skip to content

Commit f81bc67

Browse files
Katrina Owennywilken
authored andcommitted
Move assertion for solution metadata out of helper (#659)
* Move assertion for solution metadata out of helper The solution metadata varies on a test-by-test basis. I don't like hiding that away in a helper. * Delete tmpdir after download test completes * Compact the JSON in the download test for readability
1 parent 5c5f24f commit f81bc67

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

cmd/download_test.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
57
"io/ioutil"
68
"net/http"
@@ -108,6 +110,7 @@ func TestDownload(t *testing.T) {
108110

109111
for _, tc := range testCases {
110112
tmpDir, err := ioutil.TempDir("", "download-cmd")
113+
defer os.RemoveAll(tmpDir)
111114
assert.NoError(t, err)
112115

113116
ts := fakeDownloadServer(tc.requestor)
@@ -130,7 +133,25 @@ func TestDownload(t *testing.T) {
130133
err = runDownload(cfg, flags, []string{})
131134
assert.NoError(t, err)
132135

133-
assertDownloadedCorrectFiles(t, filepath.Join(tmpDir, tc.expectedDir), tc.requestor)
136+
targetDir := filepath.Join(tmpDir, tc.expectedDir)
137+
assertDownloadedCorrectFiles(t, targetDir, tc.requestor)
138+
139+
metadata := `{
140+
"track": "bogus-track",
141+
"exercise":"bogus-exercise",
142+
"id":"bogus-id",
143+
"url":"",
144+
"handle":"alice",
145+
"is_requester":%s,
146+
"auto_approve":false
147+
}`
148+
metadata = fmt.Sprintf(metadata, tc.requestor)
149+
metadata = compact(t, metadata)
150+
151+
path := filepath.Join(targetDir, "bogus-track", "bogus-exercise", ".solution.json")
152+
b, err := ioutil.ReadFile(path)
153+
assert.NoError(t, err)
154+
assert.Equal(t, metadata, string(b), "the solution metadata file")
134155
}
135156
}
136157

@@ -165,7 +186,6 @@ func fakeDownloadServer(requestor string) *httptest.Server {
165186
}
166187

167188
func assertDownloadedCorrectFiles(t *testing.T, targetDir, requestor string) {
168-
metadata := `{"track":"bogus-track","exercise":"bogus-exercise","id":"bogus-id","url":"","handle":"alice","is_requester":%s,"auto_approve":false}`
169189
expectedFiles := []struct {
170190
desc string
171191
path string
@@ -181,11 +201,6 @@ func assertDownloadedCorrectFiles(t *testing.T, targetDir, requestor string) {
181201
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", "subdir", "file-2.txt"),
182202
contents: "this is file 2",
183203
},
184-
{
185-
desc: "the solution metadata file",
186-
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", ".solution.json"),
187-
contents: fmt.Sprintf(metadata, requestor),
188-
},
189204
}
190205

191206
for _, file := range expectedFiles {
@@ -233,3 +248,10 @@ const payloadTemplate = `
233248
}
234249
}
235250
`
251+
252+
func compact(t *testing.T, s string) string {
253+
buffer := new(bytes.Buffer)
254+
err := json.Compact(buffer, []byte(s))
255+
assert.NoError(t, err)
256+
return buffer.String()
257+
}

0 commit comments

Comments
 (0)