Skip to content

Commit

Permalink
import should get absolute path before glob (caddyserver#929)
Browse files Browse the repository at this point in the history
* import should get absolute path before glob

* fix test: import should get absolute path before glob

* try to fix test on windows

* use complete path as the dispenser filename

* fix caddyfile test
  • Loading branch information
pedronasser authored and mholt committed Jul 13, 2016
1 parent 3fd3fee commit 2125ae5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
3 changes: 1 addition & 2 deletions caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net"
"os"
"os/exec"
"path"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -399,7 +398,7 @@ func startWithListenerFds(cdyfile Input, inst *Instance, restartFds map[string]r

inst.caddyfileInput = cdyfile

sblocks, err := loadServerBlocks(stypeName, path.Base(cdyfile.Path()), bytes.NewReader(cdyfile.Body()))
sblocks, err := loadServerBlocks(stypeName, cdyfile.Path(), bytes.NewReader(cdyfile.Body()))
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,15 @@ func (p *parser) doImport() error {

// make path relative to Caddyfile rather than current working directory (issue #867)
// and then use glob to get list of matching filenames
var matches []string
relImportPattern, err := filepath.Rel(filepath.Dir(p.Dispenser.filename), importPattern)
if err == nil {
matches, err = filepath.Glob(relImportPattern)
} else {
matches, err = filepath.Glob(importPattern)
absFile, err := filepath.Abs(p.Dispenser.filename)
if err != nil {
return p.Errf("Failed to get absolute path of file: %s", p.Dispenser.filename)
}

var matches []string
relImportPattern := filepath.Join(filepath.Dir(absFile), importPattern)
matches, err = filepath.Glob(relImportPattern)

if err != nil {
return p.Errf("Failed to use import pattern %s: %v", importPattern, err)
}
Expand Down
12 changes: 5 additions & 7 deletions caddyfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,24 +187,24 @@ func TestParseOneAndImport(t *testing.T) {

{`localhost
dir1 arg1
import ../testdata/import_test1.txt`, false, []string{
import testdata/import_test1.txt`, false, []string{
"localhost",
}, map[string]int{
"dir1": 2,
"dir2": 3,
"dir3": 1,
}},

{`import ../testdata/import_test2.txt`, false, []string{
{`import testdata/import_test2.txt`, false, []string{
"host1",
}, map[string]int{
"dir1": 1,
"dir2": 2,
}},

{`import ../testdata/import_test1.txt ../testdata/import_test2.txt`, true, []string{}, map[string]int{}},
{`import testdata/import_test1.txt testdata/import_test2.txt`, true, []string{}, map[string]int{}},

{`import ../testdata/not_found.txt`, true, []string{}, map[string]int{}},
{`import testdata/not_found.txt`, true, []string{}, map[string]int{}},

{`""`, false, []string{}, map[string]int{}},

Expand Down Expand Up @@ -394,8 +394,6 @@ func TestEnvironmentReplacement(t *testing.T) {

func testParser(input string) parser {
buf := strings.NewReader(input)
// use relative path to test that import paths are relative to
// the Caddyfile rather than the current working directory (issue #867)
p := parser{Dispenser: NewDispenser("../Test", buf)}
p := parser{Dispenser: NewDispenser("Caddyfile", buf)}
return p
}

0 comments on commit 2125ae5

Please sign in to comment.