blob: a38fa6a550a12f2f43e71b725b1ae107f63430b9 [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
22func TestCreateBazelFiles_AddsTopLevelFiles(t *testing.T) {
23 files := CreateBazelFiles(map[string]RuleShim{}, map[string][]BazelTarget{})
24 expectedFilePaths := []struct {
25 dir string
26 basename string
27 }{
28 {
29 dir: "",
30 basename: "BUILD",
31 },
32 {
33 dir: "",
34 basename: "WORKSPACE",
35 },
36 {
37 dir: bazelRulesSubDir,
38 basename: "BUILD",
39 },
40 {
41 dir: bazelRulesSubDir,
42 basename: "providers.bzl",
43 },
44 {
45 dir: bazelRulesSubDir,
46 basename: "soong_module.bzl",
47 },
48 }
49
50 if g, w := len(files), len(expectedFilePaths); g != w {
51 t.Errorf("Expected %d files, got %d", w, g)
52 }
53
54 sort.Slice(files, func(i, j int) bool {
55 if dir1, dir2 := files[i].Dir, files[j].Dir; dir1 == dir2 {
56 return files[i].Basename < files[j].Basename
57 } else {
58 return dir1 < dir2
59 }
60 })
61
62 for i := range files {
63 if g, w := files[i], expectedFilePaths[i]; g.Dir != w.dir || g.Basename != w.basename {
64 t.Errorf("Did not find expected file %s/%s", g.Dir, g.Basename)
65 } else if g.Basename == "BUILD" || g.Basename == "WORKSPACE" {
66 if g.Contents != "" {
67 t.Errorf("Expected %s to have no content.", g)
68 }
69 } else if g.Contents == "" {
70 t.Errorf("Contents of %s unexpected empty.", g)
71 }
72 }
73}