blob: 856af117f5780e8e640f51cb0b66219d2759dc47 [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
Patrice Arruda39282062019-06-20 16:35:12 -0700766func TestGetConfigArgsBuildModules(t *testing.T) {
767 tests := []buildActionTestCase{{
768 description: "normal execution from the root source tree directory",
769 dirsInTrees: []string{"0/1/2", "0/2", "0/3"},
770 buildFiles: []string{"0/1/2/Android.mk", "0/2/Android.bp", "0/3/Android.mk"},
771 args: []string{"-j", "fake_module", "fake_module2"},
772 curDir: ".",
773 tidyOnly: "",
774 expectedArgs: []string{"-j", "fake_module", "fake_module2"},
775 expectedEnvVars: []envVar{},
776 }, {
777 description: "normal execution in deep directory",
778 dirsInTrees: []string{"0/1/2", "0/2", "0/3", "1/2/3/4/5/6/7/8/9/1/2/3/4/5/6"},
779 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"},
780 args: []string{"-j", "fake_module", "fake_module2", "-k"},
781 curDir: "1/2/3/4/5/6/7/8/9",
782 tidyOnly: "",
783 expectedArgs: []string{"-j", "fake_module", "fake_module2", "-k"},
784 expectedEnvVars: []envVar{},
785 }, {
786 description: "normal execution in deep directory, no targets",
787 dirsInTrees: []string{"0/1/2", "0/2", "0/3", "1/2/3/4/5/6/7/8/9/1/2/3/4/5/6"},
788 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"},
789 args: []string{"-j", "-k"},
790 curDir: "1/2/3/4/5/6/7/8/9",
791 tidyOnly: "",
792 expectedArgs: []string{"-j", "-k"},
793 expectedEnvVars: []envVar{},
794 }, {
795 description: "normal execution in root source tree, no args",
796 dirsInTrees: []string{"0/1/2", "0/2", "0/3"},
797 buildFiles: []string{"0/1/2/Android.mk", "0/2/Android.bp"},
798 args: []string{},
799 curDir: "1/2/3/4/5/6/7/8/9",
800 tidyOnly: "",
801 expectedArgs: []string{},
802 expectedEnvVars: []envVar{},
803 }}
804 for _, tt := range tests {
805 t.Run("build action BUILD_MODULES with dependencies, "+tt.description, func(t *testing.T) {
806 testGetConfigArgs(t, tt, BUILD_MODULES, true)
807 })
808 }
809}
810
Patrice Arruda13848222019-04-22 17:12:02 -0700811// TODO: Remove this test case once mm shell build command has been deprecated.
812func TestGetConfigArgsBuildModulesInDirecotoryNoDeps(t *testing.T) {
813 tests := []buildActionTestCase{{
814 description: "normal execution in a directory",
815 dirsInTrees: []string{"0/1/2"},
816 buildFiles: []string{"0/1/2/Android.mk"},
817 args: []string{"-j", "-k", "showcommands", "fake-module"},
818 curDir: "0/1/2",
819 tidyOnly: "",
820 expectedArgs: []string{"-j", "-k", "showcommands", "fake-module", "MODULES-IN-0-1-2"},
821 expectedEnvVars: []envVar{
822 envVar{
823 name: "ONE_SHOT_MAKEFILE",
824 value: "0/1/2/Android.mk"}},
825 }, {
826 description: "makefile in parent directory",
827 dirsInTrees: []string{"0/1/2"},
828 buildFiles: []string{"0/1/Android.mk"},
829 args: []string{},
830 curDir: "0/1/2",
831 tidyOnly: "",
832 expectedArgs: []string{"MODULES-IN-0-1"},
833 expectedEnvVars: []envVar{
834 envVar{
835 name: "ONE_SHOT_MAKEFILE",
836 value: "0/1/Android.mk"}},
837 }, {
838 description: "build file not found",
839 dirsInTrees: []string{"0/1/2"},
840 buildFiles: []string{},
841 args: []string{},
842 curDir: "0/1/2",
843 tidyOnly: "",
844 expectedArgs: []string{"MODULES-IN-0-1-2"},
845 expectedEnvVars: []envVar{
846 envVar{
847 name: "ONE_SHOT_MAKEFILE",
848 value: "0/1/2/Android.mk"}},
849 }, {
850 description: "build action executed at root directory",
851 dirsInTrees: []string{},
852 buildFiles: []string{},
853 args: []string{},
854 curDir: ".",
855 tidyOnly: "",
856 expectedArgs: []string{},
857 expectedEnvVars: []envVar{
858 envVar{
859 name: "ONE_SHOT_MAKEFILE",
860 value: ""}},
861 }, {
862 description: "GET-INSTALL-PATH specified,",
863 dirsInTrees: []string{"0/1/2"},
864 buildFiles: []string{"0/1/Android.mk"},
865 args: []string{"GET-INSTALL-PATH"},
866 curDir: "0/1/2",
867 tidyOnly: "",
868 expectedArgs: []string{"GET-INSTALL-PATH-IN-0-1"},
869 expectedEnvVars: []envVar{
870 envVar{
871 name: "ONE_SHOT_MAKEFILE",
872 value: "0/1/Android.mk"}},
873 }, {
874 description: "tidy only environment variable specified,",
875 dirsInTrees: []string{"0/1/2"},
876 buildFiles: []string{"0/1/Android.mk"},
877 args: []string{"GET-INSTALL-PATH"},
878 curDir: "0/1/2",
879 tidyOnly: "true",
880 expectedArgs: []string{"tidy_only"},
881 expectedEnvVars: []envVar{
882 envVar{
883 name: "ONE_SHOT_MAKEFILE",
884 value: "0/1/Android.mk"}},
885 }}
886 for _, tt := range tests {
887 t.Run("build action BUILD_MODULES_IN_DIR without their dependencies, "+tt.description, func(t *testing.T) {
888 testGetConfigArgs(t, tt, BUILD_MODULES_IN_A_DIRECTORY, false)
889 })
890 }
891}
892
893func TestGetConfigArgsBuildModulesInDirectory(t *testing.T) {
894 tests := []buildActionTestCase{{
895 description: "normal execution in a directory",
896 dirsInTrees: []string{"0/1/2"},
897 buildFiles: []string{"0/1/2/Android.mk"},
898 args: []string{"fake-module"},
899 curDir: "0/1/2",
900 tidyOnly: "",
901 expectedArgs: []string{"fake-module", "MODULES-IN-0-1-2"},
902 expectedEnvVars: []envVar{},
903 }, {
904 description: "build file in parent directory",
905 dirsInTrees: []string{"0/1/2"},
906 buildFiles: []string{"0/1/Android.mk"},
907 args: []string{},
908 curDir: "0/1/2",
909 tidyOnly: "",
910 expectedArgs: []string{"MODULES-IN-0-1"},
911 expectedEnvVars: []envVar{},
912 },
913 {
914 description: "build file in parent directory, multiple module names passed in",
915 dirsInTrees: []string{"0/1/2"},
916 buildFiles: []string{"0/1/Android.mk"},
917 args: []string{"fake-module1", "fake-module2", "fake-module3"},
918 curDir: "0/1/2",
919 tidyOnly: "",
920 expectedArgs: []string{"fake-module1", "fake-module2", "fake-module3", "MODULES-IN-0-1"},
921 expectedEnvVars: []envVar{},
922 }, {
923 description: "build file in 2nd level parent directory",
924 dirsInTrees: []string{"0/1/2"},
925 buildFiles: []string{"0/Android.bp"},
926 args: []string{},
927 curDir: "0/1/2",
928 tidyOnly: "",
929 expectedArgs: []string{"MODULES-IN-0"},
930 expectedEnvVars: []envVar{},
931 }, {
932 description: "build action executed at root directory",
933 dirsInTrees: []string{},
934 buildFiles: []string{},
935 args: []string{},
936 curDir: ".",
937 tidyOnly: "",
938 expectedArgs: []string{},
939 expectedEnvVars: []envVar{},
940 }, {
941 description: "build file not found - no error is expected to return",
942 dirsInTrees: []string{"0/1/2"},
943 buildFiles: []string{},
944 args: []string{},
945 curDir: "0/1/2",
946 tidyOnly: "",
947 expectedArgs: []string{"MODULES-IN-0-1-2"},
948 expectedEnvVars: []envVar{},
949 }, {
950 description: "GET-INSTALL-PATH specified,",
951 dirsInTrees: []string{"0/1/2"},
952 buildFiles: []string{"0/1/Android.mk"},
953 args: []string{"GET-INSTALL-PATH", "-j", "-k", "GET-INSTALL-PATH"},
954 curDir: "0/1/2",
955 tidyOnly: "",
956 expectedArgs: []string{"-j", "-k", "GET-INSTALL-PATH-IN-0-1"},
957 expectedEnvVars: []envVar{},
958 }, {
959 description: "tidy only environment variable specified,",
960 dirsInTrees: []string{"0/1/2"},
961 buildFiles: []string{"0/1/Android.mk"},
962 args: []string{"GET-INSTALL-PATH"},
963 curDir: "0/1/2",
964 tidyOnly: "true",
965 expectedArgs: []string{"tidy_only"},
966 expectedEnvVars: []envVar{},
967 }, {
968 description: "normal execution in root directory with args",
969 dirsInTrees: []string{},
970 buildFiles: []string{},
971 args: []string{"-j", "-k", "fake_module"},
972 curDir: "",
973 tidyOnly: "",
974 expectedArgs: []string{"-j", "-k", "fake_module"},
975 expectedEnvVars: []envVar{},
976 }}
977 for _, tt := range tests {
978 t.Run("build action BUILD_MODULES_IN_DIR, "+tt.description, func(t *testing.T) {
979 testGetConfigArgs(t, tt, BUILD_MODULES_IN_A_DIRECTORY, true)
980 })
981 }
982}
983
984// TODO: Remove this test case once mmm shell build command has been deprecated.
985func TestGetConfigArgsBuildModulesInDirectoriesNoDeps(t *testing.T) {
986 tests := []buildActionTestCase{{
987 description: "normal execution in a directory",
988 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
989 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
990 args: []string{"3.1/:t1,t2", "3.2/:t3,t4", "3.3/:t5,t6"},
991 curDir: "0/1/2",
992 tidyOnly: "",
993 expectedArgs: []string{"t1", "t2", "t3", "t4", "t5", "t6"},
994 expectedEnvVars: []envVar{
995 envVar{
996 name: "ONE_SHOT_MAKEFILE",
997 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
998 }, {
999 description: "GET-INSTALL-PATH specified",
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{"GET-INSTALL-PATH", "3.1/", "3.2/", "3.3/:t6"},
1003 curDir: "0/1/2",
1004 tidyOnly: "",
1005 expectedArgs: []string{"GET-INSTALL-PATH-IN-0-1-2-3.1", "GET-INSTALL-PATH-IN-0-1-2-3.2", "t6"},
1006 expectedEnvVars: []envVar{
1007 envVar{
1008 name: "ONE_SHOT_MAKEFILE",
1009 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
1010 }, {
1011 description: "tidy only environment variable specified",
1012 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1013 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1014 args: []string{"GET-INSTALL-PATH", "3.1/", "3.2/", "3.3/:t6"},
1015 curDir: "0/1/2",
1016 tidyOnly: "1",
1017 expectedArgs: []string{"tidy_only"},
1018 expectedEnvVars: []envVar{
1019 envVar{
1020 name: "ONE_SHOT_MAKEFILE",
1021 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
1022 }, {
1023 description: "normal execution from top dir directory",
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{"0/1/2/3.1", "0/1/2/3.2/:t3,t4", "0/1/2/3.3/:t5,t6"},
1027 curDir: ".",
1028 tidyOnly: "",
1029 expectedArgs: []string{"MODULES-IN-0-1-2-3.1", "t3", "t4", "t5", "t6"},
1030 expectedEnvVars: []envVar{
1031 envVar{
1032 name: "ONE_SHOT_MAKEFILE",
1033 value: "0/1/2/3.1/Android.mk 0/1/2/3.2/Android.mk 0/1/2/3.3/Android.mk"}},
1034 }}
1035 for _, tt := range tests {
1036 t.Run("build action BUILD_MODULES_IN_DIRS_NO_DEPS, "+tt.description, func(t *testing.T) {
1037 testGetConfigArgs(t, tt, BUILD_MODULES_IN_DIRECTORIES, false)
1038 })
1039 }
1040}
1041
1042func TestGetConfigArgsBuildModulesInDirectories(t *testing.T) {
1043 tests := []buildActionTestCase{{
1044 description: "normal execution in a directory",
1045 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1046 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1047 args: []string{"3.1/", "3.2/", "3.3/"},
1048 curDir: "0/1/2",
1049 tidyOnly: "",
1050 expectedArgs: []string{"MODULES-IN-0-1-2-3.1", "MODULES-IN-0-1-2-3.2", "MODULES-IN-0-1-2-3.3"},
1051 expectedEnvVars: []envVar{
1052 envVar{
1053 name: "ONE_SHOT_MAKEFILE",
1054 value: ""}},
1055 }, {
1056 description: "GET-INSTALL-PATH specified",
1057 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3"},
1058 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/Android.bp"},
1059 args: []string{"GET-INSTALL-PATH", "2/3.1/", "2/3.2", "3"},
1060 curDir: "0/1",
1061 tidyOnly: "",
1062 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"},
1063 expectedEnvVars: []envVar{
1064 envVar{
1065 name: "ONE_SHOT_MAKEFILE",
1066 value: ""}},
1067 }, {
1068 description: "tidy only environment variable specified",
1069 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/2/3.3"},
1070 buildFiles: []string{"0/1/2/3.1/Android.bp", "0/1/2/3.2/Android.bp", "0/1/2/3.3/Android.bp"},
1071 args: []string{"GET-INSTALL-PATH", "3.1/", "3.2/", "3.3"},
1072 curDir: "0/1/2",
1073 tidyOnly: "1",
1074 expectedArgs: []string{"tidy_only"},
1075 expectedEnvVars: []envVar{
1076 envVar{
1077 name: "ONE_SHOT_MAKEFILE",
1078 value: ""}},
1079 }, {
1080 description: "normal execution from top dir directory",
1081 dirsInTrees: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3", "0/2"},
1082 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"},
1083 args: []string{"0/1/2/3.1", "0/1/2/3.2", "0/1/3", "0/2"},
1084 curDir: ".",
1085 tidyOnly: "",
1086 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"},
1087 expectedEnvVars: []envVar{
1088 envVar{
1089 name: "ONE_SHOT_MAKEFILE",
1090 value: ""}},
1091 }}
1092 for _, tt := range tests {
1093 t.Run("build action BUILD_MODULES_IN_DIRS, "+tt.description, func(t *testing.T) {
1094 testGetConfigArgs(t, tt, BUILD_MODULES_IN_DIRECTORIES, true)
1095 })
1096 }
1097}