-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathblast_radius_test.go
More file actions
169 lines (145 loc) · 4.78 KB
/
blast_radius_test.go
File metadata and controls
169 lines (145 loc) · 4.78 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
package main
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"codemap/scanner"
)
func makeBlastRadiusGitRepo(t *testing.T) string {
t.Helper()
root := t.TempDir()
files := map[string]string{
"go.mod": "module example.com/demo\n\ngo 1.22\n",
"pkg/math/math.go": `package math
func ComputeTotal(a, b int) int {
return a + b
}
`,
"internal/service/service.go": `package service
import "example.com/demo/pkg/math"
func Run() int {
return math.ComputeTotal(2, 3)
}
`,
"internal/worker/worker.go": `package worker
import "example.com/demo/pkg/math"
func Work() int {
return math.ComputeTotal(4, 5)
}
`,
"main.go": `package main
import "example.com/demo/internal/service"
func main() {
_ = service.Run()
}
`,
}
for relPath, content := range files {
fullPath := filepath.Join(root, relPath)
if err := os.MkdirAll(filepath.Dir(fullPath), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(fullPath, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
runGitMainTestCmd(t, root, "init")
runGitMainTestCmd(t, root, "add", ".")
runGitMainTestCmd(t, root, "-c", "user.name=Test", "-c", "user.email=test@example.com", "commit", "-m", "init")
runGitMainTestCmd(t, root, "branch", "-M", "main")
updated := `package math
func ComputeTotal(a, b int) int {
return a + b + 1
}
`
if err := os.WriteFile(filepath.Join(root, "pkg", "math", "math.go"), []byte(updated), 0o644); err != nil {
t.Fatal(err)
}
return root
}
func TestBlastRadiusSubcommandMarkdownAndJSON(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
if !scanner.NewAstGrepAnalyzer().Available() {
t.Skip("ast-grep not available")
}
root := makeBlastRadiusGitRepo(t)
markdown, stderr, err := runCodemapWithInput("", "blast-radius", "--ref", "HEAD", root)
if err != nil {
t.Fatalf("blast-radius markdown failed: %v\nstderr=%s", err, stderr)
}
for _, check := range []string{
"# Codemap Blast Radius",
"## Affected Outside Diff",
"## Impact Snippets",
"`internal/service/service.go` via `pkg/math/math.go`",
"ComputeTotal",
} {
if !strings.Contains(markdown, check) {
t.Fatalf("expected %q in markdown output, got:\n%s", check, markdown)
}
}
jsonOut, stderr, err := runCodemapWithInput("", "blast-radius", "--json", "--ref", "HEAD", root)
if err != nil {
t.Fatalf("blast-radius json failed: %v\nstderr=%s", err, stderr)
}
var bundle blastRadiusBundle
if err := json.Unmarshal([]byte(jsonOut), &bundle); err != nil {
t.Fatalf("expected blast-radius JSON output, got error %v with body:\n%s", err, jsonOut)
}
if bundle.Ref != "HEAD" {
t.Fatalf("bundle ref = %q, want HEAD", bundle.Ref)
}
if bundle.Summary.ChangedFiles != 1 || bundle.Summary.ChangedFilesTotal != 1 {
t.Fatalf("unexpected changed-file summary: %+v", bundle.Summary)
}
if bundle.Summary.ImpactedOutsideDiffShown == 0 {
t.Fatalf("expected impacted files outside diff, got %+v", bundle.Summary)
}
if len(bundle.Snippets) == 0 {
t.Fatalf("expected at least one impact snippet, got %+v", bundle)
}
if bundle.Snippets[0].MatchedTerm != "ComputeTotal" {
t.Fatalf("expected snippet match to target ComputeTotal, got %+v", bundle.Snippets[0])
}
}
func TestBlastRadiusSubcommandNoChanges(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
root := makeMainGitRepo(t, "main")
markdown, stderr, err := runCodemapWithInput("", "blast-radius", "--ref", "HEAD", root)
if err != nil {
t.Fatalf("blast-radius no-changes markdown failed: %v\nstderr=%s", err, stderr)
}
if !strings.Contains(markdown, "## No Changes") {
t.Fatalf("expected no-changes section in markdown output, got:\n%s", markdown)
}
if !strings.Contains(markdown, "No files changed vs `HEAD`.") {
t.Fatalf("expected no-changes message in markdown output, got:\n%s", markdown)
}
if strings.Contains(markdown, "## Diff") {
t.Fatalf("expected no diff section when there are no changes, got:\n%s", markdown)
}
jsonOut, stderr, err := runCodemapWithInput("", "blast-radius", "--json", "--ref", "HEAD", root)
if err != nil {
t.Fatalf("blast-radius no-changes json failed: %v\nstderr=%s", err, stderr)
}
var bundle blastRadiusBundle
if err := json.Unmarshal([]byte(jsonOut), &bundle); err != nil {
t.Fatalf("expected no-changes blast-radius JSON output, got error %v with body:\n%s", err, jsonOut)
}
if bundle.Summary.ChangedFiles != 0 || bundle.Summary.ChangedFilesTotal != 0 {
t.Fatalf("expected zero changed files in no-changes output, got %+v", bundle.Summary)
}
if bundle.Rendered.Diff != "No files changed vs HEAD\n" {
t.Fatalf("unexpected no-changes diff text: %q", bundle.Rendered.Diff)
}
if bundle.Rendered.Deps != "No changed source files to analyze.\n" {
t.Fatalf("unexpected no-changes deps text: %q", bundle.Rendered.Deps)
}
}