blob: 9fd68172cc70841574ce6aa2ef89892401dcac91 [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
Jingwen Chen73850672020-12-14 08:25:34 -050027func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
Jingwen Chen40067de2021-01-26 21:58:43 -050028 files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
Jingwen Chen73850672020-12-14 08:25:34 -050029 expectedFilePaths := []filepath{
Liz Kammer2dd9ca42020-11-25 16:06:39 -080030 {
31 dir: "",
32 basename: "BUILD",
33 },
34 {
35 dir: "",
36 basename: "WORKSPACE",
37 },
38 {
39 dir: bazelRulesSubDir,
40 basename: "BUILD",
41 },
42 {
43 dir: bazelRulesSubDir,
44 basename: "providers.bzl",
45 },
46 {
47 dir: bazelRulesSubDir,
48 basename: "soong_module.bzl",
49 },
50 }
51
Jingwen Chen6c309cd2021-04-01 07:11:11 +000052 // Compare number of files
53 if a, e := len(files), len(expectedFilePaths); a != e {
54 t.Errorf("Expected %d files, got %d", e, a)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080055 }
56
Jingwen Chen6c309cd2021-04-01 07:11:11 +000057 // Sort the files to be deterministic
58 sort.Slice(files, func(i, j int) bool {
59 if dir1, dir2 := files[i].Dir, files[j].Dir; dir1 == dir2 {
60 return files[i].Basename < files[j].Basename
61 } else {
62 return dir1 < dir2
63 }
64 })
65
66 // Compare the file contents
67 for i := range files {
68 actualFile, expectedFile := files[i], expectedFilePaths[i]
69
70 if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
71 t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
72 } else if actualFile.Basename == "BUILD" || actualFile.Basename == "WORKSPACE" {
73 if actualFile.Contents != "" {
74 t.Errorf("Expected %s to have no content.", actualFile)
75 }
76 } else if actualFile.Contents == "" {
77 t.Errorf("Contents of %s unexpected empty.", actualFile)
78 }
79 }
80}
81
82func TestCreateBazelFiles_Bp2Build_CreatesNoFilesWithNoTargets(t *testing.T) {
83 files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, Bp2Build)
84 if len(files) != 0 {
85 t.Errorf("Expected no files, got %d", len(files))
86 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080087}