Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 15 additions & 31 deletions cmd/download_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strconv"
"testing"

"github.com/exercism/cli/config"
"github.com/exercism/cli/workspace"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -87,22 +88,22 @@ func TestDownload(t *testing.T) {
}()

testCases := []struct {
requestor string
requester bool
expectedDir string
flags map[string]string
}{
{
requestor: requestorSelf,
requester: true,
expectedDir: "",
flags: map[string]string{"exercise": "bogus-exercise"},
},
{
requestor: requestorSelf,
requester: true,
expectedDir: "",
flags: map[string]string{"uuid": "bogus-id"},
},
{
requestor: requestorOther,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These constants are still defined below but no longer used

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you for catching that.

requester: false,
expectedDir: filepath.Join("users", "alice"),
flags: map[string]string{"uuid": "bogus-id"},
},
Expand All @@ -113,7 +114,7 @@ func TestDownload(t *testing.T) {
defer os.RemoveAll(tmpDir)
assert.NoError(t, err)

ts := fakeDownloadServer(tc.requestor)
ts := fakeDownloadServer(strconv.FormatBool(tc.requester))
defer ts.Close()

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

targetDir := filepath.Join(tmpDir, tc.expectedDir)
assertDownloadedCorrectFiles(t, targetDir, tc.requestor)

metadata := `{
"track": "bogus-track",
"exercise":"bogus-exercise",
"id":"bogus-id",
"url":"",
"handle":"alice",
"is_requester":%s,
"auto_approve":false
}`
metadata = fmt.Sprintf(metadata, tc.requestor)
metadata = compact(t, metadata)
assertDownloadedCorrectFiles(t, targetDir)

path := filepath.Join(targetDir, "bogus-track", "bogus-exercise", ".solution.json")
b, err := ioutil.ReadFile(path)
var s workspace.Solution
err = json.Unmarshal(b, &s)
assert.NoError(t, err)
assert.Equal(t, metadata, string(b), "the solution metadata file")

assert.Equal(t, "bogus-track", s.Track)
assert.Equal(t, "bogus-exercise", s.Exercise)
assert.Equal(t, tc.requester, s.IsRequester)
}
}

Expand Down Expand Up @@ -182,7 +176,7 @@ func fakeDownloadServer(requestor string) *httptest.Server {
return server
}

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

const requestorSelf = "true"
const requestorOther = "false"

const payloadTemplate = `
{
"solution": {
Expand Down Expand Up @@ -245,10 +236,3 @@ const payloadTemplate = `
}
}
`

func compact(t *testing.T, s string) string {
buffer := new(bytes.Buffer)
err := json.Compact(buffer, []byte(s))
assert.NoError(t, err)
return buffer.String()
}