Skip to content

Commit

Permalink
up: add more unit tests, remove dep stretchr/testify
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 15, 2022
1 parent c8a8ab2 commit 7df8e94
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 282 deletions.
80 changes: 40 additions & 40 deletions dotenv/dotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,67 @@ import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

func TestLoad(t *testing.T) {
defer ClearLoaded()
err := Load("./testdata", "not-exist", ".env")
assert.Error(t, err)
assert.Err(t, err)

assert.Equal(t, "", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "", os.Getenv("DONT_ENV_TEST"))

err = Load("./testdata")
assert.NoError(t, err)
assert.Equal(t, "blog", os.Getenv("DONT_ENV_TEST"))
assert.Equal(t, "blog", Get("DONT_ENV_TEST"))
assert.NoErr(t, err)
assert.Eq(t, "blog", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "blog", Get("DONT_ENV_TEST"))
_ = os.Unsetenv("DONT_ENV_TEST") // clear

err = Load("./testdata", "error.ini")
assert.Error(t, err)
assert.Err(t, err)

err = Load("./testdata", "invalid_key.ini")
assert.Error(t, err)
assert.Err(t, err)

assert.Equal(t, "def-val", Get("NOT-EXIST", "def-val"))
assert.Eq(t, "def-val", Get("NOT-EXIST", "def-val"))
}

func TestLoadFiles(t *testing.T) {
defer Reset()
assert.Error(t, LoadFiles("./testdata/not-exist"))
assert.Equal(t, "", os.Getenv("DONT_ENV_TEST"))
assert.Err(t, LoadFiles("./testdata/not-exist"))
assert.Eq(t, "", os.Getenv("DONT_ENV_TEST"))

err := LoadFiles("./testdata/.env")

assert.NoError(t, err)
assert.Equal(t, "blog", os.Getenv("DONT_ENV_TEST"))
assert.Equal(t, "blog", Get("DONT_ENV_TEST"))
assert.NoErr(t, err)
assert.Eq(t, "blog", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "blog", Get("DONT_ENV_TEST"))
}

func TestLoadExists(t *testing.T) {
defer Reset()
assert.Equal(t, "", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "", os.Getenv("DONT_ENV_TEST"))

err := LoadExists("./testdata", "not-exist", ".env")

assert.NoError(t, err)
assert.Equal(t, "blog", os.Getenv("DONT_ENV_TEST"))
assert.Equal(t, "blog", Get("DONT_ENV_TEST"))
assert.NoErr(t, err)
assert.Eq(t, "blog", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "blog", Get("DONT_ENV_TEST"))
}

func TestLoadExistFiles(t *testing.T) {
defer Reset()
assert.Equal(t, "", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "", os.Getenv("DONT_ENV_TEST"))

err := LoadExistFiles("./testdata/not-exist", "./testdata/.env")

assert.NoError(t, err)
assert.Equal(t, "blog", os.Getenv("DONT_ENV_TEST"))
assert.Equal(t, "blog", Get("DONT_ENV_TEST"))
assert.NoErr(t, err)
assert.Eq(t, "blog", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "blog", Get("DONT_ENV_TEST"))
}

func TestLoadFromMap(t *testing.T) {
assert.Equal(t, "", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "", os.Getenv("DONT_ENV_TEST"))

err := LoadFromMap(map[string]string{
"DONT_ENV_TEST": "blog",
Expand All @@ -75,42 +75,42 @@ func TestLoadFromMap(t *testing.T) {
"dont_env_bool": "true",
})

assert.NoError(t, err)
assert.NoErr(t, err)

// fmt.Println(os.Environ())
envStr := fmt.Sprint(os.Environ())
assert.Contains(t, envStr, "DONT_ENV_TEST=blog")
assert.Contains(t, envStr, "DONT_ENV_TEST1=val1")

assert.Equal(t, "blog", Get("DONT_ENV_TEST"))
assert.Equal(t, "blog", os.Getenv("DONT_ENV_TEST"))
assert.Equal(t, "val1", Get("DONT_ENV_TEST1"))
assert.Equal(t, 23, Int("DONT_ENV_TEST2"))
assert.Eq(t, "blog", Get("DONT_ENV_TEST"))
assert.Eq(t, "blog", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "val1", Get("DONT_ENV_TEST1"))
assert.Eq(t, 23, Int("DONT_ENV_TEST2"))
assert.True(t, Bool("dont_env_bool"))

assert.Equal(t, "val1", Get("dont_env_test1"))
assert.Equal(t, 23, Int("dont_env_test2"))
assert.Eq(t, "val1", Get("dont_env_test1"))
assert.Eq(t, 23, Int("dont_env_test2"))

assert.Equal(t, 20, Int("dont_env_test1", 20))
assert.Equal(t, 20, Int("dont_env_not_exist", 20))
assert.Eq(t, 20, Int("dont_env_test1", 20))
assert.Eq(t, 20, Int("dont_env_not_exist", 20))
assert.False(t, Bool("dont_env_not_exist", false))

// check cache
assert.Contains(t, LoadedData(), "DONT_ENV_TEST2")

// clear
ClearLoaded()
assert.Equal(t, "", os.Getenv("DONT_ENV_TEST"))
assert.Equal(t, "", Get("DONT_ENV_TEST1"))
assert.Eq(t, "", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "", Get("DONT_ENV_TEST1"))

err = LoadFromMap(map[string]string{
"": "val",
})
assert.Error(t, err)
assert.Err(t, err)
}

func TestDontUpperEnvKey(t *testing.T) {
assert.Equal(t, "", os.Getenv("DONT_ENV_TEST"))
assert.Eq(t, "", os.Getenv("DONT_ENV_TEST"))

DontUpperEnvKey()

Expand All @@ -119,14 +119,14 @@ func TestDontUpperEnvKey(t *testing.T) {
})

assert.Contains(t, fmt.Sprint(os.Environ()), "dont_env_test=val")
assert.NoError(t, err)
assert.Equal(t, "val", Get("dont_env_test"))
assert.NoErr(t, err)
assert.Eq(t, "val", Get("dont_env_test"))

// on windows, os.Getenv() not case sensitive
if runtime.GOOS == "windows" {
assert.Equal(t, "val", Get("DONT_ENV_TEST"))
assert.Eq(t, "val", Get("DONT_ENV_TEST"))
} else {
assert.Equal(t, "", Get("DONT_ENV_TEST"))
assert.Eq(t, "", Get("DONT_ENV_TEST"))
}

UpperEnvKey = true // revert
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ go 1.16
require (
github.com/gookit/goutil v0.5.12
github.com/mitchellh/mapstructure v1.5.0
github.com/stretchr/testify v1.8.0
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
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=
Expand Down
13 changes: 4 additions & 9 deletions ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package ini

import (
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
Expand Down Expand Up @@ -97,9 +96,10 @@ func NewWithOptions(opts ...func(*Options)) *Ini {
}

// Default config instance
func Default() *Ini {
return dc
}
func Default() *Ini { return dc }

// ResetStd instance
func ResetStd() { dc = New() }

func (c *Ini) ensureInit() {
if !c.IsEmpty() {
Expand Down Expand Up @@ -392,11 +392,6 @@ func (c *Ini) Error() error {
return c.err
}

// format and record error
func (c *Ini) addErrorf(format string, a ...interface{}) {
c.err = fmt.Errorf(format, a...)
}

/*************************************************************
* internal helper methods
*************************************************************/
Expand Down
Loading

0 comments on commit 7df8e94

Please sign in to comment.