aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/testutil/FakeAttributeMapper.java
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2017-07-16 04:23:16 +0200
committerGravatar Jakob Buchgraber <buchgr@google.com>2017-07-17 10:11:11 +0200
commit455110ce3c4bcccfec30fc03f7976cdae05c92ce (patch)
tree9ebd22e5b5f4545188ce85eca39ed1bb360683ac /src/test/java/com/google/devtools/build/lib/testutil/FakeAttributeMapper.java
parentfd00638483d9c5f6683f6a8bac9278c92f86d5d5 (diff)
ResourceFilter supports dynamically configured resource filtering
Dynamically Configured Resource Filtering change 3/6 Resource filtering behaves somewhat differently when dynamically configured. Resources obtained from dependencies will have already been filtered and do not need to be filtered again. Resources that were filtered out do not need to be tracked since resource processing will never receive a reference to them anyway. Also, to make builds where ResourceFilter is dynamically configured, add equals and hashCode methods (otherwise, the configuration code throws a NullPointerException) and a global output prefix (otherwise, conflicts can occur). To ensure that the global output prefix (and the ResourceFilter object itself) is the same regardless of the ordering of filters in the android_binary, build the filters into a set, not a list, and sort them as part of creating the ResourceFilter object. This way, for example, objects built with filters "en,fr", "fr,en", and "en,en,fr" will all end up equal. Finally, rename the getFilteredResources method to better reflect its new role, and improve the isPrefiltering method to not try to prefilter if there are no filters. Add tests for all of this, and helper methods for all of those tests, including, most notably, a makeResourceFilter method that instantiates a ResourceFilter similarly to how it is actually created outside of tests (rather than directly calling the constructor) and a fake implementation of AttributeMap to support this. RELNOTES: none PiperOrigin-RevId: 162099178
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/testutil/FakeAttributeMapper.java')
-rw-r--r--src/test/java/com/google/devtools/build/lib/testutil/FakeAttributeMapper.java101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/testutil/FakeAttributeMapper.java b/src/test/java/com/google/devtools/build/lib/testutil/FakeAttributeMapper.java
new file mode 100644
index 0000000000..a73d46e7cb
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/testutil/FakeAttributeMapper.java
@@ -0,0 +1,101 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.google.devtools.build.lib.testutil;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.common.truth.Truth.assertWithMessage;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.devtools.build.lib.packages.AbstractAttributeMapper;
+import com.google.devtools.build.lib.syntax.Type;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+
+/**
+ * Faked implementation of {@link AbstractAttributeMapper} for use in testing.
+ */
+public class FakeAttributeMapper extends AbstractAttributeMapper {
+ private final Map<String, FakeAttributeMapperEntry<?>> attrs;
+
+ private FakeAttributeMapper(Map<String, FakeAttributeMapperEntry<?>> attrs) {
+ super(null, null, null, null);
+ this.attrs = ImmutableMap.copyOf(attrs);
+ }
+
+ @Override
+ @Nullable
+ public <T> T get(String attributeName, Type<T> type) {
+ FakeAttributeMapperEntry<?> entry = attrs.get(attributeName);
+ if (entry == null) {
+ // Not specified in attributes or defaults
+ assertWithMessage("Attribute " + attributeName + " not in attributes!").fail();
+ return null;
+ }
+
+ return entry.validateAndGet(type);
+ }
+
+ @Override
+ public boolean isAttributeValueExplicitlySpecified(String attributeName) {
+ return attrs.containsKey(attributeName);
+ }
+
+ public static FakeAttributeMapper empty() {
+ return builder().build();
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder to construct a {@link FakeAttributeMapper}. If no attributes are needed, use {@link
+ * #empty()} instead.
+ */
+ public static class Builder {
+ private final ImmutableMap.Builder<String, FakeAttributeMapperEntry<?>> mapBuilder =
+ ImmutableMap.builder();
+
+ private Builder() { }
+
+ public Builder withStringList(String attribute, List<String> value) {
+ mapBuilder.put(attribute, FakeAttributeMapperEntry.forStringList(value));
+ return this;
+ }
+
+ public FakeAttributeMapper build() {
+ return new FakeAttributeMapper(mapBuilder.build());
+ }
+ }
+
+ private static class FakeAttributeMapperEntry<T> {
+ private final Type<T> type;
+ private final T value;
+
+ private FakeAttributeMapperEntry(Type<T> type, T value) {
+ this.type = type;
+ this.value = value;
+ }
+
+ private static FakeAttributeMapperEntry<List<String>> forStringList(List<String> list) {
+ return new FakeAttributeMapperEntry<>(Type.STRING_LIST, list);
+ }
+
+ private <U> U validateAndGet(Type<U> otherType) {
+ assertThat(type).isSameAs(otherType);
+ return otherType.cast(value);
+ }
+ }
+}