-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_only_test.go
More file actions
117 lines (112 loc) · 4.41 KB
/
Copy pathfile_only_test.go
File metadata and controls
117 lines (112 loc) · 4.41 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
package codegraph
import (
"context"
"errors"
"reflect"
"testing"
)
func TestDocumentOnlyDocumentsEnterGraph(t *testing.T) {
g, r, err := Build(context.Background(), "rev", []Document{
{Path: "main.go", Content: []byte("package app\nfunc Entry(){}\n")},
{Path: "notes.cg-unrecognized", Content: []byte("plain text notes\n")},
}, Options{})
if err != nil || (len(r.Diagnostics) == 0) {
t.Fatal(r, err)
}
if !reflect.DeepEqual(r.Documents, []string{"main.go", "notes.cg-unrecognized"}) {
t.Fatalf("file-only document missing from coverage: %v", r.Documents)
}
if !hasDiagnostic(r, "unsupported_language") {
t.Fatalf("coverage gap not diagnosable: %+v", r.Diagnostics)
}
rows := query(t, g, `MATCH (f:Document {path:'notes.cg-unrecognized'}) RETURN f`, nil)
if len(rows) != 1 {
t.Fatalf("file node = %v", rows)
}
f := rows[0]["f"].(Node)
if f.Kind != DocumentKind || f.Language != "" || f.Name != "notes.cg-unrecognized" {
t.Fatalf("file-only node = %+v", f)
}
if got := query(t, g, `MATCH (:Document {path:'notes.cg-unrecognized'})-[:declares]->(n) RETURN n`, nil); len(got) != 0 {
t.Fatalf("file-only document parsed declarations: %v", got)
}
if got := query(t, g, `MATCH (f:Document) RETURN f`, nil); len(got) != 2 {
t.Fatalf("mixed batch file nodes = %v", got)
}
if got := query(t, g, `MATCH (:Document {path:'main.go'})-[:declares]->(n:Function {name:'Entry'}) RETURN n`, nil); len(got) != 1 {
t.Fatalf("grammar-backed document lost extraction: %v", got)
}
}
func TestDocumentOnlyDocumentsIdentity(t *testing.T) {
ctx := context.Background()
g, err := New("rev", Options{})
if err != nil {
t.Fatal(err)
}
notes := Document{Path: "notes.cg-unrecognized", Content: []byte("plain text notes")}
if _, err := g.addDocumentsSync(ctx, notes); err != nil {
t.Fatal(err)
}
nodes, relations, report := g.Nodes(), g.Relations(), g.Report()
if _, err := g.addDocumentsSync(ctx, notes, notes); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(nodes, g.Nodes()) || !reflect.DeepEqual(relations, g.Relations()) || !reflect.DeepEqual(report, g.Report()) {
t.Fatal("re-adding a file-only document changed identities or coverage")
}
if _, err := g.addDocumentsSync(ctx, Document{Path: notes.Path, Content: []byte("changed notes")}); !errors.Is(err, ErrSnapshotChanged) {
t.Fatalf("conflicting file-only content = %v", err)
}
if !reflect.DeepEqual(nodes, g.Nodes()) || !reflect.DeepEqual(relations, g.Relations()) || !reflect.DeepEqual(report, g.Report()) {
t.Fatal("conflicting input changed graph")
}
}
func TestDocumentOnlyDocumentsOwnContent(t *testing.T) {
ctx := context.Background()
content := []byte("original notes")
g, err := New("rev", Options{})
if err != nil {
t.Fatal(err)
}
if _, err := g.addDocumentsSync(ctx, Document{Path: "notes.cg-unrecognized", Content: content}); err != nil {
t.Fatal(err)
}
for i := range content {
content[i] = 'x'
}
if _, err := g.addDocumentsSync(ctx, Document{Path: "notes.cg-unrecognized", Content: []byte("original notes")}); err != nil {
t.Fatalf("caller mutation corrupted retained source: %v", err)
}
if _, err := g.addDocumentsSync(ctx, Document{Path: "notes.cg-unrecognized", Content: content}); !errors.Is(err, ErrSnapshotChanged) {
t.Fatalf("mutated caller memory matched retained source: %v", err)
}
}
func TestDocumentOnlyDocumentsBudget(t *testing.T) {
seed := Document{Path: "seed.go", Content: []byte("package app\nfunc Seed(){}\n")}
notes := Document{Path: "notes.cg-unrecognized", Content: []byte("plain text notes longer than the seed file")}
for _, tc := range []struct {
name string
opts Options
}{
{"file count", Options{MaxDocuments: 1}},
{"file bytes", Options{MaxDocumentBytes: int64(len(seed.Content))}},
{"source bytes", Options{MaxSourceBytes: int64(len(seed.Content) + len(notes.Content) - 1)}},
} {
t.Run(tc.name, func(t *testing.T) {
g, err := New("rev", tc.opts)
if err != nil {
t.Fatal(err)
}
if _, err := g.addDocumentsSync(context.Background(), seed); err != nil {
t.Fatal(err)
}
nodes, relations, report := g.Nodes(), g.Relations(), g.Report()
if _, err := g.addDocumentsSync(context.Background(), notes); !errors.Is(err, ErrBuildBudget) {
t.Fatalf("file-only document bypassed budget: %v", err)
}
if !reflect.DeepEqual(nodes, g.Nodes()) || !reflect.DeepEqual(relations, g.Relations()) || !reflect.DeepEqual(report, g.Report()) {
t.Fatal("failed batch changed published state")
}
})
}
}