diff --git a/context/context_test.go b/context/context_test.go index b2e42fe8..16ecbb2b 100644 --- a/context/context_test.go +++ b/context/context_test.go @@ -84,6 +84,6 @@ func (s *AptlyContextSuite) TestGetPublishedStorageBadFS(c *C) { // storage never exists. c.Assert(func() { s.context.GetPublishedStorage("filesystem:fuji") }, FatalErrorPanicMatches, - &FatalError{ReturnCode: 1, Message: fmt.Sprintf("error loading config file %s/.aptly.conf: not valid yaml or json", + &FatalError{ReturnCode: 1, Message: fmt.Sprintf("error loading config file %s/.aptly.conf: invalid yaml (EOF) or json (EOF)", os.Getenv("HOME"))}) } diff --git a/system/t02_config/BadConfigTest_gold b/system/t02_config/BadConfigTest_gold index 0b0ca447..f6864d06 100644 --- a/system/t02_config/BadConfigTest_gold +++ b/system/t02_config/BadConfigTest_gold @@ -1 +1 @@ -ERROR: error loading config file ${HOME}/.aptly.conf: not valid yaml or json +ERROR: error loading config file ${HOME}/.aptly.conf: invalid yaml (yaml: line 1: did not find expected ',' or '}') or json (invalid character 's' looking for beginning of object key string) diff --git a/utils/config.go b/utils/config.go index fa542ac4..c0cc12d6 100644 --- a/utils/config.go +++ b/utils/config.go @@ -2,7 +2,6 @@ package utils import ( "encoding/json" - "errors" "fmt" "os" "path/filepath" @@ -258,9 +257,11 @@ func LoadConfig(filename string, config *ConfigStructure) error { if err = decJSON.Decode(&config); err != nil { f.Seek(0, 0) decYAML := yaml.NewDecoder(f) - if err = decYAML.Decode(&config); err != nil { - err = errors.New("not valid yaml or json") - } + if err2 := decYAML.Decode(&config); err2 != nil { + err = fmt.Errorf("invalid yaml (%s) or json (%s)", err2, err) + } else { + err = nil + } } return err } diff --git a/utils/config_test.go b/utils/config_test.go index 087ad258..3317f05d 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -175,9 +175,22 @@ func (s *ConfigSuite) TestLoadYAMLConfig(c *C) { c.Check(s.config.DatabaseOpenAttempts, Equals, 10) } -func (s *ConfigSuite) TestSaveYAMLConfig(c *C) { +func (s *ConfigSuite) TestLoadYAMLErrorConfig(c *C) { configname := filepath.Join(c.MkDir(), "aptly.yaml2") f, _ := os.Create(configname) + f.WriteString(configFileYAMLError) + f.Close() + + // start with empty config + s.config = ConfigStructure{} + + err := LoadConfig(configname, &s.config) + c.Assert(err.Error(), Equals, "invalid yaml (unknown pool storage type: invalid) or json (invalid character 'p' looking for beginning of value)") +} + +func (s *ConfigSuite) TestSaveYAMLConfig(c *C) { + configname := filepath.Join(c.MkDir(), "aptly.yaml3") + f, _ := os.Create(configname) f.WriteString(configFileYAML) f.Close() @@ -207,7 +220,7 @@ func (s *ConfigSuite) TestSaveYAML2Config(c *C) { s.config.PackagePoolStorage.Local = &LocalPoolStorage{"/tmp/aptly-pool"} s.config.PackagePoolStorage.Azure = nil - configname := filepath.Join(c.MkDir(), "aptly.yaml3") + configname := filepath.Join(c.MkDir(), "aptly.yaml4") err := SaveConfigYAML(configname, &s.config) c.Assert(err, IsNil) @@ -260,7 +273,7 @@ func (s *ConfigSuite) TestSaveYAML2Config(c *C) { } func (s *ConfigSuite) TestLoadEmptyConfig(c *C) { - configname := filepath.Join(c.MkDir(), "aptly.yaml4") + configname := filepath.Join(c.MkDir(), "aptly.yaml5") f, _ := os.Create(configname) f.Close() @@ -268,7 +281,7 @@ func (s *ConfigSuite) TestLoadEmptyConfig(c *C) { s.config = ConfigStructure{} err := LoadConfig(configname, &s.config) - c.Assert(err.Error(), Equals, "not valid yaml or json") + c.Assert(err.Error(), Equals, "invalid yaml (EOF) or json (EOF)") } const configFile = `{"rootDir": "/opt/aptly/", "downloadConcurrency": 33, "databaseOpenAttempts": 33}` @@ -355,3 +368,6 @@ packagepool_storage: account_key: a key endpoint: "" ` +const configFileYAMLError = `packagepool_storage: + type: invalid +`