-
Notifications
You must be signed in to change notification settings - Fork 1
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
607a9e0
commit 4f9dea7
Showing
18 changed files
with
3,782 additions
and
3 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
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
// - []time.Time | ||
// - time.Duration | ||
// - bool | ||
// - url.URL | ||
package getenv | ||
|
||
import ( | ||
|
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ package getenv_test | |
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"time" | ||
|
||
|
@@ -65,10 +66,19 @@ func ExampleEnvOrDefault() { | |
val = getenv.EnvOrDefault(key, time.Second) | ||
fmt.Printf("[%T]: %v\n", val, val) | ||
|
||
// url.URL | ||
if err := os.Setenv(key, "https://test:[email protected]:8000/tutorials/intro?type=advance&compact=false#history"); err != nil { | ||
panic(err) | ||
} | ||
|
||
val = getenv.EnvOrDefault(key, url.URL{}) | ||
fmt.Printf("[%T]: %v\n", val, val) | ||
|
||
// Output: | ||
// [string]: golly | ||
// [int]: 123 | ||
// [time.Time]: 2022-01-20 00:00:00 +0000 UTC | ||
// [[]float64]: [26.89 0.67] | ||
// [time.Duration]: 2h35m0s | ||
// [url.URL]: {https test:abcd123 golangbyexample.com:8000 /tutorials/intro false false type=advance&compact=false history } | ||
} |
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package getenv_test | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/obalunenko/getenv" | ||
"github.com/obalunenko/getenv/option" | ||
|
@@ -2454,3 +2456,88 @@ func TestInt16SliceOrDefault(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func getURL(tb testing.TB, rawURL string) url.URL { | ||
val, err := url.Parse(rawURL) | ||
require.NoError(tb, err) | ||
|
||
return *val | ||
} | ||
|
||
func TestURLOrDefault(t *testing.T) { | ||
const rawDefault = "https://test:[email protected]:8000/tutorials/intro?type=advance&compact=false#history" | ||
|
||
type args struct { | ||
key string | ||
defaultVal url.URL | ||
} | ||
|
||
type expected struct { | ||
val url.URL | ||
} | ||
|
||
var tests = []struct { | ||
name string | ||
precond precondition | ||
args args | ||
expected expected | ||
}{ | ||
{ | ||
name: "env not set - default returned", | ||
precond: precondition{ | ||
setenv: setenv{ | ||
isSet: false, | ||
val: "postgres://user:[email protected]:5432/path?k=v#f", | ||
}, | ||
}, | ||
args: args{ | ||
key: testEnvKey, | ||
defaultVal: getURL(t, rawDefault), | ||
}, | ||
expected: expected{ | ||
val: getURL(t, rawDefault), | ||
}, | ||
}, | ||
{ | ||
name: "env set - env value returned", | ||
precond: precondition{ | ||
setenv: setenv{ | ||
isSet: true, | ||
val: "postgres://user:[email protected]:5432/path?k=v#f", | ||
}, | ||
}, | ||
args: args{ | ||
key: testEnvKey, | ||
defaultVal: getURL(t, rawDefault), | ||
}, | ||
expected: expected{ | ||
val: getURL(t, "postgres://user:[email protected]:5432/path?k=v#f"), | ||
}, | ||
}, | ||
{ | ||
name: "empty env value set - default returned", | ||
precond: precondition{ | ||
setenv: setenv{ | ||
isSet: true, | ||
val: "", | ||
}, | ||
}, | ||
args: args{ | ||
key: testEnvKey, | ||
defaultVal: getURL(t, rawDefault), | ||
}, | ||
expected: expected{ | ||
val: getURL(t, rawDefault), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.precond.maybeSetEnv(t, tt.args.key) | ||
|
||
got := getenv.EnvOrDefault(tt.args.key, tt.args.defaultVal) | ||
assert.Equal(t, tt.expected.val, got) | ||
}) | ||
} | ||
} |
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
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
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
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
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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package internal | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -1652,6 +1653,84 @@ func Test_timeOrDefault(t *testing.T) { | |
} | ||
} | ||
|
||
func TestURLOrDefault(t *testing.T) { | ||
const rawDefault = "https://test:[email protected]:8000/tutorials/intro?type=advance&compact=false#history" | ||
|
||
type args struct { | ||
key string | ||
defaultVal url.URL | ||
} | ||
|
||
type expected struct { | ||
val url.URL | ||
} | ||
|
||
var tests = []struct { | ||
name string | ||
precond precondition | ||
args args | ||
expected expected | ||
}{ | ||
{ | ||
name: "env not set - default returned", | ||
precond: precondition{ | ||
setenv: setenv{ | ||
isSet: false, | ||
val: "postgres://user:[email protected]:5432/path?k=v#f", | ||
}, | ||
}, | ||
args: args{ | ||
key: testEnvKey, | ||
defaultVal: getURL(t, rawDefault), | ||
}, | ||
expected: expected{ | ||
val: getURL(t, rawDefault), | ||
}, | ||
}, | ||
{ | ||
name: "env set - env value returned", | ||
precond: precondition{ | ||
setenv: setenv{ | ||
isSet: true, | ||
val: "postgres://user:[email protected]:5432/path?k=v#f", | ||
}, | ||
}, | ||
args: args{ | ||
key: testEnvKey, | ||
defaultVal: getURL(t, rawDefault), | ||
}, | ||
expected: expected{ | ||
val: getURL(t, "postgres://user:[email protected]:5432/path?k=v#f"), | ||
}, | ||
}, | ||
{ | ||
name: "empty env value set - default returned", | ||
precond: precondition{ | ||
setenv: setenv{ | ||
isSet: true, | ||
val: "", | ||
}, | ||
}, | ||
args: args{ | ||
key: testEnvKey, | ||
defaultVal: getURL(t, rawDefault), | ||
}, | ||
expected: expected{ | ||
val: getURL(t, rawDefault), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.precond.maybeSetEnv(t, tt.args.key) | ||
|
||
got := urlOrDefault(tt.args.key, tt.args.defaultVal) | ||
assert.Equal(t, tt.expected.val, got) | ||
}) | ||
} | ||
} | ||
|
||
func Test_timeSliceOrDefault(t *testing.T) { | ||
const layout = "2006/02/01 15:04" | ||
|
||
|
Oops, something went wrong.