-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
59 changed files
with
197 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.2.708 | ||
0.2.709 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
cmd/templ/lspcmd/testdata/templates_templ.go → ...l/testproject/testdata/templates_templ.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.