xref: /aosp_15_r20/build/bazel/rules/java/merged_txts_test.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1"""
2Copyright (C) 2023 The Android Open Source Project
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15"""
16
17load("@bazel_skylib//lib:paths.bzl", "paths")
18load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
19load(":merged_txts.bzl", "merged_txts")
20load(":sdk_library.bzl", "java_sdk_library")
21
22SCOPE_TO_JAVA_SDK_LIBRARY_FILE = {
23    "public": "sdk_public.txt",
24    "system": "sdk_system.txt",
25    "module-lib": "sdk_module_lib.txt",
26    "system-server": "sdk_system_server.txt",
27}
28
29def _basic_merged_txts_test_impl(ctx):
30    env = analysistest.begin(ctx)
31    actions = analysistest.target_actions(env)
32
33    base_file = paths.join(paths.dirname(ctx.build_file_path), ctx.attr.base)
34    asserts.true(
35        env,
36        base_file in actions[0].argv,
37        "Base file {} of scope {} is not in args list".format(base_file, ctx.attr.scope),
38    )
39
40    java_sdk_library_file = paths.join(
41        paths.dirname(ctx.build_file_path),
42        SCOPE_TO_JAVA_SDK_LIBRARY_FILE[ctx.attr.scope],
43    )
44    asserts.true(
45        env,
46        java_sdk_library_file in actions[0].argv,
47        "java_sdk_library file {} of scope {} is not in args list".format(java_sdk_library_file, ctx.attr.scope),
48    )
49
50    return analysistest.end(env)
51
52basic_merged_txts_test = analysistest.make(
53    _basic_merged_txts_test_impl,
54    attrs = {
55        "scope": attr.string(),
56        "base": attr.string(),
57    },
58)
59
60def test_generated_current_txt():
61    name = "generated_current_txt_test"
62    target_name = name + "_target"
63    scope = "public"
64    base = "non-updatable-current.txt"
65    merged_txts(
66        name = target_name,
67        scope = scope,
68        base = base,
69        deps = ["dep"],
70        tags = ["manual"],
71    )
72    java_sdk_library(
73        name = "dep",
74        public = SCOPE_TO_JAVA_SDK_LIBRARY_FILE["public"],
75        system = SCOPE_TO_JAVA_SDK_LIBRARY_FILE["system"],
76        module_lib = SCOPE_TO_JAVA_SDK_LIBRARY_FILE["module-lib"],
77        system_server = SCOPE_TO_JAVA_SDK_LIBRARY_FILE["system-server"],
78    )
79    basic_merged_txts_test(
80        name = name,
81        target_under_test = target_name,
82        scope = scope,
83        base = base,
84    )
85    return name
86
87def test_generated_system_current_txt():
88    name = "generated_system_current_txt_test"
89    target_name = name + "_target"
90    scope = "system"
91    base = "non-updatable-system-current.txt"
92    merged_txts(
93        name = target_name,
94        scope = scope,
95        base = base,
96        deps = ["dep"],
97        tags = ["manual"],
98    )
99    basic_merged_txts_test(
100        name = name,
101        target_under_test = target_name,
102        scope = scope,
103        base = base,
104    )
105    return name
106
107def test_generated_module_lib_current_txt():
108    name = "generated_module_lib_current_txt_test"
109    target_name = name + "_target"
110    scope = "module-lib"
111    base = "non-updatable-module-lib_current.txt"
112    merged_txts(
113        name = target_name,
114        scope = scope,
115        base = base,
116        deps = ["dep"],
117        tags = ["manual"],
118    )
119    basic_merged_txts_test(
120        name = name,
121        target_under_test = target_name,
122        scope = scope,
123        base = base,
124    )
125    return name
126
127def test_generated_system_server_current_txt():
128    name = "generated_system_server_current_txt_test"
129    target_name = name + "_target"
130    scope = "system-server"
131    base = "non-updatable-system-server-current.txt"
132    merged_txts(
133        name = target_name,
134        scope = scope,
135        base = base,
136        deps = ["dep"],
137        tags = ["manual"],
138    )
139    basic_merged_txts_test(
140        name = name,
141        target_under_test = target_name,
142        scope = scope,
143        base = base,
144    )
145    return name
146
147def merged_txts_test_suite(name):
148    native.test_suite(
149        name = name,
150        tests = [
151            test_generated_current_txt(),
152            test_generated_system_current_txt(),
153            test_generated_module_lib_current_txt(),
154            test_generated_system_server_current_txt(),
155        ],
156    )
157