-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer_integration_test.go
More file actions
141 lines (120 loc) · 3.43 KB
/
analyzer_integration_test.go
File metadata and controls
141 lines (120 loc) · 3.43 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
package main
import (
"os"
"path/filepath"
"testing"
)
func TestAnalyzeImpact(t *testing.T) {
// Create a temporary directory structure for testing
tmpDir := t.TempDir()
// Create some test Go files
files := map[string]string{
"central/deployment/manager.go": `package deployment
import "github.com/test/pkg/v2"
func main() {}`,
"sensor/client.go": `package sensor
import "github.com/test/pkg/v2"
func main() {}`,
"pkg/utils/helper.go": `package utils
import "github.com/test/pkg/v2"
func main() {}`,
"other/file.go": `package other
import "github.com/different/pkg"
func main() {}`,
}
for path, content := range files {
fullPath := filepath.Join(tmpDir, path)
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
t.Fatalf("Failed to write file: %v", err)
}
}
// Test impact analysis
impact, err := AnalyzeImpact("github.com/test/pkg", tmpDir)
if err != nil {
t.Fatalf("AnalyzeImpact failed: %v", err)
}
if impact.FilesAffected != 3 {
t.Errorf("Expected 3 files affected, got %d", impact.FilesAffected)
}
// Check components
expectedComponents := map[string]bool{
"central": true,
"sensor": true,
"pkg": true,
}
for _, comp := range impact.Components {
if !expectedComponents[comp] {
t.Errorf("Unexpected component: %s", comp)
}
delete(expectedComponents, comp)
}
if len(expectedComponents) > 0 {
t.Errorf("Missing expected components: %v", expectedComponents)
}
// Test IsCritical
if !impact.IsCritical() {
t.Error("Expected impact to be critical (contains 'central' and 'sensor')")
}
}
func TestAnalyzeImpactNonExistentPackage(t *testing.T) {
tmpDir := t.TempDir()
// Create a file that doesn't import our package
testFile := filepath.Join(tmpDir, "test.go")
if err := os.WriteFile(testFile, []byte(`package test
import "fmt"
func main() { fmt.Println("hello") }`), 0644); err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
impact, err := AnalyzeImpact("github.com/nonexistent/pkg", tmpDir)
if err != nil {
t.Fatalf("AnalyzeImpact failed: %v", err)
}
if impact.FilesAffected != 0 {
t.Errorf("Expected 0 files affected, got %d", impact.FilesAffected)
}
if impact.IsCritical() {
t.Error("Expected impact to not be critical")
}
}
func TestAnalyzeAllImpacts(t *testing.T) {
tmpDir := t.TempDir()
// Create test files
testFile := filepath.Join(tmpDir, "central", "test.go")
if err := os.MkdirAll(filepath.Dir(testFile), 0755); err != nil {
t.Fatalf("Failed to create dir: %v", err)
}
if err := os.WriteFile(testFile, []byte(`package central
import "github.com/pkg1/lib/v2"
import "github.com/pkg2/lib/v3"`), 0644); err != nil {
t.Fatalf("Failed to write file: %v", err)
}
candidates := []UpgradeCandidate{
{
Dependency: Dependency{
BasePath: "github.com/pkg1/lib",
},
},
{
Dependency: Dependency{
BasePath: "github.com/pkg2/lib",
},
},
}
err := AnalyzeAllImpacts(candidates, tmpDir)
if err != nil {
t.Fatalf("AnalyzeAllImpacts failed: %v", err)
}
// Both should have impact now
if candidates[0].Impact == nil {
t.Error("Expected first candidate to have impact")
}
if candidates[1].Impact == nil {
t.Error("Expected second candidate to have impact")
}
if candidates[0].Impact.FilesAffected != 1 {
t.Errorf("Expected 1 file affected for pkg1, got %d", candidates[0].Impact.FilesAffected)
}
}