blob: 1ef5456665c3e392e114b1afc9a39403560fd8fa [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
693 // ********* Action *********
694 // Arguments passed in to soong_ui.
695 args []string
696
697 // Directory where the build action was invoked.
698 curDir string
699
700 // WITH_TIDY_ONLY environment variable specified.
701 tidyOnly string
702
703 // ********* Validation *********
704 // Expected arguments to be in Config instance.
705 expectedArgs []string
706
707 // Expected environment variables to be set.
708 expectedEnvVars []envVar
709}
710
711func testGetConfigArgs(t *testing.T, tt buildActionTestCase, action BuildAction, buildDependencies bool) {
712 ctx := testContext()
713
714 // Environment variables to set it to blank on every test case run.
715 resetEnvVars := []string{
716 "ONE_SHOT_MAKEFILE",
717 "WITH_TIDY_ONLY",
718 }
719
720 for _, name := range resetEnvVars {
721 if err := os.Unsetenv(name); err != nil {
722 t.Fatalf("failed to unset environment variable %s: %v", name, err)
723 }
724 }
725 if tt.tidyOnly != "" {
726 if err := os.Setenv("WITH_TIDY_ONLY", tt.tidyOnly); err != nil {
727 t.Errorf("failed to set WITH_TIDY_ONLY to %s: %v", tt.tidyOnly, err)
728 }
729 }
730
731 // Create the root source tree.
732 topDir, err := ioutil.TempDir("", "")
733 if err != nil {
734 t.Fatalf("failed to create temp dir: %v", err)
735 }
736 defer os.RemoveAll(topDir)
737
738 createDirectories(t, topDir, tt.dirsInTrees)
739 createBuildFiles(t, topDir, tt.buildFiles)
740
741 r := setTop(t, topDir)
742 defer r()
743
744 // The next block is to create the root build file.
745 rootBuildFileDir := filepath.Dir(srcDirFileCheck)
746 if err := os.MkdirAll(rootBuildFileDir, 0755); err != nil {
747 t.Fatalf("Failed to create %s directory: %v", rootBuildFileDir, err)
748 }
749
750 if err := ioutil.WriteFile(srcDirFileCheck, []byte{}, 0644); err != nil {
751 t.Fatalf("failed to create %s file: %v", srcDirFileCheck, err)
752 }
753
754 args := getConfigArgs(action, tt.curDir, buildDependencies, ctx, tt.args)
755 if !reflect.DeepEqual(tt.expectedArgs, args) {
756 t.Fatalf("expected %v, got %v for config arguments", tt.expectedArgs, args)
757 }
758
759 for _, env := range tt.expectedEnvVars {
760 if val := os.Getenv(env.name); val != env.value {
761 t.Errorf("expecting %s, got %s for environment variable %s", env.value, val, env.name)
762 }
763 }
764}
765
766// TODO: Remove this test case once mm shell build command has been deprecated.
767func TestGetConfigArgsBuildModulesInDirecotoryNoDeps(t *testing.T) {
768 tests := []buildActionTestCase{{
769 description: "normal execution in a directory",
770 dirsInTrees: []string{"0/1/2"},
771 buildFiles: []string{"0/1/2/Android.mk"},
772 args: []string{"-j", "-k", "showcommands", "fake-module"},
773 curDir: "0/1/2",
774 tidyOnly: "",
775 expectedArgs: []string{"-j", "-k", "showcommands", "fake-module", "MODULES-IN-0-1-2"},
776 expectedEnvVars: []envVar{
777 envVar{
778 name: "ONE_SHOT_MAKEFILE",
779 value: "0/1/2/Android.mk"}},
780 }, {
781 description: "makefile in parent directory",
782 dirsInTrees: []string{"0/1/2"},
783 buildFiles: []string{"0/1/Android.mk"},
784 args: []string{},
785 curDir: "0/1/2",
786 tidyOnly: "",
787 expectedArgs: []string{"MODULES-IN-0-1"},
788 expectedEnvVars: []envVar{
789 envVar{
790 name: "ONE_SHOT_MAKEFILE",
791 value: "0/1/Android.mk"}},
792 }, {
793 description: "build file not found",
794 dirsInTrees: []string{"0/1/2"},
795 buildFiles: []string{},
796 args: []string{},
797 curDir: "0/1/2",
798 tidyOnly: "",
799 expectedArgs: []string{"MODULES-IN-0-1-2"},
800 expectedEnvVars: []envVar{
801 envVar{
802 name: "ONE_SHOT_MAKEFILE",
803 value: "0/1/2/Android.mk"}},
804 }, {
805 description: "build action executed at root directory",
806 dirsInTrees: []string{},
807 buildFiles: []string{},
808 args: []string{},
809 curDir: ".",
810 tidyOnly: "",
811 expectedArgs: []string{},
812 expectedEnvVars: []envVar{
813 envVar{
814 name: "ONE_SHOT_MAKEFILE",
815 value: ""}},
816 }, {
817 description: "GET-INSTALL-PATH specified,",
818 dirsInTrees: []string{"0/1/2"},
819 buildFiles: []string{"0/1/Android.mk"},
820 args: []string{"GET-INSTALL-PATH"},
821 curDir: "0/1/2",
822 tidyOnly: "",
823 expectedArgs: []string{"GET-INSTALL-PATH-IN-0-1"},
824 expectedEnvVars: []envVar{
825 envVar{
826 name: "ONE_SHOT_MAKEFILE",
827 value: "0/1/Android.mk"}},
828 }, {
829 description: "tidy only environment variable specified,",
830 dirsInTrees: []string{"0/1/2"},
831 buildFiles: []string{"0/1/Android.mk"},
832 args: []string{"GET-INSTALL-PATH"},
833 curDir: "0/1/2",
834 tidyOnly: "true",
835 expectedArgs: []string{"tidy_only"},
836 expectedEnvVars: []envVar{
837 envVar{
838 name: "ONE_SHOT_MAKEFILE",
839 value: "0/1/Android.mk"}},
840 }}
841 for _, tt := range tests {
842 t.Run("build action BUILD_MODULES_IN_DIR without their dependencies, "+tt.description, func(t *testing.T) {
843 testGetConfigArgs(t, tt, BUILD_MODULES_IN_A_DIRECTORY, false)
844 })
845 }
846}
847
848func TestGetConfigArgsBuildModulesInDirectory(t *testing.T) {
849 tests := []buildActionTestCase{{
850 description: "normal execution in a directory",
851 dirsInTrees: []string{"0/1/2"},
852 buildFiles: []string{"0/1/2/Android.mk"},
853 args: []string{"fake-module"},
854 curDir: "0/1/2",
855 tidyOnly: "",
856 expectedArgs: []string{"fake-module", "MODULES-IN-0-1-2"},
857 expectedEnvVars: []envVar{},
858 }, {
859 description: "build file in parent directory",
860 dirsInTrees: []string{"0/1/2"},
861 buildFiles: []string{"0/1/Android.mk"},
862 args: []string{},
863 curDir: "0/1/2",
864 tidyOnly: "",
865 expectedArgs: []string{"MODULES-IN-0-1"},
866 expectedEnvVars: []envVar{},
867 },
868 {
869 description: "build file in parent directory, multiple module names passed in",
870 dirsInTrees: []string{"0/1/2"},
871 buildFiles: []string{"0/1/Android.mk"},
872 args: []string{"fake-module1", "fake-module2", "fake-module3"},
873 curDir: "0/1/2",
874 tidyOnly: "",
875 expectedArgs: []string{"fake-module1", "fake-module2", "fake-module3", "MODULES-IN-0-1"},
876 expectedEnvVars: []envVar{},
877 }, {
878 description: "build file in 2nd level parent directory",
879 dirsInTrees: []string{"0/1/2"},
880 buildFiles: []string{"0/Android.bp"},
881 args: []string{},
882 curDir: "0/1/2",
883 tidyOnly: "",
884 expectedArgs: []string{"MODULES-IN-0"},
885 expectedEnvVars: []envVar{},
886 }, {
887 description: "build action executed at root directory",
888 dirsInTrees: []string{},
889 buildFiles: []string{},
890 args: []string{},
891 curDir: ".",
892 tidyOnly: "",
893 expectedArgs: []string{},
894 expectedEnvVars: []envVar{},
895 }, {
896 description: "build file not found - no error is expected to return",
897 dirsInTrees: []string{"0/1/2"},
898 buildFiles: []string{},
899 args: []string{},
900 curDir: "0/1/2",
901 tidyOnly: "",
902 expectedArgs: []string{"MODULES-IN-0-1-2"},
903 expectedEnvVars: []envVar{},
904 }, {
905 description: "GET-INSTALL-PATH specified,",
906 dirsInTrees: []string{"0/1/2"},
907 buildFiles: []string{"0/1/Android.mk"},
908 args: []string{"GET-INSTALL-PATH", "-j", "-k", "GET-INSTALL-PATH"},
909 curDir: "0/1/2",
910 tidyOnly: "",
911 expectedArgs: []string{"-j", "-k", "GET-INSTALL-PATH-IN-0-1"},
912 expectedEnvVars: []envVar{},
913 }, {
914 description: "tidy only environment variable specified,",
915 dirsInTrees: []string{"0/1/2"},
916 buildFiles: []string{"0/1/Android.mk"},
917 args: []string{"GET-INSTALL-PATH"},
918 curDir: "0/1/2",
919 tidyOnly: "true",
920 expectedArgs: []string{"tidy_only"},
921 expectedEnvVars: []envVar{},
922 }, {
923 description: "normal execution in root directory with args",
924 dirsInTrees: []string{},
925 buildFiles: []string{},
926 args: []string{"-j", "-k", "fake_module"},
927 curDir: "",
928 tidyOnly: "",
929 expectedArgs: []string{"-j", "-k", "fake_module"},
930 expectedEnvVars: []envVar{},
931 }}
932 for _, tt := range tests {
933 t.Run("build action BUILD_MODULES_IN_DIR, "+tt.description, func(t *testing.T) {
934 testGetConfigArgs(t, tt, BUILD_MODULES_IN_A_DIRECTORY, true)
935 })
936 }
937}
938
939// TODO: Remove this test case once mmm shell build command has been deprecated.
940func TestGetConfigArgsBuildModulesInDirectoriesNoDeps(t *testing.T) {
941 tests := []buildActionTestCase{{
942 description: "normal execution in a directory",
943 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
944 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
945 args: []string{"3.1/:t1,t2", "3.2/:t3,t4", "3.3/:t5,t6"},
946 curDir: "0/1/2",
947 tidyOnly: "",
948 expectedArgs: []string{"t1", "t2", "t3", "t4", "t5", "t6"},
949 expectedEnvVars: []envVar{
950 envVar{
951 name: "ONE_SHOT_MAKEFILE",
952 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
953 }, {
954 description: "GET-INSTALL-PATH specified",
955 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
956 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
957 args: []string{"GET-INSTALL-PATH", "3.1/", "3.2/", "3.3/:t6"},
958 curDir: "0/1/2",
959 tidyOnly: "",
960 expectedArgs: []string{"GET-INSTALL-PATH-IN-0-1-2-3.1", "GET-INSTALL-PATH-IN-0-1-2-3.2", "t6"},
961 expectedEnvVars: []envVar{
962 envVar{
963 name: "ONE_SHOT_MAKEFILE",
964 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
965 }, {
966 description: "tidy only environment variable specified",
967 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
968 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
969 args: []string{"GET-INSTALL-PATH", "3.1/", "3.2/", "3.3/:t6"},
970 curDir: "0/1/2",
971 tidyOnly: "1",
972 expectedArgs: []string{"tidy_only"},
973 expectedEnvVars: []envVar{
974 envVar{
975 name: "ONE_SHOT_MAKEFILE",
976 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
977 }, {
978 description: "normal execution from top dir directory",
979 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
980 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
981 args: []string{"0/1/2/3.1", "0/1/2/3.2/:t3,t4", "0/1/2/3.3/:t5,t6"},
982 curDir: ".",
983 tidyOnly: "",
984 expectedArgs: []string{"MODULES-IN-0-1-2-3.1", "t3", "t4", "t5", "t6"},
985 expectedEnvVars: []envVar{
986 envVar{
987 name: "ONE_SHOT_MAKEFILE",
988 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
989 }}
990 for _, tt := range tests {
991 t.Run("build action BUILD_MODULES_IN_DIRS_NO_DEPS, "+tt.description, func(t *testing.T) {
992 testGetConfigArgs(t, tt, BUILD_MODULES_IN_DIRECTORIES, false)
993 })
994 }
995}
996
997func TestGetConfigArgsBuildModulesInDirectories(t *testing.T) {
998 tests := []buildActionTestCase{{
999 description: "normal execution in a directory",
1000 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1001 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1002 args: []string{"3.1/", "3.2/", "3.3/"},
1003 curDir: "0/1/2",
1004 tidyOnly: "",
1005 expectedArgs: []string{"MODULES-IN-0-1-2-3.1", "MODULES-IN-0-1-2-3.2", "MODULES-IN-0-1-2-3.3"},
1006 expectedEnvVars: []envVar{
1007 envVar{
1008 name: "ONE_SHOT_MAKEFILE",
1009 value: ""}},
1010 }, {
1011 description: "GET-INSTALL-PATH specified",
1012 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3"},
1013 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/Android.bp"},
1014 args: []string{"GET-INSTALL-PATH", "2/3.1/", "2/3.2", "3"},
1015 curDir: "0/1",
1016 tidyOnly: "",
1017 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"},
1018 expectedEnvVars: []envVar{
1019 envVar{
1020 name: "ONE_SHOT_MAKEFILE",
1021 value: ""}},
1022 }, {
1023 description: "tidy only environment variable specified",
1024 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1025 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1026 args: []string{"GET-INSTALL-PATH", "3.1/", "3.2/", "3.3"},
1027 curDir: "0/1/2",
1028 tidyOnly: "1",
1029 expectedArgs: []string{"tidy_only"},
1030 expectedEnvVars: []envVar{
1031 envVar{
1032 name: "ONE_SHOT_MAKEFILE",
1033 value: ""}},
1034 }, {
1035 description: "normal execution from top dir directory",
1036 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3", "0/2"},
1037 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"},
1038 args: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3", "0/2"},
1039 curDir: ".",
1040 tidyOnly: "",
1041 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"},
1042 expectedEnvVars: []envVar{
1043 envVar{
1044 name: "ONE_SHOT_MAKEFILE",
1045 value: ""}},
1046 }}
1047 for _, tt := range tests {
1048 t.Run("build action BUILD_MODULES_IN_DIRS, "+tt.description, func(t *testing.T) {
1049 testGetConfigArgs(t, tt, BUILD_MODULES_IN_DIRECTORIES, true)
1050 })
1051 }
1052}