generated from ZEISS/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c250921
commit 8fd5e5d
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package envx | ||
|
||
// Env is the environment variables. | ||
type Env map[string]string | ||
|
||
// Copy returns a copy of the environment. | ||
func (e Env) Copy() Env { | ||
out := Env{} | ||
for k, v := range e { | ||
out[k] = v | ||
} | ||
|
||
return out | ||
} | ||
|
||
// Strings returns the current environment as a list of strings, suitable for | ||
// os executions. | ||
func (e Env) Strings() []string { | ||
result := make([]string, 0, len(e)) | ||
for k, v := range e { | ||
result = append(result, k+"="+v) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package envx_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/zeiss/pkg/envx" | ||
) | ||
|
||
func TestEnv(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Create a new environment. | ||
e := envx.Env{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
} | ||
|
||
e2 := e.Copy() | ||
assert.Equal(t, e, e2) | ||
|
||
strings := e.Strings() | ||
assert.Len(t, strings, 2) | ||
assert.Contains(t, strings, "key1=value1") | ||
assert.Contains(t, strings, "key2=value2") | ||
} |