-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: map go formatting errors to their locations in templ files (#737)
Co-authored-by: Adrian Hesketh <[email protected]>
- Loading branch information
1 parent
1ecd566
commit b7a4eba
Showing
4 changed files
with
142 additions
and
1 deletion.
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
100 changes: 100 additions & 0 deletions
100
cmd/templ/generatecmd/test-eventhandler/eventhandler_test.go
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,100 @@ | ||
package testeventhandler | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"go/scanner" | ||
"go/token" | ||
"io" | ||
"log/slog" | ||
"os" | ||
"testing" | ||
|
||
"github.com/a-h/templ/cmd/templ/generatecmd" | ||
"github.com/a-h/templ/generator" | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestErrorLocationMapping(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
rawFileName string | ||
errorPositions []token.Position | ||
}{ | ||
{ | ||
name: "single error outputs location in srcFile", | ||
rawFileName: "single_error.templ.error", | ||
errorPositions: []token.Position{ | ||
{Offset: 46, Line: 3, Column: 20}, | ||
}, | ||
}, | ||
{ | ||
name: "multiple errors all output locations in srcFile", | ||
rawFileName: "multiple_errors.templ.error", | ||
errorPositions: []token.Position{ | ||
{Offset: 41, Line: 3, Column: 15}, | ||
{Offset: 101, Line: 7, Column: 22}, | ||
{Offset: 126, Line: 10, Column: 1}, | ||
}, | ||
}, | ||
} | ||
|
||
slog := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{})) | ||
fseh := generatecmd.NewFSEventHandler(slog, ".", false, []generator.GenerateOpt{}, false, false, true) | ||
for _, test := range tests { | ||
// The raw files cannot end in .templ because they will cause the generator to fail. Instead, | ||
// we create a tmp file that ends in .templ only for the duration of the test. | ||
rawFile, err := os.Open(test.rawFileName) | ||
if err != nil { | ||
t.Errorf("%s: Failed to open file %s: %v", test.name, test.rawFileName, err) | ||
break | ||
} | ||
file, err := os.CreateTemp("", fmt.Sprintf("*%s.templ", test.rawFileName)) | ||
if err != nil { | ||
t.Errorf("%s: Failed to create a tmp file at %s: %v", test.name, file.Name(), err) | ||
break | ||
} | ||
defer os.Remove(file.Name()) | ||
if _, err = io.Copy(file, rawFile); err != nil { | ||
t.Errorf("%s: Failed to copy contents from raw file %s to tmp %s: %v", test.name, test.rawFileName, file.Name(), err) | ||
} | ||
|
||
event := fsnotify.Event{Name: file.Name(), Op: fsnotify.Write} | ||
_, _, err = fseh.HandleEvent(context.Background(), event) | ||
if err == nil { | ||
t.Errorf("%s: no error was thrown", test.name) | ||
break | ||
} | ||
list, ok := err.(scanner.ErrorList) | ||
for !ok { | ||
err = errors.Unwrap(err) | ||
if err == nil { | ||
t.Errorf("%s: reached end of error wrapping before finding an ErrorList", test.name) | ||
break | ||
} else { | ||
list, ok = err.(scanner.ErrorList) | ||
} | ||
} | ||
if !ok { | ||
break | ||
} | ||
|
||
if len(list) != len(test.errorPositions) { | ||
t.Errorf("%s: expected %d errors but got %d", test.name, len(test.errorPositions), len(list)) | ||
break | ||
} | ||
for i, err := range list { | ||
test.errorPositions[i].Filename = file.Name() | ||
diff := cmp.Diff(test.errorPositions[i], err.Pos) | ||
if diff != "" { | ||
t.Error(diff) | ||
t.Error("expected:") | ||
t.Error(test.errorPositions[i]) | ||
t.Error("actual:") | ||
t.Error(err.Pos) | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
cmd/templ/generatecmd/test-eventhandler/multiple_errors.templ.error
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,10 @@ | ||
package testeventhandler | ||
|
||
func invalid(a: string) string { | ||
return "foo" | ||
} | ||
|
||
templ multipleError(a: string) { | ||
<div/> | ||
} | ||
l |
5 changes: 5 additions & 0 deletions
5
cmd/templ/generatecmd/test-eventhandler/single_error.templ.error
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,5 @@ | ||
package testeventhandler | ||
|
||
templ singleError(a: string) { | ||
<div/> | ||
} |