diff options
| author | Shulhan <ms@kilabit.info> | 2023-11-17 03:18:52 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-11-17 03:18:52 +0700 |
| commit | 19f5053c19bdd2f9f4f764eb17ef0f096eb87f3e (patch) | |
| tree | 2c6553269f2c3d562fb9f0f7950268c90dd6a198 /awwan_env_test.go | |
| parent | 68cbc52f2195063a0b4c9ce8c6c4cf030182c53a (diff) | |
| download | awwan-19f5053c19bdd2f9f4f764eb17ef0f096eb87f3e.tar.xz | |
all: implement command "env-get" to get value from environment files
The env-get command get the value from environment files.
Syntax,
----
<key> [dir]
----
The "key" argument define the key where value is stored in environment
using "section:sub:name" format.
The "dir" argument is optional, its define the directory where environment
files will be loaded, recursively, from BaseDir to dir.
If its empty default to the current directory.
Diffstat (limited to 'awwan_env_test.go')
| -rw-r--r-- | awwan_env_test.go | 73 |
1 files changed, 72 insertions, 1 deletions
diff --git a/awwan_env_test.go b/awwan_env_test.go index 963658b..3fc6f67 100644 --- a/awwan_env_test.go +++ b/awwan_env_test.go @@ -11,6 +11,69 @@ import ( "github.com/shuLhan/share/lib/test" ) +func TestAwwanEnvGet(t *testing.T) { + var ( + baseDir = `testdata/env-get` + + aww *Awwan + err error + ) + + aww, err = New(baseDir) + if err != nil { + t.Fatal(err) + } + + type testCase struct { + desc string + dir string + key string + exp string + } + var cases = []testCase{{ + desc: `withEmptyKey`, + dir: baseDir, + exp: `EnvGet: empty key`, + }, { + desc: `withMissingSection`, + dir: baseDir, + key: `::name`, + }, { + desc: `withMissingName`, + dir: baseDir, + key: `host`, + }, { + desc: `withValidName`, + dir: baseDir, + key: `host::name`, + exp: `localhost`, + }, { + desc: `fromVault`, + dir: baseDir, + key: `user:database:pass`, + exp: `s3cret`, + }, { + desc: `fromSubdirMyhost`, + dir: filepath.Join(baseDir, `myhost`), + key: `host::name`, + exp: `myhost`, + }} + + var ( + c testCase + got string + ) + for _, c = range cases { + got, err = aww.EnvGet(c.dir, c.key) + if err != nil { + test.Assert(t, c.desc+` error`, c.exp, err.Error()) + continue + } + + test.Assert(t, c.desc, c.exp, got) + } +} + func TestAwwanEnvSet(t *testing.T) { var ( baseDir = t.TempDir() @@ -19,7 +82,7 @@ func TestAwwanEnvSet(t *testing.T) { err error ) - testInitWorkspace(baseDir) + testInitWorkspace(baseDir, nil, nil) tdata, err = test.LoadData(`testdata/env_set_test.data`) if err != nil { @@ -84,6 +147,7 @@ func TestAwwanEnvSet(t *testing.T) { var ( c testCase gotContent []byte + gotValue string ) for _, c = range cases { @@ -99,5 +163,12 @@ func TestAwwanEnvSet(t *testing.T) { } test.Assert(t, c.desc, c.exp, string(gotContent)) + + gotValue, err = aww.EnvGet(baseDir, c.key) + if err != nil { + t.Fatal(err) + } + + test.Assert(t, c.desc+` EnvGet`, c.val, gotValue) } } |
