Skip to content

Commit

Permalink
Merging develop to master for v0.13.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jun 6, 2016
2 parents a0647cd + 6821dd3 commit 75f5ee6
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 9 deletions.
74 changes: 74 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,77 @@ func TestMerge(t *testing.T) {
t.Errorf("Expected '[X] x.four' to be 'x4' but instead it was '%s'", result)
}
}

func TestLoadContextOneConf(t *testing.T) {
ctx, err := LoadContext("app.conf", []string{"testdata/conf-path1"})
if err != nil {
t.Errorf("Error: %v", err)
t.FailNow()
}

ctx.SetSection("X")
result, found := ctx.String("x.three")
if !strings.EqualFold("conf1-sourcex3", result) {
t.Errorf("Expected '[X] x.three' to be 'conf1-sourcex3' but instead it was '%s'", result)
}

_, found = ctx.String("x.notexists")
if found {
t.Error("Config 'x.notexists' shouldn't found")
}

ctx.SetSection("Y")
result, found = ctx.String("y.one")
if !strings.EqualFold("conf1-sourcey1", result) {
t.Errorf("Expected '[Y] y.one' to be 'conf1-sourcey1' but instead it was '%s'", result)
}

_, found = ctx.String("y.notexists")
if found {
t.Error("Config 'y.notexists' shouldn't found")
}
}

func TestLoadContextMultipleConfWithPriority(t *testing.T) {
ctx, err := LoadContext("app.conf", []string{"testdata/conf-path1", "testdata/conf-path2"})
if err != nil {
t.Errorf("Error: %v", err)
t.FailNow()
}

ctx.SetSection("X")
result, found := ctx.String("x.two")
if !strings.EqualFold("override-conf2-sourcex2", result) {
t.Errorf("Expected '[X] x.two' to be 'override-conf2-sourcex2' but instead it was '%s'", result)
}

_, found = ctx.String("x.notexists")
if found {
t.Error("Config 'x.notexists' shouldn't be found")
}

ctx.SetSection("Y")
result, found = ctx.String("y.three")
if !strings.EqualFold("override-conf2-sourcey3", result) {
t.Errorf("Expected '[Y] y.three' to be 'override-conf2-sourcey3' but instead it was '%s'", result)
}

_, found = ctx.String("y.notexists")
if found {
t.Error("Config 'y.notexists' shouldn't be found")
}
}

func TestLoadContextConfNotFound(t *testing.T) {
_, err := LoadContext("notfound.conf", []string{"testdata/conf-path1"})
if err != nil && !strings.EqualFold("open testdata/conf-path1/notfound.conf: no such file or directory", err.Error()) {
t.Errorf("This is not expected error: %v", err)
}
}

func TestLoadContextInvalidConf(t *testing.T) {
_, err := LoadContext("app-invalid.conf", []string{"testdata"})
if err != nil && !strings.EqualFold("testdata/app-invalid.conf: could not parse line #7: %(two)s + %(four)s", err.Error()) {
t.Errorf("This is not expected error: %v", err)
}
}
21 changes: 14 additions & 7 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package config

import (
"path"
"fmt"
"os"
"path/filepath"
"strings"
)

Expand All @@ -33,15 +35,20 @@ func NewContext() *Context {
}

func LoadContext(confName string, confPaths []string) (*Context, error) {
var err error
var conf *Config
ctx := NewContext()
for _, confPath := range confPaths {
conf, err = ReadDefault(path.Join(confPath, confName))
if err == nil {
return &Context{config: conf}, nil
path := filepath.Join(confPath, confName)
conf, err := ReadDefault(path)
if err != nil {
if _, isPathErr := err.(*os.PathError); !isPathErr {
return nil, fmt.Errorf("%v: %v", path, err)
}
continue
}
ctx.config.Merge(conf)
}
return nil, err

return ctx, nil
}

func (c *Context) Raw() *Config {
Expand Down
6 changes: 4 additions & 2 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package config

import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"unicode"
Expand Down Expand Up @@ -58,8 +58,10 @@ func ReadDefault(fname string) (*Config, error) {
func (c *Config) read(buf *bufio.Reader) (err error) {
var section, option string
var scanner = bufio.NewScanner(buf)
lineNo := 0
for scanner.Scan() {
l := strings.TrimRightFunc(stripComments(scanner.Text()), unicode.IsSpace)
lineNo++

// Switch written for readability (not performance)
switch {
Expand Down Expand Up @@ -92,7 +94,7 @@ func (c *Config) read(buf *bufio.Reader) (err error) {
c.AddOption(section, option, value)

default:
return errors.New("could not parse line: " + l)
return fmt.Errorf("could not parse line #%v: %v", lineNo, l)
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions testdata/app-invalid.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
one=source1
two=source2
three=source3
four=4

#invalid spot
%(two)s + %(four)s

[X]
x.one=conf1-sourcex1
x.two=conf1-sourcex2
x.three=conf1-sourcex3

[Y]
y.one=conf1-sourcey1
y.two=conf1-sourcey2
y.three=conf1-sourcey3
16 changes: 16 additions & 0 deletions testdata/conf-path1/app.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
one=source1
two=source2
three=source3
four=4

two_+_four=%(two)s + %(four)s

[X]
x.one=conf1-sourcex1
x.two=conf1-sourcex2
x.three=conf1-sourcex3

[Y]
y.one=conf1-sourcey1
y.two=conf1-sourcey2
y.three=conf1-sourcey3
18 changes: 18 additions & 0 deletions testdata/conf-path2/app.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
one=source1
two=source2
three=source3
four=4

two_+_four=%(two)s + %(four)s

[X]
x.one=conf2-sourcex1
x.two=override-conf2-sourcex2
x.three=conf2-sourcex3

x.four=exists here only

[Y]
y.one=conf2-sourcey1
y.two=conf2-sourcey2
y.three=override-conf2-sourcey3

0 comments on commit 75f5ee6

Please sign in to comment.