From 6523536758f77e690dbab10f9f572d50f80e0690 Mon Sep 17 00:00:00 2001 From: Tobias Schmidt Date: Fri, 25 Jan 2013 00:21:29 +0100 Subject: [PATCH] Fix bug in config.LoadFromFile if file is not present --- config/load.go | 3 ++- config/load_test.go | 25 +++++++++++++++++++++++++ main.go | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 config/load_test.go diff --git a/config/load.go b/config/load.go index d74e4d6b1..c1bb7e3a9 100644 --- a/config/load.go +++ b/config/load.go @@ -56,9 +56,10 @@ func LoadFromString(configString string) (*Config, error) { func LoadFromFile(fileName string) (*Config, error) { configReader, err := os.Open(fileName) - defer configReader.Close() if err != nil { return &Config{}, err } + defer configReader.Close() + return LoadFromReader(configReader) } diff --git a/config/load_test.go b/config/load_test.go new file mode 100644 index 000000000..61f93edca --- /dev/null +++ b/config/load_test.go @@ -0,0 +1,25 @@ +// Copyright 2013 Prometheus Team +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package config + +import ( + "testing" +) + +func TestLoadFromFile(t *testing.T) { + _, err := LoadFromFile("file-does-not-exist.conf") + if err == nil { + t.Error(err) + } +} diff --git a/main.go b/main.go index 94f3c0cd6..dbfce3b01 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,7 @@ func main() { flag.Parse() conf, err := config.LoadFromFile(*configFile) if err != nil { - log.Fatalf("Error loading configuration from %s: %v", configFile, err) + log.Fatalf("Error loading configuration from %s: %v", *configFile, err) } persistence, err := leveldb.NewLevelDBMetricPersistence(*metricsStoragePath)