Skip to content

Commit

Permalink
Adding tests for readResourceFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlbr committed Mar 9, 2020
1 parent 433cd79 commit f0875ab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
16 changes: 9 additions & 7 deletions mule.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,18 @@ func flagging() {
func readMuleTemplate(tplname string) (*template.Template, error) {
tpl, err := template.ParseFiles(tplname)
if err != nil {
fmt.Printf("Error reading muletemplate file '%s'\n%v", tplname, err)
fmt.Printf("Error reading muletemplate file '%s'\n%v\n", tplname, err)
}
return tpl, err
}

func readTargetResource(resname string) string {
func readTargetResource(resname string) (string, error) {
res, err := ioutil.ReadFile(resname)
encoded := base64.StdEncoding.EncodeToString(res)
if err != nil {
fmt.Printf("Error reading target resource file '%s'\n%v", resname, err)
os.Exit(1)
fmt.Printf("Error reading target resource file '%s'\n%v\n", resname, err)
}

return encoded
return encoded, err
}

func formatFile(fname string) {
Expand Down Expand Up @@ -194,7 +192,11 @@ func main() {
Package: pckg,
Name: strings.Split(functionname, ".")[0],
}
data.Content = readTargetResource(inputfile)
data.Content, err = readTargetResource(inputfile)
if err != nil {
fmt.Printf("Error reading target resource file file '%s'\n%v\n", inputfile, err)
os.Exit(1)
}

outfile, err := os.Create(outfilename)
if err != nil {
Expand Down
29 changes: 25 additions & 4 deletions mule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func main() {
}
`

var TEMPLATEFILE = "embed.tpl"
var RESOURCEFILE = "example/gopher.jpg"

func TestFormatFile(t *testing.T) {

var f, _ = ioutil.TempFile(".", "muletest")
Expand Down Expand Up @@ -49,22 +52,40 @@ func TestFlagging(t *testing.T) {
os.Args = append(os.Args, args...)
flagging()
if pckg != "testpackage" {
fmt.Printf("package flag was not as expected. 'pckg=%s'", pckg)
fmt.Printf("package flag was not as expected. 'pckg=%s'\n", pckg)
t.Fail()
}

if inputfile != "/my/path/testfile.tpl" {
fmt.Printf("inputfile flag was not as expected. 'inputfile=%s'", inputfile)
fmt.Printf("inputfile flag was not as expected. 'inputfile=%s'\n", inputfile)
t.Fail()
}
if outfilename != "/my/path/testfile.go" {
fmt.Printf("output filename not correctly determined. 'outfilename=%s'", inputfile)
fmt.Printf("output filename not correctly determined. 'outfilename=%s'\n", inputfile)
t.Fail()
}

if functionname != "testfile" {
fmt.Printf("functionname not correctly determined. 'functionname=%s'", inputfile)
fmt.Printf("functionname not correctly determined. 'functionname=%s'\n", inputfile)
t.Fail()
}
fmt.Println()
}

func TestReadTemplateFile(t *testing.T) {
_, err := readMuleTemplate(TEMPLATEFILE)

if err != nil {
fmt.Printf("Error reading template file '%s'\n%v\n", TEMPLATEFILE, err)
t.Fail()
}
}

func TestReadTargetResource(t *testing.T) {
_, err := readTargetResource(RESOURCEFILE)

if err != nil {
fmt.Printf("Error reading resource file '%s'\n%v\n", RESOURCEFILE, err)
t.Fail()
}
}

0 comments on commit f0875ab

Please sign in to comment.