diff --git a/tools/generator_test.go b/tools/generator_test.go index 02f571c..4248095 100644 --- a/tools/generator_test.go +++ b/tools/generator_test.go @@ -19,8 +19,11 @@ SPDX-License-Identifier: Apache-2.0 package main import ( + "os" + "path/filepath" "strings" "testing" + "text/template" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -107,3 +110,93 @@ func TestDataFromSchema(t *testing.T) { t.Errorf("args: diff(-want,+got):\n%s", d) } } + +// TestExecuteTemplate_Success verifies that templating and code formatting +// are both applied +func TestExecuteTemplate_Success(t *testing.T) { + // Template code. The result will be compiled. + helloTemplate := `package main + +import "fmt" +func main() { + fmt.Println("Hello, {{.Name}}") +}` + // Expected output is the formatted code + expectedOutput := `package main + +import "fmt" + +func main() { + fmt.Println("Hello, World") +} +` + + // Create a temporary directory to store the generated files + tempDir, err := os.MkdirTemp("", "test-execute-template-*") + if err != nil { + t.Fatalf("error creating temporary directory: %s", err) + } + defer os.RemoveAll(tempDir) + + // Create a template file + tmpTemplateFilename := "template.txt.tmpl" + templateFile := filepath.Join(tempDir, tmpTemplateFilename) + err = os.WriteFile(templateFile, []byte(helloTemplate), 0644) + if err != nil { + t.Fatalf("error creating template file: %s", err) + } + + // Create a template set + allTemplates := template.New("all") + _, err = allTemplates.ParseFiles(templateFile) + if err != nil { + t.Fatalf("error parsing template file: %s", err) + } + + // Execute the template + outputFileName := filepath.Join(tempDir, "output.txt") + err = executeTemplate(allTemplates, tmpTemplateFilename, outputFileName, struct{ Name string }{"World"}) + if err != nil { + t.Fatalf("error executing template: %s", err) + } + + // Check the output file contents + output, err := os.ReadFile(outputFileName) + if err != nil { + t.Fatalf("error reading output file: %s", err) + } + if string(output) != expectedOutput { + t.Errorf("unexpected output: got %q, want %q", output, expectedOutput) + } +} + +// TestExecuteTemplate_Error tests the error case of executeTemplate() +func TestExecuteTemplate_Error(t *testing.T) { + // Create a temporary directory to store the generated files + tempDir, err := os.MkdirTemp("", "test-execute-template-*") + if err != nil { + t.Fatalf("error creating temporary directory: %s", err) + } + defer os.RemoveAll(tempDir) + + // Create an template file, valid template but invalid go code + templateFile := filepath.Join(tempDir, "template.txt") + err = os.WriteFile(templateFile, []byte("Hello, {{.Name}}!"), 0644) + if err != nil { + t.Fatalf("error creating template file: %s", err) + } + + // Create a template set + allTemplates := template.New("all") + _, err = allTemplates.ParseFiles(templateFile) + if err != nil { + t.Fatalf("error parsing template file: %s", err) + } + + // Execute the template + outputFileName := filepath.Join(tempDir, "output.txt") + err = executeTemplate(allTemplates, "template.txt", outputFileName, struct{ Name string }{"World"}) + if err == nil { + t.Fatal("expected error executing template, got nil") + } +}