Skip to content

Commit

Permalink
test: prefer fs.MapFS to os.File (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane committed Oct 15, 2022
1 parent 80e06ca commit 7672f5b
Showing 1 changed file with 31 additions and 62 deletions.
93 changes: 31 additions & 62 deletions aconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"strings"
"testing"
"testing/fstest"
"time"
)

Expand Down Expand Up @@ -156,14 +157,15 @@ func TestDefaults_OtherNumberFormats(t *testing.T) {
}

func TestJSON(t *testing.T) {
filepath := createTestFile(t)
const filepath = "testfile.json"

var cfg structConfig
loader := LoaderFor(&cfg, Config{
SkipDefaults: true,
SkipEnv: true,
SkipFlags: true,
Files: []string{filepath},
FileSystem: fstest.MapFS{filepath: testfile},
})
failIfErr(t, loader.Load())

Expand All @@ -172,30 +174,32 @@ func TestJSON(t *testing.T) {
}

func TestJSONWithOmitempty(t *testing.T) {
type TestConfig struct {
const filepath = "testfile.json"

var cfg struct {
APIKey string `json:"b,omitempty"`
}

var cfg TestConfig
loader := LoaderFor(&cfg, Config{
SkipDefaults: true,
SkipEnv: true,
SkipFlags: true,
AllowUnknownFields: true,
Files: []string{createTestFile(t)},
Files: []string{filepath},
FileSystem: fstest.MapFS{filepath: testfile},
})
failIfErr(t, loader.Load())
}

func TestCustomFile(t *testing.T) {
filepath := createTestFile(t, "custom.config")
const filepath = "custom.config"

var cfg structConfig
loader := LoaderFor(&cfg, Config{
SkipDefaults: true,
SkipEnv: true,
SkipFlags: true,
Files: []string{filepath},
FileSystem: fstest.MapFS{filepath: testfile},
FileDecoders: map[string]FileDecoder{
".config": &jsonDecoder{},
},
Expand Down Expand Up @@ -671,41 +675,28 @@ func TestBadDefauts(t *testing.T) {

func TestBadFiles(t *testing.T) {
f := func(filepath string) {
var cfg TestConfig
loader := LoaderFor(&cfg, Config{
SkipDefaults: true,
SkipEnv: true,
SkipFlags: true,
FailOnFileNotFound: true,
Files: []string{filepath},
t.Helper()
t.Run(filepath, func(t *testing.T) {
t.Helper()
var cfg TestConfig
loader := LoaderFor(&cfg, Config{
SkipDefaults: true,
SkipEnv: true,
SkipFlags: true,
FailOnFileNotFound: true,
Files: []string{filepath},
FileSystem: fstest.MapFS{
"bad_config.json": &fstest.MapFile{Data: []byte(`{almost": "json`)},
"unknown.ext": &fstest.MapFile{},
},
})
failIfOk(t, loader.Load())
})
failIfOk(t, loader.Load())
}

t.Run("no_such_file.json", func(*testing.T) {
f("no_such_file.json")
})

t.Run("bad_config.json", func(t *testing.T) {
filepath := t.TempDir() + "unknown.ext"
file, err := os.Create(filepath)
failIfErr(t, err)
defer file.Close()

_, err = file.WriteString(`{almost": "json`)
failIfErr(t, err)

f(filepath)
})

t.Run("unknown.ext", func(t *testing.T) {
filepath := t.TempDir() + "unknown.ext"
file, err := os.Create(filepath)
failIfErr(t, err)
defer file.Close()

f(filepath)
})
f("no_such_file.json")
f("bad_config.json")
f("unknown.ext")
}

func TestFailOnFileNotFound(t *testing.T) {
Expand Down Expand Up @@ -1104,28 +1095,6 @@ func int32Ptr(a int32) *int32 {
return &a
}

func createTestFile(t *testing.T, name ...string) string {
t.Helper()
mustEqual(t, len(name) < 2, true)

dir := t.TempDir()
t.Cleanup(func() {
os.RemoveAll(dir)
})

filepath := dir + "/testfile.json"
if len(name) == 1 {
filepath = dir + name[0]
}

f, err := os.Create(filepath)
failIfErr(t, err)
defer f.Close()
_, err = f.WriteString(testfileContent)
failIfErr(t, err)
return filepath
}

type TestConfig struct {
Str string `default:"str-def"`
Bytes []byte `default:"bytes-def"`
Expand Down Expand Up @@ -1203,7 +1172,7 @@ type structP struct {
P string `json:"P"`
}

const testfileContent = `{
var testfile = &fstest.MapFile{Data: []byte(`{
"a": "b",
"c": 10,
"e": 123.456,
Expand Down Expand Up @@ -1238,7 +1207,7 @@ const testfileContent = `{
"P": "r"
}
}
`
`)}

var wantConfig = func() structConfig {
i := int32(42)
Expand Down

0 comments on commit 7672f5b

Please sign in to comment.