From 1ea90278038ca63e36e8b1b866fb133a80ef81b1 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sun, 26 May 2024 20:25:30 +0100 Subject: [PATCH] refactor: add generate cmd test --- cmd/templ/fmtcmd/main_test.go | 18 ++++++++---- cmd/templ/generatecmd/main_test.go | 44 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 cmd/templ/generatecmd/main_test.go diff --git a/cmd/templ/fmtcmd/main_test.go b/cmd/templ/fmtcmd/main_test.go index 801b7c086..fd7e85347 100644 --- a/cmd/templ/fmtcmd/main_test.go +++ b/cmd/templ/fmtcmd/main_test.go @@ -63,9 +63,11 @@ func TestFormat(t *testing.T) { defer tp.cleanup() stdin := strings.NewReader(tp.testFiles["a.templ"].input) stdout := new(strings.Builder) - Run(log, stdin, stdout, Arguments{ + if err = Run(log, stdin, stdout, Arguments{ ToStdout: true, - }) + }); err != nil { + t.Fatalf("failed to run format command: %v", err) + } if diff := cmp.Diff(tp.testFiles["a.templ"].expected, stdout.String()); diff != "" { t.Error(diff) } @@ -77,12 +79,14 @@ func TestFormat(t *testing.T) { } defer tp.cleanup() stdout := new(strings.Builder) - Run(log, nil, stdout, Arguments{ + if err = Run(log, nil, stdout, Arguments{ ToStdout: true, Files: []string{ tp.testFiles["a.templ"].name, }, - }) + }); err != nil { + t.Fatalf("failed to run format command: %v", err) + } if diff := cmp.Diff(tp.testFiles["a.templ"].expected, stdout.String()); diff != "" { t.Error(diff) } @@ -93,11 +97,13 @@ func TestFormat(t *testing.T) { t.Fatalf("failed to setup project dir: %v", err) } defer tp.cleanup() - Run(log, nil, nil, Arguments{ + if err = Run(log, nil, nil, Arguments{ Files: []string{ tp.testFiles["a.templ"].name, }, - }) + }); err != nil { + t.Fatalf("failed to run format command: %v", err) + } data, err := os.ReadFile(tp.testFiles["a.templ"].name) if err != nil { t.Fatalf("failed to read file: %v", err) diff --git a/cmd/templ/generatecmd/main_test.go b/cmd/templ/generatecmd/main_test.go new file mode 100644 index 000000000..9b0b10914 --- /dev/null +++ b/cmd/templ/generatecmd/main_test.go @@ -0,0 +1,44 @@ +package generatecmd + +import ( + "context" + "io" + "log/slog" + "os" + "path" + "testing" + + "github.com/a-h/templ/cmd/templ/testproject" +) + +func TestGenerate(t *testing.T) { + log := slog.New(slog.NewJSONHandler(io.Discard, nil)) + t.Run("can generate a file in place", func(t *testing.T) { + // templ generate -f templates.templ + dir, err := testproject.Create("github.com/a-h/templ/cmd/templ/testproject") + if err != nil { + t.Fatalf("failed to create test project: %v", err) + } + defer os.RemoveAll(dir) + + // Delete the templates_templ.go file to ensure it is generated. + err = os.Remove(path.Join(dir, "templates_templ.go")) + if err != nil { + t.Fatalf("failed to remove templates_templ.go: %v", err) + } + + // Run the generate command. + err = Run(context.Background(), log, Arguments{ + FileName: path.Join(dir, "templates.templ"), + }) + if err != nil { + t.Fatalf("failed to run generate command: %v", err) + } + + // Check the templates_templ.go file was created. + _, err = os.Stat(path.Join(dir, "templates_templ.go")) + if err != nil { + t.Fatalf("templates_templ.go was not created: %v", err) + } + }) +}