blob: ec5f27e2179dcdd3572162eb275ca67fc975c5f6 [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001// 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
15package bp2build
16
17import (
18 "sort"
19 "testing"
20)
21
Jingwen Chen73850672020-12-14 08:25:34 -050022type filepath struct {
23 dir string
24 basename string
25}
26
27func assertFilecountsAreEqual(t *testing.T, actual []BazelFile, expected []filepath) {
28 if a, e := len(actual), len(expected); a != e {
29 t.Errorf("Expected %d files, got %d", e, a)
30 }
31}
32
33func assertFileContent(t *testing.T, actual []BazelFile, expected []filepath) {
34 for i := range actual {
35 if g, w := actual[i], expected[i]; g.Dir != w.dir || g.Basename != w.basename {
36 t.Errorf("Did not find expected file %s/%s", g.Dir, g.Basename)
37 } else if g.Basename == "BUILD" || g.Basename == "WORKSPACE" {
38 if g.Contents != "" {
39 t.Errorf("Expected %s to have no content.", g)
40 }
41 } else if g.Contents == "" {
42 t.Errorf("Contents of %s unexpected empty.", g)
43 }
44 }
45}
46
47func sortFiles(files []BazelFile) {
48 sort.Slice(files, func(i, j int) bool {
49 if dir1, dir2 := files[i].Dir, files[j].Dir; dir1 == dir2 {
50 return files[i].Basename < files[j].Basename
51 } else {
52 return dir1 < dir2
53 }
54 })
55}
56
57func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
Jingwen Chen40067de2021-01-26 21:58:43 -050058 files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
Jingwen Chen73850672020-12-14 08:25:34 -050059 expectedFilePaths := []filepath{
Liz Kammer2dd9ca42020-11-25 16:06:39 -080060 {
61 dir: "",
62 basename: "BUILD",
63 },
64 {
65 dir: "",
66 basename: "WORKSPACE",
67 },
68 {
69 dir: bazelRulesSubDir,
70 basename: "BUILD",
71 },
72 {
73 dir: bazelRulesSubDir,
74 basename: "providers.bzl",
75 },
76 {
77 dir: bazelRulesSubDir,
78 basename: "soong_module.bzl",
79 },
80 }
81
Jingwen Chen73850672020-12-14 08:25:34 -050082 assertFilecountsAreEqual(t, files, expectedFilePaths)
83 sortFiles(files)
84 assertFileContent(t, files, expectedFilePaths)
85}
86
87func TestCreateBazelFiles_Bp2Build_AddsTopLevelFiles(t *testing.T) {
Jingwen Chen40067de2021-01-26 21:58:43 -050088 files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, Bp2Build)
Jingwen Chen73850672020-12-14 08:25:34 -050089 expectedFilePaths := []filepath{
90 {
91 dir: "",
92 basename: "BUILD",
93 },
94 {
95 dir: "",
96 basename: "WORKSPACE",
97 },
98 {
99 dir: bazelRulesSubDir,
100 basename: "BUILD",
101 },
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800102 }
103
Jingwen Chen73850672020-12-14 08:25:34 -0500104 assertFilecountsAreEqual(t, files, expectedFilePaths)
105 sortFiles(files)
106 assertFileContent(t, files, expectedFilePaths)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800107}