blob: 6b100772c983dbe7bea2bece45666bc03a612447 [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001// 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
15package bp2build
16
17import (
18 "sort"
19 "testing"
Chris Parsons3b1f83d2021-10-14 14:08:38 -040020
21 "android/soong/android"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080022)
23
Lukacs T. Berkib353cca2021-04-16 13:47:36 +020024type bazelFilepath struct {
Jingwen Chen73850672020-12-14 08:25:34 -050025 dir string
26 basename string
27}
28
Jingwen Chen73850672020-12-14 08:25:34 -050029func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
usta40caf952023-08-04 16:52:14 -040030 files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +020031 expectedFilePaths := []bazelFilepath{
Liz Kammer2dd9ca42020-11-25 16:06:39 -080032 {
33 dir: "",
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040034 basename: "BUILD.bazel",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080035 },
36 {
37 dir: "",
38 basename: "WORKSPACE",
39 },
40 {
41 dir: bazelRulesSubDir,
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040042 basename: "BUILD.bazel",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080043 },
44 {
45 dir: bazelRulesSubDir,
46 basename: "providers.bzl",
47 },
48 {
49 dir: bazelRulesSubDir,
50 basename: "soong_module.bzl",
51 },
52 }
53
Jingwen Chen6c309cd2021-04-01 07:11:11 +000054 // Compare number of files
55 if a, e := len(files), len(expectedFilePaths); a != e {
56 t.Errorf("Expected %d files, got %d", e, a)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080057 }
58
Jingwen Chen6c309cd2021-04-01 07:11:11 +000059 // Sort the files to be deterministic
60 sort.Slice(files, func(i, j int) bool {
61 if dir1, dir2 := files[i].Dir, files[j].Dir; dir1 == dir2 {
62 return files[i].Basename < files[j].Basename
63 } else {
64 return dir1 < dir2
65 }
66 })
67
68 // Compare the file contents
69 for i := range files {
70 actualFile, expectedFile := files[i], expectedFilePaths[i]
71
72 if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
73 t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040074 } else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000075 if actualFile.Contents != "" {
76 t.Errorf("Expected %s to have no content.", actualFile)
77 }
78 } else if actualFile.Contents == "" {
79 t.Errorf("Contents of %s unexpected empty.", actualFile)
80 }
81 }
82}
83
Jingwen Chenbf61afb2021-05-06 13:31:18 +000084func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
Jingwen Chen01812022021-11-19 14:29:43 +000085 testConfig := android.TestConfig("", make(map[string]string), "", make(map[string][]byte))
Cole Faust6054cdf2023-09-12 10:07:07 -070086 codegenCtx := NewCodegenContext(testConfig, android.NewTestContext(testConfig).Context, Bp2Build, "")
87 files, err := createSoongInjectionDirFiles(codegenCtx, CreateCodegenMetrics())
Cole Faust9e384e22023-02-08 17:43:09 -080088 if err != nil {
89 t.Error(err)
90 }
Jingwen Chenbf61afb2021-05-06 13:31:18 +000091 expectedFilePaths := []bazelFilepath{
92 {
Sam Delmerico46d08b42022-11-15 15:51:04 -050093 dir: "android",
94 basename: GeneratedBuildFileName,
95 },
96 {
97 dir: "android",
98 basename: "constants.bzl",
99 },
100 {
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000101 dir: "cc_toolchain",
Jingwen Chenc63677b2021-06-17 05:43:19 +0000102 basename: GeneratedBuildFileName,
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000103 },
104 {
105 dir: "cc_toolchain",
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000106 basename: "config_constants.bzl",
107 },
108 {
109 dir: "cc_toolchain",
Cole Faust6054cdf2023-09-12 10:07:07 -0700110 basename: "ndk_libs.bzl",
111 },
112 {
113 dir: "cc_toolchain",
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000114 basename: "sanitizer_constants.bzl",
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000115 },
Jingwen Chenc63677b2021-06-17 05:43:19 +0000116 {
Sam Delmerico932c01c2022-03-25 16:33:26 +0000117 dir: "java_toolchain",
118 basename: GeneratedBuildFileName,
119 },
120 {
121 dir: "java_toolchain",
122 basename: "constants.bzl",
123 },
124 {
Vinh Tran80f6b212023-08-23 13:49:13 -0400125 dir: "rust_toolchain",
126 basename: GeneratedBuildFileName,
127 },
128 {
129 dir: "rust_toolchain",
130 basename: "constants.bzl",
131 },
132 {
Jingwen Chen7810e172022-07-29 02:25:34 +0000133 dir: "apex_toolchain",
134 basename: GeneratedBuildFileName,
135 },
136 {
137 dir: "apex_toolchain",
138 basename: "constants.bzl",
139 },
140 {
Jingwen Chen61174502021-09-17 08:40:45 +0000141 dir: "metrics",
Liz Kammer32c634b2023-08-25 17:42:42 -0400142 basename: "converted_modules.json",
Jingwen Chenc63677b2021-06-17 05:43:19 +0000143 },
Jingwen Chen01812022021-11-19 14:29:43 +0000144 {
Kevin Dagostino60f562a2022-09-20 03:54:47 +0000145 dir: "metrics",
Jingwen Chen61342112023-04-11 08:49:01 +0000146 basename: "BUILD.bazel",
147 },
148 {
149 dir: "metrics",
Kevin Dagostino60f562a2022-09-20 03:54:47 +0000150 basename: "converted_modules_path_map.json",
151 },
152 {
Jingwen Chen61342112023-04-11 08:49:01 +0000153 dir: "metrics",
154 basename: "converted_modules_path_map.bzl",
155 },
156 {
Jingwen Chen01812022021-11-19 14:29:43 +0000157 dir: "product_config",
158 basename: "soong_config_variables.bzl",
159 },
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000160 {
Liz Kammere8303bd2022-02-16 09:02:48 -0500161 dir: "product_config",
162 basename: "arch_configuration.bzl",
163 },
164 {
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000165 dir: "api_levels",
166 basename: GeneratedBuildFileName,
167 },
168 {
169 dir: "api_levels",
170 basename: "api_levels.json",
171 },
Yu Liufc603162022-03-01 15:44:08 -0800172 {
173 dir: "api_levels",
Cole Fausteb644cf2023-04-11 13:48:17 -0700174 basename: "platform_versions.bzl",
175 },
176 {
Cole Faust705968d2022-12-14 11:32:05 -0800177 dir: "allowlists",
Sam Delmericocb3c52c2023-02-03 17:40:08 -0500178 basename: GeneratedBuildFileName,
179 },
180 {
181 dir: "allowlists",
Cole Faust705968d2022-12-14 11:32:05 -0800182 basename: "mixed_build_prod_allowlist.txt",
183 },
184 {
185 dir: "allowlists",
186 basename: "mixed_build_staging_allowlist.txt",
187 },
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000188 }
189
Cole Faust6054cdf2023-09-12 10:07:07 -0700190 less := func(a bazelFilepath, b bazelFilepath) bool {
191 return a.dir+"/"+a.basename < b.dir+"/"+b.basename
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000192 }
193
Cole Faust6054cdf2023-09-12 10:07:07 -0700194 fileToFilepath := func(a BazelFile) bazelFilepath {
195 return bazelFilepath{basename: a.Basename, dir: a.Dir}
196 }
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000197
Cole Faust6054cdf2023-09-12 10:07:07 -0700198 sort.Slice(expectedFilePaths, func(i, j int) bool {
199 return less(expectedFilePaths[i], expectedFilePaths[j])
200 })
201 sort.Slice(files, func(i, j int) bool {
202 return less(fileToFilepath(files[i]), fileToFilepath(files[j]))
203 })
204
205 i := 0
206 j := 0
207 for i < len(expectedFilePaths) && j < len(files) {
208 expectedFile, actualFile := expectedFilePaths[i], files[j]
209
210 if actualFile.Dir == expectedFile.dir && actualFile.Basename == expectedFile.basename {
211 i++
212 j++
213 } else if less(expectedFile, fileToFilepath(actualFile)) {
214 t.Errorf("Did not find expected file %s/%s", expectedFile.dir, expectedFile.basename)
215 i++
216 } else {
217 t.Errorf("Found unexpected file %s/%s", actualFile.Dir, actualFile.Basename)
218 j++
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000219 }
Jingwen Chen6c309cd2021-04-01 07:11:11 +0000220 }
Cole Faust6054cdf2023-09-12 10:07:07 -0700221 for i < len(expectedFilePaths) {
222 expectedFile := expectedFilePaths[i]
223 t.Errorf("Did not find expected file %s/%s", expectedFile.dir, expectedFile.basename)
224 i++
225 }
226 for j < len(files) {
227 actualFile := files[j]
228 t.Errorf("Found unexpected file %s/%s", actualFile.Dir, actualFile.Basename)
229 j++
230 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800231}