Skip to content

Commit

Permalink
More TestCreateFileFailures for more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dolmen committed Mar 28, 2024
1 parent 35c9aa3 commit 87f4763
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,20 @@ func TestParseFailures(t *testing.T) {
}

func TestCreateFileFailures(t *testing.T) {
err := codegen.CreateFile("tmp_test.go", codegen.MustParse("package main_test\n").Template, 0)
const filename = "tmp_test.go"
// Remove the file just in case.
// In the normal case (test success), the file is not created because of earlier failures.
defer os.Remove(filename)

err := codegen.CreateFile(filename, codegen.MustParse("package codegen_test\n").Template, 0)
if err == nil || !strings.Contains(err.Error(), "https://golang.org/s/generatedcode") {
t.Fatal("Error expected")
}

err = codegen.MustParse("// Code generated ! DO NOT EDIT.\n\npackage codegen_test\n\n// {{ len 1 }}\n").CreateFile(filename, nil)
if err == nil || !strings.Contains(err.Error(), "len") {
t.Log("Error:", err)
t.Fatal("Error expected when evaluating the template (because of epression `len 1`)")
}

}

0 comments on commit 87f4763

Please sign in to comment.