Skip to content

Commit

Permalink
Resolve indir to absolute path at parsing time
Browse files Browse the repository at this point in the history
  • Loading branch information
cortesi committed Aug 16, 2019
1 parent 8c214cd commit 466d7c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions conf/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package conf
import (
"fmt"
"path"
"path/filepath"
"runtime"
"strings"
)
Expand Down Expand Up @@ -247,6 +248,10 @@ Loop:
dir = strings.Replace(
dir, confVarName, p.config.variables[confVarName], -1,
)
dir, err := filepath.Abs(dir)
if err != nil {
p.errorf("%s", err)
}
block.InDir = dir
case itemDaemon:
options := p.collectValues(itemBareString)
Expand Down
19 changes: 16 additions & 3 deletions conf/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package conf

import (
"path/filepath"
"syscall"
"testing"

"github.com/google/go-cmp/cmp"
)

func mustAbs(s string) string {
f, err := filepath.Abs(s)
if err != nil {
panic(err)
}
return f
}

var parseTests = []struct {
path string
input string
Expand Down Expand Up @@ -265,7 +274,7 @@ var parseTests = []struct {
"{ indir: foo\n }",
&Config{
Blocks: []Block{
{InDir: "foo"},
{InDir: mustAbs("foo")},
},
},
},
Expand All @@ -283,7 +292,7 @@ var parseTests = []struct {
"{ indir: @confdir/foo\n }",
&Config{
Blocks: []Block{
{InDir: "path/to/foo"},
{InDir: mustAbs("path/to/foo")},
},
variables: map[string]string{
"@confdir": "path/to",
Expand All @@ -292,14 +301,18 @@ var parseTests = []struct {
},
}

var parseCmpOptions = []cmp.Option{
cmp.AllowUnexported(Config{}),
}

func TestParse(t *testing.T) {
for i, tt := range parseTests {
ret, err := Parse(tt.path, tt.input)
if err != nil {
t.Fatalf("%q - %s", tt.input, err)
}

if diff := cmp.Diff(ret, tt.expected, cmp.AllowUnexported(Config{})); diff != "" {
if diff := cmp.Diff(ret, tt.expected, parseCmpOptions...); diff != "" {
t.Errorf("%d %s", i, diff)
}
}
Expand Down

0 comments on commit 466d7c8

Please sign in to comment.