Skip to content

Commit

Permalink
chore: add test to fmt cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed May 26, 2024
1 parent 19ffb1c commit efaf8e5
Show file tree
Hide file tree
Showing 59 changed files with 197 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.708
0.2.709
109 changes: 109 additions & 0 deletions cmd/templ/fmtcmd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package fmtcmd

import (
_ "embed"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"golang.org/x/tools/txtar"
)

//go:embed testdata.txtar
var testDataTxTar []byte

type testProject struct {
dir string
cleanup func()
testFiles map[string]testFile
}

type testFile struct {
name string
input, expected string
}

func setupProjectDir() (tp testProject, err error) {
tp.dir, err = os.MkdirTemp("", "fmtcmd_test_*")
if err != nil {
return tp, fmt.Errorf("failed to make test dir: %w", err)
}
tp.testFiles = make(map[string]testFile)
testData := txtar.Parse(testDataTxTar)
for i := 0; i < len(testData.Files); i += 2 {
file := testData.Files[i]
err = os.WriteFile(filepath.Join(tp.dir, file.Name), file.Data, 0660)
if err != nil {
return tp, fmt.Errorf("failed to write file: %w", err)
}
tp.testFiles[file.Name] = testFile{
name: filepath.Join(tp.dir, file.Name),
input: string(file.Data),
expected: string(testData.Files[i+1].Data),
}
}
tp.cleanup = func() {
os.RemoveAll(tp.dir)
}
return tp, nil
}

func TestFormat(t *testing.T) {
log := slog.New(slog.NewJSONHandler(io.Discard, nil))
t.Run("can format a single file from stdin to stdout", func(t *testing.T) {
tp, err := setupProjectDir()
if err != nil {
t.Fatalf("failed to setup project dir: %v", err)
}
defer tp.cleanup()
stdin := strings.NewReader(tp.testFiles["a.templ"].input)
stdout := new(strings.Builder)
Run(log, stdin, stdout, Arguments{
ToStdout: true,
})
if diff := cmp.Diff(tp.testFiles["a.templ"].expected, stdout.String()); diff != "" {
t.Error(diff)
}
})
t.Run("can process a single file to stdout", func(t *testing.T) {
tp, err := setupProjectDir()
if err != nil {
t.Fatalf("failed to setup project dir: %v", err)
}
defer tp.cleanup()
stdout := new(strings.Builder)
Run(log, nil, stdout, Arguments{
ToStdout: true,
Files: []string{
tp.testFiles["a.templ"].name,
},
})
if diff := cmp.Diff(tp.testFiles["a.templ"].expected, stdout.String()); diff != "" {
t.Error(diff)
}
})
t.Run("can process a single file in place", func(t *testing.T) {
tp, err := setupProjectDir()
if err != nil {
t.Fatalf("failed to setup project dir: %v", err)
}
defer tp.cleanup()
Run(log, nil, nil, Arguments{
Files: []string{
tp.testFiles["a.templ"].name,
},
})
data, err := os.ReadFile(tp.testFiles["a.templ"].name)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
if diff := cmp.Diff(tp.testFiles["a.templ"].expected, string(data)); diff != "" {
t.Error(diff)
}
})
}
34 changes: 34 additions & 0 deletions cmd/templ/fmtcmd/testdata.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- a.templ --
package test

templ a() {
<div><p>A
</p></div>
}
-- a.templ --
package test

templ a() {
<div>
<p>
A
</p>
</div>
}
-- b.templ --
package test

templ b() {
<div><p>B
</p></div>
}
-- a.templ --
package test

templ b() {
<div>
<p>
B
</p>
</div>
}
47 changes: 3 additions & 44 deletions cmd/templ/lspcmd/lsp_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package lspcmd

import (
"bytes"
"context"
"embed"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
Expand All @@ -17,49 +13,12 @@ import (
"github.com/a-h/protocol"
"github.com/a-h/templ/cmd/templ/generatecmd/modcheck"
"github.com/a-h/templ/cmd/templ/lspcmd/lspdiff"
"github.com/a-h/templ/cmd/templ/testproject"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/uri"
"go.uber.org/zap"
)

//go:embed testdata/*
var testdata embed.FS

func createTestProject(moduleRoot string) (dir string, err error) {
dir, err = os.MkdirTemp("", "templ_watch_test_*")
if err != nil {
return dir, fmt.Errorf("failed to make test dir: %w", err)
}
files, err := testdata.ReadDir("testdata")
if err != nil {
return dir, fmt.Errorf("failed to read embedded dir: %w", err)
}
for _, file := range files {
src := filepath.Join("testdata", file.Name())
data, err := testdata.ReadFile(src)
if err != nil {
return dir, fmt.Errorf("failed to read file: %w", err)
}

target := filepath.Join(dir, file.Name())
if file.Name() == "go.mod.embed" {
data = bytes.ReplaceAll(data, []byte("{moduleRoot}"), []byte(moduleRoot))
target = filepath.Join(dir, "go.mod")
}
err = os.WriteFile(target, data, 0660)
if err != nil {
return dir, fmt.Errorf("failed to copy file: %w", err)
}
}
return dir, nil
}

func mustReplaceLine(file string, line int, replacement string) string {
lines := strings.Split(file, "\n")
lines[line-1] = replacement
return strings.Join(lines, "\n")
}

func TestCompletion(t *testing.T) {
if testing.Short() {
return
Expand Down Expand Up @@ -145,7 +104,7 @@ func TestCompletion(t *testing.T) {
for i, test := range tests {
t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) {
// Edit the file.
updated := mustReplaceLine(string(templFile), test.line, test.replacement)
updated := testproject.MustReplaceLine(string(templFile), test.line, test.replacement)
err = server.DidChange(ctx, &protocol.DidChangeTextDocumentParams{
TextDocument: protocol.VersionedTextDocumentIdentifier{
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
Expand Down Expand Up @@ -454,7 +413,7 @@ func Setup(ctx context.Context, log *zap.Logger) (clientCtx context.Context, app
return ctx, appDir, client, server, teardown, fmt.Errorf("could not find local templ go.mod file: %v", err)
}

appDir, err = createTestProject(moduleRoot)
appDir, err = testproject.Create(moduleRoot)
if err != nil {
return ctx, appDir, client, server, teardown, fmt.Errorf("failed to create test project: %v", err)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions cmd/templ/testproject/testproject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package testproject

import (
"bytes"
"embed"
"fmt"
"os"
"path/filepath"
"strings"
)

//go:embed testdata/*
var testdata embed.FS

func Create(moduleRoot string) (dir string, err error) {
dir, err = os.MkdirTemp("", "templ_test_*")
if err != nil {
return dir, fmt.Errorf("failed to make test dir: %w", err)
}
files, err := testdata.ReadDir("testdata")
if err != nil {
return dir, fmt.Errorf("failed to read embedded dir: %w", err)
}
for _, file := range files {
src := filepath.Join("testdata", file.Name())
data, err := testdata.ReadFile(src)
if err != nil {
return dir, fmt.Errorf("failed to read file: %w", err)
}

target := filepath.Join(dir, file.Name())
if file.Name() == "go.mod.embed" {
data = bytes.ReplaceAll(data, []byte("{moduleRoot}"), []byte(moduleRoot))
target = filepath.Join(dir, "go.mod")
}
err = os.WriteFile(target, data, 0660)
if err != nil {
return dir, fmt.Errorf("failed to copy file: %w", err)
}
}
return dir, nil
}

func MustReplaceLine(file string, line int, replacement string) string {
lines := strings.Split(file, "\n")
lines[line-1] = replacement
return strings.Join(lines, "\n")
}
2 changes: 1 addition & 1 deletion parser/v2/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestFormatting(t *testing.T) {
files, _ := filepath.Glob("testdata/*.txt")
files, _ := filepath.Glob("formattestdata/*.txt")
if len(files) == 0 {
t.Errorf("no test files found")
}
Expand Down

0 comments on commit efaf8e5

Please sign in to comment.