Compare commits
2 commits
75918ffdb7
...
0e036c7c7d
Author | SHA1 | Date | |
---|---|---|---|
0e036c7c7d | |||
080af9a413 |
5 changed files with 299 additions and 320 deletions
577
config_test.go
577
config_test.go
|
@ -1,19 +1,19 @@
|
||||||
package conf
|
package conf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type testconf struct {
|
type testconf struct {
|
||||||
String string
|
|
||||||
Int int
|
|
||||||
Invariant string
|
|
||||||
inUpdate func()
|
inUpdate func()
|
||||||
|
String string
|
||||||
|
Invariant string
|
||||||
|
Int int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t testconf) Update() {
|
func (t testconf) Update() {
|
||||||
|
@ -23,401 +23,372 @@ func (t testconf) Update() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadFile(t *testing.T) {
|
func TestLoadFile(t *testing.T) {
|
||||||
Convey("Given a config struct", t, func() {
|
t.Parallel()
|
||||||
|
|
||||||
|
t.Run("with a valid file", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
c := testconf{
|
c := testconf{
|
||||||
String: "default string",
|
String: "default string",
|
||||||
Int: 1,
|
Int: 1,
|
||||||
Invariant: "should not change",
|
Invariant: "should not change",
|
||||||
}
|
}
|
||||||
|
|
||||||
Convey("Given a valid config file", func() {
|
file := "test_data/valid.json"
|
||||||
file := "test.conf"
|
|
||||||
content := []byte(`{"String": "config string", "Int": 42}`)
|
|
||||||
err := ioutil.WriteFile(file, content, 0o644)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
defer func() { _ = os.Remove(file) }()
|
|
||||||
|
|
||||||
Convey("When the config file is loaded", func() {
|
err := LoadFile(file, &c)
|
||||||
err := LoadFile(file, &c)
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("Then there is no error", func() {
|
assert.Equal(t, "config string", c.String)
|
||||||
So(err, ShouldBeNil)
|
assert.Equal(t, 42, c.Int)
|
||||||
})
|
assert.Equal(t, "should not change", c.Invariant)
|
||||||
|
})
|
||||||
|
|
||||||
Convey("Then the struct fields are filled", func() {
|
t.Run("with an invalid file", func(t *testing.T) {
|
||||||
So(c.String, ShouldEqual, "config string")
|
t.Parallel()
|
||||||
So(c.Int, ShouldEqual, 42)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Then the default values are kept", func() {
|
c := testconf{
|
||||||
So(c.Invariant, ShouldEqual, "should not change")
|
String: "default string",
|
||||||
})
|
Int: 1,
|
||||||
})
|
Invariant: "should not change",
|
||||||
})
|
}
|
||||||
|
|
||||||
Convey("Given an invalid config file", func() {
|
file := "test_data/invalid.json"
|
||||||
file := "test.conf"
|
|
||||||
content := []byte(`String: not json`)
|
|
||||||
err := ioutil.WriteFile(file, content, 0o644)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
defer func() { _ = os.Remove(file) }()
|
|
||||||
|
|
||||||
Convey("When the config file is loaded", func() {
|
err := LoadFile(file, &c)
|
||||||
err := LoadFile(file, &c)
|
require.Error(t, err)
|
||||||
|
|
||||||
Convey("Then an error is returned", func() {
|
assert.Equal(t, "default string", c.String)
|
||||||
So(err, ShouldBeError)
|
assert.Equal(t, 1, c.Int)
|
||||||
})
|
assert.Equal(t, "should not change", c.Invariant)
|
||||||
|
})
|
||||||
|
|
||||||
Convey("Then the struct fields are not filled", func() {
|
t.Run("with a non existent file", func(t *testing.T) {
|
||||||
So(c.String, ShouldEqual, "default string")
|
t.Parallel()
|
||||||
So(c.Int, ShouldEqual, 1)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given a non existent config file", func() {
|
c := testconf{
|
||||||
file := "does-not-exist.conf.conf"
|
String: "default string",
|
||||||
|
Int: 1,
|
||||||
|
Invariant: "should not change",
|
||||||
|
}
|
||||||
|
|
||||||
Convey("When the config file is loaded", func() {
|
file := "does-not-exist.conf"
|
||||||
err := LoadFile(file, &c)
|
|
||||||
|
|
||||||
Convey("Then an error is returned", func() {
|
err := LoadFile(file, &c)
|
||||||
So(err, ShouldBeError)
|
require.Error(t, err)
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Then the struct fields are not filled", func() {
|
assert.Equal(t, "default string", c.String)
|
||||||
So(c.String, ShouldEqual, "default string")
|
assert.Equal(t, 1, c.Int)
|
||||||
So(c.Int, ShouldEqual, 1)
|
assert.Equal(t, "should not change", c.Invariant)
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadFiles(t *testing.T) {
|
func TestLoadFiles(t *testing.T) {
|
||||||
Convey("Given a config struct", t, func() {
|
t.Parallel()
|
||||||
c := &testconf{}
|
|
||||||
|
t.Run("with two valid files with different options", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
c := testconf{}
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
|
|
||||||
Convey("Given two existing files setting different options", func() {
|
content1 := []byte(`{"String": "foo"}`)
|
||||||
content1 := []byte(`{"String": "foo"}`)
|
content2 := []byte(`{"Int": 42}`)
|
||||||
content2 := []byte(`{"Int": 42}`)
|
paths := []string{
|
||||||
paths := []string{
|
filepath.Join(tmpDir, "file1.json"),
|
||||||
filepath.Join(tmpDir, "file1.json"),
|
filepath.Join(tmpDir, "file2.json"),
|
||||||
filepath.Join(tmpDir, "file2.json"),
|
}
|
||||||
}
|
|
||||||
|
|
||||||
err := ioutil.WriteFile(paths[0], content1, 0o600)
|
err := os.WriteFile(paths[0], content1, 0o600)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
err = ioutil.WriteFile(paths[1], content2, 0o600)
|
err = os.WriteFile(paths[1], content2, 0o600)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("When LoadFiles is called", func() {
|
err = LoadFiles(&c, paths...)
|
||||||
err := LoadFiles(&c, paths...)
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("Then there is no error", func() {
|
assert.Equal(t, "foo", c.String)
|
||||||
So(err, ShouldBeNil)
|
assert.Equal(t, 42, c.Int)
|
||||||
|
})
|
||||||
|
|
||||||
Convey("And the options from both files have been set", func() {
|
t.Run("with two valid files with the same option", func(t *testing.T) {
|
||||||
So(c.String, ShouldEqual, "foo")
|
t.Parallel()
|
||||||
So(c.Int, ShouldEqual, 42)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given two existing files setting the same option", func() {
|
c := testconf{}
|
||||||
content1 := []byte(`{"String": "foo"}`)
|
tmpDir := t.TempDir()
|
||||||
content2 := []byte(`{"String": "bar"}`)
|
|
||||||
paths := []string{
|
|
||||||
filepath.Join(tmpDir, "file1.json"),
|
|
||||||
filepath.Join(tmpDir, "file2.json"),
|
|
||||||
}
|
|
||||||
|
|
||||||
err := ioutil.WriteFile(paths[0], content1, 0o600)
|
content1 := []byte(`{"String": "foo"}`)
|
||||||
So(err, ShouldBeNil)
|
content2 := []byte(`{"String": "bar"}`)
|
||||||
err = ioutil.WriteFile(paths[1], content2, 0o600)
|
paths := []string{
|
||||||
So(err, ShouldBeNil)
|
filepath.Join(tmpDir, "file1.json"),
|
||||||
|
filepath.Join(tmpDir, "file2.json"),
|
||||||
|
}
|
||||||
|
|
||||||
Convey("When LoadFiles is called", func() {
|
err := os.WriteFile(paths[0], content1, 0o600)
|
||||||
err := LoadFiles(&c, paths...)
|
require.NoError(t, err)
|
||||||
|
err = os.WriteFile(paths[1], content2, 0o600)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("Then there is no error", func() {
|
err = LoadFiles(&c, paths...)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("And the last file overwrote the first", func() {
|
assert.Equal(t, "bar", c.String)
|
||||||
So(c.String, ShouldEqual, "bar")
|
})
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given one non-existing and one existing files", func() {
|
t.Run("with one non-existing and one existing file", func(t *testing.T) {
|
||||||
content2 := []byte(`{"String": "bar"}`)
|
t.Parallel()
|
||||||
paths := []string{
|
|
||||||
"does-not-exist.json",
|
|
||||||
filepath.Join(tmpDir, "file2.json"),
|
|
||||||
}
|
|
||||||
|
|
||||||
err := ioutil.WriteFile(paths[1], content2, 0o600)
|
c := testconf{}
|
||||||
So(err, ShouldBeNil)
|
tmpDir := t.TempDir()
|
||||||
|
|
||||||
Convey("When LoadFiles is called", func() {
|
content2 := []byte(`{"String": "bar"}`)
|
||||||
err := LoadFiles(&c, paths...)
|
paths := []string{
|
||||||
|
"does-not-exist.json",
|
||||||
|
filepath.Join(tmpDir, "file2.json"),
|
||||||
|
}
|
||||||
|
|
||||||
Convey("Then there is no error", func() {
|
err := os.WriteFile(paths[1], content2, 0o600)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("And the options from the last file have been set", func() {
|
err = LoadFiles(&c, paths...)
|
||||||
So(c.String, ShouldEqual, "bar")
|
require.NoError(t, err)
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given one invalid and one valid files", func() {
|
assert.Equal(t, "bar", c.String)
|
||||||
content1 := []byte(`{"`)
|
})
|
||||||
content2 := []byte(`{"String": "bar"}`)
|
|
||||||
paths := []string{
|
|
||||||
filepath.Join(tmpDir, "file1.json"),
|
|
||||||
filepath.Join(tmpDir, "file2.json"),
|
|
||||||
}
|
|
||||||
|
|
||||||
err := ioutil.WriteFile(paths[0], content1, 0o600)
|
t.Run("with one valid and one invalid file", func(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
t.Parallel()
|
||||||
err = ioutil.WriteFile(paths[1], content2, 0o600)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
Convey("When LoadFiles is called", func() {
|
c := testconf{}
|
||||||
err := LoadFiles(&c, paths...)
|
tmpDir := t.TempDir()
|
||||||
|
|
||||||
Convey("Then an error is returned", func() {
|
content1 := []byte(`{"`)
|
||||||
So(err, ShouldBeError)
|
content2 := []byte(`{"String": "bar"}`)
|
||||||
|
paths := []string{
|
||||||
|
filepath.Join(tmpDir, "file1.json"),
|
||||||
|
filepath.Join(tmpDir, "file2.json"),
|
||||||
|
}
|
||||||
|
|
||||||
Convey("And the last file has not been read", func() {
|
err := os.WriteFile(paths[0], content1, 0o600)
|
||||||
So(c.String, ShouldNotEqual, "bar")
|
require.NoError(t, err)
|
||||||
})
|
err = os.WriteFile(paths[1], content2, 0o600)
|
||||||
})
|
require.NoError(t, err)
|
||||||
})
|
|
||||||
})
|
err = LoadFiles(&c, paths...)
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, "", c.String)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSaveFile(t *testing.T) {
|
func TestSaveFile(t *testing.T) {
|
||||||
Convey("Given a config struct", t, func() {
|
t.Parallel()
|
||||||
|
|
||||||
|
t.Run("with a valid path", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
c := testconf{
|
c := testconf{
|
||||||
String: "default string",
|
String: "default string",
|
||||||
Int: 1,
|
|
||||||
Invariant: "should not change",
|
Invariant: "should not change",
|
||||||
|
Int: 1,
|
||||||
}
|
}
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
file := filepath.Join(tmpDir, "test.conf")
|
||||||
|
|
||||||
Convey("Given a valid path", func() {
|
err := SaveFile(file, &c)
|
||||||
file := "test.conf"
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("When the config file is saved", func() {
|
assert.FileExists(t, file)
|
||||||
err := SaveFile(file, &c)
|
|
||||||
defer func() { _ = os.Remove(file) }()
|
|
||||||
|
|
||||||
Convey("Then there is no error", func() {
|
expected := "{\n \"String\": \"default string\",\n \"Invariant\": \"should not change\",\n \"Int\": 1\n}"
|
||||||
So(err, ShouldBeNil)
|
got, err := os.ReadFile(file)
|
||||||
})
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, string(got))
|
||||||
|
})
|
||||||
|
|
||||||
Convey("Then the file exists", func() {
|
t.Run("with a valid path and invalid data", func(t *testing.T) {
|
||||||
_, err := os.Stat(file)
|
t.Parallel()
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
Convey("Then the content is correct", func() {
|
tmpDir := t.TempDir()
|
||||||
expected := "{\n \"String\": \"default string\",\n \"Int\": 1,\n \"Invariant\": \"should not change\"\n}"
|
file := filepath.Join(tmpDir, "test.conf")
|
||||||
got, err := ioutil.ReadFile(file)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(string(got), ShouldEqual, expected)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("When the config file is saved with invalid data", func() {
|
err := SaveFile(file, func() error { return nil })
|
||||||
err := SaveFile(file, func() error { return nil })
|
require.Error(t, err)
|
||||||
defer func() { _ = os.Remove(file) }()
|
|
||||||
|
|
||||||
Convey("Then an error is returned", func() {
|
assert.NoFileExists(t, file)
|
||||||
So(err, ShouldBeError)
|
})
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Then the file does not exist", func() {
|
t.Run("with an invalid path", func(t *testing.T) {
|
||||||
_, err := os.Stat(file)
|
t.Parallel()
|
||||||
So(os.IsNotExist(err), ShouldBeTrue)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given an invalid path", func() {
|
c := testconf{
|
||||||
file := "cannot/write/here.conf"
|
String: "default string",
|
||||||
|
Invariant: "should not change",
|
||||||
|
Int: 1,
|
||||||
|
}
|
||||||
|
file := "cannot/write/here.conf"
|
||||||
|
|
||||||
Convey("When the config file is loaded", func() {
|
err := SaveFile(file, &c)
|
||||||
err := SaveFile(file, &c)
|
require.Error(t, err)
|
||||||
|
|
||||||
Convey("Then an error is returned", func() {
|
assert.NoFileExists(t, file)
|
||||||
So(err, ShouldBeError)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Then the file does not exist", func() {
|
|
||||||
_, err := os.Stat(file)
|
|
||||||
So(os.IsNotExist(err), ShouldBeTrue)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadAndUpdateFile(t *testing.T) {
|
func TestLoadAndUpdateFile(t *testing.T) {
|
||||||
Convey("Given a config struct and a path", t, func() {
|
t.Parallel()
|
||||||
updated := false
|
|
||||||
|
|
||||||
|
t.Run("when the target file does not exist", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
updated := false
|
||||||
c := testconf{
|
c := testconf{
|
||||||
String: "default string",
|
String: "default string",
|
||||||
Int: 1,
|
Int: 1,
|
||||||
Invariant: "should not change",
|
Invariant: "should not change",
|
||||||
|
inUpdate: func() { updated = true },
|
||||||
}
|
}
|
||||||
c.inUpdate = func() { updated = true }
|
tmpDir := t.TempDir()
|
||||||
file := "test.conf"
|
file := filepath.Join(tmpDir, "test.conf")
|
||||||
|
|
||||||
Convey("Given no file exists at this path", func() {
|
err := LoadAndUpdateFile(file, &c)
|
||||||
_, err := os.Stat(file)
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeError)
|
|
||||||
So(os.IsNotExist(err), ShouldBeTrue)
|
|
||||||
|
|
||||||
Convey("When LoadAndUpdateFile is called", func() {
|
var c2 testconf
|
||||||
err := LoadAndUpdateFile(file, &c)
|
err = read(file, &c2)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
defer func() { _ = os.Remove(file) }()
|
assert.Equal(t, c.String, c2.String)
|
||||||
|
assert.Equal(t, c.Int, c2.Int)
|
||||||
|
assert.Equal(t, c.Invariant, c2.Invariant)
|
||||||
|
|
||||||
Convey("Then the config file has been created", func() {
|
assert.False(t, updated)
|
||||||
_, err := os.Stat(file)
|
})
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
Convey("and it has the default configuration", func() {
|
t.Run("when the path cannot be written", func(t *testing.T) {
|
||||||
var c2 testconf
|
t.Parallel()
|
||||||
err := read(file, &c2)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
c.inUpdate = nil
|
|
||||||
So(c2, ShouldResemble, c)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given the path cannot be written", func() {
|
updated := false
|
||||||
file = "does-not-exist/test.conf"
|
c := testconf{
|
||||||
_, err := os.Stat(file)
|
String: "default string",
|
||||||
So(err, ShouldBeError)
|
Int: 1,
|
||||||
So(os.IsNotExist(err), ShouldBeTrue)
|
Invariant: "should not change",
|
||||||
|
inUpdate: func() { updated = true },
|
||||||
|
}
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
file := filepath.Join(tmpDir, "does-not-exist", "test.conf")
|
||||||
|
|
||||||
Convey("When LoadAndUpdateFile is called", func() {
|
err := LoadAndUpdateFile(file, &c)
|
||||||
err := LoadAndUpdateFile(file, &c)
|
require.Error(t, err)
|
||||||
defer func() { _ = os.Remove(file) }()
|
|
||||||
|
|
||||||
Convey("Then an error is returned", func() {
|
assert.NoFileExists(t, file)
|
||||||
So(err, ShouldBeError)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given a config file with custom values", func() {
|
assert.False(t, updated)
|
||||||
content := []byte(`{"String": "config string", "Int": 42}`)
|
})
|
||||||
err := ioutil.WriteFile(file, content, 0o644)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
defer func() { _ = os.Remove(file) }()
|
|
||||||
|
|
||||||
Convey("When LoadAndUpdateFile is called", func() {
|
t.Run("when the config file is invalid", func(t *testing.T) {
|
||||||
err := LoadAndUpdateFile(file, &c)
|
t.Parallel()
|
||||||
|
|
||||||
Convey("Then there is no error", func() {
|
updated := false
|
||||||
So(err, ShouldBeNil)
|
c := testconf{
|
||||||
})
|
String: "default string",
|
||||||
|
Int: 1,
|
||||||
|
Invariant: "should not change",
|
||||||
|
inUpdate: func() { updated = true },
|
||||||
|
}
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
file := filepath.Join(tmpDir, "test.conf")
|
||||||
|
|
||||||
Convey("Then the struct fields are filled", func() {
|
content := []byte(`String: not json`)
|
||||||
So(c.String, ShouldEqual, "config string")
|
err := os.WriteFile(file, content, 0o644)
|
||||||
So(c.Int, ShouldEqual, 42)
|
require.NoError(t, err)
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given an invalid configuration file", func() {
|
err = LoadAndUpdateFile(file, &c)
|
||||||
content := []byte(`String: not json`)
|
require.Error(t, err)
|
||||||
err := ioutil.WriteFile(file, content, 0o644)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
defer func() { _ = os.Remove(file) }()
|
|
||||||
|
|
||||||
Convey("When LoadAndUpdateFile is called", func() {
|
assert.False(t, updated)
|
||||||
err := LoadAndUpdateFile(file, &c)
|
})
|
||||||
defer func() { _ = os.Remove(file) }()
|
|
||||||
|
|
||||||
Convey("Then an error is returned", func() {
|
t.Run("when the config file is valid", func(t *testing.T) {
|
||||||
So(err, ShouldBeError)
|
t.Parallel()
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given a config file with missing options", func() {
|
updated := false
|
||||||
content := []byte(`{"String": "config string"}`)
|
c := testconf{
|
||||||
err := ioutil.WriteFile(file, content, 0o644)
|
String: "default string",
|
||||||
So(err, ShouldBeNil)
|
Int: 1,
|
||||||
defer func() { _ = os.Remove(file) }()
|
Invariant: "should not change",
|
||||||
|
inUpdate: func() { updated = true },
|
||||||
|
}
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
file := filepath.Join(tmpDir, "test.conf")
|
||||||
|
|
||||||
Convey("When LoadAndUpdateFile is called", func() {
|
content := []byte(`{"String": "config string", "Int": 42}`)
|
||||||
err := LoadAndUpdateFile(file, &c)
|
err := os.WriteFile(file, content, 0o644)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("Then there is no error", func() {
|
err = LoadAndUpdateFile(file, &c)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Then the config file has been updated with new options", func() {
|
var c2 testconf
|
||||||
content, err := ioutil.ReadFile(file)
|
err = read(file, &c2)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
So(string(content), ShouldContainSubstring, `"Int"`)
|
assert.Equal(t, "config string", c2.String)
|
||||||
})
|
assert.Equal(t, 42, c2.Int)
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given a config file with old options", func() {
|
assert.True(t, updated)
|
||||||
content := []byte(`{"String": "config string", "Foo": "blah"}`)
|
})
|
||||||
err := ioutil.WriteFile(file, content, 0o644)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
defer func() { _ = os.Remove(file) }()
|
|
||||||
|
|
||||||
Convey("When LoadAndUpdateFile is called", func() {
|
t.Run("when the config file is missing options", func(t *testing.T) {
|
||||||
err := LoadAndUpdateFile(file, &c)
|
t.Parallel()
|
||||||
|
|
||||||
Convey("Then there is no error", func() {
|
updated := false
|
||||||
So(err, ShouldBeNil)
|
c := testconf{
|
||||||
})
|
String: "default string",
|
||||||
|
Int: 1,
|
||||||
|
Invariant: "should not change",
|
||||||
|
inUpdate: func() { updated = true },
|
||||||
|
}
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
file := filepath.Join(tmpDir, "test.conf")
|
||||||
|
|
||||||
Convey("Then the config file has been updated with new options", func() {
|
content := []byte(`{"String": "config string"}`)
|
||||||
content, err := ioutil.ReadFile(file)
|
err := os.WriteFile(file, content, 0o644)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
So(string(content), ShouldNotContainSubstring, `"Foo"`)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given a valid config file", func() {
|
err = LoadAndUpdateFile(file, &c)
|
||||||
content := []byte(`{"String": "config string"}`)
|
require.NoError(t, err)
|
||||||
err := ioutil.WriteFile(file, content, 0o644)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
defer func() { _ = os.Remove(file) }()
|
|
||||||
|
|
||||||
Convey("When LoadAndUpdateFile is called", func() {
|
newContent, err := os.ReadFile(file)
|
||||||
err := LoadAndUpdateFile(file, &c)
|
require.NoError(t, err)
|
||||||
|
assert.Contains(t, string(newContent), "Int")
|
||||||
|
assert.Contains(t, string(newContent), "Invariant")
|
||||||
|
|
||||||
Convey("Then there is no error", func() {
|
assert.True(t, updated)
|
||||||
So(err, ShouldBeNil)
|
})
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Then the update method of the struct has been called", func() {
|
t.Run("when the config contains unknown options", func(t *testing.T) {
|
||||||
So(updated, ShouldBeTrue)
|
t.Parallel()
|
||||||
})
|
|
||||||
})
|
updated := false
|
||||||
})
|
c := testconf{
|
||||||
|
String: "default string",
|
||||||
|
Int: 1,
|
||||||
|
Invariant: "should not change",
|
||||||
|
inUpdate: func() { updated = true },
|
||||||
|
}
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
file := filepath.Join(tmpDir, "test.conf")
|
||||||
|
|
||||||
|
content := []byte(`{"String": "config string", "Foo": "blah"}`)
|
||||||
|
err := os.WriteFile(file, content, 0o644)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = LoadAndUpdateFile(file, &c)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
newContent, err := os.ReadFile(file)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotContains(t, string(newContent), "Foo")
|
||||||
|
|
||||||
|
assert.True(t, updated)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
8
go.mod
8
go.mod
|
@ -2,10 +2,10 @@ module code.bcarlin.xyz/go/conf
|
||||||
|
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require github.com/smartystreets/goconvey v1.7.2
|
require github.com/stretchr/testify v1.9.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/jtolds/gls v4.20.0+incompatible // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/smartystreets/assertions v1.2.0 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
32
go.sum
32
go.sum
|
@ -1,13 +1,19 @@
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
1
test_data/invalid.json
Normal file
1
test_data/invalid.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
String: not json
|
1
test_data/valid.json
Normal file
1
test_data/valid.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"String": "config string", "Int": 42}
|
Loading…
Reference in a new issue