-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompile_contract_test.go
More file actions
216 lines (211 loc) · 7.39 KB
/
Copy pathcompile_contract_test.go
File metadata and controls
216 lines (211 loc) · 7.39 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package flow
import (
"context"
"go/ast"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestEventWatchCompileContract(t *testing.T) {
event := DefineEvent[string]("compile.watch")
var watchFn func(context.Context, *Runtime, RunID) (*EventWatch[string], error) = event.Watch
var watch *EventWatch[string]
var nextFn func(context.Context) (string, string, error) = watch.Next
var closeFn func() = watch.Close
_ = watchFn
_ = nextFn
_ = closeFn
}
func TestRemovedPublicAPINamesStayRemoved(t *testing.T) {
_, currentFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("cannot locate flow package")
}
denied := map[string]struct{}{
"Plan": {}, "PlanDef": {}, "DefinePlan": {}, "Fact": {}, "Facts": {},
"Action": {}, "DefineAction": {}, "HandleAction": {},
"Task": {}, "Step": {}, "Checkpoint": {}, "Workflow": {},
"Coordinator": {}, "Coordination": {}, "Handler": {}, "DefineCoordinator": {},
"OnStart": {}, "OnEvent": {}, "OnOutcome": {}, "Received": {}, "CoordinatorID": {},
"Outcome": {}, "OutcomeOf": {}, "ResultSource": {}, "Scope": {}, "WithCoordinatorConcurrency": {},
"ReadEvent": {}, "LookupEventValue": {}, "DeliverToLive": {},
"RunHandle": {}, "RunReceipt": {}, "Execute": {}, "Call": {},
"Execution": {}, "ExecutionID": {}, "ExecutionStatus": {}, "ExecutionOption": {},
"ExecutionFilter": {}, "ExecutionPage": {}, "ExecutionTrace": {},
"GetExecution": {}, "AwaitExecution": {}, "ListExecutions": {}, "CancelExecution": {},
"LookupLiveExecution": {}, "WithExecutionDeadline": {}, "WithoutExecutionDeadline": {},
"WithMaxCommandsPerExecution": {}, "ObservationExecution": {},
"HistoryExecutionStarted": {}, "HistoryExecutionFailing": {}, "BoundCommand": {},
"Node": {}, "LiveWork": {}, "LiveWorkFilter": {}, "LiveWorkPage": {},
"ListLiveWork": {}, "ListHistoryByKeys": {}, "GetQueueDepth": {}, "QueueDepth": {}, "TraceOption": {},
"CommandFailure": {}, "StatusSucceeded": {}, "StatusFailed": {}, "StatusCancelled": {},
"StatusExpired": {}, "NopObserver": {}, "WithFailFast": {}, "WithMetadata": {},
}
removedFields := map[string]map[string]struct{}{
"Run": {"Type": {}, "Version": {}, "Key": {}, "Created": {}, "FailFast": {}, "Metadata": {}},
"TraceCommand": {"State": {}, "Required": {}},
"RunFilter": {"Type": {}, "Metadata": {}},
}
entries, err := os.ReadDir(filepath.Dir(currentFile))
if err != nil {
t.Fatal(err)
}
files := token.NewFileSet()
for _, entry := range entries {
name := entry.Name()
if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
continue
}
file, err := parser.ParseFile(files, filepath.Join(filepath.Dir(currentFile), name), nil, 0)
if err != nil {
t.Fatalf("parse %s: %v", name, err)
}
check := func(identifier *ast.Ident) {
if identifier == nil || !ast.IsExported(identifier.Name) {
return
}
if _, removed := denied[identifier.Name]; removed {
t.Errorf("removed package declaration %s reappeared in %s", identifier.Name, name)
}
}
for _, declaration := range file.Decls {
switch value := declaration.(type) {
case *ast.FuncDecl:
check(value.Name)
if value.Recv != nil && (value.Name.Name == "With" || value.Name.Name == "Execute" || value.Name.Name == "Emit" || value.Name.Name == "Optional") {
t.Errorf("removed method %s reappeared in %s", value.Name.Name, name)
}
case *ast.GenDecl:
for _, specification := range value.Specs {
switch specification := specification.(type) {
case *ast.TypeSpec:
check(specification.Name)
if structure, ok := specification.Type.(*ast.StructType); ok {
for _, field := range structure.Fields.List {
for _, name := range field.Names {
if name.IsExported() && strings.HasPrefix(name.Name, "Execution") {
t.Errorf("removed public field %s.%s reappeared in %s", specification.Name.Name, name.Name, entry.Name())
}
if fields := removedFields[specification.Name.Name]; fields != nil {
if _, removed := fields[name.Name]; removed {
t.Errorf("removed public field %s.%s reappeared in %s", specification.Name.Name, name.Name, entry.Name())
}
}
}
}
}
case *ast.ValueSpec:
for _, identifier := range specification.Names {
check(identifier)
}
}
}
}
}
}
}
func TestProductionLookupSymbolsStayRemoved(t *testing.T) {
_, currentFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("cannot locate flow module")
}
root := filepath.Dir(currentFile)
files := token.NewFileSet()
err := filepath.WalkDir(root, func(path string, entry os.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if entry.IsDir() {
if path != root && (entry.Name() == ".git" || entry.Name() == "specs") {
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return nil
}
file, err := parser.ParseFile(files, path, nil, 0)
if err != nil {
return err
}
for _, declaration := range file.Decls {
function, ok := declaration.(*ast.FuncDecl)
if ok && strings.HasPrefix(function.Name.Name, "Lookup") {
t.Errorf("production Lookup symbol %s reappeared in %s", function.Name.Name, path)
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
}
// TestGenericAPIMisuseDoesNotCompile keeps the most important static-safety
// claims executable. Each fixture is a separate package so one expected type
// error cannot hide another.
func TestGenericAPIMisuseDoesNotCompile(t *testing.T) {
_, currentFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("cannot locate flow module")
}
moduleRoot := filepath.Dir(currentFile)
tests := map[string]string{
"command_arguments": `package misuse
import (
"context"
flow "github.com/goware/flow"
)
var command = flow.DefineCommand[string, int]("compile.command", 1)
func invalid(rt *flow.Runtime) {
_, _ = command.Enqueue(context.Background(), rt, "key", 42)
}
`,
"worker_result": `package misuse
import (
"context"
flow "github.com/goware/flow"
)
var command = flow.DefineCommand[string, int]("compile.worker", 1)
var _ = flow.Handle(command, func(context.Context, *flow.Work[string]) (string, error) {
return "wrong", nil
})
`,
"event_payload": `package misuse
import (
"context"
flow "github.com/goware/flow"
)
var event = flow.DefineEvent[string]("compile.event")
func invalid(client flow.Client, id flow.RunID) {
_ = event.Deliver(context.Background(), client, id, "key", 42)
}
`,
}
for name, source := range tests {
t.Run(name, func(t *testing.T) {
directory := t.TempDir()
goMod := "module compilemisuse\n\ngo 1.24\n\nrequire github.com/goware/flow v0.0.0\nreplace github.com/goware/flow => " + moduleRoot + "\n"
if err := os.WriteFile(filepath.Join(directory, "go.mod"), []byte(goMod), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(directory, "misuse.go"), []byte(source), 0o600); err != nil {
t.Fatal(err)
}
command := exec.Command("go", "test", "-mod=mod", ".")
command.Dir = directory
output, err := command.CombinedOutput()
if err == nil {
t.Fatalf("invalid generic API use compiled successfully:\n%s", output)
}
message := string(output)
if !strings.Contains(message, "cannot use") && !strings.Contains(message, "does not match inferred type") {
t.Fatalf("compile failure did not report a type mismatch:\n%s", output)
}
})
}
}