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) { |
Sasha Smundak | 0fd93e0 | 2022-05-19 19:34:31 -0700 | [diff] [blame] | 30 | files := CreateBazelFiles(android.NullConfig("out", "out/soong"), |
| 31 | map[string]RuleShim{}, map[string]BazelTargets{}, QueryView) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 32 | expectedFilePaths := []bazelFilepath{ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 33 | { |
| 34 | dir: "", |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 35 | basename: "BUILD.bazel", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 36 | }, |
| 37 | { |
| 38 | dir: "", |
| 39 | basename: "WORKSPACE", |
| 40 | }, |
| 41 | { |
| 42 | dir: bazelRulesSubDir, |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 43 | basename: "BUILD.bazel", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 44 | }, |
| 45 | { |
| 46 | dir: bazelRulesSubDir, |
| 47 | basename: "providers.bzl", |
| 48 | }, |
| 49 | { |
| 50 | dir: bazelRulesSubDir, |
| 51 | basename: "soong_module.bzl", |
| 52 | }, |
| 53 | } |
| 54 | |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 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) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 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) |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 75 | } else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" { |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 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 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 85 | func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) { |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 86 | testConfig := android.TestConfig("", make(map[string]string), "", make(map[string][]byte)) |
usta | 4f5d2c1 | 2022-10-28 23:32:01 -0400 | [diff] [blame] | 87 | files := CreateSoongInjectionFiles(testConfig, CreateCodegenMetrics()) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 88 | |
| 89 | expectedFilePaths := []bazelFilepath{ |
| 90 | { |
| 91 | dir: "cc_toolchain", |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 92 | basename: GeneratedBuildFileName, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 93 | }, |
| 94 | { |
| 95 | dir: "cc_toolchain", |
| 96 | basename: "constants.bzl", |
| 97 | }, |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 98 | { |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 99 | dir: "java_toolchain", |
| 100 | basename: GeneratedBuildFileName, |
| 101 | }, |
| 102 | { |
| 103 | dir: "java_toolchain", |
| 104 | basename: "constants.bzl", |
| 105 | }, |
| 106 | { |
Jingwen Chen | 7810e17 | 2022-07-29 02:25:34 +0000 | [diff] [blame] | 107 | dir: "apex_toolchain", |
| 108 | basename: GeneratedBuildFileName, |
| 109 | }, |
| 110 | { |
| 111 | dir: "apex_toolchain", |
| 112 | basename: "constants.bzl", |
| 113 | }, |
| 114 | { |
Jingwen Chen | 6117450 | 2021-09-17 08:40:45 +0000 | [diff] [blame] | 115 | dir: "metrics", |
| 116 | basename: "converted_modules.txt", |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 117 | }, |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 118 | { |
Kevin Dagostino | 60f562a | 2022-09-20 03:54:47 +0000 | [diff] [blame] | 119 | dir: "metrics", |
| 120 | basename: "converted_modules_path_map.json", |
| 121 | }, |
| 122 | { |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 123 | dir: "product_config", |
| 124 | basename: "soong_config_variables.bzl", |
| 125 | }, |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 126 | { |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 127 | dir: "product_config", |
| 128 | basename: "arch_configuration.bzl", |
| 129 | }, |
| 130 | { |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 131 | dir: "api_levels", |
| 132 | basename: GeneratedBuildFileName, |
| 133 | }, |
| 134 | { |
| 135 | dir: "api_levels", |
| 136 | basename: "api_levels.json", |
| 137 | }, |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 138 | { |
| 139 | dir: "api_levels", |
| 140 | basename: "api_levels.bzl", |
| 141 | }, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if len(files) != len(expectedFilePaths) { |
| 145 | t.Errorf("Expected %d file, got %d", len(expectedFilePaths), len(files)) |
| 146 | } |
| 147 | |
| 148 | for i := range files { |
| 149 | actualFile, expectedFile := files[i], expectedFilePaths[i] |
| 150 | |
| 151 | if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename { |
| 152 | t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename) |
| 153 | } |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 154 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 155 | } |