-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper_test.go
More file actions
518 lines (460 loc) · 13.5 KB
/
wrapper_test.go
File metadata and controls
518 lines (460 loc) · 13.5 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package wrapper
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
)
func TestCheckNinjaExists(t *testing.T) {
// Test when ninja is available
origPath := os.Getenv("PATH")
defer func() {
if err := os.Setenv("PATH", origPath); err != nil {
t.Errorf("Failed to restore PATH: %v", err)
}
}()
// Create temp dir with mock ninja
tempDir := t.TempDir()
// Windows needs .exe extension for executables
exeSuffix := ""
if runtime.GOOS == "windows" {
exeSuffix = ".exe"
}
mockNinja := filepath.Join(tempDir, "distninja"+exeSuffix)
// Create mock executable
if runtime.GOOS == "windows" {
// On Windows, create a simple batch file as our mock
if err := os.WriteFile(mockNinja, []byte("@echo mock"), 0755); err != nil {
t.Fatalf("Failed to create mock ninja: %v", err)
}
} else {
if err := os.WriteFile(mockNinja, []byte("#!/bin/sh\necho mock"), 0755); err != nil {
t.Fatalf("Failed to create mock ninja: %v", err)
}
}
// Add temp dir to PATH
if err := os.Setenv("PATH", tempDir); err != nil {
t.Fatalf("Failed to set PATH: %v", err)
}
err := checkNinjaExists()
if err != nil {
t.Errorf("Expected no error when ninja exists, got: %v", err)
}
// Test when ninja is not available
if err := os.Setenv("PATH", ""); err != nil {
t.Fatalf("Failed to clear PATH: %v", err)
}
err = checkNinjaExists()
if err == nil {
t.Error("Expected error when ninja doesn't exist, got nil")
}
}
func TestDetermineCompileType(t *testing.T) {
tests := []struct {
name string
args []string
expectedType string
expectedTgts []string
}{
{
name: "Full build with no args",
args: []string{},
expectedType: "full",
expectedTgts: []string{},
},
{
name: "mm command",
args: []string{"mm"},
expectedType: "module",
expectedTgts: []string{"current-dir"},
},
{
name: "mmm command with path",
args: []string{"mmm", "system/core"},
expectedType: "module",
expectedTgts: []string{"system/core"},
},
{
name: "m command with module",
args: []string{"m", "libutils"},
expectedType: "module",
expectedTgts: []string{"libutils"},
},
{
name: "build.sh with module",
args: []string{"./build.sh", "libutils"},
expectedType: "module",
expectedTgts: []string{"libutils"},
},
{
name: "env check command",
args: []string{"showcommands"},
expectedType: "env_check",
expectedTgts: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Mock current directory for mm test
if len(tt.args) > 0 && tt.args[0] == "mm" {
origDir, _ := os.Getwd()
defer func() {
if err := os.Chdir(origDir); err != nil {
t.Errorf("Failed to restore original directory: %v", err)
}
}()
tempDir := t.TempDir()
if err := os.Chdir(tempDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
tt.expectedTgts = []string{filepath.Base(tempDir)}
}
compileType, targets := determineCompileType(tt.args)
if compileType != tt.expectedType {
t.Errorf("Expected type %s, got %s", tt.expectedType, compileType)
}
if len(targets) != len(tt.expectedTgts) && tt.expectedType != "module" {
t.Errorf("Expected %d targets, got %d", len(tt.expectedTgts), len(targets))
}
})
}
}
func TestParseCompdbEntry(t *testing.T) {
tests := []struct {
name string
entry map[string]interface{}
expected CompilerCommandInfo
}{
{
name: "Basic clang compilation",
entry: map[string]interface{}{
"command": "clang -Iinclude -DFOO=1 -c foo.c -o foo.o",
"directory": "/src",
"file": "foo.c",
"output": "foo.o",
},
expected: CompilerCommandInfo{
Command: "clang -Iinclude -DFOO=1 -c foo.c -o foo.o",
CompilerType: "clang",
InputFiles: []string{"foo.c"},
OutputFile: "foo.o",
WorkingDir: "/src",
Includes: []string{"include"},
Defines: []string{"FOO=1"},
Flags: []string{"-c"},
},
},
{
name: "C++ compilation with multiple inputs",
entry: map[string]interface{}{
"command": "clang++ -Iinclude1 -Iinclude2 -DFOO -DBAR=2 -O2 -c foo.cpp bar.cpp -o out.o",
"directory": "/src",
"input_files": []interface{}{"foo.cpp", "bar.cpp"},
"target": "out.o",
},
expected: CompilerCommandInfo{
Command: "clang++ -Iinclude1 -Iinclude2 -DFOO -DBAR=2 -O2 -c foo.cpp bar.cpp -o out.o",
CompilerType: "clang++",
InputFiles: []string{"foo.cpp", "bar.cpp"},
OutputFile: "out.o",
WorkingDir: "/src",
Includes: []string{"include1", "include2"},
Defines: []string{"FOO", "BAR=2"},
Flags: []string{"-O2", "-c"},
},
},
{
name: "Java compilation",
entry: map[string]interface{}{
"command": "javac -d out -sourcepath src src/com/example/Foo.java",
"directory": "/java",
"file": "src/com/example/Foo.java",
},
expected: CompilerCommandInfo{
Command: "javac -d out -sourcepath src src/com/example/Foo.java",
CompilerType: "javac",
InputFiles: []string{"src/com/example/Foo.java"},
WorkingDir: "/java",
Flags: []string{"-d", "out", "-sourcepath", "src"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := parseCompdbEntry(tt.entry, "/default/dir")
if result.Command != tt.expected.Command {
t.Errorf("Command mismatch: expected %q, got %q", tt.expected.Command, result.Command)
}
if result.CompilerType != tt.expected.CompilerType {
t.Errorf("CompilerType mismatch: expected %q, got %q", tt.expected.CompilerType, result.CompilerType)
}
if !reflect.DeepEqual(result.InputFiles, tt.expected.InputFiles) {
t.Errorf("InputFiles mismatch: expected %v, got %v", tt.expected.InputFiles, result.InputFiles)
}
if result.OutputFile != tt.expected.OutputFile {
t.Errorf("OutputFile mismatch: expected %q, got %q", tt.expected.OutputFile, result.OutputFile)
}
if !reflect.DeepEqual(result.Includes, tt.expected.Includes) {
t.Errorf("Includes mismatch: expected %v, got %v", tt.expected.Includes, result.Includes)
}
if !reflect.DeepEqual(result.Defines, tt.expected.Defines) {
t.Errorf("Defines mismatch: expected %v, got %v", tt.expected.Defines, result.Defines)
}
if result.WorkingDir != tt.expected.WorkingDir {
t.Errorf("WorkingDir mismatch: expected %q, got %q", tt.expected.WorkingDir, result.WorkingDir)
}
})
}
}
func TestExtractModuleNameFromPath(t *testing.T) {
tests := []struct {
path string
expected string
}{
{
path: "out/target/product/generic/obj/SHARED_LIBRARIES/libutils_intermediates/utils.o",
expected: "libutils",
},
{
path: "out/soong/.intermediates/system/core/libutils/android_arm64_armv8-a_shared/libutils.so",
expected: "libutils",
},
{
path: "out/soong/.intermediates/packages/apps/Settings/Settings/android_common/Settings.apk",
expected: "Settings",
},
{
path: "out/host/linux-x86/obj/EXECUTABLES/aidl_intermediates/aidl",
expected: "aidl",
},
{
path: "out/soong/.intermediates/system/sepolicy/apex/com.android.sepolicy.cil/android_common/com.android.sepolicy.cil",
expected: "com.android.sepolicy.cil",
},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
result := extractModuleNameFromPath(tt.path)
if result != tt.expected {
t.Errorf("For path %q\nExpected %q, got %q", tt.path, tt.expected, result)
}
})
}
}
func TestWriteCompileCommands(t *testing.T) {
tempDir := t.TempDir()
commands := CommandDatabase{
Commands: []CompilerCommandInfo{
{
Command: "clang -c foo.c -o foo.o",
CompilerType: "clang",
InputFiles: []string{"foo.c"},
OutputFile: "foo.o",
},
},
}
err := writeCompileCommands(tempDir, commands)
if err != nil {
t.Fatalf("writeCompileCommands failed: %v", err)
}
// Verify file was created
outputFile := filepath.Join(tempDir, "compile_commands.json")
if _, err := os.Stat(outputFile); os.IsNotExist(err) {
t.Fatalf("Output file was not created")
}
// Verify content
content, err := os.ReadFile(outputFile)
if err != nil {
t.Fatalf("Failed to read output file: %v", err)
}
var parsed CommandDatabase
if err := json.Unmarshal(content, &parsed); err != nil {
t.Fatalf("Failed to parse output JSON: %v", err)
}
if len(parsed.Commands) != 1 {
t.Errorf("Expected 1 command, got %d", len(parsed.Commands))
}
if parsed.Commands[0].Command != commands.Commands[0].Command {
t.Errorf("Command mismatch in output file")
}
}
func TestExpandModuleTargets(t *testing.T) {
tests := []struct {
input []string
expected []string
}{
{
input: []string{"system/core/init"},
expected: []string{"system/core/init", "system_core_init", "init", "libinit"},
},
{
input: []string{"libutils"},
expected: []string{"libutils", "utils"},
},
{
input: []string{"Settings"},
expected: []string{"Settings", "libSettings"},
},
{
input: []string{"framework-res"},
expected: []string{"framework-res", "libframework-res"},
},
}
for _, tt := range tests {
t.Run(strings.Join(tt.input, ","), func(t *testing.T) {
result := expandModuleTargets(tt.input)
// Check that all expected items are present
for _, expected := range tt.expected {
found := false
for _, actual := range result {
if actual == expected {
found = true
break
}
}
if !found {
t.Errorf("Expected target %q not found in result", expected)
}
}
// Check no unexpected items are present
for _, actual := range result {
found := false
for _, expected := range tt.expected {
if actual == expected {
found = true
break
}
}
if !found {
t.Errorf("Unexpected target %q in result", actual)
}
}
})
}
}
func TestCreateTempNinjaFile(t *testing.T) {
tempDir := t.TempDir()
origNinja := filepath.Join(tempDir, "build.ninja")
err := os.WriteFile(origNinja, []byte("rule cc\n command = clang $in -o $out"), 0644)
if err != nil {
t.Fatalf("Failed to create test ninja file: %v", err)
}
// Get absolute path since the function may need it
absOrigNinja, err := filepath.Abs(origNinja)
if err != nil {
t.Fatalf("Failed to get absolute path: %v", err)
}
tempFile, err := createTempNinjaFile(absOrigNinja)
if err != nil {
t.Fatalf("createTempNinjaFile failed: %v", err)
}
defer func() {
if tempFile != "" {
if err := os.Remove(tempFile); err != nil {
t.Errorf("Failed to remove temp file: %v", err)
}
}
}()
// Verify temp file exists
if _, err := os.Stat(tempFile); os.IsNotExist(err) {
t.Fatalf("Temporary ninja file was not created")
}
// Verify content
content, err := os.ReadFile(tempFile)
if err != nil {
t.Fatalf("Failed to read temp file: %v", err)
}
contentStr := string(content)
t.Logf("File content: %s", contentStr)
// Check if key structural elements exist
if !strings.Contains(contentStr, "pool highmem_pool") {
t.Errorf("Expected content to contain 'pool highmem_pool'")
}
if !strings.Contains(contentStr, "depth = 1") {
t.Errorf("Expected content to contain 'depth = 1'")
}
// Checks if the file contains subninja and some part of the original file path
if !strings.Contains(contentStr, "subninja ") {
t.Errorf("Expected content to contain 'subninja' directive")
}
// Checks if the original filename appears in the content, not caring about the full path format
origFileName := filepath.Base(absOrigNinja)
if !strings.Contains(contentStr, origFileName) {
t.Errorf("Expected content to reference original file '%s'", origFileName)
}
}
func TestParseNinjaTargetsOutput(t *testing.T) {
tests := []struct {
name string
input string
expected []string
}{
{
name: "Simple targets",
input: `target1: phony
target2: phony
target3: CUSTOM`,
expected: []string{"target1", "target2", "target3"},
},
{
name: "Mixed formats",
input: `target1: CUSTOM || target2
target2: phony input1 input2 | order-only
target3: phony`,
expected: []string{"target1", "target2", "target3"},
},
{
name: "Windows line endings",
input: "target1: phony\r\ntarget2: CUSTOM\r\ntarget3: phony",
expected: []string{"target1", "target2", "target3"},
},
{
name: "Empty lines and comments",
input: `# Comment
target1: phony
target2: CUSTOM # Another comment
`,
expected: []string{"target1", "target2"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := bytes.NewBufferString(tt.input)
result := parseNinjaTargetsOutput(buf)
// Debug output to see which targets are actually returned
t.Logf("Result targets: %v", result)
// Check that the expected targets are in the results
expectedMap := make(map[string]bool)
for _, target := range tt.expected {
expectedMap[target] = true
}
// Note which additional targets are returned
for _, target := range result {
if !expectedMap[target] {
// Just log the extra target, don't accumulate it
t.Logf("Additional target found: %q", target)
}
}
// Check if any expected targets are missing
resultMap := make(map[string]bool)
for _, target := range result {
resultMap[target] = true
}
var missing bool
for _, target := range tt.expected {
if !resultMap[target] {
missing = true
t.Errorf("Expected target missing from result: %q", target)
}
}
// Report an error only if the expected target is missing
if missing {
t.Errorf("Some expected targets are missing from result")
}
})
}
}