blob: 8c240934cebfd715bc4c49527d26214a4dd14b3e [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) {
Sasha Smundak0fd93e02022-05-19 19:34:31 -070030 files := CreateBazelFiles(android.NullConfig("out", "out/soong"),
31 map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +020032 expectedFilePaths := []bazelFilepath{
Liz Kammer2dd9ca42020-11-25 16:06:39 -080033 {
34 dir: "",
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040035 basename: "BUILD.bazel",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080036 },
37 {
38 dir: "",
39 basename: "WORKSPACE",
40 },
41 {
42 dir: bazelRulesSubDir,
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040043 basename: "BUILD.bazel",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080044 },
45 {
46 dir: bazelRulesSubDir,
47 basename: "providers.bzl",
48 },
49 {
50 dir: bazelRulesSubDir,
51 basename: "soong_module.bzl",
52 },
53 }
54
Jingwen Chen6c309cd2021-04-01 07:11:11 +000055 // Compare number of files
56 if a, e := len(files), len(expectedFilePaths); a != e {
57 t.Errorf("Expected %d files, got %d", e, a)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080058 }
59
Jingwen Chen6c309cd2021-04-01 07:11:11 +000060 // Sort the files to be deterministic
61 sort.Slice(files, func(i, j int) bool {
62 if dir1, dir2 := files[i].Dir, files[j].Dir; dir1 == dir2 {
63 return files[i].Basename < files[j].Basename
64 } else {
65 return dir1 < dir2
66 }
67 })
68
69 // Compare the file contents
70 for i := range files {
71 actualFile, expectedFile := files[i], expectedFilePaths[i]
72
73 if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
74 t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040075 } else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000076 if actualFile.Contents != "" {
77 t.Errorf("Expected %s to have no content.", actualFile)
78 }
79 } else if actualFile.Contents == "" {
80 t.Errorf("Contents of %s unexpected empty.", actualFile)
81 }
82 }
83}
84
Jingwen Chenbf61afb2021-05-06 13:31:18 +000085func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
Jingwen Chen01812022021-11-19 14:29:43 +000086 testConfig := android.TestConfig("", make(map[string]string), "", make(map[string][]byte))
Spandan Das83e787e2023-01-11 02:50:00 +000087 files := soongInjectionFiles(testConfig, CreateCodegenMetrics())
Jingwen Chenbf61afb2021-05-06 13:31:18 +000088
89 expectedFilePaths := []bazelFilepath{
90 {
Sam Delmerico46d08b42022-11-15 15:51:04 -050091 dir: "android",
92 basename: GeneratedBuildFileName,
93 },
94 {
95 dir: "android",
96 basename: "constants.bzl",
97 },
98 {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000099 dir: "cc_toolchain",
Jingwen Chenc63677b2021-06-17 05:43:19 +0000100 basename: GeneratedBuildFileName,
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000101 },
102 {
103 dir: "cc_toolchain",
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000104 basename: "config_constants.bzl",
105 },
106 {
107 dir: "cc_toolchain",
108 basename: "sanitizer_constants.bzl",
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000109 },
Jingwen Chenc63677b2021-06-17 05:43:19 +0000110 {
Sam Delmerico932c01c2022-03-25 16:33:26 +0000111 dir: "java_toolchain",
112 basename: GeneratedBuildFileName,
113 },
114 {
115 dir: "java_toolchain",
116 basename: "constants.bzl",
117 },
118 {
Jingwen Chen7810e172022-07-29 02:25:34 +0000119 dir: "apex_toolchain",
120 basename: GeneratedBuildFileName,
121 },
122 {
123 dir: "apex_toolchain",
124 basename: "constants.bzl",
125 },
126 {
Jingwen Chen61174502021-09-17 08:40:45 +0000127 dir: "metrics",
128 basename: "converted_modules.txt",
Jingwen Chenc63677b2021-06-17 05:43:19 +0000129 },
Jingwen Chen01812022021-11-19 14:29:43 +0000130 {
Kevin Dagostino60f562a2022-09-20 03:54:47 +0000131 dir: "metrics",
132 basename: "converted_modules_path_map.json",
133 },
134 {
Jingwen Chen01812022021-11-19 14:29:43 +0000135 dir: "product_config",
136 basename: "soong_config_variables.bzl",
137 },
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000138 {
Liz Kammere8303bd2022-02-16 09:02:48 -0500139 dir: "product_config",
140 basename: "arch_configuration.bzl",
141 },
142 {
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000143 dir: "api_levels",
144 basename: GeneratedBuildFileName,
145 },
146 {
147 dir: "api_levels",
148 basename: "api_levels.json",
149 },
Yu Liufc603162022-03-01 15:44:08 -0800150 {
151 dir: "api_levels",
152 basename: "api_levels.bzl",
153 },
Cole Faust705968d2022-12-14 11:32:05 -0800154 {
155 dir: "allowlists",
Sam Delmericocb3c52c2023-02-03 17:40:08 -0500156 basename: GeneratedBuildFileName,
157 },
158 {
159 dir: "allowlists",
160 basename: "env.bzl",
161 },
162 {
163 dir: "allowlists",
Cole Faust705968d2022-12-14 11:32:05 -0800164 basename: "mixed_build_prod_allowlist.txt",
165 },
166 {
167 dir: "allowlists",
168 basename: "mixed_build_staging_allowlist.txt",
169 },
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000170 }
171
172 if len(files) != len(expectedFilePaths) {
173 t.Errorf("Expected %d file, got %d", len(expectedFilePaths), len(files))
174 }
175
176 for i := range files {
177 actualFile, expectedFile := files[i], expectedFilePaths[i]
178
179 if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
180 t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
181 }
Jingwen Chen6c309cd2021-04-01 07:11:11 +0000182 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800183}