Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [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 ( |
| 18 | "sort" |
| 19 | "testing" |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 22 | ) |
| 23 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 24 | type bazelFilepath struct { |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 25 | dir string |
| 26 | basename string |
| 27 | } |
| 28 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 29 | func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) { |
usta | 40caf95 | 2023-08-04 16:52:14 -0400 | [diff] [blame^] | 30 | files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 31 | expectedFilePaths := []bazelFilepath{ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 32 | { |
| 33 | dir: "", |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 34 | basename: "BUILD.bazel", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 35 | }, |
| 36 | { |
| 37 | dir: "", |
| 38 | basename: "WORKSPACE", |
| 39 | }, |
| 40 | { |
| 41 | dir: bazelRulesSubDir, |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 42 | basename: "BUILD.bazel", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 43 | }, |
| 44 | { |
| 45 | dir: bazelRulesSubDir, |
| 46 | basename: "providers.bzl", |
| 47 | }, |
| 48 | { |
| 49 | dir: bazelRulesSubDir, |
| 50 | basename: "soong_module.bzl", |
| 51 | }, |
| 52 | } |
| 53 | |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 54 | // 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 Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 59 | // 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 Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 74 | } else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" { |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 75 | 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 Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 84 | func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) { |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 85 | testConfig := android.TestConfig("", make(map[string]string), "", make(map[string][]byte)) |
Cole Faust | 9e384e2 | 2023-02-08 17:43:09 -0800 | [diff] [blame] | 86 | files, err := soongInjectionFiles(testConfig, CreateCodegenMetrics()) |
| 87 | if err != nil { |
| 88 | t.Error(err) |
| 89 | } |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 90 | expectedFilePaths := []bazelFilepath{ |
| 91 | { |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 92 | dir: "android", |
| 93 | basename: GeneratedBuildFileName, |
| 94 | }, |
| 95 | { |
| 96 | dir: "android", |
| 97 | basename: "constants.bzl", |
| 98 | }, |
| 99 | { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 100 | dir: "cc_toolchain", |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 101 | basename: GeneratedBuildFileName, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 102 | }, |
| 103 | { |
| 104 | dir: "cc_toolchain", |
Trevor Radcliffe | 4f95ee9 | 2023-01-19 16:02:47 +0000 | [diff] [blame] | 105 | basename: "config_constants.bzl", |
| 106 | }, |
| 107 | { |
| 108 | dir: "cc_toolchain", |
| 109 | basename: "sanitizer_constants.bzl", |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 110 | }, |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 111 | { |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 112 | dir: "java_toolchain", |
| 113 | basename: GeneratedBuildFileName, |
| 114 | }, |
| 115 | { |
| 116 | dir: "java_toolchain", |
| 117 | basename: "constants.bzl", |
| 118 | }, |
| 119 | { |
Jingwen Chen | 7810e17 | 2022-07-29 02:25:34 +0000 | [diff] [blame] | 120 | dir: "apex_toolchain", |
| 121 | basename: GeneratedBuildFileName, |
| 122 | }, |
| 123 | { |
| 124 | dir: "apex_toolchain", |
| 125 | basename: "constants.bzl", |
| 126 | }, |
| 127 | { |
Jingwen Chen | 6117450 | 2021-09-17 08:40:45 +0000 | [diff] [blame] | 128 | dir: "metrics", |
| 129 | basename: "converted_modules.txt", |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 130 | }, |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 131 | { |
Kevin Dagostino | 60f562a | 2022-09-20 03:54:47 +0000 | [diff] [blame] | 132 | dir: "metrics", |
Jingwen Chen | 6134211 | 2023-04-11 08:49:01 +0000 | [diff] [blame] | 133 | basename: "BUILD.bazel", |
| 134 | }, |
| 135 | { |
| 136 | dir: "metrics", |
Kevin Dagostino | 60f562a | 2022-09-20 03:54:47 +0000 | [diff] [blame] | 137 | basename: "converted_modules_path_map.json", |
| 138 | }, |
| 139 | { |
Jingwen Chen | 6134211 | 2023-04-11 08:49:01 +0000 | [diff] [blame] | 140 | dir: "metrics", |
| 141 | basename: "converted_modules_path_map.bzl", |
| 142 | }, |
| 143 | { |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 144 | dir: "product_config", |
| 145 | basename: "soong_config_variables.bzl", |
| 146 | }, |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 147 | { |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 148 | dir: "product_config", |
| 149 | basename: "arch_configuration.bzl", |
| 150 | }, |
| 151 | { |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 152 | dir: "api_levels", |
| 153 | basename: GeneratedBuildFileName, |
| 154 | }, |
| 155 | { |
| 156 | dir: "api_levels", |
| 157 | basename: "api_levels.json", |
| 158 | }, |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 159 | { |
| 160 | dir: "api_levels", |
Cole Faust | eb644cf | 2023-04-11 13:48:17 -0700 | [diff] [blame] | 161 | basename: "platform_versions.bzl", |
| 162 | }, |
| 163 | { |
Cole Faust | 705968d | 2022-12-14 11:32:05 -0800 | [diff] [blame] | 164 | dir: "allowlists", |
Sam Delmerico | cb3c52c | 2023-02-03 17:40:08 -0500 | [diff] [blame] | 165 | basename: GeneratedBuildFileName, |
| 166 | }, |
| 167 | { |
| 168 | dir: "allowlists", |
Cole Faust | 705968d | 2022-12-14 11:32:05 -0800 | [diff] [blame] | 169 | basename: "mixed_build_prod_allowlist.txt", |
| 170 | }, |
| 171 | { |
| 172 | dir: "allowlists", |
| 173 | basename: "mixed_build_staging_allowlist.txt", |
| 174 | }, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if len(files) != len(expectedFilePaths) { |
| 178 | t.Errorf("Expected %d file, got %d", len(expectedFilePaths), len(files)) |
| 179 | } |
| 180 | |
| 181 | for i := range files { |
| 182 | actualFile, expectedFile := files[i], expectedFilePaths[i] |
| 183 | |
| 184 | if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename { |
| 185 | t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename) |
| 186 | } |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 187 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 188 | } |