From 13195d209c523d3d45f3456086bce61223950dab Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 8 Apr 2021 17:57:55 +0200 Subject: Fixed wrong implementation of yaml default values and of the corresponding tests --- pkg/config/config.go | 10 ++++++++-- pkg/config/config_test.go | 13 +++++++++++-- pkg/config/test_data/complete.yaml | 4 ++++ pkg/config/test_data/minimal.yaml | 2 -- 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 pkg/config/test_data/complete.yaml (limited to 'pkg') diff --git a/pkg/config/config.go b/pkg/config/config.go index 6e4bba5..795ccd0 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -13,9 +13,9 @@ var validTrainStop = regexp.MustCompile(`^[a-zA-Z0-9:_]+$`) type Config struct { // Address is the hostname or ip the web server will listen to - Address string `yaml:"address",default:"127.0.0.1"` + Address string `yaml:"address"` // Port is the tcp port number or service name the web server will listen to - Port string `yaml:"port",default:"8080"` + Port string `yaml:"port"` // Token is the sncf api token Token string `yaml:"token"` // TrainStop is the navitia code of the train stop the webapp will monitor @@ -24,12 +24,18 @@ type Config struct { func (c *Config) validate() error { // address + if c.Address == "" { + c.Address = "127.0.0.1" + } if ip := net.ParseIP(c.Address); ip == nil { if _, err := net.LookupIP(c.Address); err != nil { return newInvalidAddressError(c.Address, err) } } // port + if c.Port == "" { + c.Port = "8080" + } if _, err := net.LookupPort("tcp", c.Port); err != nil { return newInvalidPortError(c.Port, err) } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index a4c170b..af18fc1 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -11,8 +11,8 @@ import ( func TestLoadFile(t *testing.T) { // Minimal yaml file minimalConfig := Config{ - Address: "127.0.0.2", - Port: "8082", + Address: "127.0.0.1", + Port: "8080", Token: "12345678-9abc-def0-1234-56789abcdef0", TrainStop: "ABCD:test:01", } @@ -25,6 +25,14 @@ func TestLoadFile(t *testing.T) { TrainStop: "VWXY_Z:test:90", } + // Complete yaml file + completeConfig := Config{ + Address: "127.0.0.2", + Port: "8082", + Token: "12345678-9abc-def0-1234-56789abcdef0", + TrainStop: "ABCD:test:01", + } + // Test cases testCases := []struct { name string @@ -41,6 +49,7 @@ func TestLoadFile(t *testing.T) { {"Invalid trainStop should fail to load", "test_data/invalid_trainStop.yaml", nil, &InvalidTrainStopError{}}, {"Minimal config", "test_data/minimal.yaml", &minimalConfig, nil}, {"Minimal config with resolving", "test_data/minimal_with_hostname.yaml", &minimalConfigWithResolving, nil}, + {"Complete config", "test_data/complete.yaml", &completeConfig, nil}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/config/test_data/complete.yaml b/pkg/config/test_data/complete.yaml new file mode 100644 index 0000000..e34db15 --- /dev/null +++ b/pkg/config/test_data/complete.yaml @@ -0,0 +1,4 @@ +address: 127.0.0.2 +port: 8082 +token: 12345678-9abc-def0-1234-56789abcdef0 +trainStop: "ABCD:test:01" diff --git a/pkg/config/test_data/minimal.yaml b/pkg/config/test_data/minimal.yaml index e34db15..5092997 100644 --- a/pkg/config/test_data/minimal.yaml +++ b/pkg/config/test_data/minimal.yaml @@ -1,4 +1,2 @@ -address: 127.0.0.2 -port: 8082 token: 12345678-9abc-def0-1234-56789abcdef0 trainStop: "ABCD:test:01" -- cgit v1.2.3