| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 1 | // Copyright 2020 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 |  | 
 | 15 | package bp2build | 
 | 16 |  | 
 | 17 | import ( | 
| Chris Parsons | d078337 | 2023-10-05 15:47:07 +0000 | [diff] [blame] | 18 | 	"testing" | 
 | 19 |  | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 20 | 	"android/soong/android" | 
 | 21 | 	"android/soong/genrule" | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 22 | ) | 
 | 23 |  | 
| Chris Parsons | d078337 | 2023-10-05 15:47:07 +0000 | [diff] [blame] | 24 | func registerModulesForGensrcsTests(ctx android.RegistrationContext) { | 
 | 25 | 	ctx.RegisterModuleType("filegroup", android.FileGroupFactory) | 
 | 26 | } | 
 | 27 |  | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 28 | func TestGensrcs(t *testing.T) { | 
 | 29 | 	testcases := []struct { | 
| Chris Parsons | d078337 | 2023-10-05 15:47:07 +0000 | [diff] [blame] | 30 | 		name                    string | 
 | 31 | 		bp                      string | 
 | 32 | 		expectedBazelAttrs      AttrNameToString | 
 | 33 | 		stubbedBuildDefinitions []string | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 34 | 	}{ | 
 | 35 | 		{ | 
 | 36 | 			name: "gensrcs with common usage of properties", | 
 | 37 | 			bp: ` | 
 | 38 | 			gensrcs { | 
 | 39 |                 name: "foo", | 
 | 40 |                 srcs: ["test/input.txt", ":external_files"], | 
 | 41 |                 tool_files: ["program.py"], | 
| Liz Kammer | 8bd9242 | 2023-06-09 13:41:08 -0400 | [diff] [blame] | 42 |                 cmd: "$(location program.py) $(in) $(out) $(location foo/file.txt) $(location :external_files)", | 
 | 43 |                 data: ["foo/file.txt", ":external_files"], | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 44 |                 output_extension: "out", | 
 | 45 |                 bazel_module: { bp2build_available: true }, | 
| Chris Parsons | d078337 | 2023-10-05 15:47:07 +0000 | [diff] [blame] | 46 | 			} | 
 | 47 |       filegroup { | 
 | 48 |                 name: "external_files", | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 49 | 			}`, | 
| Chris Parsons | d078337 | 2023-10-05 15:47:07 +0000 | [diff] [blame] | 50 | 			stubbedBuildDefinitions: []string{"external_files"}, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 51 | 			expectedBazelAttrs: AttrNameToString{ | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 52 | 				"srcs": `[ | 
 | 53 |         "test/input.txt", | 
| Chris Parsons | d078337 | 2023-10-05 15:47:07 +0000 | [diff] [blame] | 54 |         ":external_files", | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 55 |     ]`, | 
 | 56 | 				"tools":            `["program.py"]`, | 
 | 57 | 				"output_extension": `"out"`, | 
| Chris Parsons | d078337 | 2023-10-05 15:47:07 +0000 | [diff] [blame] | 58 | 				"cmd":              `"$(location program.py) $(SRC) $(OUT) $(location foo/file.txt) $(location :external_files)"`, | 
| Liz Kammer | 8bd9242 | 2023-06-09 13:41:08 -0400 | [diff] [blame] | 59 | 				"data": `[ | 
 | 60 |         "foo/file.txt", | 
| Chris Parsons | d078337 | 2023-10-05 15:47:07 +0000 | [diff] [blame] | 61 |         ":external_files", | 
| Liz Kammer | 8bd9242 | 2023-06-09 13:41:08 -0400 | [diff] [blame] | 62 |     ]`, | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 63 | 			}, | 
 | 64 | 		}, | 
 | 65 | 		{ | 
 | 66 | 			name: "gensrcs with out_extension unset", | 
 | 67 | 			bp: ` | 
 | 68 | 			gensrcs { | 
 | 69 |                 name: "foo", | 
 | 70 |                 srcs: ["input.txt"], | 
 | 71 |                 cmd: "cat $(in) > $(out)", | 
 | 72 |                 bazel_module: { bp2build_available: true }, | 
 | 73 | 			}`, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 74 | 			expectedBazelAttrs: AttrNameToString{ | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 75 | 				"srcs": `["input.txt"]`, | 
 | 76 | 				"cmd":  `"cat $(SRC) > $(OUT)"`, | 
 | 77 | 			}, | 
 | 78 | 		}, | 
 | 79 | 	} | 
 | 80 |  | 
 | 81 | 	for _, test := range testcases { | 
 | 82 | 		expectedBazelTargets := []string{ | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 83 | 			MakeBazelTargetNoRestrictions("gensrcs", "foo", test.expectedBazelAttrs), | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 84 | 		} | 
 | 85 | 		t.Run(test.name, func(t *testing.T) { | 
| Chris Parsons | d078337 | 2023-10-05 15:47:07 +0000 | [diff] [blame] | 86 | 			RunBp2BuildTestCase(t, registerModulesForGensrcsTests, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 87 | 				Bp2buildTestCase{ | 
 | 88 | 					ModuleTypeUnderTest:        "gensrcs", | 
 | 89 | 					ModuleTypeUnderTestFactory: genrule.GenSrcsFactory, | 
 | 90 | 					Blueprint:                  test.bp, | 
 | 91 | 					ExpectedBazelTargets:       expectedBazelTargets, | 
| Chris Parsons | d078337 | 2023-10-05 15:47:07 +0000 | [diff] [blame] | 92 | 					StubbedBuildDefinitions:    test.stubbedBuildDefinitions, | 
| Vinh Tran | b69e1ae | 2022-05-20 18:54:09 -0400 | [diff] [blame] | 93 | 				}) | 
 | 94 | 		}) | 
 | 95 | 	} | 
 | 96 | } |