Skip to content

Commit

Permalink
refactor: add generate cmd test
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed May 26, 2024
1 parent efaf8e5 commit 1ea9027
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
18 changes: 12 additions & 6 deletions cmd/templ/fmtcmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions cmd/templ/generatecmd/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}

0 comments on commit 1ea9027

Please sign in to comment.