This repository was archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathconfigurer_test.go
95 lines (82 loc) · 2.53 KB
/
configurer_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
package config
import (
"errors"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type infiniteNewLineReader struct{}
// Read fills the buffer with newlines.
func (r infiniteNewLineReader) Read(b []byte) (int, error) {
for i := range b {
b[i] = '\n'
}
return len(b), nil
}
// TestObtain test the user data acquisition phase of the cli.
func TestObtain(t *testing.T) {
cli := NewConfigurer().(*cliConfigurer)
exfg, _ := cli.Obtain(infiniteNewLineReader{})
assert.NotNil(t, exfg)
}
type faultyReader struct{}
// Read always fails.
func (r faultyReader) Read(b []byte) (int, error) {
return 0, errors.New("error")
}
// TestObtainWithErrorReading test error handling.
func TestObtainWithErrorReading(t *testing.T) {
cli := NewConfigurer().(*cliConfigurer)
_, err := cli.Obtain(faultyReader{})
assert.Error(t, err)
}
// TestWrite test the configuration write phase of the cli.
func TestWrite(t *testing.T) {
tf, err := ioutil.TempFile("", "test_fritzctl.yml.")
assert.NoError(t, err)
defer tf.Close()
defer os.Remove(tf.Name())
extendedCfg := ExtendedConfig{}
extendedCfg.file = tf.Name()
err = extendedCfg.Write()
assert.NoError(t, err)
}
// TestWriteAndRead test the configuration write with subsequent re-read.
func TestWriteAndRead(t *testing.T) {
tf, err := ioutil.TempFile("", "test_fritzctl.yml.")
assert.NoError(t, err)
defer tf.Close()
defer os.Remove(tf.Name())
extendedCfg := ExtendedConfig{fritzCfg: Config{Net: new(Net), Login: new(Login), Pki: new(Pki)}}
extendedCfg.file = tf.Name()
err = extendedCfg.Write()
assert.NoError(t, err)
re, err := New(tf.Name())
assert.NoError(t, err)
assert.NotNil(t, re)
assert.Equal(t, *extendedCfg.fritzCfg.Net, *re.Net)
assert.Equal(t, *extendedCfg.fritzCfg.Login, *re.Login)
assert.Equal(t, *extendedCfg.fritzCfg.Pki, *re.Pki)
}
// TestWriteWithIOError test the write phase of the cli with error.
func TestWriteWithIOError(t *testing.T) {
extendedCfg := ExtendedConfig{file: ""}
err := extendedCfg.Write()
assert.Error(t, err)
}
// TestGreet tests the greeting.
func TestGreet(t *testing.T) {
cli := NewConfigurer().(*cliConfigurer)
assert.NotPanics(t, func() {
cli.Greet()
})
}
// TestDefaultConfigFileLoc asserts the default config file behavior.
func TestDefaultConfigFileLoc(t *testing.T) {
c := cliConfigurer{}
assert.NotEmpty(t,
c.defaultConfigLocation(func(file string) (string, error) { return "/path/to/folder/" + file, nil }))
assert.NotEmpty(t,
c.defaultConfigLocation(func(file string) (string, error) { return "", errors.New("didn't work") }))
}