1// 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
22type bazelFilepath struct {
23	dir      string
24	basename string
25}
26
27func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
28	files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
29	expectedFilePaths := []bazelFilepath{
30		{
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
52	// Compare number of files
53	if a, e := len(files), len(expectedFilePaths); a != e {
54		t.Errorf("Expected %d files, got %d", e, a)
55	}
56
57	// 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_CreatesDefaultFiles(t *testing.T) {
83	files := CreateSoongInjectionFiles()
84
85	expectedFilePaths := []bazelFilepath{
86		{
87			dir:      "cc_toolchain",
88			basename: "BUILD",
89		},
90		{
91			dir:      "cc_toolchain",
92			basename: "constants.bzl",
93		},
94	}
95
96	if len(files) != len(expectedFilePaths) {
97		t.Errorf("Expected %d file, got %d", len(expectedFilePaths), len(files))
98	}
99
100	for i := range files {
101		actualFile, expectedFile := files[i], expectedFilePaths[i]
102
103		if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
104			t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
105		}
106
107		if expectedFile.basename != "BUILD" && actualFile.Contents == "" {
108			t.Errorf("Contents of %s unexpected empty.", actualFile)
109		}
110	}
111}
112