Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions runtime_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"context"
"fmt"
"sort"

"github.com/wailsapp/wails/v3/pkg/application"
)
Expand All @@ -24,11 +25,14 @@ func NewWithFactories(app *application.App, factories map[string]ServiceFactory)
WithWails(app),
}

for _, name := range []string{} {
factory, ok := factories[name]
if !ok {
return nil, fmt.Errorf("service %s factory not provided", name)
}
names := make([]string, 0, len(factories))
for name := range factories {
names = append(names, name)
}
sort.Strings(names)

for _, name := range names {
factory := factories[name]
svc, err := factory()
if err != nil {
return nil, fmt.Errorf("failed to create service %s: %w", name, err)
Expand Down
28 changes: 25 additions & 3 deletions runtime_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,31 @@ func TestNewWithFactories_Good(t *testing.T) {
rt, err := NewWithFactories(nil, factories)
assert.NoError(t, err)
assert.NotNil(t, rt)
// The production code doesn't actually use the factories, so we can't test that a service is created.
// We can only test that the function runs without error.
assert.Nil(t, rt.Core.Service("test"))
svc := rt.Core.Service("test")
assert.NotNil(t, svc)
mockSvc, ok := svc.(*MockService)
assert.True(t, ok)
assert.Equal(t, "test", mockSvc.Name)
}

func TestNewWithFactories_Bad(t *testing.T) {
factories := map[string]ServiceFactory{
"test": func() (any, error) {
return nil, assert.AnError
},
}
_, err := NewWithFactories(nil, factories)
assert.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
}

func TestNewWithFactories_Ugly(t *testing.T) {
factories := map[string]ServiceFactory{
"test": nil,
}
assert.Panics(t, func() {
_, _ = NewWithFactories(nil, factories)
})
}

func TestRuntime_Lifecycle_Good(t *testing.T) {
Expand Down