|
| 1 | +"""A custom @rules_testing subject for the JavaInfo provider""" |
| 2 | + |
| 3 | +load("@rules_testing//lib:truth.bzl", "subjects", "truth") |
| 4 | +load("//java/common:java_info.bzl", "JavaInfo") |
| 5 | + |
| 6 | +def _new_java_info_subject(java_info, meta): |
| 7 | + self = struct(actual = java_info, meta = meta.derive("JavaInfo")) |
| 8 | + public = struct( |
| 9 | + compilation_args = lambda: _new_java_compilation_args_subject(self.actual, self.meta), |
| 10 | + plugins = lambda: _new_java_info_plugins_subject(self.actual, self.meta), |
| 11 | + ) |
| 12 | + return public |
| 13 | + |
| 14 | +def _java_info_subject_from_target(env, target): |
| 15 | + return _new_java_info_subject(target[JavaInfo], meta = truth.expect(env).meta.derive( |
| 16 | + format_str_kwargs = { |
| 17 | + "name": target.label.name, |
| 18 | + "package": target.label.package, |
| 19 | + }, |
| 20 | + )) |
| 21 | + |
| 22 | +def _new_java_compilation_args_subject(java_info, meta): |
| 23 | + actual = struct( |
| 24 | + transitive_runtime_jars = java_info.transitive_runtime_jars, |
| 25 | + compile_jars = java_info.compile_jars, |
| 26 | + transitive_compile_time_jars = java_info.transitive_compile_time_jars, |
| 27 | + full_compile_jars = java_info.full_compile_jars, |
| 28 | + _transitive_full_compile_time_jars = java_info._transitive_full_compile_time_jars, |
| 29 | + _compile_time_java_dependencies = java_info._compile_time_java_dependencies, |
| 30 | + _is_binary = getattr(java_info, "_is_binary", False), |
| 31 | + ) |
| 32 | + self = struct( |
| 33 | + actual = actual, |
| 34 | + meta = meta, |
| 35 | + ) |
| 36 | + return struct( |
| 37 | + equals = lambda other: _java_compilation_args_equals(self, other), |
| 38 | + self = self, |
| 39 | + actual = actual, |
| 40 | + ) |
| 41 | + |
| 42 | +def _java_compilation_args_equals(self, other): |
| 43 | + if self.actual == other.actual: |
| 44 | + return |
| 45 | + for attr in dir(other.actual): |
| 46 | + other_attr = getattr(other.actual, attr) |
| 47 | + this_attr = getattr(self.actual, attr) |
| 48 | + if this_attr != other_attr: |
| 49 | + self.meta.derive(attr).add_failure( |
| 50 | + "expected: {}".format(other_attr), |
| 51 | + "actual: {}".format(this_attr), |
| 52 | + ) |
| 53 | + |
| 54 | +def _new_java_info_plugins_subject(java_info, meta): |
| 55 | + self = struct( |
| 56 | + actual = java_info.plugins, |
| 57 | + meta = meta.derive("plugins"), |
| 58 | + ) |
| 59 | + public = struct( |
| 60 | + processor_jars = lambda: subjects.depset_file(self.actual.processor_jars, meta = self.meta.derive("processor_jars")), |
| 61 | + ) |
| 62 | + return public |
| 63 | + |
| 64 | +java_info_subject = struct( |
| 65 | + new = _new_java_info_subject, |
| 66 | + from_target = _java_info_subject_from_target, |
| 67 | +) |
0 commit comments