2020-11-26 01:06:39 +01:00
|
|
|
// Copyright 2020 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package bp2build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"testing"
|
2021-10-14 20:08:38 +02:00
|
|
|
|
|
|
|
"android/soong/android"
|
2020-11-26 01:06:39 +01:00
|
|
|
)
|
|
|
|
|
2021-04-16 13:47:36 +02:00
|
|
|
type bazelFilepath struct {
|
2020-12-14 14:25:34 +01:00
|
|
|
dir string
|
|
|
|
basename string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
|
2023-08-04 22:52:14 +02:00
|
|
|
files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
|
2021-04-16 13:47:36 +02:00
|
|
|
expectedFilePaths := []bazelFilepath{
|
2020-11-26 01:06:39 +01:00
|
|
|
{
|
|
|
|
dir: "",
|
2021-05-18 13:47:15 +02:00
|
|
|
basename: "BUILD.bazel",
|
2020-11-26 01:06:39 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: "",
|
|
|
|
basename: "WORKSPACE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: bazelRulesSubDir,
|
2021-05-18 13:47:15 +02:00
|
|
|
basename: "BUILD.bazel",
|
2020-11-26 01:06:39 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: bazelRulesSubDir,
|
|
|
|
basename: "providers.bzl",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: bazelRulesSubDir,
|
|
|
|
basename: "soong_module.bzl",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-01 09:11:11 +02:00
|
|
|
// Compare number of files
|
|
|
|
if a, e := len(files), len(expectedFilePaths); a != e {
|
|
|
|
t.Errorf("Expected %d files, got %d", e, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the files to be deterministic
|
|
|
|
sort.Slice(files, func(i, j int) bool {
|
|
|
|
if dir1, dir2 := files[i].Dir, files[j].Dir; dir1 == dir2 {
|
|
|
|
return files[i].Basename < files[j].Basename
|
|
|
|
} else {
|
|
|
|
return dir1 < dir2
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Compare the file contents
|
|
|
|
for i := range files {
|
|
|
|
actualFile, expectedFile := files[i], expectedFilePaths[i]
|
|
|
|
|
|
|
|
if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
|
|
|
|
t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
|
2021-05-18 13:47:15 +02:00
|
|
|
} else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" {
|
2021-04-01 09:11:11 +02:00
|
|
|
if actualFile.Contents != "" {
|
|
|
|
t.Errorf("Expected %s to have no content.", actualFile)
|
|
|
|
}
|
|
|
|
} else if actualFile.Contents == "" {
|
|
|
|
t.Errorf("Contents of %s unexpected empty.", actualFile)
|
|
|
|
}
|
|
|
|
}
|
2020-12-14 14:25:34 +01:00
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
|
2021-05-06 15:31:18 +02:00
|
|
|
func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
|
2021-11-19 15:29:43 +01:00
|
|
|
testConfig := android.TestConfig("", make(map[string]string), "", make(map[string][]byte))
|
2023-09-12 19:07:07 +02:00
|
|
|
codegenCtx := NewCodegenContext(testConfig, android.NewTestContext(testConfig).Context, Bp2Build, "")
|
|
|
|
files, err := createSoongInjectionDirFiles(codegenCtx, CreateCodegenMetrics())
|
2023-02-09 02:43:09 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-05-06 15:31:18 +02:00
|
|
|
expectedFilePaths := []bazelFilepath{
|
2022-11-15 21:51:04 +01:00
|
|
|
{
|
|
|
|
dir: "android",
|
|
|
|
basename: GeneratedBuildFileName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: "android",
|
|
|
|
basename: "constants.bzl",
|
|
|
|
},
|
2021-05-06 15:31:18 +02:00
|
|
|
{
|
|
|
|
dir: "cc_toolchain",
|
2021-06-17 07:43:19 +02:00
|
|
|
basename: GeneratedBuildFileName,
|
2021-05-06 15:31:18 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: "cc_toolchain",
|
2023-01-19 17:02:47 +01:00
|
|
|
basename: "config_constants.bzl",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: "cc_toolchain",
|
|
|
|
basename: "sanitizer_constants.bzl",
|
2021-05-06 15:31:18 +02:00
|
|
|
},
|
2022-03-25 17:33:26 +01:00
|
|
|
{
|
|
|
|
dir: "java_toolchain",
|
|
|
|
basename: GeneratedBuildFileName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: "java_toolchain",
|
|
|
|
basename: "constants.bzl",
|
|
|
|
},
|
2023-08-23 19:49:13 +02:00
|
|
|
{
|
|
|
|
dir: "rust_toolchain",
|
|
|
|
basename: GeneratedBuildFileName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: "rust_toolchain",
|
|
|
|
basename: "constants.bzl",
|
|
|
|
},
|
2022-07-29 04:25:34 +02:00
|
|
|
{
|
|
|
|
dir: "apex_toolchain",
|
|
|
|
basename: GeneratedBuildFileName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: "apex_toolchain",
|
|
|
|
basename: "constants.bzl",
|
|
|
|
},
|
2021-06-17 07:43:19 +02:00
|
|
|
{
|
2021-09-17 10:40:45 +02:00
|
|
|
dir: "metrics",
|
2023-08-25 23:42:42 +02:00
|
|
|
basename: "converted_modules.json",
|
2021-06-17 07:43:19 +02:00
|
|
|
},
|
2023-04-11 10:49:01 +02:00
|
|
|
{
|
|
|
|
dir: "metrics",
|
|
|
|
basename: "BUILD.bazel",
|
|
|
|
},
|
2022-09-20 05:54:47 +02:00
|
|
|
{
|
|
|
|
dir: "metrics",
|
|
|
|
basename: "converted_modules_path_map.json",
|
|
|
|
},
|
2023-04-11 10:49:01 +02:00
|
|
|
{
|
|
|
|
dir: "metrics",
|
|
|
|
basename: "converted_modules_path_map.bzl",
|
|
|
|
},
|
2021-11-19 15:29:43 +01:00
|
|
|
{
|
|
|
|
dir: "product_config",
|
|
|
|
basename: "soong_config_variables.bzl",
|
|
|
|
},
|
2022-02-16 15:02:48 +01:00
|
|
|
{
|
|
|
|
dir: "product_config",
|
|
|
|
basename: "arch_configuration.bzl",
|
|
|
|
},
|
2022-01-07 15:55:29 +01:00
|
|
|
{
|
|
|
|
dir: "api_levels",
|
|
|
|
basename: GeneratedBuildFileName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: "api_levels",
|
|
|
|
basename: "api_levels.json",
|
|
|
|
},
|
2023-04-11 22:48:17 +02:00
|
|
|
{
|
|
|
|
dir: "api_levels",
|
|
|
|
basename: "platform_versions.bzl",
|
|
|
|
},
|
2023-02-03 23:40:08 +01:00
|
|
|
{
|
|
|
|
dir: "allowlists",
|
|
|
|
basename: GeneratedBuildFileName,
|
|
|
|
},
|
2022-12-14 20:32:05 +01:00
|
|
|
{
|
|
|
|
dir: "allowlists",
|
|
|
|
basename: "mixed_build_prod_allowlist.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dir: "allowlists",
|
|
|
|
basename: "mixed_build_staging_allowlist.txt",
|
|
|
|
},
|
2021-05-06 15:31:18 +02:00
|
|
|
}
|
|
|
|
|
2023-09-12 19:07:07 +02:00
|
|
|
less := func(a bazelFilepath, b bazelFilepath) bool {
|
|
|
|
return a.dir+"/"+a.basename < b.dir+"/"+b.basename
|
2021-05-06 15:31:18 +02:00
|
|
|
}
|
|
|
|
|
2023-09-12 19:07:07 +02:00
|
|
|
fileToFilepath := func(a BazelFile) bazelFilepath {
|
|
|
|
return bazelFilepath{basename: a.Basename, dir: a.Dir}
|
|
|
|
}
|
2021-05-06 15:31:18 +02:00
|
|
|
|
2023-09-12 19:07:07 +02:00
|
|
|
sort.Slice(expectedFilePaths, func(i, j int) bool {
|
|
|
|
return less(expectedFilePaths[i], expectedFilePaths[j])
|
|
|
|
})
|
|
|
|
sort.Slice(files, func(i, j int) bool {
|
|
|
|
return less(fileToFilepath(files[i]), fileToFilepath(files[j]))
|
|
|
|
})
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
j := 0
|
|
|
|
for i < len(expectedFilePaths) && j < len(files) {
|
|
|
|
expectedFile, actualFile := expectedFilePaths[i], files[j]
|
|
|
|
|
|
|
|
if actualFile.Dir == expectedFile.dir && actualFile.Basename == expectedFile.basename {
|
|
|
|
i++
|
|
|
|
j++
|
|
|
|
} else if less(expectedFile, fileToFilepath(actualFile)) {
|
|
|
|
t.Errorf("Did not find expected file %s/%s", expectedFile.dir, expectedFile.basename)
|
|
|
|
i++
|
|
|
|
} else {
|
|
|
|
t.Errorf("Found unexpected file %s/%s", actualFile.Dir, actualFile.Basename)
|
|
|
|
j++
|
2021-05-06 15:31:18 +02:00
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
2023-09-12 19:07:07 +02:00
|
|
|
for i < len(expectedFilePaths) {
|
|
|
|
expectedFile := expectedFilePaths[i]
|
|
|
|
t.Errorf("Did not find expected file %s/%s", expectedFile.dir, expectedFile.basename)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
for j < len(files) {
|
|
|
|
actualFile := files[j]
|
|
|
|
t.Errorf("Found unexpected file %s/%s", actualFile.Dir, actualFile.Basename)
|
|
|
|
j++
|
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|