-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathparse_test.go
More file actions
327 lines (262 loc) · 8.51 KB
/
Copy pathparse_test.go
File metadata and controls
327 lines (262 loc) · 8.51 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2025 Pinterest
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package thriftcheck
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"go.uber.org/thriftrw/ast"
)
const testStructContent = `struct TestStruct { 1: string field }`
type failingReader struct{}
func (failingReader) Read([]byte) (int, error) {
return 0, fmt.Errorf("read error")
}
func TestParseReadError(t *testing.T) {
_, _, err := Parse(failingReader{})
if err == nil {
t.Fatal("expected read error")
}
if err.Error() != "read error" {
t.Errorf("expected 'read error', got: %v", err)
}
}
func TestFileParserReadError(t *testing.T) {
parser := NewFileParser(nil)
absPath, _ := filepath.Abs("test.thrift")
_, _, err := parser.Parse(failingReader{}, absPath)
if err == nil {
t.Fatal("expected read error")
}
}
func TestFileParserParseFileAbsoluteNotFound(t *testing.T) {
parser := NewFileParser(nil)
absPath := "/nonexistent/absolute/path/test.thrift"
_, _, err := parser.ParseFile(absPath)
if err == nil {
t.Fatal("expected error for nonexistent absolute path")
}
if !strings.Contains(err.Error(), "not found") {
t.Errorf("expected 'not found' error, got: %v", err)
}
}
func TestFileParserCacheHit(t *testing.T) {
parser := NewFileParser(nil)
absPath, err := filepath.Abs("test.thrift")
if err != nil {
t.Fatalf("failed to get absolute path: %v", err)
}
prog1, info1, err := parser.Parse(strings.NewReader(testStructContent), absPath)
if err != nil {
t.Fatalf("first parse failed: %v", err)
}
prog2, info2, err := parser.Parse(strings.NewReader(testStructContent), absPath)
if err != nil {
t.Fatalf("second parse failed: %v", err)
}
if prog1 != prog2 {
t.Error("expected cached program (same pointer)")
}
if info1 != info2 {
t.Error("expected cached info (same pointer)")
}
}
func TestFileParserCacheMiss(t *testing.T) {
parser := NewFileParser(nil)
content1 := `struct Struct1 { 1: string field1 }`
content2 := `struct Struct2 { 1: string field2 }`
absPath1, _ := filepath.Abs("test1.thrift")
absPath2, _ := filepath.Abs("test2.thrift")
prog1, _, err := parser.Parse(strings.NewReader(content1), absPath1)
if err != nil {
t.Fatalf("parse 1 failed: %v", err)
}
prog2, _, err := parser.Parse(strings.NewReader(content2), absPath2)
if err != nil {
t.Fatalf("parse 2 failed: %v", err)
}
if prog1 == prog2 {
t.Error("expected different programs for different files")
}
if len(parser.cache) != 2 {
t.Errorf("expected 2 cache entries, got %d", len(parser.cache))
}
}
func TestFileParserErrorNotCached(t *testing.T) {
parser := NewFileParser(nil)
invalidContent := `struct Invalid {` // Missing closing brace
absPath, _ := filepath.Abs("invalid.thrift")
_, _, err1 := parser.Parse(strings.NewReader(invalidContent), absPath)
if err1 == nil {
t.Fatal("expected parse error for invalid content")
}
if len(parser.cache) != 0 {
t.Errorf("expected empty cache after error, got %d entries", len(parser.cache))
}
_, _, err2 := parser.Parse(strings.NewReader(invalidContent), absPath)
if err2 == nil {
t.Fatal("expected parse error on retry")
}
if len(parser.cache) != 0 {
t.Errorf("expected empty cache after retry, got %d entries", len(parser.cache))
}
}
func TestFileParserAbsolutePathNormalization(t *testing.T) {
parser := NewFileParser(nil)
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
relPath := "test.thrift"
absPath := filepath.Join(cwd, relPath)
prog1, _, err := parser.Parse(strings.NewReader(testStructContent), relPath)
if err != nil {
t.Fatalf("parse with relative path failed: %v", err)
}
prog2, _, err := parser.Parse(strings.NewReader(testStructContent), absPath)
if err != nil {
t.Fatalf("parse with absolute path failed: %v", err)
}
if prog1 != prog2 {
t.Error("expected same cached program for relative and absolute paths")
}
if len(parser.cache) != 1 {
t.Errorf("expected 1 cache entry, got %d", len(parser.cache))
}
}
func TestFileParserConcurrentAccess(t *testing.T) {
parser := NewFileParser(nil)
const numGoroutines = 10
const numIterations = 100
absPath, _ := filepath.Abs("concurrent.thrift")
var wg sync.WaitGroup
// Launch multiple goroutines that all parse the same file concurrently
for range numGoroutines {
wg.Add(1)
go func() {
defer wg.Done()
for range numIterations {
_, _, err := parser.Parse(strings.NewReader(testStructContent), absPath)
if err != nil {
t.Errorf("concurrent parse error: %v", err)
return
}
}
}()
}
wg.Wait()
if len(parser.cache) != 1 {
t.Errorf("expected 1 cache entry, got %d", len(parser.cache))
}
}
func TestFileParserConcurrentDifferentFiles(t *testing.T) {
parser := NewFileParser(nil)
const numGoroutines = 10
var wg sync.WaitGroup
// Each goroutine parses a different file
for i := range numGoroutines {
wg.Add(1)
go func(id int) {
defer wg.Done()
content := fmt.Sprintf("struct TestStruct%d { 1: string field }", id)
absPath, _ := filepath.Abs(fmt.Sprintf("test%d.thrift", id))
_, _, err := parser.Parse(strings.NewReader(content), absPath)
if err != nil {
t.Errorf("concurrent parse error: %v", err)
}
}(i)
}
wg.Wait()
if len(parser.cache) != numGoroutines {
t.Errorf("expected %d cache entries, got %d", numGoroutines, len(parser.cache))
}
}
func TestFileParserParseFile(t *testing.T) {
// Create temporary directory structure for testing
tmpDir := t.TempDir()
subDir := filepath.Join(tmpDir, "subdir")
if err := os.Mkdir(subDir, 0755); err != nil {
t.Fatalf("failed to create test directory: %v", err)
}
// Create test file in subdirectory
testFile := filepath.Join(subDir, "test.thrift")
content := []byte(`struct TestStruct { 1: string field }`)
if err := os.WriteFile(testFile, content, 0644); err != nil {
t.Fatalf("failed to write test file: %v", err)
}
// Test 1: Parse with absolute path
parser := NewFileParser(nil)
_, _, err := parser.ParseFile(testFile)
if err != nil {
t.Fatalf("ParseFile with absolute path failed: %v", err)
}
// Test 2: Parse with relative path and includes directory
parser2 := NewFileParser([]string{subDir})
_, _, err = parser2.ParseFile("test.thrift")
if err != nil {
t.Fatalf("ParseFile with relative path failed: %v", err)
}
// Test 3: File not found
parser3 := NewFileParser(nil)
_, _, err = parser3.ParseFile("nonexistent.thrift")
if err == nil {
t.Fatal("expected error for nonexistent file")
}
if !strings.Contains(err.Error(), "not found") {
t.Errorf("expected 'not found' error, got: %v", err)
}
}
func TestFileParserParseFileCurrentDirectory(t *testing.T) {
tmpDir := t.TempDir()
// Create two subdirectories
dir1 := filepath.Join(tmpDir, "dir1")
dir2 := filepath.Join(tmpDir, "dir2")
if err := os.Mkdir(dir1, 0755); err != nil {
t.Fatalf("failed to create dir1: %v", err)
}
if err := os.Mkdir(dir2, 0755); err != nil {
t.Fatalf("failed to create dir2: %v", err)
}
// Create same-named file in both directories with different content
file1 := filepath.Join(dir1, "test.thrift")
file2 := filepath.Join(dir2, "test.thrift")
if err := os.WriteFile(file1, []byte(`struct Struct1 { 1: string field1 }`), 0644); err != nil {
t.Fatalf("failed to write file1: %v", err)
}
if err := os.WriteFile(file2, []byte(`struct Struct2 { 1: string field2 }`), 0644); err != nil {
t.Fatalf("failed to write file2: %v", err)
}
// Parser with dir2 in includes, but parsing from dir1
// Should find test.thrift in dir1 (current directory) not dir2
parser := NewFileParser([]string{dir2})
// Parse test.thrift from dir1 - should use dir1 version, not dir2
prog, _, err := parser.ParseFile(filepath.Join(dir1, "test.thrift"))
if err != nil {
t.Fatalf("ParseFile failed: %v", err)
}
// Check that we got Struct1 (from dir1) not Struct2 (from dir2)
if len(prog.Definitions) == 0 {
t.Fatal("expected at least one definition")
}
structDef, ok := prog.Definitions[0].(*ast.Struct)
if !ok {
t.Fatal("expected struct definition")
}
if structDef.Name != "Struct1" {
t.Errorf("expected Struct1 from dir1, got %s", structDef.Name)
}
}