Skip to content

Commit 75f5ee6

Browse files
committed
Merging develop to master for v0.13.0 release
2 parents a0647cd + 6821dd3 commit 75f5ee6

File tree

6 files changed

+143
-9
lines changed

6 files changed

+143
-9
lines changed

Diff for: all_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,77 @@ func TestMerge(t *testing.T) {
398398
t.Errorf("Expected '[X] x.four' to be 'x4' but instead it was '%s'", result)
399399
}
400400
}
401+
402+
func TestLoadContextOneConf(t *testing.T) {
403+
ctx, err := LoadContext("app.conf", []string{"testdata/conf-path1"})
404+
if err != nil {
405+
t.Errorf("Error: %v", err)
406+
t.FailNow()
407+
}
408+
409+
ctx.SetSection("X")
410+
result, found := ctx.String("x.three")
411+
if !strings.EqualFold("conf1-sourcex3", result) {
412+
t.Errorf("Expected '[X] x.three' to be 'conf1-sourcex3' but instead it was '%s'", result)
413+
}
414+
415+
_, found = ctx.String("x.notexists")
416+
if found {
417+
t.Error("Config 'x.notexists' shouldn't found")
418+
}
419+
420+
ctx.SetSection("Y")
421+
result, found = ctx.String("y.one")
422+
if !strings.EqualFold("conf1-sourcey1", result) {
423+
t.Errorf("Expected '[Y] y.one' to be 'conf1-sourcey1' but instead it was '%s'", result)
424+
}
425+
426+
_, found = ctx.String("y.notexists")
427+
if found {
428+
t.Error("Config 'y.notexists' shouldn't found")
429+
}
430+
}
431+
432+
func TestLoadContextMultipleConfWithPriority(t *testing.T) {
433+
ctx, err := LoadContext("app.conf", []string{"testdata/conf-path1", "testdata/conf-path2"})
434+
if err != nil {
435+
t.Errorf("Error: %v", err)
436+
t.FailNow()
437+
}
438+
439+
ctx.SetSection("X")
440+
result, found := ctx.String("x.two")
441+
if !strings.EqualFold("override-conf2-sourcex2", result) {
442+
t.Errorf("Expected '[X] x.two' to be 'override-conf2-sourcex2' but instead it was '%s'", result)
443+
}
444+
445+
_, found = ctx.String("x.notexists")
446+
if found {
447+
t.Error("Config 'x.notexists' shouldn't be found")
448+
}
449+
450+
ctx.SetSection("Y")
451+
result, found = ctx.String("y.three")
452+
if !strings.EqualFold("override-conf2-sourcey3", result) {
453+
t.Errorf("Expected '[Y] y.three' to be 'override-conf2-sourcey3' but instead it was '%s'", result)
454+
}
455+
456+
_, found = ctx.String("y.notexists")
457+
if found {
458+
t.Error("Config 'y.notexists' shouldn't be found")
459+
}
460+
}
461+
462+
func TestLoadContextConfNotFound(t *testing.T) {
463+
_, err := LoadContext("notfound.conf", []string{"testdata/conf-path1"})
464+
if err != nil && !strings.EqualFold("open testdata/conf-path1/notfound.conf: no such file or directory", err.Error()) {
465+
t.Errorf("This is not expected error: %v", err)
466+
}
467+
}
468+
469+
func TestLoadContextInvalidConf(t *testing.T) {
470+
_, err := LoadContext("app-invalid.conf", []string{"testdata"})
471+
if err != nil && !strings.EqualFold("testdata/app-invalid.conf: could not parse line #7: %(two)s + %(four)s", err.Error()) {
472+
t.Errorf("This is not expected error: %v", err)
473+
}
474+
}

Diff for: context.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package config
1616

1717
import (
18-
"path"
18+
"fmt"
19+
"os"
20+
"path/filepath"
1921
"strings"
2022
)
2123

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

3537
func LoadContext(confName string, confPaths []string) (*Context, error) {
36-
var err error
37-
var conf *Config
38+
ctx := NewContext()
3839
for _, confPath := range confPaths {
39-
conf, err = ReadDefault(path.Join(confPath, confName))
40-
if err == nil {
41-
return &Context{config: conf}, nil
40+
path := filepath.Join(confPath, confName)
41+
conf, err := ReadDefault(path)
42+
if err != nil {
43+
if _, isPathErr := err.(*os.PathError); !isPathErr {
44+
return nil, fmt.Errorf("%v: %v", path, err)
45+
}
46+
continue
4247
}
48+
ctx.config.Merge(conf)
4349
}
44-
return nil, err
50+
51+
return ctx, nil
4552
}
4653

4754
func (c *Context) Raw() *Config {

Diff for: read.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package config
1616

1717
import (
1818
"bufio"
19-
"errors"
19+
"fmt"
2020
"os"
2121
"strings"
2222
"unicode"
@@ -58,8 +58,10 @@ func ReadDefault(fname string) (*Config, error) {
5858
func (c *Config) read(buf *bufio.Reader) (err error) {
5959
var section, option string
6060
var scanner = bufio.NewScanner(buf)
61+
lineNo := 0
6162
for scanner.Scan() {
6263
l := strings.TrimRightFunc(stripComments(scanner.Text()), unicode.IsSpace)
64+
lineNo++
6365

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

9496
default:
95-
return errors.New("could not parse line: " + l)
97+
return fmt.Errorf("could not parse line #%v: %v", lineNo, l)
9698
}
9799
}
98100
}

Diff for: testdata/app-invalid.conf

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
one=source1
2+
two=source2
3+
three=source3
4+
four=4
5+
6+
#invalid spot
7+
%(two)s + %(four)s
8+
9+
[X]
10+
x.one=conf1-sourcex1
11+
x.two=conf1-sourcex2
12+
x.three=conf1-sourcex3
13+
14+
[Y]
15+
y.one=conf1-sourcey1
16+
y.two=conf1-sourcey2
17+
y.three=conf1-sourcey3

Diff for: testdata/conf-path1/app.conf

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
one=source1
2+
two=source2
3+
three=source3
4+
four=4
5+
6+
two_+_four=%(two)s + %(four)s
7+
8+
[X]
9+
x.one=conf1-sourcex1
10+
x.two=conf1-sourcex2
11+
x.three=conf1-sourcex3
12+
13+
[Y]
14+
y.one=conf1-sourcey1
15+
y.two=conf1-sourcey2
16+
y.three=conf1-sourcey3

Diff for: testdata/conf-path2/app.conf

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
one=source1
2+
two=source2
3+
three=source3
4+
four=4
5+
6+
two_+_four=%(two)s + %(four)s
7+
8+
[X]
9+
x.one=conf2-sourcex1
10+
x.two=override-conf2-sourcex2
11+
x.three=conf2-sourcex3
12+
13+
x.four=exists here only
14+
15+
[Y]
16+
y.one=conf2-sourcey1
17+
y.two=conf2-sourcey2
18+
y.three=override-conf2-sourcey3

0 commit comments

Comments
 (0)