Commit 97a48e35 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

template: ParseFile

parent d74dacc4
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"os"
"sort" "sort"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
...@@ -290,3 +291,15 @@ func Parse(r io.Reader) (*Template, error) { ...@@ -290,3 +291,15 @@ func Parse(r io.Reader) (*Template, error) {
// Return the template parsed from the raw structure // Return the template parsed from the raw structure
return rawTpl.Template() return rawTpl.Template()
} }
// ParseFile is the same as Parse but is a helper to automatically open
// a file for parsing.
func ParseFile(path string) (*Template, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return Parse(f)
}
package template package template
import ( import (
"os"
"reflect" "reflect"
"testing" "testing"
"time" "time"
...@@ -272,13 +271,7 @@ func TestParse(t *testing.T) { ...@@ -272,13 +271,7 @@ func TestParse(t *testing.T) {
} }
for _, tc := range cases { for _, tc := range cases {
f, err := os.Open(fixtureDir(tc.File)) tpl, err := ParseFile(fixtureDir(tc.File))
if err != nil {
t.Fatalf("err: %s", err)
}
tpl, err := Parse(f)
f.Close()
if (err != nil) != tc.Err { if (err != nil) != tc.Err {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment