Skip to content

Commit ea7d503

Browse files
author
Katrina Owen
authored
Merge pull request #676 from exercism/better-metadata
Improve the assertions for the downloaded solution metadata
2 parents 92f5af6 + 0c3ac4d commit ea7d503

1 file changed

Lines changed: 15 additions & 31 deletions

File tree

cmd/download_test.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package cmd
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"fmt"
76
"io/ioutil"
87
"net/http"
98
"net/http/httptest"
109
"os"
1110
"path/filepath"
11+
"strconv"
1212
"testing"
1313

1414
"github.com/exercism/cli/config"
15+
"github.com/exercism/cli/workspace"
1516
"github.com/spf13/pflag"
1617
"github.com/spf13/viper"
1718
"github.com/stretchr/testify/assert"
@@ -87,22 +88,22 @@ func TestDownload(t *testing.T) {
8788
}()
8889

8990
testCases := []struct {
90-
requestor string
91+
requester bool
9192
expectedDir string
9293
flags map[string]string
9394
}{
9495
{
95-
requestor: requestorSelf,
96+
requester: true,
9697
expectedDir: "",
9798
flags: map[string]string{"exercise": "bogus-exercise"},
9899
},
99100
{
100-
requestor: requestorSelf,
101+
requester: true,
101102
expectedDir: "",
102103
flags: map[string]string{"uuid": "bogus-id"},
103104
},
104105
{
105-
requestor: requestorOther,
106+
requester: false,
106107
expectedDir: filepath.Join("users", "alice"),
107108
flags: map[string]string{"uuid": "bogus-id"},
108109
},
@@ -113,7 +114,7 @@ func TestDownload(t *testing.T) {
113114
defer os.RemoveAll(tmpDir)
114115
assert.NoError(t, err)
115116

116-
ts := fakeDownloadServer(tc.requestor)
117+
ts := fakeDownloadServer(strconv.FormatBool(tc.requester))
117118
defer ts.Close()
118119

119120
v := viper.New()
@@ -134,24 +135,17 @@ func TestDownload(t *testing.T) {
134135
assert.NoError(t, err)
135136

136137
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)
138+
assertDownloadedCorrectFiles(t, targetDir)
150139

151140
path := filepath.Join(targetDir, "bogus-track", "bogus-exercise", ".solution.json")
152141
b, err := ioutil.ReadFile(path)
142+
var s workspace.Solution
143+
err = json.Unmarshal(b, &s)
153144
assert.NoError(t, err)
154-
assert.Equal(t, metadata, string(b), "the solution metadata file")
145+
146+
assert.Equal(t, "bogus-track", s.Track)
147+
assert.Equal(t, "bogus-exercise", s.Exercise)
148+
assert.Equal(t, tc.requester, s.IsRequester)
155149
}
156150
}
157151

@@ -182,7 +176,7 @@ func fakeDownloadServer(requestor string) *httptest.Server {
182176
return server
183177
}
184178

185-
func assertDownloadedCorrectFiles(t *testing.T, targetDir, requestor string) {
179+
func assertDownloadedCorrectFiles(t *testing.T, targetDir string) {
186180
expectedFiles := []struct {
187181
desc string
188182
path string
@@ -213,9 +207,6 @@ func assertDownloadedCorrectFiles(t *testing.T, targetDir, requestor string) {
213207
assert.True(t, os.IsNotExist(err), "It should not write the file if empty.")
214208
}
215209

216-
const requestorSelf = "true"
217-
const requestorOther = "false"
218-
219210
const payloadTemplate = `
220211
{
221212
"solution": {
@@ -245,10 +236,3 @@ const payloadTemplate = `
245236
}
246237
}
247238
`
248-
249-
func compact(t *testing.T, s string) string {
250-
buffer := new(bytes.Buffer)
251-
err := json.Compact(buffer, []byte(s))
252-
assert.NoError(t, err)
253-
return buffer.String()
254-
}

0 commit comments

Comments
 (0)