blob: d09238a2bc199839a60f1babc8100a06b16ed01c [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"
Chris Parsons3b1f83d2021-10-14 14:08:38 -040020
21 "android/soong/android"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080022)
23
Lukacs T. Berkib353cca2021-04-16 13:47:36 +020024type bazelFilepath struct {
Jingwen Chen73850672020-12-14 08:25:34 -050025 dir string
26 basename string
27}
28
Jingwen Chen73850672020-12-14 08:25:34 -050029func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
Jingwen Chen40067de2021-01-26 21:58:43 -050030 files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +020031 expectedFilePaths := []bazelFilepath{
Liz Kammer2dd9ca42020-11-25 16:06:39 -080032 {
33 dir: "",
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040034 basename: "BUILD.bazel",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080035 },
36 {
37 dir: "",
38 basename: "WORKSPACE",
39 },
40 {
41 dir: bazelRulesSubDir,
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040042 basename: "BUILD.bazel",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080043 },
44 {
45 dir: bazelRulesSubDir,
46 basename: "providers.bzl",
47 },
48 {
49 dir: bazelRulesSubDir,
50 basename: "soong_module.bzl",
51 },
52 }
53
Jingwen Chen6c309cd2021-04-01 07:11:11 +000054 // Compare number of files
55 if a, e := len(files), len(expectedFilePaths); a != e {
56 t.Errorf("Expected %d files, got %d", e, a)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080057 }
58
Jingwen Chen6c309cd2021-04-01 07:11:11 +000059 // 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)
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040074 } else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000075 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
Jingwen Chenbf61afb2021-05-06 13:31:18 +000084func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
Chris Parsons3b1f83d2021-10-14 14:08:38 -040085 files := CreateSoongInjectionFiles(android.Config{}, CodegenMetrics{})
Jingwen Chenbf61afb2021-05-06 13:31:18 +000086
87 expectedFilePaths := []bazelFilepath{
88 {
89 dir: "cc_toolchain",
Jingwen Chenc63677b2021-06-17 05:43:19 +000090 basename: GeneratedBuildFileName,
Jingwen Chenbf61afb2021-05-06 13:31:18 +000091 },
92 {
93 dir: "cc_toolchain",
94 basename: "constants.bzl",
95 },
Jingwen Chenc63677b2021-06-17 05:43:19 +000096 {
Jingwen Chen61174502021-09-17 08:40:45 +000097 dir: "metrics",
98 basename: "converted_modules.txt",
Jingwen Chenc63677b2021-06-17 05:43:19 +000099 },
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000100 }
101
102 if len(files) != len(expectedFilePaths) {
103 t.Errorf("Expected %d file, got %d", len(expectedFilePaths), len(files))
104 }
105
106 for i := range files {
107 actualFile, expectedFile := files[i], expectedFilePaths[i]
108
109 if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
110 t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
111 }
Jingwen Chen6c309cd2021-04-01 07:11:11 +0000112 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800113}