/
util_test.go
107 lines (85 loc) · 2.46 KB
/
util_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"flag"
"os"
"testing"
"time"
"github.com/mdb/seaweed"
"github.com/urfave/cli"
)
func TestConcat(t *testing.T) {
joined := concat([]string{
"foo",
"bar",
})
if joined != "foobar" {
t.Error("concat should properly concatenate strings")
}
}
func TestCacheAgeWithEnvVar(t *testing.T) {
os.Setenv("MAGIC_SEAWEED_CACHE_AGE", "10m")
age, _ := time.ParseDuration("10m")
if cacheAge() != age {
t.Error("cacheAge should properly use the $MAGIC_SEAWEED_CACHE_AGE value")
}
}
func TestCacheAgeWithNoEnvVar(t *testing.T) {
os.Setenv("MAGIC_SEAWEED_CACHE_AGE", "")
age, _ := time.ParseDuration("5m")
if cacheAge() != age {
t.Error("cacheAge should properly default to 5m when no $MAGIC_SEAWEED_CACHE_AGE is set")
}
}
func TestCacheDirWithEnvVar(t *testing.T) {
os.Setenv("MAGIC_SEAWEED_CACHE_DIR", "/tmp")
if cacheDir() != "/tmp" {
t.Error("cacheDir should properly use the $MAGIC_SEAWEED_CACHE_DIR value")
}
}
func TestCacheDirWithNoEnvVar(t *testing.T) {
os.Setenv("MAGIC_SEAWEED_CACHE_DIR", "")
if cacheDir() != os.TempDir() {
t.Error("cacheDir should properly default to os.TempDir() when no $MAGIC_SEAWEED_CACHE_DIR is set")
}
}
func TestClientWithDefaults(t *testing.T) {
os.Setenv("MAGIC_SEAWEED_API_KEY", "123")
c := getTestClient()
cache, _ := time.ParseDuration("5m")
if c.APIKey != "123" {
t.Error("client.APIKey should be properly set")
}
if c.CacheAge != cache {
t.Error("client.CacheAge should default to 5m")
}
if c.CacheDir != os.TempDir() {
t.Error("client.CacheDir should default to os.TempDir()")
}
}
func TestClientWithEnvVars(t *testing.T) {
os.Setenv("MAGIC_SEAWEED_API_KEY", "123")
os.Setenv("MAGIC_SEAWEED_CACHE_DIR", "/tmp")
os.Setenv("MAGIC_SEAWEED_CACHE_AGE", "10m")
c := getTestClient()
cache, _ := time.ParseDuration("10m")
if c.APIKey != "123" {
t.Error("client.APIKey should be properly set")
}
if c.CacheAge != cache {
t.Error("client.CacheAge should be set to $MAGIC_SEAWEED_CACHE_AGE")
}
if c.CacheDir != "/tmp" {
t.Error("client.CacheDir should be set to $MAGIC_SEAWEED_CACHE_DIR")
}
}
func getTestClient() *seaweed.Client {
set := flag.NewFlagSet("test", 0)
set.Int("myflag", 12, "doc")
set.Float64("myflag64", float64(17), "doc")
globalSet := flag.NewFlagSet("test", 0)
globalSet.Int("myflag", 42, "doc")
globalSet.Float64("myflag64", float64(47), "doc")
globalCtx := cli.NewContext(nil, globalSet, nil)
context := cli.NewContext(nil, set, globalCtx)
return client(context)
}