-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlangserver_test.go
More file actions
201 lines (183 loc) · 4.83 KB
/
langserver_test.go
File metadata and controls
201 lines (183 loc) · 4.83 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
package langserver
import (
"os"
"path/filepath"
"strings"
"testing"
)
// Note: TestFindEnvFilePathAtPosition was removed as part of Principle VII simplification.
// The functionality is now tested via validation.FindEnvFilesInConfig in the validation package.
func TestFindVarPositionInEnvFile(t *testing.T) {
// Create a temporary directory
tmpDir := t.TempDir()
// Create a test .env file
envContent := `# This is a comment
API_KEY=secret123
DATABASE_URL=postgres://localhost/db
# Another comment
DEBUG=true
`
envPath := filepath.Join(tmpDir, ".env.test")
if err := os.WriteFile(envPath, []byte(envContent), 0600); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
varName string
expectFound bool
expectedLine int
expectedCol int
}{
{
name: "find API_KEY",
varName: "API_KEY",
expectFound: true,
expectedLine: 1,
expectedCol: 0,
},
{
name: "find DATABASE_URL",
varName: "DATABASE_URL",
expectFound: true,
expectedLine: 2,
expectedCol: 0,
},
{
name: "find DEBUG",
varName: "DEBUG",
expectFound: true,
expectedLine: 4,
expectedCol: 0,
},
{
name: "variable not found",
varName: "NONEXISTENT",
expectFound: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
location, err := findVarPositionInEnvFile(tmpDir, ".env.test", tt.varName)
if tt.expectFound {
if err != nil {
t.Fatalf("findVarPositionInEnvFile() error = %v", err)
}
if location == nil {
t.Fatal("findVarPositionInEnvFile() returned nil location")
}
if int(location.Range.Start.Line) != tt.expectedLine {
t.Errorf("line = %d, want %d", location.Range.Start.Line, tt.expectedLine)
}
if int(location.Range.Start.Character) != tt.expectedCol {
t.Errorf("col = %d, want %d", location.Range.Start.Character, tt.expectedCol)
}
} else if location != nil {
t.Errorf("expected nil location for nonexistent variable, got %v", location)
}
})
}
}
func TestResolveEnvFileLocation(t *testing.T) {
// Create a temporary directory with an env file
tmpDir := t.TempDir()
envPath := filepath.Join(tmpDir, ".env.exists")
if err := os.WriteFile(envPath, []byte("VAR=value"), 0600); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
envFilePath string
expectFound bool
}{
{
name: "existing file",
envFilePath: ".env.exists",
expectFound: true,
},
{
name: "nonexistent file",
envFilePath: ".env.notfound",
expectFound: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
location := resolveEnvFileLocation(tt.envFilePath, tmpDir)
if tt.expectFound {
if location == nil {
t.Error("expected location, got nil")
return
}
// Should point to line 0, char 0
if location.Range.Start.Line != 0 || location.Range.Start.Character != 0 {
t.Errorf("expected position (0, 0), got (%d, %d)",
location.Range.Start.Line, location.Range.Start.Character)
}
} else if location != nil {
t.Errorf("expected nil for nonexistent file, got %v", location)
}
})
}
}
func TestFindVariableInConfigEnvFiles(t *testing.T) {
// Create a temp directory with env files
tmpDir := t.TempDir()
// Create .env.base with BASE_URL
baseEnvPath := filepath.Join(tmpDir, ".env.base")
if err := os.WriteFile(baseEnvPath, []byte("BASE_URL=http://localhost:3000\n"), 0600); err != nil {
t.Fatal(err)
}
// Create .env.override with API_KEY
overrideEnvPath := filepath.Join(tmpDir, ".env.override")
if err := os.WriteFile(overrideEnvPath, []byte("API_KEY=secret123\n"), 0600); err != nil {
t.Fatal(err)
}
configText := `yapi: v1
url: ${BASE_URL}/api
headers:
Authorization: Bearer ${API_KEY}
env_files:
- .env.base
- .env.override`
tests := []struct {
name string
varName string
expectFound bool
expectFile string
}{
{
name: "find BASE_URL in .env.base",
varName: "BASE_URL",
expectFound: true,
expectFile: ".env.base",
},
{
name: "find API_KEY in .env.override",
varName: "API_KEY",
expectFound: true,
expectFile: ".env.override",
},
{
name: "variable not found",
varName: "NONEXISTENT",
expectFound: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
location := findVariableInConfigEnvFiles(configText, tt.varName, tmpDir)
if tt.expectFound {
if location == nil {
t.Error("expected location, got nil")
return
}
// Check that the URI contains the expected file
if !strings.Contains(location.URI, tt.expectFile) {
t.Errorf("expected URI to contain %q, got %q", tt.expectFile, location.URI)
}
} else if location != nil {
t.Errorf("expected nil for nonexistent variable, got %v", location)
}
})
}
}