From 6c309cd0ac3d4a818b0d351ab10f480ab548ca3e Mon Sep 17 00:00:00 2001 From: Jingwen Chen Date: Thu, 1 Apr 2021 07:11:11 +0000 Subject: [PATCH] bp2build: don't generate a WORKSPACE. bp2build doesn't need to generate a WORKSPACE file. Also cleanup conversion_test.go. Test: TH Change-Id: Iba2f4b2f71d72ccf8b148cd11d78c190f93560c4 --- bp2build/conversion.go | 7 ++-- bp2build/conversion_test.go | 78 ++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 48 deletions(-) diff --git a/bp2build/conversion.go b/bp2build/conversion.go index 787222d6e..6b47cd1f1 100644 --- a/bp2build/conversion.go +++ b/bp2build/conversion.go @@ -19,12 +19,13 @@ func CreateBazelFiles( ruleShims map[string]RuleShim, buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile { - files := make([]BazelFile, 0, len(ruleShims)+len(buildToTargets)+numAdditionalFiles) - // Write top level files: WORKSPACE. These files are empty. - files = append(files, newFile("", "WORKSPACE", "")) + var files []BazelFile if mode == QueryView { + // Write top level WORKSPACE. + files = append(files, newFile("", "WORKSPACE", "")) + // Used to denote that the top level directory is a package. files = append(files, newFile("", GeneratedBuildFileName, "")) diff --git a/bp2build/conversion_test.go b/bp2build/conversion_test.go index a115ddcd6..9fd68172c 100644 --- a/bp2build/conversion_test.go +++ b/bp2build/conversion_test.go @@ -24,36 +24,6 @@ type filepath struct { 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]BazelTargets{}, QueryView) expectedFilePaths := []filepath{ @@ -79,21 +49,39 @@ func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) { }, } - assertFilecountsAreEqual(t, files, expectedFilePaths) - sortFiles(files) - assertFileContent(t, files, expectedFilePaths) -} - -func TestCreateBazelFiles_Bp2Build_AddsTopLevelFiles(t *testing.T) { - files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, Bp2Build) - expectedFilePaths := []filepath{ - { - dir: "", - basename: "WORKSPACE", - }, + // Compare number of files + if a, e := len(files), len(expectedFilePaths); a != e { + t.Errorf("Expected %d files, got %d", e, a) } - assertFilecountsAreEqual(t, files, expectedFilePaths) - sortFiles(files) - assertFileContent(t, files, expectedFilePaths) + // 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) + } else if actualFile.Basename == "BUILD" || actualFile.Basename == "WORKSPACE" { + if actualFile.Contents != "" { + t.Errorf("Expected %s to have no content.", actualFile) + } + } else if actualFile.Contents == "" { + t.Errorf("Contents of %s unexpected empty.", actualFile) + } + } +} + +func TestCreateBazelFiles_Bp2Build_CreatesNoFilesWithNoTargets(t *testing.T) { + files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, Bp2Build) + if len(files) != 0 { + t.Errorf("Expected no files, got %d", len(files)) + } }