Repository navigation
Expand file tree
/
Copy pathcompilation_test.go
More file actions
305 lines (294 loc) · 10.3 KB
/
Copy pathcompilation_test.go
File metadata and controls
305 lines (294 loc) · 10.3 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package codegraph
import (
"context"
"errors"
"reflect"
"strings"
"testing"
"github.com/compforge/codegraph/internal/analysis"
"github.com/compforge/codegraph/internal/language"
"github.com/compforge/codegraph/internal/pipeline"
)
// +case:id=publication_budget,desc=`Many source uses fit a small selected graph without allocating Reference nodes`
func TestPublicationBudgetExcludesUnselectedUses(t *testing.T) {
ctx := context.Background()
doc := Document{Path: "app.ts", Content: []byte("function run() {}\nfunction handle() {\n" + strings.Repeat("run();\n", 100) + "}\n")}
full, _, err := Build(ctx, "full", []Document{doc}, Options{})
if err != nil {
t.Fatal(err)
}
policy := AnalysisAdmission()
graph, report, err := Build(ctx, "analysis", []Document{doc}, Options{Admission: policy, MaxNodes: 5})
if err != nil {
t.Fatal(err)
}
if report.Publication.References != 0 || report.Analysis.Uses < 100 || len(full.Nodes()) <= 100 || len(graph.Nodes()) > 5 {
t.Fatalf("unexpected work: %+v", report)
}
for _, node := range graph.Nodes() {
if node.Kind == Reference {
t.Fatal("unselected use was materialized")
}
}
if _, _, err := Build(ctx, "full", []Document{doc}, Options{MaxNodes: 5}); !errors.Is(err, ErrBuildBudget) {
t.Fatalf("full graph should exhaust publication: %v", err)
}
}
// +case:id=analysis_budget_atomicity,desc=`Every analysis and selection budget fails without replacing a previous graph`
func TestAnalysisBudgetsPreservePublication(t *testing.T) {
doc := Document{Path: "app.ts", Content: []byte("function run(){}\nfunction handle(){const local=run();run();return local;}\n")}
for _, tc := range []struct {
name string
limits AnalysisLimits
stage string
}{
{"Entities", AnalysisLimits{Entities: 1}, "analysis"},
{"Uses", AnalysisLimits{Uses: 1}, "analysis"},
{"Relations", AnalysisLimits{Relations: 1}, "analysis"},
{"Evidence", AnalysisLimits{Evidence: 1}, "analysis"},
{"SelectionSteps", AnalysisLimits{SelectionSteps: 1}, "selection"},
} {
t.Run(tc.name, func(t *testing.T) {
b, err := NewBuilder("budget", Options{Admission: AnalysisAdmission(), Analysis: tc.limits})
if err != nil {
t.Fatal(err)
}
old := b.Result()
_, err = b.addDocumentsSync(context.Background(), doc)
var budget *BuildBudgetError
if !errors.As(err, &budget) || !errors.Is(err, ErrBuildBudget) || budget.Stage != tc.stage || budget.Resource != tc.name {
t.Fatalf("wrong failure: %v", err)
}
if b.Result() != old {
t.Fatal("failed stage replaced the previous graph")
}
})
}
}
func TestSelectorAndAssemblerLeaveSemanticFactsUnchanged(t *testing.T) {
ctx := context.Background()
extractor := newTestExtractor(t, nil)
facts := extractTestFacts(t, extractor, "app.ts", "function run(){}\nfunction handle(){const local=run();return local;}")
scope := analysis.NewBuildScope(map[string]analysis.Facts{"app.ts": *facts.raw}, "")
analyzer := pipeline.Analyzer{Limits: analysis.Limits{Entities: 1000, Relations: 1000, Evidence: 1000}, Lookup: language.Lookup}
index, _, err := analyzer.Analyze(ctx, scope)
if err != nil {
t.Fatal(err)
}
entities := make(map[analysis.Ref]analysis.Entity)
for k, v := range index.Entities {
entities[k] = v
}
edges := append([]analysis.Edge(nil), index.Edges...)
bindings := append([]analysis.Binding(nil), facts.raw.Lexical.Bindings...)
opts := Options{Admission: AnalysisAdmission()}
if err := defaults(&opts); err != nil {
t.Fatal(err)
}
tags, err := NewTagMatcher(nil)
if err != nil {
t.Fatal(err)
}
var previous SelectionReport
for i := 0; i < 10; i++ {
selector := entitySelector{index: index, policy: opts.Admission, limit: opts.Analysis.SelectionSteps}
selected, err := selector.Select(ctx)
if err != nil {
t.Fatal(err)
}
assembler := newGraphAssembler(index, selected, opts, tags)
if err := assembler.Assemble(ctx, selected, nil); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(index.Entities, entities) || !reflect.DeepEqual(index.Edges, edges) || !reflect.DeepEqual(facts.raw.Lexical.Bindings, bindings) {
t.Fatal("publication mutated semantics")
}
current := SelectionReport{Steps: selector.steps}
if i > 0 && current != previous {
t.Fatal("selection work depends on map traversal order")
}
previous = current
}
}
func TestSymbolOnlyPolicyPreservesFailureContextByPolicy(t *testing.T) {
for _, kind := range []NodeKind{DocumentNodeKind, DirectoryNodeKind, Function} {
b, err := NewBuilder("failure", Options{Admission: &AdmissionPolicy{NodeKinds: []NodeKind{kind}}})
if err != nil {
t.Fatal(err)
}
if err := b.AddFailure(Document{Path: "sub/bad.ts", Content: []byte("bad")}, errors.New("fixture failure")); err != nil {
t.Fatal(err)
}
g, r, err := b.Build(context.Background())
if err != nil {
t.Fatal(err)
}
if len(r.Diagnostics) != 1 {
t.Fatal(r)
}
for _, n := range g.Nodes() {
if n.Kind != kind {
t.Fatalf("unexpected context %s for %s", n.Kind, kind)
}
}
}
}
func TestExcludedMaterialDoesNotConsumeAdmissionBudget(t *testing.T) {
ctx := context.Background()
docs := []Document{{Path: "node_modules/dependency.ts", Content: []byte("function dependency(){}")}, {Path: "app.ts", Content: []byte("function app(){}")}}
for _, external := range []bool{false, true} {
b, err := NewBuilder("excluded", Options{Admission: AnalysisAdmission(), MaxDocuments: 1, MaxSourceBytes: int64(len(docs[1].Content))})
if err != nil {
t.Fatal(err)
}
if external {
extractor := newTestExtractor(t, nil)
for _, doc := range docs {
facts := extractTestFacts(t, extractor, doc.Path, string(doc.Content))
if err := b.Add(facts); err != nil {
t.Fatal(err)
}
}
if _, _, err := b.Build(ctx); err != nil {
t.Fatal(err)
}
} else {
if _, err := b.addDocumentsSync(ctx, docs...); err != nil {
t.Fatal(err)
}
}
if len(b.Report().Documents) != 1 || !hasDiagnostic(b.Report(), "excluded_material") {
t.Fatal(b.Report())
}
if want := (MaterialReport{Submitted: 2, Accepted: 1, Excluded: 1}); b.Report().Materials != want {
t.Fatalf("material outcomes = %+v, want %+v", b.Report().Materials, want)
}
}
}
// +case:id=material_outcomes,desc=`Scope exclusions and parse failures reconcile with submitted materials`
func TestMaterialOutcomesReconcile(t *testing.T) {
ctx := context.Background()
_, report, err := Build(ctx, "scope", []Document{
{Path: "src/main.go", Content: []byte("package p\nfunc F(){}\n")},
{Path: "outside.go", Content: []byte("package p\n")},
}, Options{Scope: []string{"src"}})
if err != nil {
t.Fatal(err)
}
if want := (MaterialReport{Submitted: 2, Accepted: 1, Excluded: 1}); report.Materials != want {
t.Fatalf("scope outcomes = %+v, want %+v", report.Materials, want)
}
if !hasDiagnostic(report, "out_of_scope") {
t.Fatal("scope diagnostic lost")
}
b, err := NewBuilder("failed", Options{})
if err != nil {
t.Fatal(err)
}
if err := b.AddFailure(Document{Path: "bad.go"}, errors.New("parser failed")); err != nil {
t.Fatal(err)
}
_, report, err = b.Build(ctx)
if err != nil {
t.Fatal(err)
}
if want := (MaterialReport{Submitted: 1, Failed: 1}); report.Materials != want {
t.Fatalf("parse outcomes = %+v, want %+v", report.Materials, want)
}
}
// +case:id=excluded_snapshot_identity,desc=`Material exclusion cannot hide changes to an admitted or staged path`
func TestExcludedMaterialPreservesSnapshotIdentity(t *testing.T) {
ctx := context.Background()
extractor := newTestExtractor(t, nil)
link, err := extractor.Extract(ctx, Document{Path: "main.go", Gitlink: strings.Repeat("a", 40)})
if err != nil {
t.Fatal(err)
}
source := extractTestFacts(t, extractor, "main.go", "package p\nfunc F(){}\n")
for _, batch := range []bool{false, true} {
b, err := NewBuilder("identity", Options{Admission: &AdmissionPolicy{ExcludedTags: []Tag{SourceTag}}})
if err != nil {
t.Fatal(err)
}
if !batch {
if err := b.Add(link); err != nil {
t.Fatal(err)
}
if _, _, err := b.Build(ctx); err != nil {
t.Fatal(err)
}
}
previous := b.Result()
if batch {
err = b.Add(link, source)
} else {
err = b.Add(source)
}
if !errors.Is(err, ErrSnapshotChanged) {
t.Fatalf("batch=%v: want snapshot error, got %v", batch, err)
}
if b.Result() != previous {
t.Fatal("rejected admission replaced the graph")
}
_, report, err := b.Build(ctx)
if err != nil {
t.Fatal(err)
}
want := MaterialReport{Submitted: 1, Accepted: 1}
if batch {
want = MaterialReport{}
}
if report.Materials != want || hasDiagnostic(report, "excluded_material") {
t.Fatalf("rejected batch changed materials: %+v", report)
}
}
}
// Excluded facts are not admitted; a later eligible material owns the path outcome.
func TestAdmittedMaterialReplacesExcludedOutcome(t *testing.T) {
ctx := context.Background()
extractor := newTestExtractor(t, nil)
source := extractTestFacts(t, extractor, "main.go", "package p\n")
link, err := extractor.Extract(ctx, Document{Path: "main.go", Gitlink: strings.Repeat("a", 40)})
if err != nil {
t.Fatal(err)
}
b, err := NewBuilder("outcome", Options{Admission: &AdmissionPolicy{ExcludedTags: []Tag{SourceTag}}})
if err != nil {
t.Fatal(err)
}
if err := b.Add(source, link); err != nil {
t.Fatal(err)
}
_, report, err := b.Build(ctx)
if err != nil {
t.Fatal(err)
}
if report.Materials != (MaterialReport{Submitted: 1, Accepted: 1}) || hasDiagnostic(report, "excluded_material") {
t.Fatalf("stale excluded outcome: %+v", report)
}
}
// +case:id=resolver_budget,desc=`Resolver exhaustion retains typed budget classification and the previous graph`
func TestResolverBudgetPreservesClassification(t *testing.T) {
ctx := context.Background()
extractor := newTestExtractor(t, nil)
facts := extractTestFacts(t, extractor, "main.go", "package p\nfunc F(){}\nfunc G(){F();F();F();F();F();}\n")
b, err := NewBuilder("budget", Options{Analysis: AnalysisLimits{Evidence: 8}})
if err != nil {
t.Fatal(err)
}
previous := b.Result()
if err := b.Add(facts); err != nil {
t.Fatal(err)
}
_, _, err = b.Build(ctx)
var budget *BuildBudgetError
if !errors.Is(err, ErrBuildBudget) || !errors.As(err, &budget) {
t.Fatalf("resolver lost budget classification: %v", err)
}
if budget.Stage != "analysis" || budget.Resource != "Evidence" || budget.Limit != 8 || budget.Used+budget.Adding <= budget.Limit {
t.Fatalf("wrong resolver budget details: %+v", budget)
}
if b.Result() != previous {
t.Fatal("failed resolver replaced graph")
}
}