Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 21 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -35,13 +35,13 @@ type Config struct {

// ToFile writes a Config to a JSON file.
func ToFile(path string, c Config) error {
sanitize(&c)
bytes, err := json.Marshal(c)
f, err := os.Create(path) // truncates existing file if it exists
if err != nil {
return err
}
defer f.Close()

err = ioutil.WriteFile(path, bytes, 0644)
err = Encode(f, c)
if err != nil {
return err
}
Expand All @@ -51,18 +51,30 @@ func ToFile(path string, c Config) error {

// FromFile loads a Config object from a JSON file.
func FromFile(path string) (c Config, err error) {
bytes, err := ioutil.ReadFile(path)
f, err := os.Open(path)
if err != nil {
return
return c, err
}
defer f.Close()
return Decode(f)
}

func Encode(w io.Writer, c Config) error {
sanitize(&c)
e := json.NewEncoder(w)
return e.Encode(c)
}

err = json.Unmarshal(bytes, &c)
func Decode(r io.Reader) (Config, error) {
d := json.NewDecoder(r)
var c Config
err := d.Decode(&c)
if err != nil {
return
return c, err
}
sanitize(&c)

return
return c, err
}

// HomeDir return's the user's canonical home directory.
Expand Down
47 changes: 28 additions & 19 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -34,41 +35,49 @@ func TestReadingWritingConfig(t *testing.T) {
filename := Filename(tmpDir)
assert.NoError(t, err)

currentConfig := Config{
GithubUsername: "user",
APIKey: "MyKey",
ExercismDirectory: "/exercism/directory",
Hostname: "localhost\r\n",
}
sanitizedConfig := Config{
writtenConfig := Config{
GithubUsername: "user",
APIKey: "MyKey",
ExercismDirectory: "/exercism/directory",
Hostname: "localhost",
}

ToFile(filename, currentConfig)
ToFile(filename, writtenConfig)

loadedConfig, err := FromFile(filename)
assert.NoError(t, err)

assert.Equal(t, sanitizedConfig, loadedConfig)
assert.Equal(t, writtenConfig, loadedConfig)
}

func TestSanitizeFields(t *testing.T) {
config := Config{
GithubUsername: "user ",
APIKey: "MyKey ",
ExercismDirectory: "/home/user name\r\n",
Hostname: "localhost\n",
}
func TestDecodingConfig(t *testing.T) {
unsanitizedJson := `{"githubUsername":"user ","apiKey":"MyKey ","exercismDirectory":"/exercism/directory\r\n","hostname":"localhost \r\n"}`
sanitizedConfig := Config{
GithubUsername: "user",
APIKey: "MyKey",
ExercismDirectory: "/home/user name",
ExercismDirectory: "/exercism/directory",
Hostname: "localhost",
}
sanitize(&config)
b := bytes.NewBufferString(unsanitizedJson)
c, err := Decode(b)

assert.Equal(t, config, sanitizedConfig)
assert.NoError(t, err)
assert.Equal(t, sanitizedConfig, c)
}

func TestEncodingConfig(t *testing.T) {
currentConfig := Config{
GithubUsername: "user\r\n",
APIKey: "MyKey ",
ExercismDirectory: "/home/user name ",
Hostname: "localhost ",
}
sanitizedJson := `{"githubUsername":"user","apiKey":"MyKey","exercismDirectory":"/home/user name","hostname":"localhost"}
`

buf := new(bytes.Buffer)
err := Encode(buf, currentConfig)

assert.NoError(t, err)
assert.Equal(t, sanitizedJson, buf.String())
}