diff options
author | 2020-12-14 08:25:34 -0500 | |
---|---|---|
committer | 2021-01-21 22:46:11 -0500 | |
commit | 7385067640a3a50c9315e040947067c9996df418 (patch) | |
tree | 9e2073cc1c80fa13aa973912d7f3b6699e3f59cf /bp2build/conversion_test.go | |
parent | de1357378b9d1bd0a53d77cc2869726d38d15ab6 (diff) |
bp2build: framework for generating BazelTargetModules.
This CL creates the framework necessary for generating
BazelTargetModules from regular Soong Android modules.
BazelTargetModules are code-generated into Bazel targets in BUILD files.
See the follow-up CL for examples of creating filegroup/genrule
BazelTargetModules.
Test: GENERATE_BAZEL_FILES=true m nothing # creates out/soong/bp2build
with no BUILD files, because there are no BazelTargetModules in the
module graph.
Change-Id: I33a96365bd439043b13af6db9e439592e9983188
Diffstat (limited to 'bp2build/conversion_test.go')
-rw-r--r-- | bp2build/conversion_test.go | 88 |
1 files changed, 61 insertions, 27 deletions
diff --git a/bp2build/conversion_test.go b/bp2build/conversion_test.go index a38fa6a55..b40aa1b25 100644 --- a/bp2build/conversion_test.go +++ b/bp2build/conversion_test.go @@ -19,12 +19,44 @@ import ( "testing" ) -func TestCreateBazelFiles_AddsTopLevelFiles(t *testing.T) { - files := CreateBazelFiles(map[string]RuleShim{}, map[string][]BazelTarget{}) - expectedFilePaths := []struct { - dir string - basename string - }{ +type filepath struct { + dir string + basename string +} + +func assertFilecountsAreEqual(t *testing.T, actual []BazelFile, expected []filepath) { + if a, e := len(actual), len(expected); a != e { + t.Errorf("Expected %d files, got %d", e, a) + } +} + +func assertFileContent(t *testing.T, actual []BazelFile, expected []filepath) { + for i := range actual { + if g, w := actual[i], expected[i]; g.Dir != w.dir || g.Basename != w.basename { + t.Errorf("Did not find expected file %s/%s", g.Dir, g.Basename) + } else if g.Basename == "BUILD" || g.Basename == "WORKSPACE" { + if g.Contents != "" { + t.Errorf("Expected %s to have no content.", g) + } + } else if g.Contents == "" { + t.Errorf("Contents of %s unexpected empty.", g) + } + } +} + +func sortFiles(files []BazelFile) { + 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 + } + }) +} + +func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) { + files := CreateBazelFiles(map[string]RuleShim{}, map[string][]BazelTarget{}, false) + expectedFilePaths := []filepath{ { dir: "", basename: "BUILD", @@ -47,27 +79,29 @@ func TestCreateBazelFiles_AddsTopLevelFiles(t *testing.T) { }, } - if g, w := len(files), len(expectedFilePaths); g != w { - t.Errorf("Expected %d files, got %d", w, g) - } - - 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 - } - }) + assertFilecountsAreEqual(t, files, expectedFilePaths) + sortFiles(files) + assertFileContent(t, files, expectedFilePaths) +} - for i := range files { - if g, w := files[i], expectedFilePaths[i]; g.Dir != w.dir || g.Basename != w.basename { - t.Errorf("Did not find expected file %s/%s", g.Dir, g.Basename) - } else if g.Basename == "BUILD" || g.Basename == "WORKSPACE" { - if g.Contents != "" { - t.Errorf("Expected %s to have no content.", g) - } - } else if g.Contents == "" { - t.Errorf("Contents of %s unexpected empty.", g) - } +func TestCreateBazelFiles_Bp2Build_AddsTopLevelFiles(t *testing.T) { + files := CreateBazelFiles(map[string]RuleShim{}, map[string][]BazelTarget{}, true) + expectedFilePaths := []filepath{ + { + dir: "", + basename: "BUILD", + }, + { + dir: "", + basename: "WORKSPACE", + }, + { + dir: bazelRulesSubDir, + basename: "BUILD", + }, } + + assertFilecountsAreEqual(t, files, expectedFilePaths) + sortFiles(files) + assertFileContent(t, files, expectedFilePaths) } |