This repository was archived by the owner on Sep 26, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathseed_test.go
More file actions
100 lines (79 loc) · 2.29 KB
/
Copy pathseed_test.go
File metadata and controls
100 lines (79 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-License-Identifier: Apache-2.0
package pluginkit_test
import (
"context"
"errors"
"slices"
"testing"
"github.com/gopherium/pluginkit"
)
var _ pluginkit.Seeder = (*seedingPlugin)(nil)
type seedingPlugin struct {
fakePlugin
seedErr error
seedPanic bool
}
func (s *seedingPlugin) Seed(_ context.Context) error {
*s.calls = append(*s.calls, s.id+" seed")
if s.seedPanic {
panic("boom")
}
return s.seedErr
}
func TestHostSeedsEverySeederInOrder(t *testing.T) {
t.Parallel()
var calls []string
host := pluginkit.NewHost(
&seedingPlugin{fakePlugin: fakePlugin{id: "alpha", calls: &calls}},
&fakePlugin{id: "beta", calls: &calls},
&seedingPlugin{fakePlugin: fakePlugin{id: "gamma", calls: &calls}},
)
if err := host.Seed(t.Context()); err != nil {
t.Fatalf("Seed() error = %v, want nil", err)
}
want := []string{"alpha seed", "gamma seed"}
if !slices.Equal(want, calls) {
t.Errorf("seed ordering = %v, want %v", calls, want)
}
}
func TestHostStopsSeedingAtTheFirstFailure(t *testing.T) {
t.Parallel()
errSeed := errors.New("seed exploded")
var calls []string
host := pluginkit.NewHost(
&seedingPlugin{fakePlugin: fakePlugin{id: "alpha", calls: &calls}, seedErr: errSeed},
&seedingPlugin{fakePlugin: fakePlugin{id: "beta", calls: &calls}},
)
err := host.Seed(t.Context())
if !errors.Is(err, errSeed) {
t.Fatalf("Seed() error = %v, want %v in its chain", err, errSeed)
}
if !slices.Equal([]string{"alpha seed"}, calls) {
t.Errorf("calls = %v, want seeding to stop at the failure", calls)
}
}
func TestHostTagsASeedPanicWithItsPlugin(t *testing.T) {
t.Parallel()
var calls []string
host := pluginkit.NewHost(
&seedingPlugin{fakePlugin: fakePlugin{id: "alpha", calls: &calls}, seedPanic: true},
)
err := host.Seed(t.Context())
if err == nil {
t.Fatal("Seed() error = nil, want the panic reported")
}
if err.Error() != "pluginkit: alpha seed panicked: boom" {
t.Errorf("error = %q, want it to name the plugin and the operation", err)
}
}
func TestHostSeedsNothingWithoutASeeder(t *testing.T) {
t.Parallel()
var calls []string
host := pluginkit.NewHost(&fakePlugin{id: "alpha", calls: &calls})
if err := host.Seed(t.Context()); err != nil {
t.Fatalf("Seed() error = %v, want nil", err)
}
if len(calls) != 0 {
t.Errorf("calls = %v, want none", calls)
}
}