Skip to content

Commit

Permalink
Merge pull request #819 from hairyhenderson/parse-file-paths-properly…
Browse files Browse the repository at this point in the history
…-as-urls-809

Fix broken file datasource URL parsing (query string was ignored)
  • Loading branch information
Dave Henderson authored and GitHub committed Apr 23, 2020
2 parents 46ceeee + 55bea11 commit ff80053
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
11 changes: 6 additions & 5 deletions data/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func parseSource(value string) (source *Source, err error) {
err = errors.Errorf("Invalid datasource (%s). Must provide an alias with files not in working directory", value)
return nil, err
}
source.URL, err = absURL(f)
source.URL, err = absFileURL(f)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -272,15 +272,15 @@ func parseSourceURL(value string) (*url.URL, error) {
}

if !srcURL.IsAbs() {
srcURL, err = absURL(value)
srcURL, err = absFileURL(value)
if err != nil {
return nil, err
}
}
return srcURL, nil
}

func absURL(value string) (*url.URL, error) {
func absFileURL(value string) (*url.URL, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Wrapf(err, "can't get working directory")
Expand All @@ -290,8 +290,9 @@ func absURL(value string) (*url.URL, error) {
Scheme: "file",
Path: urlCwd + "/",
}
relURL := &url.URL{
Path: value,
relURL, err := url.Parse(value)
if err != nil {
return nil, fmt.Errorf("can't parse value %s as URL: %w", value, err)
}
resolved := baseURL.ResolveReference(relURL)
// deal with Windows drive letters
Expand Down
44 changes: 44 additions & 0 deletions data/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package data
import (
"fmt"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -303,6 +305,15 @@ func TestDefineDatasource(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "data", s.Alias)
assert.Nil(t, s.URL)

d = &Data{}
_, err = d.DefineDatasource("data", "/otherdir/foo?type=application/x-env")
s = d.Sources["data"]
assert.NoError(t, err)
assert.Equal(t, "data", s.Alias)
m, err := s.mimeType("")
assert.NoError(t, err)
assert.Equal(t, "application/x-env", m)
}

func TestMimeType(t *testing.T) {
Expand Down Expand Up @@ -411,3 +422,36 @@ func TestQueryParse(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, expected, u)
}

func TestAbsFileURL(t *testing.T) {
cwd, _ := os.Getwd()
// make this pass on Windows
cwd = filepath.ToSlash(cwd)
expected := &url.URL{
Scheme: "file",
Host: "",
Path: "/tmp/foo",
}
u, err := absFileURL("/tmp/foo")
assert.NoError(t, err)
assert.EqualValues(t, expected, u)

expected = &url.URL{
Scheme: "file",
Host: "",
Path: cwd + "/tmp/foo",
}
u, err = absFileURL("tmp/foo")
assert.NoError(t, err)
assert.EqualValues(t, expected, u)

expected = &url.URL{
Scheme: "file",
Host: "",
Path: cwd + "/tmp/foo",
RawQuery: "q=p",
}
u, err = absFileURL("tmp/foo?q=p")
assert.NoError(t, err)
assert.EqualValues(t, expected, u)
}
6 changes: 6 additions & 0 deletions tests/integration/datasources_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (s *FileDatasourcesSuite) SetUpSuite(c *C) {
s.tmpDir = fs.NewDir(c, "gomplate-inttests",
fs.WithFiles(map[string]string{
"config.json": `{"foo": {"bar": "baz"}}`,
"ajsonfile": `{"foo": {"bar": "baz"}}`,
"encrypted.json": `{
"_public_key": "dfcf98785869cdfc4a59273bbdfe1bfcf6c44850a11ea9d84db21c89a802c057",
"password": "EJ[1:Cb1AY94Dl76xwHHrnJyh+Y+fAeovijPlFQZXSAuvZBc=:oCGZM6lbeXXOl2ONSKfLQ0AgaltrTpNU:VjegqQPPkOK1hSylMAbmcfusQImfkHCWZw==]"
Expand Down Expand Up @@ -99,6 +100,11 @@ func (s *FileDatasourcesSuite) TestFileDatasources(c *C) {
)
result.Assert(c, icmd.Expected{ExitCode: 0, Out: "foobarbaz"})

result = icmd.RunCommand(GomplateBin,
"-i", `foo{{defineDatasource "config" `+"`"+s.tmpDir.Join("ajsonfile")+"?type=application/json`"+`}}bar{{(datasource "config").foo.bar}}`,
)
result.Assert(c, icmd.Expected{ExitCode: 0, Out: "foobarbaz"})

result = icmd.RunCommand(GomplateBin,
"-d", "config="+s.tmpDir.Join("config.yml"),
"-i", `{{(datasource "config").foo.bar}}`,
Expand Down

0 comments on commit ff80053

Please sign in to comment.