• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	"android/soong/android"
22)
23
24type bazelFilepath struct {
25	dir      string
26	basename string
27}
28
29func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
30	files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
31	expectedFilePaths := []bazelFilepath{
32		{
33			dir:      "",
34			basename: "BUILD.bazel",
35		},
36		{
37			dir:      "",
38			basename: "WORKSPACE",
39		},
40		{
41			dir:      bazelRulesSubDir,
42			basename: "BUILD.bazel",
43		},
44		{
45			dir:      bazelRulesSubDir,
46			basename: "providers.bzl",
47		},
48		{
49			dir:      bazelRulesSubDir,
50			basename: "soong_module.bzl",
51		},
52	}
53
54	// Compare number of files
55	if a, e := len(files), len(expectedFilePaths); a != e {
56		t.Errorf("Expected %d files, got %d", e, a)
57	}
58
59	// Sort the files to be deterministic
60	sort.Slice(files, func(i, j int) bool {
61		if dir1, dir2 := files[i].Dir, files[j].Dir; dir1 == dir2 {
62			return files[i].Basename < files[j].Basename
63		} else {
64			return dir1 < dir2
65		}
66	})
67
68	// Compare the file contents
69	for i := range files {
70		actualFile, expectedFile := files[i], expectedFilePaths[i]
71
72		if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
73			t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
74		} else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" {
75			if actualFile.Contents != "" {
76				t.Errorf("Expected %s to have no content.", actualFile)
77			}
78		} else if actualFile.Contents == "" {
79			t.Errorf("Contents of %s unexpected empty.", actualFile)
80		}
81	}
82}
83
84func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
85	testConfig := android.TestConfig("", make(map[string]string), "", make(map[string][]byte))
86	files := CreateSoongInjectionFiles(testConfig, CodegenMetrics{})
87
88	expectedFilePaths := []bazelFilepath{
89		{
90			dir:      "cc_toolchain",
91			basename: GeneratedBuildFileName,
92		},
93		{
94			dir:      "cc_toolchain",
95			basename: "constants.bzl",
96		},
97		{
98			dir:      "java_toolchain",
99			basename: GeneratedBuildFileName,
100		},
101		{
102			dir:      "java_toolchain",
103			basename: "constants.bzl",
104		},
105		{
106			dir:      "metrics",
107			basename: "converted_modules.txt",
108		},
109		{
110			dir:      "product_config",
111			basename: "soong_config_variables.bzl",
112		},
113		{
114			dir:      "product_config",
115			basename: "arch_configuration.bzl",
116		},
117		{
118			dir:      "api_levels",
119			basename: GeneratedBuildFileName,
120		},
121		{
122			dir:      "api_levels",
123			basename: "api_levels.json",
124		},
125		{
126			dir:      "api_levels",
127			basename: "api_levels.bzl",
128		},
129	}
130
131	if len(files) != len(expectedFilePaths) {
132		t.Errorf("Expected %d file, got %d", len(expectedFilePaths), len(files))
133	}
134
135	for i := range files {
136		actualFile, expectedFile := files[i], expectedFilePaths[i]
137
138		if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
139			t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
140		}
141	}
142}
143