Procházet zdrojové kódy

Add test for reading config

tags/v2.1.0^2
9seconds před 4 roky
rodič
revize
ed91290e47

+ 55
- 0
internal/utils/read_config_test.go Zobrazit soubor

@@ -0,0 +1,55 @@
1
+package utils_test
2
+
3
+import (
4
+	"path/filepath"
5
+	"testing"
6
+
7
+	"github.com/9seconds/mtg/v2/internal/utils"
8
+	"github.com/stretchr/testify/suite"
9
+)
10
+
11
+type ReadConfigTestSuite struct {
12
+	suite.Suite
13
+}
14
+
15
+func (suite *ReadConfigTestSuite) GetConfigPath(filename string) string {
16
+	return filepath.Join("testdata", filename)
17
+}
18
+
19
+func (suite *ReadConfigTestSuite) TestReadMinimal() {
20
+	conf, err := utils.ReadConfig(suite.GetConfigPath("minimal.toml"))
21
+	suite.NoError(err)
22
+	suite.NoError(conf.Validate())
23
+	suite.Equal("0.0.0.0:80", conf.BindTo.Get(""))
24
+	suite.Equal("7mqFMMq3P2Tvvt_rPx5qhmFnb29nbGUuY29t", conf.Secret.Base64())
25
+}
26
+
27
+func (suite *ReadConfigTestSuite) TestReadAbsentFile() {
28
+	_, err := utils.ReadConfig(suite.GetConfigPath("unknown.file"))
29
+	suite.Error(err)
30
+}
31
+
32
+func (suite *ReadConfigTestSuite) TestBrokenFile() {
33
+	_, err := utils.ReadConfig(suite.GetConfigPath("broken.toml"))
34
+	suite.Error(err)
35
+}
36
+
37
+func (suite *ReadConfigTestSuite) TestMissedBindTo() {
38
+	_, err := utils.ReadConfig(suite.GetConfigPath("missed-bindto.toml"))
39
+	suite.Error(err)
40
+}
41
+
42
+func (suite *ReadConfigTestSuite) TestMissedSecret() {
43
+	_, err := utils.ReadConfig(suite.GetConfigPath("missed-secret.toml"))
44
+	suite.Error(err)
45
+}
46
+
47
+func (suite *ReadConfigTestSuite) TestEmpty() {
48
+	_, err := utils.ReadConfig(suite.GetConfigPath("empty.toml"))
49
+	suite.Error(err)
50
+}
51
+
52
+func TestReadConfig(t *testing.T) {
53
+	t.Parallel()
54
+	suite.Run(t, &ReadConfigTestSuite{})
55
+}

+ 1
- 0
internal/utils/testdata/broken.toml Zobrazit soubor

@@ -0,0 +1 @@
1
+11

+ 0
- 0
internal/utils/testdata/empty.toml Zobrazit soubor


internal/cli/testdata/minimal.toml → internal/utils/testdata/minimal.toml Zobrazit soubor


+ 2
- 0
internal/utils/testdata/missed-bindto.toml Zobrazit soubor

@@ -0,0 +1,2 @@
1
+
2
+secret = "7mqFMMq3P2Tvvt_rPx5qhmFnb29nbGUuY29t"

+ 1
- 0
internal/utils/testdata/missed-secret.toml Zobrazit soubor

@@ -0,0 +1 @@
1
+bind-to = "0.0.0.0:80"

Načítá se…
Zrušit
Uložit