ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.devtools.build.lib.analysis; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertThrows; |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 19 | |
| 20 | import com.google.common.base.Suppliers; |
dannark | c7c5ab1 | 2018-06-21 14:40:46 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.Artifact; |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.ArtifactRoot; |
Googler | f0b0c39 | 2021-01-27 17:56:52 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.ArtifactRoot.RootType; |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.LocationExpander.LocationFunction; |
| 27 | import com.google.devtools.build.lib.cmdline.Label; |
dannark | c7c5ab1 | 2018-06-21 14:40:46 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.cmdline.RepositoryName; |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 30 | import com.google.devtools.build.lib.vfs.FileSystem; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 31 | import com.google.devtools.build.lib.vfs.Root; |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 32 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 33 | import java.util.Arrays; |
| 34 | import java.util.Collection; |
| 35 | import java.util.HashMap; |
| 36 | import java.util.Map; |
| 37 | import java.util.stream.Collectors; |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
| 40 | import org.junit.runners.JUnit4; |
| 41 | |
| 42 | /** Unit tests for {@link LocationExpander.LocationFunction}. */ |
| 43 | @RunWith(JUnit4.class) |
| 44 | public class LocationFunctionTest { |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 45 | |
| 46 | @Test |
| 47 | public void absoluteAndRelativeLabels() throws Exception { |
| 48 | LocationFunction func = |
| 49 | new LocationFunctionBuilder("//foo", false).add("//foo", "/exec/src/bar").build(); |
dannark | c7c5ab1 | 2018-06-21 14:40:46 -0700 | [diff] [blame] | 50 | assertThat(func.apply("//foo", ImmutableMap.of())).isEqualTo("src/bar"); |
| 51 | assertThat(func.apply(":foo", ImmutableMap.of())).isEqualTo("src/bar"); |
| 52 | assertThat(func.apply("foo", ImmutableMap.of())).isEqualTo("src/bar"); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void pathUnderExecRootUsesDotSlash() throws Exception { |
| 57 | LocationFunction func = |
| 58 | new LocationFunctionBuilder("//foo", false).add("//foo", "/exec/bar").build(); |
dannark | c7c5ab1 | 2018-06-21 14:40:46 -0700 | [diff] [blame] | 59 | assertThat(func.apply("//foo", ImmutableMap.of())).isEqualTo("./bar"); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void noSuchLabel() throws Exception { |
| 64 | LocationFunction func = new LocationFunctionBuilder("//foo", false).build(); |
jcater | 42edea6 | 2019-05-01 08:46:18 -0700 | [diff] [blame] | 65 | IllegalStateException expected = |
| 66 | assertThrows(IllegalStateException.class, () -> func.apply("//bar", ImmutableMap.of())); |
| 67 | assertThat(expected) |
| 68 | .hasMessageThat() |
| 69 | .isEqualTo( |
| 70 | "label '//bar:bar' in $(location) expression is not a declared prerequisite of this " |
| 71 | + "rule"); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void emptyList() throws Exception { |
| 76 | LocationFunction func = new LocationFunctionBuilder("//foo", false).add("//foo").build(); |
jcater | 42edea6 | 2019-05-01 08:46:18 -0700 | [diff] [blame] | 77 | IllegalStateException expected = |
| 78 | assertThrows(IllegalStateException.class, () -> func.apply("//foo", ImmutableMap.of())); |
| 79 | assertThat(expected) |
| 80 | .hasMessageThat() |
| 81 | .isEqualTo("label '//foo:foo' in $(location) expression expands to no files"); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void tooMany() throws Exception { |
| 86 | LocationFunction func = |
| 87 | new LocationFunctionBuilder("//foo", false).add("//foo", "/exec/1", "/exec/2").build(); |
jcater | 42edea6 | 2019-05-01 08:46:18 -0700 | [diff] [blame] | 88 | IllegalStateException expected = |
| 89 | assertThrows(IllegalStateException.class, () -> func.apply("//foo", ImmutableMap.of())); |
| 90 | assertThat(expected) |
| 91 | .hasMessageThat() |
| 92 | .isEqualTo( |
| 93 | "label '//foo:foo' in $(location) expression expands to more than one file, " |
| 94 | + "please use $(locations //foo:foo) instead. Files (at most 5 shown) are: " |
| 95 | + "[./1, ./2]"); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void noSuchLabelMultiple() throws Exception { |
| 100 | LocationFunction func = new LocationFunctionBuilder("//foo", true).build(); |
jcater | 42edea6 | 2019-05-01 08:46:18 -0700 | [diff] [blame] | 101 | IllegalStateException expected = |
| 102 | assertThrows(IllegalStateException.class, () -> func.apply("//bar", ImmutableMap.of())); |
| 103 | assertThat(expected) |
| 104 | .hasMessageThat() |
| 105 | .isEqualTo( |
| 106 | "label '//bar:bar' in $(locations) expression is not a declared prerequisite of this " |
| 107 | + "rule"); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void fileWithSpace() throws Exception { |
| 112 | LocationFunction func = |
| 113 | new LocationFunctionBuilder("//foo", false).add("//foo", "/exec/file/with space").build(); |
dannark | c7c5ab1 | 2018-06-21 14:40:46 -0700 | [diff] [blame] | 114 | assertThat(func.apply("//foo", ImmutableMap.of())).isEqualTo("'file/with space'"); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void multipleFiles() throws Exception { |
| 119 | LocationFunction func = new LocationFunctionBuilder("//foo", true) |
| 120 | .add("//foo", "/exec/foo/bar", "/exec/out/foo/foobar") |
| 121 | .build(); |
dannark | c7c5ab1 | 2018-06-21 14:40:46 -0700 | [diff] [blame] | 122 | assertThat(func.apply("//foo", ImmutableMap.of())).isEqualTo("foo/bar foo/foobar"); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void filesWithSpace() throws Exception { |
| 127 | LocationFunction func = new LocationFunctionBuilder("//foo", true) |
| 128 | .add("//foo", "/exec/file/with space", "/exec/file/with spaces ") |
| 129 | .build(); |
dannark | c7c5ab1 | 2018-06-21 14:40:46 -0700 | [diff] [blame] | 130 | assertThat(func.apply("//foo", ImmutableMap.of())) |
| 131 | .isEqualTo("'file/with space' 'file/with spaces '"); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void execPath() throws Exception { |
| 136 | LocationFunction func = new LocationFunctionBuilder("//foo", true) |
| 137 | .setExecPaths(true) |
| 138 | .add("//foo", "/exec/bar", "/exec/out/foobar") |
| 139 | .build(); |
dannark | c7c5ab1 | 2018-06-21 14:40:46 -0700 | [diff] [blame] | 140 | assertThat(func.apply("//foo", ImmutableMap.of())).isEqualTo("./bar out/foobar"); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void locationFunctionWithMappingReplace() throws Exception { |
| 145 | RepositoryName a = RepositoryName.create("@a"); |
| 146 | RepositoryName b = RepositoryName.create("@b"); |
| 147 | ImmutableMap<RepositoryName, RepositoryName> repositoryMapping = ImmutableMap.of(a, b); |
| 148 | LocationFunction func = |
| 149 | new LocationFunctionBuilder("//foo", false).add("@b//foo", "/exec/src/bar").build(); |
| 150 | assertThat(func.apply("@a//foo", repositoryMapping)).isEqualTo("src/bar"); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void locationFunctionWithMappingIgnoreRepo() throws Exception { |
| 155 | RepositoryName a = RepositoryName.create("@a"); |
| 156 | RepositoryName b = RepositoryName.create("@b"); |
| 157 | ImmutableMap<RepositoryName, RepositoryName> repositoryMapping = ImmutableMap.of(a, b); |
| 158 | LocationFunction func = |
| 159 | new LocationFunctionBuilder("//foo", false).add("@potato//foo", "/exec/src/bar").build(); |
| 160 | assertThat(func.apply("@potato//foo", repositoryMapping)).isEqualTo("src/bar"); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 161 | } |
dannark | 25d5efc | 2018-04-30 09:27:58 -0700 | [diff] [blame] | 162 | } |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 163 | |
dannark | 25d5efc | 2018-04-30 09:27:58 -0700 | [diff] [blame] | 164 | final class LocationFunctionBuilder { |
| 165 | private final Label root; |
| 166 | private final boolean multiple; |
| 167 | private boolean execPaths; |
Googler | 181bd1a | 2020-11-12 08:15:06 -0800 | [diff] [blame] | 168 | private boolean legacyExternalRunfiles; |
dannark | 25d5efc | 2018-04-30 09:27:58 -0700 | [diff] [blame] | 169 | private final Map<Label, Collection<Artifact>> labelMap = new HashMap<>(); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 170 | |
dannark | 25d5efc | 2018-04-30 09:27:58 -0700 | [diff] [blame] | 171 | LocationFunctionBuilder(String rootLabel, boolean multiple) { |
| 172 | this.root = Label.parseAbsoluteUnchecked(rootLabel); |
| 173 | this.multiple = multiple; |
| 174 | } |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 175 | |
dannark | 25d5efc | 2018-04-30 09:27:58 -0700 | [diff] [blame] | 176 | public LocationFunction build() { |
Googler | 181bd1a | 2020-11-12 08:15:06 -0800 | [diff] [blame] | 177 | return new LocationFunction( |
| 178 | root, Suppliers.ofInstance(labelMap), execPaths, legacyExternalRunfiles, multiple); |
dannark | 25d5efc | 2018-04-30 09:27:58 -0700 | [diff] [blame] | 179 | } |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 180 | |
dannark | 25d5efc | 2018-04-30 09:27:58 -0700 | [diff] [blame] | 181 | public LocationFunctionBuilder setExecPaths(boolean execPaths) { |
| 182 | this.execPaths = execPaths; |
| 183 | return this; |
| 184 | } |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 185 | |
Googler | 181bd1a | 2020-11-12 08:15:06 -0800 | [diff] [blame] | 186 | public LocationFunctionBuilder setLegacyExternalRunfiles(boolean legacyExternalRunfiles) { |
| 187 | this.legacyExternalRunfiles = legacyExternalRunfiles; |
| 188 | return this; |
| 189 | } |
| 190 | |
dannark | 25d5efc | 2018-04-30 09:27:58 -0700 | [diff] [blame] | 191 | public LocationFunctionBuilder add(String label, String... paths) { |
| 192 | labelMap.put( |
| 193 | Label.parseAbsoluteUnchecked(label), |
| 194 | Arrays.stream(paths) |
| 195 | .map(LocationFunctionBuilder::makeArtifact) |
| 196 | .collect(Collectors.toList())); |
| 197 | return this; |
| 198 | } |
| 199 | |
| 200 | private static Artifact makeArtifact(String path) { |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 201 | FileSystem fs = new InMemoryFileSystem(DigestHashFunction.SHA256); |
dannark | 25d5efc | 2018-04-30 09:27:58 -0700 | [diff] [blame] | 202 | if (path.startsWith("/exec/out")) { |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 203 | return ActionsTestUtil.createArtifact( |
Googler | f0b0c39 | 2021-01-27 17:56:52 -0800 | [diff] [blame] | 204 | ArtifactRoot.asDerivedRoot(fs.getPath("/exec"), RootType.Output, "out"), |
Googler | 660f5b2 | 2021-01-25 09:40:17 -0800 | [diff] [blame] | 205 | fs.getPath(path)); |
dannark | 25d5efc | 2018-04-30 09:27:58 -0700 | [diff] [blame] | 206 | } else { |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 207 | return ActionsTestUtil.createArtifact( |
| 208 | ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/exec"))), fs.getPath(path)); |
ulfjack | 04509fa | 2017-10-02 14:52:14 +0200 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |