Skip to content

Commit

Permalink
cmd: test we can override a non-0 value with 0
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentbernat committed Nov 28, 2023
1 parent 0e37c8c commit ee0075c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd_test
import (
"bytes"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -497,3 +498,64 @@ func TestDevNullDefault(t *testing.T) {
t.Errorf("Parse() (-got, +want):\n%s", diff)
}
}

func TestZeroCanOverrideDefault(t *testing.T) {
config := `---
module1:
topic: flows
workers: 10
module2:
details:
workers: 0
stuff: ""
`
configFile := filepath.Join(t.TempDir(), "config.yaml")
os.WriteFile(configFile, []byte(config), 0o644)

c := cmd.ConfigRelatedOptions{
Path: configFile,
Dump: true,
}

parsed := dummyConfiguration{}
expected := dummyConfiguration{
Module1: dummyModule1Configuration{
Listen: "127.0.0.1:8080",
Topic: "flows",
Workers: 10,
},
Module2: dummyModule2Configuration{
Details: dummyModule2DetailsConfiguration{
Workers: 0,
IntervalValue: time.Minute,
},
MoreDetails: MoreDetails{
Stuff: "",
},
Elements: []dummyModule2ElementsConfiguration{
{
Name: "el1",
Gauge: 10,
}, {
Name: "el2",
Gauge: 11,
},
},
},
}
out := bytes.NewBuffer([]byte{})
if err := c.Parse(out, "dummy", &parsed); err != nil {
t.Fatalf("Parse() error:\n%+v", err)
}
if diff := helpers.Diff(parsed, expected); diff != "" {
t.Fatalf("Parse() (-got, +want):\n%s", diff)
}

log.Printf("output:\n%s", out.Bytes())
if !bytes.Contains(out.Bytes(), []byte(`stuff: ""`)) {
t.Errorf("Dumped configuration should contain `stuff: \"\"`")
}
if !bytes.Contains(out.Bytes(), []byte(`workers: 0`)) {
t.Errorf("Dumped configuration should contain `workers: 0`")
}
}

0 comments on commit ee0075c

Please sign in to comment.