-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_test.go
56 lines (44 loc) · 1.11 KB
/
load_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
56
// Copyright (c) 2023, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package env
import (
"os"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testEnviron() Map {
restore := Environ()
os.Clearenv()
return restore
}
func restoreEnviron(restore Map) {
os.Clearenv()
if err := Load(restore); err != nil {
panic(err)
}
}
func TestLoad(t *testing.T) {
restore := testEnviron()
defer restoreEnviron(restore)
want := Value("this value is not overwritten")
key := randKey()
require.NoError(t, Setenv(key, want))
assert.Nil(t, Load(Map{key: "foobar"}))
assert.Equal(t, want, Getenv(key))
}
func TestMap_Overload(t *testing.T) {
restore := testEnviron()
defer restoreEnviron(restore)
want := Value("foobar")
key := randKey()
require.NoError(t, Setenv(key, "overwrite me!"))
assert.Nil(t, Overload(Map{key: want}))
assert.Equal(t, want, Getenv(key))
}
func randKey() string {
return "somewhat_random_key_" + strconv.FormatInt(time.Now().Unix(), 10)
}