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 15package bp2build 16 17import ( 18 "sort" 19 "testing" 20 21 "android/soong/android" 22) 23 24type bazelFilepath struct { 25 dir string 26 basename string 27} 28 29func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) { 30 files := CreateBazelFiles(android.NullConfig("out", "out/soong"), 31 map[string]RuleShim{}, map[string]BazelTargets{}, QueryView) 32 expectedFilePaths := []bazelFilepath{ 33 { 34 dir: "", 35 basename: "BUILD.bazel", 36 }, 37 { 38 dir: "", 39 basename: "WORKSPACE", 40 }, 41 { 42 dir: bazelRulesSubDir, 43 basename: "BUILD.bazel", 44 }, 45 { 46 dir: bazelRulesSubDir, 47 basename: "providers.bzl", 48 }, 49 { 50 dir: bazelRulesSubDir, 51 basename: "soong_module.bzl", 52 }, 53 } 54 55 // Compare number of files 56 if a, e := len(files), len(expectedFilePaths); a != e { 57 t.Errorf("Expected %d files, got %d", e, a) 58 } 59 60 // 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) 75 } else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" { 76 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 85func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) { 86 testConfig := android.TestConfig("", make(map[string]string), "", make(map[string][]byte)) 87 files, err := soongInjectionFiles(testConfig, CreateCodegenMetrics()) 88 if err != nil { 89 t.Error(err) 90 } 91 expectedFilePaths := []bazelFilepath{ 92 { 93 dir: "android", 94 basename: GeneratedBuildFileName, 95 }, 96 { 97 dir: "android", 98 basename: "constants.bzl", 99 }, 100 { 101 dir: "cc_toolchain", 102 basename: GeneratedBuildFileName, 103 }, 104 { 105 dir: "cc_toolchain", 106 basename: "config_constants.bzl", 107 }, 108 { 109 dir: "cc_toolchain", 110 basename: "sanitizer_constants.bzl", 111 }, 112 { 113 dir: "java_toolchain", 114 basename: GeneratedBuildFileName, 115 }, 116 { 117 dir: "java_toolchain", 118 basename: "constants.bzl", 119 }, 120 { 121 dir: "apex_toolchain", 122 basename: GeneratedBuildFileName, 123 }, 124 { 125 dir: "apex_toolchain", 126 basename: "constants.bzl", 127 }, 128 { 129 dir: "metrics", 130 basename: "converted_modules.txt", 131 }, 132 { 133 dir: "metrics", 134 basename: "BUILD.bazel", 135 }, 136 { 137 dir: "metrics", 138 basename: "converted_modules_path_map.json", 139 }, 140 { 141 dir: "metrics", 142 basename: "converted_modules_path_map.bzl", 143 }, 144 { 145 dir: "product_config", 146 basename: "soong_config_variables.bzl", 147 }, 148 { 149 dir: "product_config", 150 basename: "arch_configuration.bzl", 151 }, 152 { 153 dir: "api_levels", 154 basename: GeneratedBuildFileName, 155 }, 156 { 157 dir: "api_levels", 158 basename: "api_levels.json", 159 }, 160 { 161 dir: "api_levels", 162 basename: "api_levels.bzl", 163 }, 164 { 165 dir: "api_levels", 166 basename: "platform_versions.bzl", 167 }, 168 { 169 dir: "allowlists", 170 basename: GeneratedBuildFileName, 171 }, 172 { 173 dir: "allowlists", 174 basename: "env.bzl", 175 }, 176 { 177 dir: "allowlists", 178 basename: "mixed_build_prod_allowlist.txt", 179 }, 180 { 181 dir: "allowlists", 182 basename: "mixed_build_staging_allowlist.txt", 183 }, 184 } 185 186 if len(files) != len(expectedFilePaths) { 187 t.Errorf("Expected %d file, got %d", len(expectedFilePaths), len(files)) 188 } 189 190 for i := range files { 191 actualFile, expectedFile := files[i], expectedFilePaths[i] 192 193 if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename { 194 t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename) 195 } 196 } 197} 198