Example #1
0
func TestLoad(t *testing.T) {
	tmpDir, err := ioutil.TempDir("", "")
	if err != nil {
		t.Fatal(err)
	}
	configPath := filepath.Join(tmpDir, "config.json")
	if err := os.Link(fixturePath(t, "config.json"), configPath); err != nil {
		t.Fatal(err)
	}
	dirtyPath := filepath.Join(tmpDir, "dirty.json")
	if err := os.Link(fixturePath(t, "dirty.json"), dirtyPath); err != nil {
		t.Fatal(err)
	}
	paths.Home = tmpDir

	testCases := []struct {
		desc                string
		in                  string // the name of the file passed as a command line argument
		out                 string // the name of the file that the config will be written to
		dir, key, api, xapi string // the actual config values
	}{
		{
			desc: "defaults",
			in:   "",
			out:  paths.Config(""),
			dir:  paths.Exercises(""),
			key:  "",
			api:  hostAPI,
			xapi: hostXAPI,
		},
		{
			desc: "file exists",
			in:   configPath,
			out:  configPath,
			dir:  "/a/b/c",
			key:  "abc123",
			api:  "http://api.example.com",
			xapi: "http://x.example.com",
		},
		{
			desc: "unexpanded path",
			in:   "~/config.json",
			out:  configPath,
			dir:  "/a/b/c",
			key:  "abc123",
			api:  "http://api.example.com",
			xapi: "http://x.example.com",
		},
		{
			desc: "sanitizes whitespace",
			in:   "~/dirty.json",
			out:  filepath.Join(tmpDir, "dirty.json"),
			dir:  "/a/b/c",
			key:  "abc123",
			api:  "http://api.example.com",
			xapi: "http://x.example.com",
		},
	}

	for _, tc := range testCases {
		c, err := New(tc.in)
		if err != nil {
			t.Fatal(err)
		}
		assert.Equal(t, tc.out, c.File, tc.desc)
		assert.Equal(t, tc.dir, c.Dir, tc.desc)
		assert.Equal(t, tc.key, c.APIKey, tc.desc)
		assert.Equal(t, tc.api, c.API, tc.desc)
		assert.Equal(t, tc.xapi, c.XAPI, tc.desc)
	}
}
Example #2
0
// New returns a configuration struct with content from the exercism.json file
func New(path string) (*Config, error) {
	c := &Config{}
	err := c.load(paths.Config(path))
	return c, err
}