blob: e9fc61d43a609d62e24f893d4c2125b8a8db5dbb [file] [log] [blame]
Vinh Tranb69e1ae2022-05-20 18:54:09 -04001// 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 (
Chris Parsonsd0783372023-10-05 15:47:07 +000018 "testing"
19
Vinh Tranb69e1ae2022-05-20 18:54:09 -040020 "android/soong/android"
21 "android/soong/genrule"
Vinh Tranb69e1ae2022-05-20 18:54:09 -040022)
23
Chris Parsonsd0783372023-10-05 15:47:07 +000024func registerModulesForGensrcsTests(ctx android.RegistrationContext) {
25 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
26}
27
Vinh Tranb69e1ae2022-05-20 18:54:09 -040028func TestGensrcs(t *testing.T) {
29 testcases := []struct {
Chris Parsonsd0783372023-10-05 15:47:07 +000030 name string
31 bp string
32 expectedBazelAttrs AttrNameToString
33 stubbedBuildDefinitions []string
Vinh Tranb69e1ae2022-05-20 18:54:09 -040034 }{
35 {
36 name: "gensrcs with common usage of properties",
37 bp: `
38 gensrcs {
39 name: "foo",
40 srcs: ["test/input.txt", ":external_files"],
41 tool_files: ["program.py"],
Liz Kammer8bd92422023-06-09 13:41:08 -040042 cmd: "$(location program.py) $(in) $(out) $(location foo/file.txt) $(location :external_files)",
43 data: ["foo/file.txt", ":external_files"],
Vinh Tranb69e1ae2022-05-20 18:54:09 -040044 output_extension: "out",
45 bazel_module: { bp2build_available: true },
Chris Parsonsd0783372023-10-05 15:47:07 +000046 }
47 filegroup {
48 name: "external_files",
Vinh Tranb69e1ae2022-05-20 18:54:09 -040049 }`,
Chris Parsonsd0783372023-10-05 15:47:07 +000050 stubbedBuildDefinitions: []string{"external_files"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +000051 expectedBazelAttrs: AttrNameToString{
Vinh Tranb69e1ae2022-05-20 18:54:09 -040052 "srcs": `[
53 "test/input.txt",
Chris Parsonsd0783372023-10-05 15:47:07 +000054 ":external_files",
Vinh Tranb69e1ae2022-05-20 18:54:09 -040055 ]`,
56 "tools": `["program.py"]`,
57 "output_extension": `"out"`,
Chris Parsonsd0783372023-10-05 15:47:07 +000058 "cmd": `"$(location program.py) $(SRC) $(OUT) $(location foo/file.txt) $(location :external_files)"`,
Liz Kammer8bd92422023-06-09 13:41:08 -040059 "data": `[
60 "foo/file.txt",
Chris Parsonsd0783372023-10-05 15:47:07 +000061 ":external_files",
Liz Kammer8bd92422023-06-09 13:41:08 -040062 ]`,
Vinh Tranb69e1ae2022-05-20 18:54:09 -040063 },
64 },
65 {
66 name: "gensrcs with out_extension unset",
67 bp: `
68 gensrcs {
69 name: "foo",
70 srcs: ["input.txt"],
71 cmd: "cat $(in) > $(out)",
72 bazel_module: { bp2build_available: true },
73 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000074 expectedBazelAttrs: AttrNameToString{
Vinh Tranb69e1ae2022-05-20 18:54:09 -040075 "srcs": `["input.txt"]`,
76 "cmd": `"cat $(SRC) > $(OUT)"`,
77 },
78 },
79 }
80
81 for _, test := range testcases {
82 expectedBazelTargets := []string{
Sam Delmerico3177a6e2022-06-21 19:28:33 +000083 MakeBazelTargetNoRestrictions("gensrcs", "foo", test.expectedBazelAttrs),
Vinh Tranb69e1ae2022-05-20 18:54:09 -040084 }
85 t.Run(test.name, func(t *testing.T) {
Chris Parsonsd0783372023-10-05 15:47:07 +000086 RunBp2BuildTestCase(t, registerModulesForGensrcsTests,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000087 Bp2buildTestCase{
88 ModuleTypeUnderTest: "gensrcs",
89 ModuleTypeUnderTestFactory: genrule.GenSrcsFactory,
90 Blueprint: test.bp,
91 ExpectedBazelTargets: expectedBazelTargets,
Chris Parsonsd0783372023-10-05 15:47:07 +000092 StubbedBuildDefinitions: test.stubbedBuildDefinitions,
Vinh Tranb69e1ae2022-05-20 18:54:09 -040093 })
94 })
95 }
96}