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
This commit is contained in:
Jingwen Chen 2021-04-01 07:11:11 +00:00
parent 25c47a43a5
commit 6c309cd0ac
2 changed files with 37 additions and 48 deletions

View File

@ -19,12 +19,13 @@ func CreateBazelFiles(
ruleShims map[string]RuleShim, ruleShims map[string]RuleShim,
buildToTargets map[string]BazelTargets, buildToTargets map[string]BazelTargets,
mode CodegenMode) []BazelFile { mode CodegenMode) []BazelFile {
files := make([]BazelFile, 0, len(ruleShims)+len(buildToTargets)+numAdditionalFiles)
// Write top level files: WORKSPACE. These files are empty. var files []BazelFile
files = append(files, newFile("", "WORKSPACE", ""))
if mode == QueryView { if mode == QueryView {
// Write top level WORKSPACE.
files = append(files, newFile("", "WORKSPACE", ""))
// Used to denote that the top level directory is a package. // Used to denote that the top level directory is a package.
files = append(files, newFile("", GeneratedBuildFileName, "")) files = append(files, newFile("", GeneratedBuildFileName, ""))

View File

@ -24,36 +24,6 @@ type filepath struct {
basename 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) { func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView) files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
expectedFilePaths := []filepath{ expectedFilePaths := []filepath{
@ -79,21 +49,39 @@ func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
}, },
} }
assertFilecountsAreEqual(t, files, expectedFilePaths) // Compare number of files
sortFiles(files) if a, e := len(files), len(expectedFilePaths); a != e {
assertFileContent(t, files, expectedFilePaths) t.Errorf("Expected %d files, got %d", e, a)
}
func TestCreateBazelFiles_Bp2Build_AddsTopLevelFiles(t *testing.T) {
files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, Bp2Build)
expectedFilePaths := []filepath{
{
dir: "",
basename: "WORKSPACE",
},
} }
assertFilecountsAreEqual(t, files, expectedFilePaths) // Sort the files to be deterministic
sortFiles(files) sort.Slice(files, func(i, j int) bool {
assertFileContent(t, files, expectedFilePaths) 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))
}
} }