5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-07 23:02:31 +08:00
wails/v3/pkg/application/internal/tests/utils.go
Fabio Massaioli e7c134de4e
[v3] Late service registration and error handling overhaul (#4066)
* Add service registration method

* Fix error handling and formatting in messageprocessor

* Add configurable error handling

* Improve error strings

* Fix service shutdown on macOS

* Add post shutdown hook

* Better fatal errors

* Add startup/shutdown sequence tests

* Improve debug messages

* Update JS runtime

* Update docs

* Update changelog

* Fix log message in clipboard message processor

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Remove panic in RegisterService

* Fix linux tests (hopefully)

* Fix error formatting everywhere

* Fix typo in windows webview

* Tidy example mods

* Set application name in tests

* Fix ubuntu test workflow

* Cleanup template test pipeline

* Fix dev build detection on Go 1.24

* Update template go.mod/sum to Go 1.24

* Remove redundant caching in template tests

* Final format string cleanup

* Fix wails3 tool references

* Fix legacy log calls

* Remove formatJS and simplify format strings

* Fix indirect import

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-02-19 09:27:41 +01:00

75 lines
1.3 KiB
Go

package tests
import (
"errors"
"os"
"runtime"
"testing"
"github.com/wailsapp/wails/v3/pkg/application"
)
var appChan chan *application.App = make(chan *application.App, 1)
var errChan chan error = make(chan error, 1)
var endChan chan error = make(chan error, 1)
func init() { runtime.LockOSThread() }
func New(t *testing.T, options application.Options) *application.App {
var app *application.App
app = application.Get()
if app != nil {
return app
}
if options.Name == "" {
options.Name = t.Name()
}
errorHandler := options.ErrorHandler
options.ErrorHandler = func(err error) {
if fatal := (*application.FatalError)(nil); errors.As(err, &fatal) {
endChan <- err
select {} // Block forever
} else if errorHandler != nil {
errorHandler(err)
} else {
app.Logger.Error(err.Error())
}
}
postShutdown := options.PostShutdown
options.PostShutdown = func() {
if postShutdown != nil {
postShutdown()
}
endChan <- nil
select {} // Block forever
}
return application.New(options)
}
func Run(t *testing.T, app *application.App) error {
appChan <- app
select {
case err := <-errChan:
return err
case fatal := <-endChan:
if fatal != nil {
t.Fatal(fatal)
}
return fatal
}
}
func Main(m *testing.M) {
go func() {
os.Exit(m.Run())
}()
errChan <- (<-appChan).Run()
select {} // Block forever
}