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
|
package nixfig
import (
"os"
"testing"
"github.com/alecthomas/assert/v2"
)
type Config struct {
User string
Log struct {
Level string
File string
}
HTTP struct {
Host string
Port int
Admins []string
}
}
var testCfg = Config{
User: "jolheiser",
Log: struct {
Level string
File string
}{
Level: "warn",
File: "jolheiser.log",
},
HTTP: struct {
Host string
Port int
Admins []string
}{
Host: "0.0.0.0",
Port: 1234,
Admins: []string{"jolheiser", "admin"},
},
}
func TestNixNotFound(t *testing.T) {
oldNix := Nix
Nix = ""
_, err := Marshal(nil)
assert.IsError(t, err, ErrNixNotFound)
Nix = oldNix
}
func TestUnmarshal(t *testing.T) {
data, err := os.ReadFile("testdata/config.nix")
assert.NoError(t, err)
var cfg Config
err = Unmarshal(data, &cfg)
assert.NoError(t, err)
assert.Equal(t, testCfg, cfg)
}
func TestMarshal(t *testing.T) {
data, err := Marshal(testCfg)
assert.NoError(t, err)
var cfg Config
err = Unmarshal(data, &cfg)
assert.NoError(t, err)
assert.Equal(t, testCfg, cfg)
}
|