forked from gofor-little/env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
55 lines (42 loc) · 1.79 KB
/
env_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package env_test
import (
"os"
"testing"
env "github.com/2pisoftware/gofor-little-env"
"github.com/stretchr/testify/require"
)
func TestLoad(t *testing.T) {
require.NoError(t, env.Load("test-data/test-1.env", "test-data/test-2.env"))
require.True(t, env.Get("PUBLIC_KEY", "") == os.Getenv("PUBLIC_KEY"))
require.True(t, env.Get("PRIVATE_KEY", "") == os.Getenv("PRIVATE_KEY"))
require.True(t, env.Get("DB_NAME", "") == os.Getenv("DB_NAME"))
require.True(t, env.Get("DB_PASSWORD", "") == os.Getenv("DB_PASSWORD"))
require.True(t, env.Get("SPECIAL_CHARACTERS", "") == os.Getenv("SPECIAL_CHARACTERS"))
}
func TestWrite(t *testing.T) {
require.NoError(t, env.Write("PUBLIC_KEY_NEW", "\"public\\nkey\\nnew\"", "test-data/test-1.env", true))
require.NoError(t, env.Write("DB_PASSWORD", "db_password_new", "test-data/test-2.env", true))
}
func TestGet(t *testing.T) {
require.NoError(t, env.Load("test-data/test-1.env", "test-data/test-2.env"))
require.True(t, env.Get("DB_NAME", "db_name_default") == "db_name")
require.True(t, env.Get("SPECIAL_CHARACTERS", "") == "special=characters")
require.True(t, env.Get("FAKE_KEY", "db_name_default") == "db_name_default")
}
func TestMustGet(t *testing.T) {
require.NoError(t, env.Load("test-data/test-1.env", "test-data/test-2.env"))
value, err := env.MustGet("DB_NAME")
require.NoError(t, err)
require.True(t, value == "db_name")
value, err = env.MustGet("SPECIAL_CHARACTERS")
require.NoError(t, err)
require.True(t, value == "special=characters")
_, err = env.MustGet("FAKE_KEY")
require.True(t, err != nil)
}
func TestSet(t *testing.T) {
require.NoError(t, env.Load("test-data/test-1.env", "test-data/test-2.env"))
require.NoError(t, env.Set("DB_NAME", "db_name_override"))
value := os.Getenv("DB_NAME")
require.True(t, value == "db_name_override")
}