diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java | 30 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/analysis/LocationExpanderTest.java | 93 |
2 files changed, 117 insertions, 6 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java b/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java index 1d068e03bf..689094a5a2 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java @@ -14,7 +14,8 @@ package com.google.devtools.build.lib.analysis; -import com.google.common.base.Joiner; +import static java.util.stream.Collectors.joining; + import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -35,6 +36,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.stream.Stream; /** * Expands $(location) tags inside target attributes. @@ -61,6 +63,7 @@ public class LocationExpander { private static final int MAX_PATHS_SHOWN = 5; private static final String LOCATION = "$(location"; + private final RuleContext ruleContext; private final ImmutableSet<Options> options; @@ -194,11 +197,7 @@ public class LocationExpander { Collection<String> paths = resolveLabel(label, message, multiple); result.append(value, restart, start); - if (multiple) { - Joiner.on(' ').appendTo(result, paths); - } else { - result.append(Iterables.getOnlyElement(paths)); - } + appendPaths(result, paths, multiple); } catch (IllegalStateException ise) { reporter.report(ruleContext, ise.getMessage()); return value; @@ -257,6 +256,25 @@ public class LocationExpander { return paths; } + private void appendPaths(StringBuilder result, Collection<String> paths, boolean multiple) { + Stream<String> stream = paths.stream(); + if (!multiple) { + stream = stream.limit(1); + } + + String pathString = stream.map(LocationExpander::quotePath).collect(joining(" ")); + + result.append(pathString); + } + + private static String quotePath(String path) { + // TODO(jcater): Handle more cases where escaping is needed. + if (path.contains(" ")) { + path = "'" + path + "'"; + } + return path; + } + /** * Extracts all possible target locations from target specification. * diff --git a/src/test/java/com/google/devtools/build/lib/analysis/LocationExpanderTest.java b/src/test/java/com/google/devtools/build/lib/analysis/LocationExpanderTest.java new file mode 100644 index 0000000000..173c90087b --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/analysis/LocationExpanderTest.java @@ -0,0 +1,93 @@ +// 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.analysis; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link LocationExpander}. */ +@RunWith(JUnit4.class) +public class LocationExpanderTest extends BuildViewTestCase { + + @Before + public void createFiles() throws Exception { + // Set up a rule to test expansion in. + scratch.file("files/fileA"); + scratch.file("files/fileB"); + + scratch.file( + "files/BUILD", + "filegroup(name='files',", + " srcs = ['fileA', 'fileB'])", + "sh_library(name='lib',", + " deps = [':files'])"); + } + + private LocationExpander makeExpander(String label) throws Exception { + ConfiguredTarget target = getConfiguredTarget(label); + RuleContext ruleContext = getRuleContext(target); + return new LocationExpander(ruleContext); + } + + @Test + public void location_absolute() throws Exception { + LocationExpander expander = makeExpander("//files:files"); + String input = "foo $(location //files:fileA) bar"; + String result = expander.expand(input); + + assertThat(result).isEqualTo("foo files/fileA bar"); + } + + @Test + public void locations_spaces() throws Exception { + scratch.file("spaces/file with space A"); + scratch.file("spaces/file with space B"); + scratch.file( + "spaces/BUILD", + "filegroup(name='files',", + " srcs = ['file with space A', 'file with space B'])", + "sh_library(name='lib',", + " deps = [':files'])"); + + LocationExpander expander = makeExpander("//spaces:lib"); + String input = "foo $(locations :files) bar"; + String result = expander.expand(input); + + assertThat(result).isEqualTo("foo 'spaces/file with space A' 'spaces/file with space B' bar"); + } + + @Test + public void location_relative() throws Exception { + LocationExpander expander = makeExpander("//files:files"); + String input = "foo $(location :fileA) bar"; + String result = expander.expand(input); + + assertThat(result).isEqualTo("foo files/fileA bar"); + } + + @Test + public void locations_relative() throws Exception { + LocationExpander expander = makeExpander("//files:lib"); + String input = "foo $(locations :files) bar"; + String result = expander.expand(input); + + assertThat(result).isEqualTo("foo files/fileA files/fileB bar"); + } +} |