blob: 7a1ee1703667bb17c7ad9cd46e531748559e5ced [file] [log] [blame]
Dan Willemsen9b587492017-07-10 22:13:00 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package build
16
17import (
18 "bytes"
19 "context"
Patrice Arruda13848222019-04-22 17:12:02 -070020 "fmt"
21 "io/ioutil"
22 "os"
23 "path/filepath"
Dan Willemsen9b587492017-07-10 22:13:00 -070024 "reflect"
25 "strings"
26 "testing"
27
28 "android/soong/ui/logger"
29)
30
31func testContext() Context {
32 return Context{&ContextImpl{
Dan Willemsenb82471a2018-05-17 16:37:09 -070033 Context: context.Background(),
34 Logger: logger.New(&bytes.Buffer{}),
Colin Cross097ed2a2019-06-08 21:48:58 -070035 Writer: &bytes.Buffer{},
Dan Willemsen9b587492017-07-10 22:13:00 -070036 }}
37}
38
39func TestConfigParseArgsJK(t *testing.T) {
40 ctx := testContext()
41
42 testCases := []struct {
43 args []string
44
45 parallel int
46 keepGoing int
47 remaining []string
48 }{
49 {nil, -1, -1, nil},
50
51 {[]string{"-j"}, -1, -1, nil},
52 {[]string{"-j1"}, 1, -1, nil},
53 {[]string{"-j1234"}, 1234, -1, nil},
54
55 {[]string{"-j", "1"}, 1, -1, nil},
56 {[]string{"-j", "1234"}, 1234, -1, nil},
57 {[]string{"-j", "1234", "abc"}, 1234, -1, []string{"abc"}},
58 {[]string{"-j", "abc"}, -1, -1, []string{"abc"}},
59 {[]string{"-j", "1abc"}, -1, -1, []string{"1abc"}},
60
61 {[]string{"-k"}, -1, 0, nil},
62 {[]string{"-k0"}, -1, 0, nil},
63 {[]string{"-k1"}, -1, 1, nil},
64 {[]string{"-k1234"}, -1, 1234, nil},
65
66 {[]string{"-k", "0"}, -1, 0, nil},
67 {[]string{"-k", "1"}, -1, 1, nil},
68 {[]string{"-k", "1234"}, -1, 1234, nil},
69 {[]string{"-k", "1234", "abc"}, -1, 1234, []string{"abc"}},
70 {[]string{"-k", "abc"}, -1, 0, []string{"abc"}},
71 {[]string{"-k", "1abc"}, -1, 0, []string{"1abc"}},
72
73 // TODO: These are supported in Make, should we support them?
74 //{[]string{"-kj"}, -1, 0},
75 //{[]string{"-kj8"}, 8, 0},
76
77 // -jk is not valid in Make
78 }
79
80 for _, tc := range testCases {
81 t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
82 defer logger.Recover(func(err error) {
83 t.Fatal(err)
84 })
85
86 c := &configImpl{
87 parallel: -1,
88 keepGoing: -1,
89 }
90 c.parseArgs(ctx, tc.args)
91
92 if c.parallel != tc.parallel {
93 t.Errorf("for %q, parallel:\nwant: %d\n got: %d\n",
94 strings.Join(tc.args, " "),
95 tc.parallel, c.parallel)
96 }
97 if c.keepGoing != tc.keepGoing {
98 t.Errorf("for %q, keep going:\nwant: %d\n got: %d\n",
99 strings.Join(tc.args, " "),
100 tc.keepGoing, c.keepGoing)
101 }
102 if !reflect.DeepEqual(c.arguments, tc.remaining) {
103 t.Errorf("for %q, remaining arguments:\nwant: %q\n got: %q\n",
104 strings.Join(tc.args, " "),
105 tc.remaining, c.arguments)
106 }
107 })
108 }
109}
Dan Willemsen091525e2017-07-11 14:17:50 -0700110
111func TestConfigParseArgsVars(t *testing.T) {
112 ctx := testContext()
113
114 testCases := []struct {
115 env []string
116 args []string
117
118 expectedEnv []string
119 remaining []string
120 }{
121 {},
122 {
123 env: []string{"A=bc"},
124
125 expectedEnv: []string{"A=bc"},
126 },
127 {
128 args: []string{"abc"},
129
130 remaining: []string{"abc"},
131 },
132
133 {
134 args: []string{"A=bc"},
135
136 expectedEnv: []string{"A=bc"},
137 },
138 {
139 env: []string{"A=a"},
140 args: []string{"A=bc"},
141
142 expectedEnv: []string{"A=bc"},
143 },
144
145 {
146 env: []string{"A=a"},
147 args: []string{"A=", "=b"},
148
149 expectedEnv: []string{"A="},
150 remaining: []string{"=b"},
151 },
152 }
153
154 for _, tc := range testCases {
155 t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
156 defer logger.Recover(func(err error) {
157 t.Fatal(err)
158 })
159
160 e := Environment(tc.env)
161 c := &configImpl{
162 environ: &e,
163 }
164 c.parseArgs(ctx, tc.args)
165
166 if !reflect.DeepEqual([]string(*c.environ), tc.expectedEnv) {
167 t.Errorf("for env=%q args=%q, environment:\nwant: %q\n got: %q\n",
168 tc.env, tc.args,
169 tc.expectedEnv, []string(*c.environ))
170 }
171 if !reflect.DeepEqual(c.arguments, tc.remaining) {
172 t.Errorf("for env=%q args=%q, remaining arguments:\nwant: %q\n got: %q\n",
173 tc.env, tc.args,
174 tc.remaining, c.arguments)
175 }
176 })
177 }
178}
Patrice Arruda13848222019-04-22 17:12:02 -0700179
180func TestConfigCheckTopDir(t *testing.T) {
181 ctx := testContext()
182 buildRootDir := filepath.Dir(srcDirFileCheck)
183 expectedErrStr := fmt.Sprintf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
184
185 tests := []struct {
186 // ********* Setup *********
187 // Test description.
188 description string
189
190 // ********* Action *********
191 // If set to true, the build root file is created.
192 rootBuildFile bool
193
194 // The current path where Soong is being executed.
195 path string
196
197 // ********* Validation *********
198 // Expecting error and validate the error string against expectedErrStr.
199 wantErr bool
200 }{{
201 description: "current directory is the root source tree",
202 rootBuildFile: true,
203 path: ".",
204 wantErr: false,
205 }, {
206 description: "one level deep in the source tree",
207 rootBuildFile: true,
208 path: "1",
209 wantErr: true,
210 }, {
211 description: "very deep in the source tree",
212 rootBuildFile: true,
213 path: "1/2/3/4/5/6/7/8/9/1/2/3/4/5/6/7/8/9/1/2/3/4/5/6/7/8/9/1/2/3/4/5/6/7",
214 wantErr: true,
215 }, {
216 description: "outside of source tree",
217 rootBuildFile: false,
218 path: "1/2/3/4/5",
219 wantErr: true,
220 }}
221
222 for _, tt := range tests {
223 t.Run(tt.description, func(t *testing.T) {
224 defer logger.Recover(func(err error) {
225 if !tt.wantErr {
226 t.Fatalf("Got unexpected error: %v", err)
227 }
228 if expectedErrStr != err.Error() {
229 t.Fatalf("expected %s, got %s", expectedErrStr, err.Error())
230 }
231 })
232
233 // Create the root source tree.
234 rootDir, err := ioutil.TempDir("", "")
235 if err != nil {
236 t.Fatal(err)
237 }
238 defer os.RemoveAll(rootDir)
239
240 // Create the build root file. This is to test if topDir returns an error if the build root
241 // file does not exist.
242 if tt.rootBuildFile {
243 dir := filepath.Join(rootDir, buildRootDir)
244 if err := os.MkdirAll(dir, 0755); err != nil {
245 t.Errorf("failed to create %s directory: %v", dir, err)
246 }
247 f := filepath.Join(rootDir, srcDirFileCheck)
248 if err := ioutil.WriteFile(f, []byte{}, 0644); err != nil {
249 t.Errorf("failed to create file %s: %v", f, err)
250 }
251 }
252
253 // Next block of code is to set the current directory.
254 dir := rootDir
255 if tt.path != "" {
256 dir = filepath.Join(dir, tt.path)
257 if err := os.MkdirAll(dir, 0755); err != nil {
258 t.Errorf("failed to create %s directory: %v", dir, err)
259 }
260 }
261 curDir, err := os.Getwd()
262 if err != nil {
263 t.Fatalf("failed to get the current directory: %v", err)
264 }
265 defer func() { os.Chdir(curDir) }()
266
267 if err := os.Chdir(dir); err != nil {
268 t.Fatalf("failed to change directory to %s: %v", dir, err)
269 }
270
271 checkTopDir(ctx)
272 })
273 }
274}
275
276func TestConfigConvertToTarget(t *testing.T) {
277 tests := []struct {
278 // ********* Setup *********
279 // Test description.
280 description string
281
282 // ********* Action *********
283 // The current directory where Soong is being executed.
284 dir string
285
286 // The current prefix string to be pre-appended to the target.
287 prefix string
288
289 // ********* Validation *********
290 // The expected target to be invoked in ninja.
291 expectedTarget string
292 }{{
293 description: "one level directory in source tree",
294 dir: "test1",
295 prefix: "MODULES-IN-",
296 expectedTarget: "MODULES-IN-test1",
297 }, {
298 description: "multiple level directories in source tree",
299 dir: "test1/test2/test3/test4",
300 prefix: "GET-INSTALL-PATH-IN-",
301 expectedTarget: "GET-INSTALL-PATH-IN-test1-test2-test3-test4",
302 }}
303 for _, tt := range tests {
304 t.Run(tt.description, func(t *testing.T) {
305 target := convertToTarget(tt.dir, tt.prefix)
306 if target != tt.expectedTarget {
307 t.Errorf("expected %s, got %s for target", tt.expectedTarget, target)
308 }
309 })
310 }
311}
312
313func setTop(t *testing.T, dir string) func() {
314 curDir, err := os.Getwd()
315 if err != nil {
316 t.Fatalf("failed to get current directory: %v", err)
317 }
318 if err := os.Chdir(dir); err != nil {
319 t.Fatalf("failed to change directory to top dir %s: %v", dir, err)
320 }
321 return func() { os.Chdir(curDir) }
322}
323
324func createBuildFiles(t *testing.T, topDir string, buildFiles []string) {
325 for _, buildFile := range buildFiles {
326 buildFile = filepath.Join(topDir, buildFile)
327 if err := ioutil.WriteFile(buildFile, []byte{}, 0644); err != nil {
328 t.Errorf("failed to create file %s: %v", buildFile, err)
329 }
330 }
331}
332
333func createDirectories(t *testing.T, topDir string, dirs []string) {
334 for _, dir := range dirs {
335 dir = filepath.Join(topDir, dir)
336 if err := os.MkdirAll(dir, 0755); err != nil {
337 t.Errorf("failed to create %s directory: %v", dir, err)
338 }
339 }
340}
341
342func TestConfigGetTargets(t *testing.T) {
343 ctx := testContext()
344 tests := []struct {
345 // ********* Setup *********
346 // Test description.
347 description string
348
349 // Directories that exist in the source tree.
350 dirsInTrees []string
351
352 // Build files that exists in the source tree.
353 buildFiles []string
354
355 // ********* Action *********
356 // Directories passed in to soong_ui.
357 dirs []string
358
359 // Current directory that the user executed the build action command.
360 curDir string
361
362 // ********* Validation *********
363 // Expected targets from the function.
364 expectedTargets []string
365
366 // Expected build from the build system.
367 expectedBuildFiles []string
368
369 // Expecting error from running test case.
370 errStr string
371 }{{
372 description: "one target dir specified",
373 dirsInTrees: []string{"0/1/2/3"},
374 buildFiles: []string{"0/1/2/3/Android.bp"},
375 dirs: []string{"1/2/3"},
376 curDir: "0",
377 expectedTargets: []string{"MODULES-IN-0-1-2-3"},
378 expectedBuildFiles: []string{"0/1/2/3/Android.mk"},
379 }, {
380 description: "one target dir specified, build file does not exist",
381 dirsInTrees: []string{"0/1/2/3"},
382 buildFiles: []string{},
383 dirs: []string{"1/2/3"},
384 curDir: "0",
385 errStr: "Build file not found for 0/1/2/3 directory",
386 }, {
387 description: "one target dir specified, invalid targets specified",
388 dirsInTrees: []string{"0/1/2/3"},
389 buildFiles: []string{},
390 dirs: []string{"1/2/3:t1:t2"},
391 curDir: "0",
392 errStr: "1/2/3:t1:t2 not in proper directory:target1,target2,... format (\":\" was specified more than once)",
393 }, {
394 description: "one target dir specified, no targets specified but has colon",
395 dirsInTrees: []string{"0/1/2/3"},
396 buildFiles: []string{"0/1/2/3/Android.bp"},
397 dirs: []string{"1/2/3:"},
398 curDir: "0",
399 expectedTargets: []string{"MODULES-IN-0-1-2-3"},
400 expectedBuildFiles: []string{"0/1/2/3/Android.mk"},
401 }, {
402 description: "one target dir specified, two targets specified",
403 dirsInTrees: []string{"0/1/2/3"},
404 buildFiles: []string{"0/1/2/3/Android.bp"},
405 dirs: []string{"1/2/3:t1,t2"},
406 curDir: "0",
407 expectedTargets: []string{"t1", "t2"},
408 expectedBuildFiles: []string{"0/1/2/3/Android.mk"},
409 }, {
410 description: "one target dir specified, no targets and has a comma",
411 dirsInTrees: []string{"0/1/2/3"},
412 buildFiles: []string{"0/1/2/3/Android.bp"},
413 dirs: []string{"1/2/3:,"},
414 curDir: "0",
415 errStr: "0/1/2/3 not in proper directory:target1,target2,... format",
416 }, {
417 description: "one target dir specified, improper targets defined",
418 dirsInTrees: []string{"0/1/2/3"},
419 buildFiles: []string{"0/1/2/3/Android.bp"},
420 dirs: []string{"1/2/3:,t1"},
421 curDir: "0",
422 errStr: "0/1/2/3 not in proper directory:target1,target2,... format",
423 }, {
424 description: "one target dir specified, blank target",
425 dirsInTrees: []string{"0/1/2/3"},
426 buildFiles: []string{"0/1/2/3/Android.bp"},
427 dirs: []string{"1/2/3:t1,"},
428 curDir: "0",
429 errStr: "0/1/2/3 not in proper directory:target1,target2,... format",
430 }, {
431 description: "one target dir specified, many targets specified",
432 dirsInTrees: []string{"0/1/2/3"},
433 buildFiles: []string{"0/1/2/3/Android.bp"},
434 dirs: []string{"1/2/3:t1,t2,t3,t4,t5,t6,t7,t8,t9,t10"},
435 curDir: "0",
436 expectedTargets: []string{"t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "t10"},
437 expectedBuildFiles: []string{"0/1/2/3/Android.mk"},
438 }, {
439 description: "one target dir specified, one target specified, build file does not exist",
440 dirsInTrees: []string{"0/1/2/3"},
441 buildFiles: []string{},
442 dirs: []string{"1/2/3:t1"},
443 curDir: "0",
444 errStr: "Build file not found for 0/1/2/3 directory",
445 }, {
446 description: "one target dir specified, one target specified, build file not in target dir",
447 dirsInTrees: []string{"0/1/2/3"},
448 buildFiles: []string{"0/1/2/Android.mk"},
449 dirs: []string{"1/2/3:t1"},
450 curDir: "0",
451 errStr: "Couldn't locate a build file from 0/1/2/3 directory",
452 }, {
453 description: "one target dir specified, build file not in target dir",
454 dirsInTrees: []string{"0/1/2/3"},
455 buildFiles: []string{"0/1/2/Android.mk"},
456 dirs: []string{"1/2/3"},
457 curDir: "0",
458 expectedTargets: []string{"MODULES-IN-0-1-2"},
459 expectedBuildFiles: []string{"0/1/2/Android.mk"},
460 }, {
461 description: "multiple targets dir specified, targets specified",
462 dirsInTrees: []string{"0/1/2/3", "0/3/4"},
463 buildFiles: []string{"0/1/2/3/Android.bp", "0/3/4/Android.mk"},
464 dirs: []string{"1/2/3:t1,t2", "3/4:t3,t4,t5"},
465 curDir: "0",
466 expectedTargets: []string{"t1", "t2", "t3", "t4", "t5"},
467 expectedBuildFiles: []string{"0/1/2/3/Android.mk", "0/3/4/Android.mk"},
468 }, {
469 description: "multiple targets dir specified, one directory has targets specified",
470 dirsInTrees: []string{"0/1/2/3", "0/3/4"},
471 buildFiles: []string{"0/1/2/3/Android.bp", "0/3/4/Android.mk"},
472 dirs: []string{"1/2/3:t1,t2", "3/4"},
473 curDir: "0",
474 expectedTargets: []string{"t1", "t2", "MODULES-IN-0-3-4"},
475 expectedBuildFiles: []string{"0/1/2/3/Android.mk", "0/3/4/Android.mk"},
476 }, {
477 description: "two dirs specified, only one dir exist",
478 dirsInTrees: []string{"0/1/2/3"},
479 buildFiles: []string{"0/1/2/3/Android.mk"},
480 dirs: []string{"1/2/3:t1", "3/4"},
481 curDir: "0",
482 errStr: "couldn't find directory 0/3/4",
483 }, {
484 description: "multiple targets dirs specified at root source tree",
485 dirsInTrees: []string{"0/1/2/3", "0/3/4"},
486 buildFiles: []string{"0/1/2/3/Android.bp", "0/3/4/Android.mk"},
487 dirs: []string{"0/1/2/3:t1,t2", "0/3/4"},
488 curDir: ".",
489 expectedTargets: []string{"t1", "t2", "MODULES-IN-0-3-4"},
490 expectedBuildFiles: []string{"0/1/2/3/Android.mk", "0/3/4/Android.mk"},
491 }, {
492 description: "no directories specified",
493 dirsInTrees: []string{"0/1/2/3", "0/3/4"},
494 buildFiles: []string{"0/1/2/3/Android.bp", "0/3/4/Android.mk"},
495 dirs: []string{},
496 curDir: ".",
497 }}
498 for _, tt := range tests {
499 t.Run(tt.description, func(t *testing.T) {
500 defer logger.Recover(func(err error) {
501 if tt.errStr == "" {
502 t.Fatalf("Got unexpected error: %v", err)
503 }
504 if tt.errStr != err.Error() {
505 t.Errorf("expected %s, got %s", tt.errStr, err.Error())
506 }
507 })
508
509 // Create the root source tree.
510 topDir, err := ioutil.TempDir("", "")
511 if err != nil {
512 t.Fatalf("failed to create temp dir: %v", err)
513 }
514 defer os.RemoveAll(topDir)
515
516 createDirectories(t, topDir, tt.dirsInTrees)
517 createBuildFiles(t, topDir, tt.buildFiles)
518 r := setTop(t, topDir)
519 defer r()
520
521 targets, buildFiles := getTargetsFromDirs(ctx, tt.curDir, tt.dirs, "MODULES-IN-")
522 if !reflect.DeepEqual(targets, tt.expectedTargets) {
523 t.Errorf("expected %v, got %v for targets", tt.expectedTargets, targets)
524 }
525 if !reflect.DeepEqual(buildFiles, tt.expectedBuildFiles) {
526 t.Errorf("expected %v, got %v for build files", tt.expectedBuildFiles, buildFiles)
527 }
528
529 // If the execution reached here and there was an expected error code, the unit test case failed.
530 if tt.errStr != "" {
531 t.Errorf("expecting error %s", tt.errStr)
532 }
533 })
534 }
535}
536
537func TestConfigFindBuildFile(t *testing.T) {
538 ctx := testContext()
539
540 tests := []struct {
541 // ********* Setup *********
542 // Test description.
543 description string
544
545 // Array of build files to create in dir.
546 buildFiles []string
547
548 // ********* Action *********
549 // Directory to create, also the base directory is where findBuildFile is invoked.
550 dir string
551
552 // ********* Validation *********
553 // Expected build file path to find.
554 expectedBuildFile string
555 }{{
556 description: "build file exists at leaf directory",
557 buildFiles: []string{"1/2/3/Android.bp"},
558 dir: "1/2/3",
559 expectedBuildFile: "1/2/3/Android.mk",
560 }, {
561 description: "build file exists in all directory paths",
562 buildFiles: []string{"1/Android.mk", "1/2/Android.mk", "1/2/3/Android.mk"},
563 dir: "1/2/3",
564 expectedBuildFile: "1/2/3/Android.mk",
565 }, {
566 description: "build file does not exist in all directory paths",
567 buildFiles: []string{},
568 dir: "1/2/3",
569 expectedBuildFile: "",
570 }, {
571 description: "build file exists only at top directory",
572 buildFiles: []string{"Android.bp"},
573 dir: "1/2/3",
574 expectedBuildFile: "",
575 }, {
576 description: "build file exist in a subdirectory",
577 buildFiles: []string{"1/2/Android.bp"},
578 dir: "1/2/3",
579 expectedBuildFile: "1/2/Android.mk",
580 }, {
581 description: "build file exists in a subdirectory",
582 buildFiles: []string{"1/Android.mk"},
583 dir: "1/2/3",
584 expectedBuildFile: "1/Android.mk",
585 }, {
586 description: "top directory",
587 buildFiles: []string{"Android.bp"},
588 dir: ".",
589 expectedBuildFile: "",
590 }}
591
592 for _, tt := range tests {
593 t.Run(tt.description, func(t *testing.T) {
594 defer logger.Recover(func(err error) {
595 t.Fatalf("Got unexpected error: %v", err)
596 })
597
598 topDir, err := ioutil.TempDir("", "")
599 if err != nil {
600 t.Fatalf("failed to create temp dir: %v", err)
601 }
602 defer os.RemoveAll(topDir)
603
604 if tt.dir != "" {
605 createDirectories(t, topDir, []string{tt.dir})
606 }
607
608 createBuildFiles(t, topDir, tt.buildFiles)
609
610 curDir, err := os.Getwd()
611 if err != nil {
612 t.Fatalf("Could not get working directory: %v", err)
613 }
614 defer func() { os.Chdir(curDir) }()
615 if err := os.Chdir(topDir); err != nil {
616 t.Fatalf("Could not change top dir to %s: %v", topDir, err)
617 }
618
619 buildFile := findBuildFile(ctx, tt.dir)
620 if buildFile != tt.expectedBuildFile {
621 t.Errorf("expected %q, got %q for build file", tt.expectedBuildFile, buildFile)
622 }
623 })
624 }
625}
626
627func TestConfigSplitArgs(t *testing.T) {
628 tests := []struct {
629 // ********* Setup *********
630 // Test description.
631 description string
632
633 // ********* Action *********
634 // Arguments passed in to soong_ui.
635 args []string
636
637 // ********* Validation *********
638 // Expected newArgs list after extracting the directories.
639 expectedNewArgs []string
640
641 // Expected directories
642 expectedDirs []string
643 }{{
644 description: "flags but no directories specified",
645 args: []string{"showcommands", "-j", "-k"},
646 expectedNewArgs: []string{"showcommands", "-j", "-k"},
647 expectedDirs: []string{},
648 }, {
649 description: "flags and one directory specified",
650 args: []string{"snod", "-j", "dir:target1,target2"},
651 expectedNewArgs: []string{"snod", "-j"},
652 expectedDirs: []string{"dir:target1,target2"},
653 }, {
654 description: "flags and directories specified",
655 args: []string{"dist", "-k", "dir1", "dir2:target1,target2"},
656 expectedNewArgs: []string{"dist", "-k"},
657 expectedDirs: []string{"dir1", "dir2:target1,target2"},
658 }, {
659 description: "only directories specified",
660 args: []string{"dir1", "dir2", "dir3:target1,target2"},
661 expectedNewArgs: []string{},
662 expectedDirs: []string{"dir1", "dir2", "dir3:target1,target2"},
663 }}
664 for _, tt := range tests {
665 t.Run(tt.description, func(t *testing.T) {
666 args, dirs := splitArgs(tt.args)
667 if !reflect.DeepEqual(tt.expectedNewArgs, args) {
668 t.Errorf("expected %v, got %v for arguments", tt.expectedNewArgs, args)
669 }
670 if !reflect.DeepEqual(tt.expectedDirs, dirs) {
671 t.Errorf("expected %v, got %v for directories", tt.expectedDirs, dirs)
672 }
673 })
674 }
675}
676
677type envVar struct {
678 name string
679 value string
680}
681
682type buildActionTestCase struct {
683 // ********* Setup *********
684 // Test description.
685 description string
686
687 // Directories that exist in the source tree.
688 dirsInTrees []string
689
690 // Build files that exists in the source tree.
691 buildFiles []string
692
Patrice Arrudababa9a92019-07-03 10:47:34 -0700693 // Create root symlink that points to topDir.
694 rootSymlink bool
695
Patrice Arruda13848222019-04-22 17:12:02 -0700696 // ********* Action *********
697 // Arguments passed in to soong_ui.
698 args []string
699
700 // Directory where the build action was invoked.
701 curDir string
702
703 // WITH_TIDY_ONLY environment variable specified.
704 tidyOnly string
705
706 // ********* Validation *********
707 // Expected arguments to be in Config instance.
708 expectedArgs []string
709
710 // Expected environment variables to be set.
711 expectedEnvVars []envVar
712}
713
714func testGetConfigArgs(t *testing.T, tt buildActionTestCase, action BuildAction, buildDependencies bool) {
715 ctx := testContext()
716
717 // Environment variables to set it to blank on every test case run.
718 resetEnvVars := []string{
719 "ONE_SHOT_MAKEFILE",
720 "WITH_TIDY_ONLY",
721 }
722
723 for _, name := range resetEnvVars {
724 if err := os.Unsetenv(name); err != nil {
725 t.Fatalf("failed to unset environment variable %s: %v", name, err)
726 }
727 }
728 if tt.tidyOnly != "" {
729 if err := os.Setenv("WITH_TIDY_ONLY", tt.tidyOnly); err != nil {
730 t.Errorf("failed to set WITH_TIDY_ONLY to %s: %v", tt.tidyOnly, err)
731 }
732 }
733
734 // Create the root source tree.
735 topDir, err := ioutil.TempDir("", "")
736 if err != nil {
737 t.Fatalf("failed to create temp dir: %v", err)
738 }
739 defer os.RemoveAll(topDir)
740
741 createDirectories(t, topDir, tt.dirsInTrees)
742 createBuildFiles(t, topDir, tt.buildFiles)
743
Patrice Arrudababa9a92019-07-03 10:47:34 -0700744 if tt.rootSymlink {
745 // Create a secondary root source tree which points to the true root source tree.
746 symlinkTopDir, err := ioutil.TempDir("", "")
747 if err != nil {
748 t.Fatalf("failed to create symlink temp dir: %v", err)
749 }
750 defer os.RemoveAll(symlinkTopDir)
751
752 symlinkTopDir = filepath.Join(symlinkTopDir, "root")
753 err = os.Symlink(topDir, symlinkTopDir)
754 if err != nil {
755 t.Fatalf("failed to create symlink: %v", err)
756 }
757 topDir = symlinkTopDir
758 }
759
Patrice Arruda13848222019-04-22 17:12:02 -0700760 r := setTop(t, topDir)
761 defer r()
762
763 // The next block is to create the root build file.
764 rootBuildFileDir := filepath.Dir(srcDirFileCheck)
765 if err := os.MkdirAll(rootBuildFileDir, 0755); err != nil {
766 t.Fatalf("Failed to create %s directory: %v", rootBuildFileDir, err)
767 }
768
769 if err := ioutil.WriteFile(srcDirFileCheck, []byte{}, 0644); err != nil {
770 t.Fatalf("failed to create %s file: %v", srcDirFileCheck, err)
771 }
772
773 args := getConfigArgs(action, tt.curDir, buildDependencies, ctx, tt.args)
774 if !reflect.DeepEqual(tt.expectedArgs, args) {
775 t.Fatalf("expected %v, got %v for config arguments", tt.expectedArgs, args)
776 }
777
778 for _, env := range tt.expectedEnvVars {
779 if val := os.Getenv(env.name); val != env.value {
780 t.Errorf("expecting %s, got %s for environment variable %s", env.value, val, env.name)
781 }
782 }
783}
784
Patrice Arruda39282062019-06-20 16:35:12 -0700785func TestGetConfigArgsBuildModules(t *testing.T) {
786 tests := []buildActionTestCase{{
787 description: "normal execution from the root source tree directory",
788 dirsInTrees: []string{"0/1/2", "0/2", "0/3"},
789 buildFiles: []string{"0/1/2/Android.mk", "0/2/Android.bp", "0/3/Android.mk"},
790 args: []string{"-j", "fake_module", "fake_module2"},
791 curDir: ".",
792 tidyOnly: "",
793 expectedArgs: []string{"-j", "fake_module", "fake_module2"},
794 expectedEnvVars: []envVar{},
795 }, {
796 description: "normal execution in deep directory",
797 dirsInTrees: []string{"0/1/2", "0/2", "0/3", "1/2/3/4/5/6/7/8/9/1/2/3/4/5/6"},
798 buildFiles: []string{"0/1/2/Android.mk", "0/2/Android.bp", "1/2/3/4/5/6/7/8/9/1/2/3/4/5/6/Android.mk"},
799 args: []string{"-j", "fake_module", "fake_module2", "-k"},
800 curDir: "1/2/3/4/5/6/7/8/9",
801 tidyOnly: "",
802 expectedArgs: []string{"-j", "fake_module", "fake_module2", "-k"},
803 expectedEnvVars: []envVar{},
804 }, {
805 description: "normal execution in deep directory, no targets",
806 dirsInTrees: []string{"0/1/2", "0/2", "0/3", "1/2/3/4/5/6/7/8/9/1/2/3/4/5/6"},
807 buildFiles: []string{"0/1/2/Android.mk", "0/2/Android.bp", "1/2/3/4/5/6/7/8/9/1/2/3/4/5/6/Android.mk"},
808 args: []string{"-j", "-k"},
809 curDir: "1/2/3/4/5/6/7/8/9",
810 tidyOnly: "",
811 expectedArgs: []string{"-j", "-k"},
812 expectedEnvVars: []envVar{},
813 }, {
814 description: "normal execution in root source tree, no args",
815 dirsInTrees: []string{"0/1/2", "0/2", "0/3"},
816 buildFiles: []string{"0/1/2/Android.mk", "0/2/Android.bp"},
817 args: []string{},
Patrice Arrudababa9a92019-07-03 10:47:34 -0700818 curDir: "0/2",
819 tidyOnly: "",
820 expectedArgs: []string{},
821 expectedEnvVars: []envVar{},
822 }, {
823 description: "normal execution in symlink root source tree, no args",
824 dirsInTrees: []string{"0/1/2", "0/2", "0/3"},
825 buildFiles: []string{"0/1/2/Android.mk", "0/2/Android.bp"},
826 rootSymlink: true,
827 args: []string{},
828 curDir: "0/2",
Patrice Arruda39282062019-06-20 16:35:12 -0700829 tidyOnly: "",
830 expectedArgs: []string{},
831 expectedEnvVars: []envVar{},
832 }}
833 for _, tt := range tests {
834 t.Run("build action BUILD_MODULES with dependencies, "+tt.description, func(t *testing.T) {
835 testGetConfigArgs(t, tt, BUILD_MODULES, true)
836 })
837 }
838}
839
Patrice Arruda13848222019-04-22 17:12:02 -0700840// TODO: Remove this test case once mm shell build command has been deprecated.
841func TestGetConfigArgsBuildModulesInDirecotoryNoDeps(t *testing.T) {
842 tests := []buildActionTestCase{{
843 description: "normal execution in a directory",
844 dirsInTrees: []string{"0/1/2"},
845 buildFiles: []string{"0/1/2/Android.mk"},
846 args: []string{"-j", "-k", "showcommands", "fake-module"},
847 curDir: "0/1/2",
848 tidyOnly: "",
849 expectedArgs: []string{"-j", "-k", "showcommands", "fake-module", "MODULES-IN-0-1-2"},
850 expectedEnvVars: []envVar{
851 envVar{
852 name: "ONE_SHOT_MAKEFILE",
853 value: "0/1/2/Android.mk"}},
854 }, {
855 description: "makefile in parent directory",
856 dirsInTrees: []string{"0/1/2"},
857 buildFiles: []string{"0/1/Android.mk"},
858 args: []string{},
859 curDir: "0/1/2",
860 tidyOnly: "",
861 expectedArgs: []string{"MODULES-IN-0-1"},
862 expectedEnvVars: []envVar{
863 envVar{
864 name: "ONE_SHOT_MAKEFILE",
865 value: "0/1/Android.mk"}},
866 }, {
867 description: "build file not found",
868 dirsInTrees: []string{"0/1/2"},
869 buildFiles: []string{},
870 args: []string{},
871 curDir: "0/1/2",
872 tidyOnly: "",
873 expectedArgs: []string{"MODULES-IN-0-1-2"},
874 expectedEnvVars: []envVar{
875 envVar{
876 name: "ONE_SHOT_MAKEFILE",
877 value: "0/1/2/Android.mk"}},
878 }, {
879 description: "build action executed at root directory",
880 dirsInTrees: []string{},
881 buildFiles: []string{},
882 args: []string{},
883 curDir: ".",
884 tidyOnly: "",
885 expectedArgs: []string{},
886 expectedEnvVars: []envVar{
887 envVar{
888 name: "ONE_SHOT_MAKEFILE",
889 value: ""}},
890 }, {
891 description: "GET-INSTALL-PATH specified,",
892 dirsInTrees: []string{"0/1/2"},
893 buildFiles: []string{"0/1/Android.mk"},
894 args: []string{"GET-INSTALL-PATH"},
895 curDir: "0/1/2",
896 tidyOnly: "",
897 expectedArgs: []string{"GET-INSTALL-PATH-IN-0-1"},
898 expectedEnvVars: []envVar{
899 envVar{
900 name: "ONE_SHOT_MAKEFILE",
901 value: "0/1/Android.mk"}},
902 }, {
903 description: "tidy only environment variable specified,",
904 dirsInTrees: []string{"0/1/2"},
905 buildFiles: []string{"0/1/Android.mk"},
906 args: []string{"GET-INSTALL-PATH"},
907 curDir: "0/1/2",
908 tidyOnly: "true",
909 expectedArgs: []string{"tidy_only"},
910 expectedEnvVars: []envVar{
911 envVar{
912 name: "ONE_SHOT_MAKEFILE",
913 value: "0/1/Android.mk"}},
914 }}
915 for _, tt := range tests {
916 t.Run("build action BUILD_MODULES_IN_DIR without their dependencies, "+tt.description, func(t *testing.T) {
917 testGetConfigArgs(t, tt, BUILD_MODULES_IN_A_DIRECTORY, false)
918 })
919 }
920}
921
922func TestGetConfigArgsBuildModulesInDirectory(t *testing.T) {
923 tests := []buildActionTestCase{{
924 description: "normal execution in a directory",
925 dirsInTrees: []string{"0/1/2"},
926 buildFiles: []string{"0/1/2/Android.mk"},
927 args: []string{"fake-module"},
928 curDir: "0/1/2",
929 tidyOnly: "",
930 expectedArgs: []string{"fake-module", "MODULES-IN-0-1-2"},
931 expectedEnvVars: []envVar{},
932 }, {
933 description: "build file in parent directory",
934 dirsInTrees: []string{"0/1/2"},
935 buildFiles: []string{"0/1/Android.mk"},
936 args: []string{},
937 curDir: "0/1/2",
938 tidyOnly: "",
939 expectedArgs: []string{"MODULES-IN-0-1"},
940 expectedEnvVars: []envVar{},
941 },
942 {
943 description: "build file in parent directory, multiple module names passed in",
944 dirsInTrees: []string{"0/1/2"},
945 buildFiles: []string{"0/1/Android.mk"},
946 args: []string{"fake-module1", "fake-module2", "fake-module3"},
947 curDir: "0/1/2",
948 tidyOnly: "",
949 expectedArgs: []string{"fake-module1", "fake-module2", "fake-module3", "MODULES-IN-0-1"},
950 expectedEnvVars: []envVar{},
951 }, {
952 description: "build file in 2nd level parent directory",
953 dirsInTrees: []string{"0/1/2"},
954 buildFiles: []string{"0/Android.bp"},
955 args: []string{},
956 curDir: "0/1/2",
957 tidyOnly: "",
958 expectedArgs: []string{"MODULES-IN-0"},
959 expectedEnvVars: []envVar{},
960 }, {
961 description: "build action executed at root directory",
962 dirsInTrees: []string{},
963 buildFiles: []string{},
Patrice Arrudababa9a92019-07-03 10:47:34 -0700964 rootSymlink: false,
965 args: []string{},
966 curDir: ".",
967 tidyOnly: "",
968 expectedArgs: []string{},
969 expectedEnvVars: []envVar{},
970 }, {
971 description: "build action executed at root directory in symlink",
972 dirsInTrees: []string{},
973 buildFiles: []string{},
974 rootSymlink: true,
Patrice Arruda13848222019-04-22 17:12:02 -0700975 args: []string{},
976 curDir: ".",
977 tidyOnly: "",
978 expectedArgs: []string{},
979 expectedEnvVars: []envVar{},
980 }, {
981 description: "build file not found - no error is expected to return",
982 dirsInTrees: []string{"0/1/2"},
983 buildFiles: []string{},
984 args: []string{},
985 curDir: "0/1/2",
986 tidyOnly: "",
987 expectedArgs: []string{"MODULES-IN-0-1-2"},
988 expectedEnvVars: []envVar{},
989 }, {
990 description: "GET-INSTALL-PATH specified,",
991 dirsInTrees: []string{"0/1/2"},
992 buildFiles: []string{"0/1/Android.mk"},
993 args: []string{"GET-INSTALL-PATH", "-j", "-k", "GET-INSTALL-PATH"},
994 curDir: "0/1/2",
995 tidyOnly: "",
996 expectedArgs: []string{"-j", "-k", "GET-INSTALL-PATH-IN-0-1"},
997 expectedEnvVars: []envVar{},
998 }, {
999 description: "tidy only environment variable specified,",
1000 dirsInTrees: []string{"0/1/2"},
1001 buildFiles: []string{"0/1/Android.mk"},
1002 args: []string{"GET-INSTALL-PATH"},
1003 curDir: "0/1/2",
1004 tidyOnly: "true",
1005 expectedArgs: []string{"tidy_only"},
1006 expectedEnvVars: []envVar{},
1007 }, {
1008 description: "normal execution in root directory with args",
1009 dirsInTrees: []string{},
1010 buildFiles: []string{},
1011 args: []string{"-j", "-k", "fake_module"},
1012 curDir: "",
1013 tidyOnly: "",
1014 expectedArgs: []string{"-j", "-k", "fake_module"},
1015 expectedEnvVars: []envVar{},
1016 }}
1017 for _, tt := range tests {
1018 t.Run("build action BUILD_MODULES_IN_DIR, "+tt.description, func(t *testing.T) {
1019 testGetConfigArgs(t, tt, BUILD_MODULES_IN_A_DIRECTORY, true)
1020 })
1021 }
1022}
1023
1024// TODO: Remove this test case once mmm shell build command has been deprecated.
1025func TestGetConfigArgsBuildModulesInDirectoriesNoDeps(t *testing.T) {
1026 tests := []buildActionTestCase{{
1027 description: "normal execution in a directory",
1028 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1029 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1030 args: []string{"3.1/:t1,t2", "3.2/:t3,t4", "3.3/:t5,t6"},
1031 curDir: "0/1/2",
1032 tidyOnly: "",
1033 expectedArgs: []string{"t1", "t2", "t3", "t4", "t5", "t6"},
1034 expectedEnvVars: []envVar{
1035 envVar{
1036 name: "ONE_SHOT_MAKEFILE",
1037 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
1038 }, {
1039 description: "GET-INSTALL-PATH specified",
1040 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1041 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1042 args: []string{"GET-INSTALL-PATH", "3.1/", "3.2/", "3.3/:t6"},
1043 curDir: "0/1/2",
1044 tidyOnly: "",
1045 expectedArgs: []string{"GET-INSTALL-PATH-IN-0-1-2-3.1", "GET-INSTALL-PATH-IN-0-1-2-3.2", "t6"},
1046 expectedEnvVars: []envVar{
1047 envVar{
1048 name: "ONE_SHOT_MAKEFILE",
1049 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
1050 }, {
1051 description: "tidy only environment variable specified",
1052 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1053 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1054 args: []string{"GET-INSTALL-PATH", "3.1/", "3.2/", "3.3/:t6"},
1055 curDir: "0/1/2",
1056 tidyOnly: "1",
1057 expectedArgs: []string{"tidy_only"},
1058 expectedEnvVars: []envVar{
1059 envVar{
1060 name: "ONE_SHOT_MAKEFILE",
1061 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
1062 }, {
1063 description: "normal execution from top dir directory",
1064 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1065 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1066 args: []string{"0/1/2/3.1", "0/1/2/3.2/:t3,t4", "0/1/2/3.3/:t5,t6"},
1067 curDir: ".",
1068 tidyOnly: "",
1069 expectedArgs: []string{"MODULES-IN-0-1-2-3.1", "t3", "t4", "t5", "t6"},
1070 expectedEnvVars: []envVar{
1071 envVar{
1072 name: "ONE_SHOT_MAKEFILE",
1073 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
1074 }}
1075 for _, tt := range tests {
1076 t.Run("build action BUILD_MODULES_IN_DIRS_NO_DEPS, "+tt.description, func(t *testing.T) {
1077 testGetConfigArgs(t, tt, BUILD_MODULES_IN_DIRECTORIES, false)
1078 })
1079 }
1080}
1081
1082func TestGetConfigArgsBuildModulesInDirectories(t *testing.T) {
1083 tests := []buildActionTestCase{{
1084 description: "normal execution in a directory",
1085 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1086 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1087 args: []string{"3.1/", "3.2/", "3.3/"},
1088 curDir: "0/1/2",
1089 tidyOnly: "",
1090 expectedArgs: []string{"MODULES-IN-0-1-2-3.1", "MODULES-IN-0-1-2-3.2", "MODULES-IN-0-1-2-3.3"},
1091 expectedEnvVars: []envVar{
1092 envVar{
1093 name: "ONE_SHOT_MAKEFILE",
1094 value: ""}},
1095 }, {
1096 description: "GET-INSTALL-PATH specified",
1097 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3"},
1098 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/Android.bp"},
1099 args: []string{"GET-INSTALL-PATH", "2/3.1/", "2/3.2", "3"},
1100 curDir: "0/1",
1101 tidyOnly: "",
1102 expectedArgs: []string{"GET-INSTALL-PATH-IN-0-1-2-3.1", "GET-INSTALL-PATH-IN-0-1-2-3.2", "GET-INSTALL-PATH-IN-0-1"},
1103 expectedEnvVars: []envVar{
1104 envVar{
1105 name: "ONE_SHOT_MAKEFILE",
1106 value: ""}},
1107 }, {
1108 description: "tidy only environment variable specified",
1109 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1110 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1111 args: []string{"GET-INSTALL-PATH", "3.1/", "3.2/", "3.3"},
1112 curDir: "0/1/2",
1113 tidyOnly: "1",
1114 expectedArgs: []string{"tidy_only"},
1115 expectedEnvVars: []envVar{
1116 envVar{
1117 name: "ONE_SHOT_MAKEFILE",
1118 value: ""}},
1119 }, {
1120 description: "normal execution from top dir directory",
1121 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3", "0/2"},
1122 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/3/Android.bp", "0/2/Android.bp"},
Patrice Arrudababa9a92019-07-03 10:47:34 -07001123 rootSymlink: false,
1124 args: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3", "0/2"},
1125 curDir: ".",
1126 tidyOnly: "",
1127 expectedArgs: []string{"MODULES-IN-0-1-2-3.1", "MODULES-IN-0-1-2-3.2", "MODULES-IN-0-1-3", "MODULES-IN-0-2"},
1128 expectedEnvVars: []envVar{
1129 envVar{
1130 name: "ONE_SHOT_MAKEFILE",
1131 value: ""}},
1132 }, {
1133 description: "normal execution from top dir directory in symlink",
1134 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3", "0/2"},
1135 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/3/Android.bp", "0/2/Android.bp"},
1136 rootSymlink: true,
Patrice Arruda13848222019-04-22 17:12:02 -07001137 args: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3", "0/2"},
1138 curDir: ".",
1139 tidyOnly: "",
1140 expectedArgs: []string{"MODULES-IN-0-1-2-3.1", "MODULES-IN-0-1-2-3.2", "MODULES-IN-0-1-3", "MODULES-IN-0-2"},
1141 expectedEnvVars: []envVar{
1142 envVar{
1143 name: "ONE_SHOT_MAKEFILE",
1144 value: ""}},
1145 }}
1146 for _, tt := range tests {
1147 t.Run("build action BUILD_MODULES_IN_DIRS, "+tt.description, func(t *testing.T) {
1148 testGetConfigArgs(t, tt, BUILD_MODULES_IN_DIRECTORIES, true)
1149 })
1150 }
1151}