Listen for events and forward to external security scanning services.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

viper_test.go 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package main
  2. import (
  3. "github.com/nate-johnston/viper"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. )
  7. func TestInitViperWillReturnErrorOnReadError(t *testing.T) {
  8. err := InitViper("fixtures/viper/not_there.yml", nil)
  9. assert.NotNil(t, err)
  10. assert.Equal(t, "open fixtures/viper/not_there.yml: no such file or directory", err.Error())
  11. }
  12. func TestInitViperEnsureItCallsValidateConfigWithConfigErrors(t *testing.T) {
  13. // By calling InitViper and expecting an error case we prove that IniViper is
  14. // calling ValidateConfig() and its working as expected.
  15. viperConfigs := []ViperConfig{
  16. ViperConfig{Key: "missing_required_string", Description: "Required String"},
  17. }
  18. err := InitViper("fixtures/viper/test.yml", viperConfigs)
  19. assert.NotNil(t, err)
  20. assert.Equal(t, "Required Configuration Missing: [Key: missing_required_string, Description: Required String]",
  21. err.Error())
  22. }
  23. func TestValidateConfig(t *testing.T) {
  24. viperConfigs := []ViperConfig{
  25. ViperConfig{Key: "required_string", Description: "Required String"},
  26. ViperConfig{Key: "nested.one", Description: "Required Nested One"},
  27. ViperConfig{Key: "test_default", Default: "Optional Value", Description: "Optional Value"},
  28. ViperConfig{Key: "test_alias", Alias: []string{"bubba", "forest"}, Description: "Optional Value"},
  29. }
  30. InitViper("fixtures/viper/test.yml", viperConfigs)
  31. err := ValidateConfig(viperConfigs)
  32. assert.Nil(t, err)
  33. assert.Equal(t, "Optional Value", viper.GetString("test_default"))
  34. assert.Equal(t, "test_alias value", viper.GetString("test_alias"))
  35. assert.Equal(t, "test_alias value", viper.GetString("bubba"))
  36. assert.Equal(t, "test_alias value", viper.GetString("forest"))
  37. }