blob: fdf64e68b9be3c1c41a4ecb719e9c18b9747904e [file] [log] [blame]
John Cateraacc3052017-08-01 01:17:02 +02001// Copyright 2017 The Bazel Authors. 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 com.google.devtools.build.lib.analysis;
16
17import static com.google.common.truth.Truth.assertThat;
18
ulfjackcff0dc92017-11-03 09:27:53 -040019import com.google.common.collect.ImmutableMap;
dannark25d5efc2018-04-30 09:27:58 -070020import com.google.devtools.build.lib.analysis.LocationExpander.LocationFunction;
pcloudy0df4d142021-09-01 01:58:07 -070021import com.google.devtools.build.lib.cmdline.RepositoryMapping;
dannarkc7c5ab12018-06-21 14:40:46 -070022import com.google.devtools.build.lib.cmdline.RepositoryName;
ulfjack04509fa2017-10-02 14:52:14 +020023import java.util.ArrayList;
24import java.util.List;
John Cateraacc3052017-08-01 01:17:02 +020025import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
28
29/** Unit tests for {@link LocationExpander}. */
30@RunWith(JUnit4.class)
ulfjack04509fa2017-10-02 14:52:14 +020031public class LocationExpanderTest {
cparsons963881a2018-10-03 14:23:55 -070032 private static final class Capture implements RuleErrorConsumer {
ulfjack04509fa2017-10-02 14:52:14 +020033 private final List<String> warnsOrErrors = new ArrayList<>();
John Cateraacc3052017-08-01 01:17:02 +020034
ulfjack04509fa2017-10-02 14:52:14 +020035 @Override
36 public void ruleWarning(String message) {
37 warnsOrErrors.add("WARN: " + message);
38 }
John Cateraacc3052017-08-01 01:17:02 +020039
ulfjack04509fa2017-10-02 14:52:14 +020040 @Override
41 public void ruleError(String message) {
42 warnsOrErrors.add("ERROR: " + message);
43 }
44
45 @Override
46 public void attributeWarning(String attrName, String message) {
47 warnsOrErrors.add("WARN-" + attrName + ": " + message);
48 }
49
50 @Override
51 public void attributeError(String attrName, String message) {
52 warnsOrErrors.add("ERROR-" + attrName + ": " + message);
53 }
cparsons0dcffc52017-10-13 23:40:31 +020054
55 @Override
cparsons0dcffc52017-10-13 23:40:31 +020056 public boolean hasErrors() {
57 return !warnsOrErrors.isEmpty();
58 }
John Cateraacc3052017-08-01 01:17:02 +020059 }
60
ulfjack04509fa2017-10-02 14:52:14 +020061 private LocationExpander makeExpander(RuleErrorConsumer ruleErrorConsumer) throws Exception {
dannark25d5efc2018-04-30 09:27:58 -070062 LocationFunction f1 = new LocationFunctionBuilder("//a", false)
63 .setExecPaths(false)
64 .add("//a", "/exec/src/a")
65 .build();
66
67 LocationFunction f2 = new LocationFunctionBuilder("//b", true)
68 .setExecPaths(false)
69 .add("//b", "/exec/src/b")
70 .build();
71
ulfjack04509fa2017-10-02 14:52:14 +020072 return new LocationExpander(
73 ruleErrorConsumer,
dannark25d5efc2018-04-30 09:27:58 -070074 ImmutableMap.<String, LocationFunction>of(
75 "location", f1,
dannarkc7c5ab12018-06-21 14:40:46 -070076 "locations", f2),
pcloudy0df4d142021-09-01 01:58:07 -070077 RepositoryMapping.ALWAYS_FALLBACK);
ulfjack04509fa2017-10-02 14:52:14 +020078 }
79
80 private String expand(String input) throws Exception {
81 return makeExpander(new Capture()).expand(input);
John Cateraacc3052017-08-01 01:17:02 +020082 }
83
84 @Test
ulfjack04509fa2017-10-02 14:52:14 +020085 public void noExpansion() throws Exception {
86 assertThat(expand("abc")).isEqualTo("abc");
John Cateraacc3052017-08-01 01:17:02 +020087 }
88
89 @Test
ulfjack04509fa2017-10-02 14:52:14 +020090 public void oneOrMore() throws Exception {
dannark25d5efc2018-04-30 09:27:58 -070091 assertThat(expand("$(location a)")).isEqualTo("src/a");
92 assertThat(expand("$(locations b)")).isEqualTo("src/b");
93 assertThat(expand("---$(location a)---")).isEqualTo("---src/a---");
John Cateraacc3052017-08-01 01:17:02 +020094 }
95
96 @Test
ulfjack04509fa2017-10-02 14:52:14 +020097 public void twoInOne() throws Exception {
dannark25d5efc2018-04-30 09:27:58 -070098 assertThat(expand("$(location a) $(locations b)")).isEqualTo("src/a src/b");
John Cateraacc3052017-08-01 01:17:02 +020099 }
100
101 @Test
ulfjack04509fa2017-10-02 14:52:14 +0200102 public void notAFunction() throws Exception {
103 assertThat(expand("$(locationz a)")).isEqualTo("$(locationz a)");
104 }
John Cateraacc3052017-08-01 01:17:02 +0200105
ulfjack04509fa2017-10-02 14:52:14 +0200106 @Test
107 public void missingClosingParen() throws Exception {
108 Capture capture = new Capture();
109 String value = makeExpander(capture).expand("foo $(location a");
110 // In case of an error, no location expansion is performed.
111 assertThat(value).isEqualTo("foo $(location a");
112 assertThat(capture.warnsOrErrors).containsExactly("ERROR: unterminated $(location) expression");
113 }
114
115 // In case of errors, the exact return value is unspecified. However, we don't want to
116 // accidentally change the behavior even in this unspecified case - that's why I added a test
117 // here.
118 @Test
119 public void noExpansionOnError() throws Exception {
120 Capture capture = new Capture();
121 String value = makeExpander(capture).expand("foo $(location a) $(location a");
122 assertThat(value).isEqualTo("foo $(location a) $(location a");
123 assertThat(capture.warnsOrErrors).containsExactly("ERROR: unterminated $(location) expression");
John Cateraacc3052017-08-01 01:17:02 +0200124 }
dannarkc7c5ab12018-06-21 14:40:46 -0700125
126 @Test
127 public void expansionWithRepositoryMapping() throws Exception {
128 LocationFunction f1 = new LocationFunctionBuilder("//a", false)
129 .setExecPaths(false)
130 .add("@bar//a", "/exec/src/a")
131 .build();
132
Googler80ada0f2021-12-22 07:15:59 -0800133 ImmutableMap<RepositoryName, RepositoryName> repositoryMapping = ImmutableMap.of(
134 RepositoryName.create("@foo"),
135 RepositoryName.create("@bar"));
dannarkc7c5ab12018-06-21 14:40:46 -0700136
pcloudy0df4d142021-09-01 01:58:07 -0700137 LocationExpander locationExpander =
138 new LocationExpander(
139 new Capture(),
140 ImmutableMap.<String, LocationFunction>of("location", f1),
141 RepositoryMapping.createAllowingFallback(repositoryMapping));
dannarkc7c5ab12018-06-21 14:40:46 -0700142
143 String value = locationExpander.expand("$(location @foo//a)");
144 assertThat(value).isEqualTo("src/a");
145 }
John Cateraacc3052017-08-01 01:17:02 +0200146}