fixes read absolute path woth readFile (#475)
This commit is contained in:
parent
35fd5ff117
commit
c6c54fdbed
|
|
@ -114,7 +114,12 @@ func (c *Context) Exec(command string, args []interface{}, inputs ...string) (st
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) ReadFile(filename string) (string, error) {
|
func (c *Context) ReadFile(filename string) (string, error) {
|
||||||
path := filepath.Join(c.basePath, filename)
|
var path string
|
||||||
|
if filepath.IsAbs(filename) {
|
||||||
|
path = filename
|
||||||
|
} else {
|
||||||
|
path = filepath.Join(c.basePath, filename)
|
||||||
|
}
|
||||||
|
|
||||||
bytes, err := c.readFile(path)
|
bytes, err := c.readFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package tmpl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
@ -11,7 +12,27 @@ func TestReadFile(t *testing.T) {
|
||||||
bar: BAR
|
bar: BAR
|
||||||
`
|
`
|
||||||
expectedFilename := "values.yaml"
|
expectedFilename := "values.yaml"
|
||||||
ctx := &Context{readFile: func(filename string) ([]byte, error) {
|
ctx := &Context{basePath: ".", readFile: func(filename string) ([]byte, error) {
|
||||||
|
if filename != expectedFilename {
|
||||||
|
return nil, fmt.Errorf("unexpected filename: expected=%v, actual=%s", expectedFilename, filename)
|
||||||
|
}
|
||||||
|
return []byte(expected), nil
|
||||||
|
}}
|
||||||
|
actual, err := ctx.ReadFile(expectedFilename)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("unexpected result: expected=%v, actual=%v", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadFile_PassAbsPath(t *testing.T) {
|
||||||
|
expected := `foo:
|
||||||
|
bar: BAR
|
||||||
|
`
|
||||||
|
expectedFilename, _ := filepath.Abs("values.yaml")
|
||||||
|
ctx := &Context{basePath: ".", readFile: func(filename string) ([]byte, error) {
|
||||||
if filename != expectedFilename {
|
if filename != expectedFilename {
|
||||||
return nil, fmt.Errorf("unexpected filename: expected=%v, actual=%s", expectedFilename, filename)
|
return nil, fmt.Errorf("unexpected filename: expected=%v, actual=%s", expectedFilename, filename)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue