ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 1 | // Copyright 2015 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 | package com.google.devtools.build.lib.packages; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 17 | import static org.junit.Assert.assertThrows; |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 18 | |
lberki | 7abdcb4 | 2019-10-22 02:17:13 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
janakr | 13b737a | 2021-07-02 14:24:25 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.ThreadStateReceiver; |
Googler | 298da7b | 2024-10-15 05:18:43 -0700 | [diff] [blame^] | 22 | import com.google.devtools.build.lib.cmdline.IgnoredSubdirectories; |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 23 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 24 | import com.google.devtools.build.lib.packages.Globber.BadGlobException; |
| 25 | import com.google.devtools.build.lib.testutil.Scratch; |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 26 | import com.google.devtools.build.lib.util.Pair; |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 27 | import com.google.devtools.build.lib.vfs.Path; |
lberki | 7abdcb4 | 2019-10-22 02:17:13 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.vfs.PathFragment; |
janakr | fc1d79c | 2022-01-27 13:02:07 -0800 | [diff] [blame] | 29 | import com.google.devtools.build.lib.vfs.SyscallCache; |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.Arrays; |
| 32 | import java.util.Collection; |
| 33 | import java.util.Collections; |
| 34 | import java.util.List; |
michajlo | a731acd1 | 2021-12-29 12:34:00 -0800 | [diff] [blame] | 35 | import java.util.concurrent.ExecutorService; |
| 36 | import java.util.concurrent.Executors; |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 37 | import org.junit.After; |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.junit.runners.JUnit4; |
| 42 | |
| 43 | /** |
| 44 | * Tests for {@link GlobCache} |
| 45 | */ |
| 46 | @RunWith(JUnit4.class) |
| 47 | public class GlobCacheTest { |
| 48 | |
| 49 | private static final List<String> NONE = Collections.emptyList(); |
| 50 | |
| 51 | private Scratch scratch = new Scratch("/workspace"); |
| 52 | |
| 53 | private Path packageDirectory; |
| 54 | private Path buildFile; |
michajlo | a731acd1 | 2021-12-29 12:34:00 -0800 | [diff] [blame] | 55 | private ExecutorService cacheThreadPool; |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 56 | private GlobCache cache; |
| 57 | |
| 58 | @Before |
| 59 | public final void createFiles() throws Exception { |
| 60 | buildFile = scratch.file("isolated/BUILD", |
| 61 | "# contents don't matter in this test"); |
| 62 | scratch.file("isolated/sub/BUILD", |
| 63 | "# contents don't matter in this test"); |
| 64 | |
| 65 | packageDirectory = buildFile.getParentDirectory(); |
| 66 | |
| 67 | scratch.file("isolated/first.txt", |
| 68 | "# this is first.txt"); |
| 69 | |
| 70 | scratch.file("isolated/second.txt", |
| 71 | "# this is second.txt"); |
| 72 | |
| 73 | scratch.file("isolated/first.js", |
| 74 | "# this is first.js"); |
| 75 | |
| 76 | scratch.file("isolated/second.js", |
| 77 | "# this is second.js"); |
| 78 | |
| 79 | // Files in subdirectories |
| 80 | |
| 81 | scratch.file("isolated/foo/first.js", |
| 82 | "# this is foo/first.js"); |
| 83 | |
| 84 | scratch.file("isolated/foo/second.js", |
| 85 | "# this is foo/second.js"); |
| 86 | |
| 87 | scratch.file("isolated/bar/first.js", |
| 88 | "# this is bar/first.js"); |
| 89 | |
| 90 | scratch.file("isolated/bar/second.js", |
| 91 | "# this is bar/second.js"); |
| 92 | |
| 93 | scratch.file("isolated/sub/sub.js", |
| 94 | "# this is sub/sub.js"); |
| 95 | |
lberki | 7abdcb4 | 2019-10-22 02:17:13 -0700 | [diff] [blame] | 96 | createCache(); |
| 97 | } |
| 98 | |
michajlo | a731acd1 | 2021-12-29 12:34:00 -0800 | [diff] [blame] | 99 | @After |
| 100 | public void shutDownThreadPoolIfExists() { |
| 101 | if (cacheThreadPool != null) { |
| 102 | cacheThreadPool.shutdownNow(); |
| 103 | } |
| 104 | } |
| 105 | |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 106 | private void createCache(PathFragment... ignoredDirectories) { |
michajlo | a731acd1 | 2021-12-29 12:34:00 -0800 | [diff] [blame] | 107 | shutDownThreadPoolIfExists(); |
| 108 | cacheThreadPool = Executors.newFixedThreadPool(10); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 109 | cache = |
| 110 | new GlobCache( |
| 111 | packageDirectory, |
| 112 | PackageIdentifier.createInMainRepo("isolated"), |
Googler | 298da7b | 2024-10-15 05:18:43 -0700 | [diff] [blame^] | 113 | IgnoredSubdirectories.of(ImmutableSet.copyOf(ignoredDirectories)), |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 114 | new CachingPackageLocator() { |
| 115 | @Override |
| 116 | public Path getBuildFileForPackage(PackageIdentifier packageId) { |
| 117 | String packageName = packageId.getPackageFragment().getPathString(); |
| 118 | if (packageName.equals("isolated")) { |
| 119 | return scratch.resolve("isolated/BUILD"); |
| 120 | } else if (packageName.equals("isolated/sub")) { |
| 121 | return scratch.resolve("isolated/sub/BUILD"); |
| 122 | } else { |
| 123 | return null; |
| 124 | } |
| 125 | } |
janakr | 845f7c1 | 2021-08-10 13:14:03 -0700 | [diff] [blame] | 126 | |
| 127 | @Override |
| 128 | public String getBaseNameForLoadedPackage(PackageIdentifier packageName) { |
| 129 | Path buildFileForPackage = getBuildFileForPackage(packageName); |
| 130 | return buildFileForPackage == null ? null : buildFileForPackage.getBaseName(); |
| 131 | } |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 132 | }, |
janakr | fc1d79c | 2022-01-27 13:02:07 -0800 | [diff] [blame] | 133 | SyscallCache.NO_CACHE, |
michajlo | a731acd1 | 2021-12-29 12:34:00 -0800 | [diff] [blame] | 134 | cacheThreadPool, |
janakr | 13b737a | 2021-07-02 14:24:25 -0700 | [diff] [blame] | 135 | -1, |
| 136 | ThreadStateReceiver.NULL_INSTANCE); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @After |
| 140 | public final void deleteFiles() throws Exception { |
jmmv | 5cc1f65 | 2019-03-20 09:34:08 -0700 | [diff] [blame] | 141 | scratch.getFileSystem().getPath("/").deleteTreesBelow(); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 145 | public void testIgnoredDirectory() throws Exception { |
lberki | 7abdcb4 | 2019-10-22 02:17:13 -0700 | [diff] [blame] | 146 | createCache(PathFragment.create("isolated/foo")); |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 147 | List<Path> paths = cache.safeGlobUnsorted("**/*.js", Globber.Operation.FILES).get(); |
lberki | 7abdcb4 | 2019-10-22 02:17:13 -0700 | [diff] [blame] | 148 | assertPathsAre( |
| 149 | paths, |
| 150 | "/workspace/isolated/first.js", |
| 151 | "/workspace/isolated/second.js", |
| 152 | "/workspace/isolated/bar/first.js", |
| 153 | "/workspace/isolated/bar/second.js"); |
| 154 | } |
| 155 | |
| 156 | @Test |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 157 | public void testSafeGlob() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 158 | List<Path> paths = cache.safeGlobUnsorted("*.js", Globber.Operation.FILES_AND_DIRS).get(); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 159 | assertPathsAre(paths, |
| 160 | "/workspace/isolated/first.js", "/workspace/isolated/second.js"); |
| 161 | } |
| 162 | |
| 163 | @Test |
Jonathan Bluett-Duncan | 52c0417 | 2017-06-21 16:37:42 +0200 | [diff] [blame] | 164 | public void testSafeGlobInvalidPattern() throws Exception { |
| 165 | String invalidPattern = "Foo?.txt"; |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 166 | assertThrows( |
| 167 | BadGlobException.class, |
| 168 | () -> cache.safeGlobUnsorted(invalidPattern, Globber.Operation.FILES_AND_DIRS).get()); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void testGetGlob() throws Exception { |
| 173 | List<String> glob = cache.getGlobUnsorted("*.js"); |
| 174 | assertThat(glob).containsExactly("first.js", "second.js"); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void testGetGlob_subdirectory() throws Exception { |
| 179 | List<String> glob = cache.getGlobUnsorted("foo/*.js"); |
| 180 | assertThat(glob).containsExactly("foo/first.js", "foo/second.js"); |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void testGetKeySet() throws Exception { |
| 185 | assertThat(cache.getKeySet()).isEmpty(); |
| 186 | |
| 187 | cache.getGlobUnsorted("*.java"); |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 188 | assertThat(cache.getKeySet()) |
| 189 | .containsExactly(Pair.of("*.java", Globber.Operation.FILES_AND_DIRS)); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 190 | |
| 191 | cache.getGlobUnsorted("*.java"); |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 192 | assertThat(cache.getKeySet()) |
| 193 | .containsExactly(Pair.of("*.java", Globber.Operation.FILES_AND_DIRS)); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 194 | |
| 195 | cache.getGlobUnsorted("*.js"); |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 196 | assertThat(cache.getKeySet()) |
| 197 | .containsExactly( |
| 198 | Pair.of("*.java", Globber.Operation.FILES_AND_DIRS), |
| 199 | Pair.of("*.js", Globber.Operation.FILES_AND_DIRS)); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 200 | |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 201 | cache.getGlobUnsorted("*.java", Globber.Operation.FILES); |
| 202 | assertThat(cache.getKeySet()) |
| 203 | .containsExactly( |
| 204 | Pair.of("*.java", Globber.Operation.FILES_AND_DIRS), |
| 205 | Pair.of("*.js", Globber.Operation.FILES_AND_DIRS), |
| 206 | Pair.of("*.java", Globber.Operation.FILES)); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 207 | |
jcater | b922677 | 2019-04-29 12:04:52 -0700 | [diff] [blame] | 208 | assertThrows(BadGlobException.class, () -> cache.getGlobUnsorted("invalid?")); |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 209 | assertThat(cache.getKeySet()) |
| 210 | .containsExactly( |
| 211 | Pair.of("*.java", Globber.Operation.FILES_AND_DIRS), |
| 212 | Pair.of("*.js", Globber.Operation.FILES_AND_DIRS), |
| 213 | Pair.of("*.java", Globber.Operation.FILES)); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 214 | |
| 215 | cache.getGlobUnsorted("foo/first.*"); |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 216 | assertThat(cache.getKeySet()) |
| 217 | .containsExactly( |
| 218 | Pair.of("*.java", Globber.Operation.FILES_AND_DIRS), |
| 219 | Pair.of("*.java", Globber.Operation.FILES), |
| 220 | Pair.of("*.js", Globber.Operation.FILES_AND_DIRS), |
| 221 | Pair.of("foo/first.*", Globber.Operation.FILES_AND_DIRS)); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Test |
| 225 | public void testGlob() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 226 | assertEmpty(cache.globUnsorted(list("*.java"), NONE, Globber.Operation.FILES, true)); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 227 | |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 228 | assertThat(cache.globUnsorted(list("*.*"), NONE, Globber.Operation.FILES, true)) |
laurentlb | b340fb0 | 2019-04-29 09:38:19 -0700 | [diff] [blame] | 229 | .containsExactly("first.js", "first.txt", "second.js", "second.txt"); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 230 | |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 231 | assertThat(cache.globUnsorted(list("*.*"), list("first.js"), Globber.Operation.FILES, true)) |
laurentlb | b340fb0 | 2019-04-29 09:38:19 -0700 | [diff] [blame] | 232 | .containsExactly("first.txt", "second.js", "second.txt"); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 233 | |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 234 | assertThat(cache.globUnsorted(list("*.txt", "first.*"), NONE, Globber.Operation.FILES, true)) |
laurentlb | b340fb0 | 2019-04-29 09:38:19 -0700 | [diff] [blame] | 235 | .containsExactly("first.txt", "second.txt", "first.js"); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Test |
| 239 | public void testRecursiveGlobDoesNotMatchSubpackage() throws Exception { |
| 240 | List<String> glob = cache.getGlobUnsorted("**/*.js"); |
| 241 | assertThat(glob).containsExactly("first.js", "second.js", "foo/first.js", "bar/first.js", |
| 242 | "foo/second.js", "bar/second.js"); |
| 243 | } |
| 244 | |
| 245 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 246 | public void testSingleFileExclude_star() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 247 | assertThat( |
| 248 | cache.globUnsorted( |
| 249 | list("*"), list("first.txt"), Globber.Operation.FILES_AND_DIRS, true)) |
laurentlb | b340fb0 | 2019-04-29 09:38:19 -0700 | [diff] [blame] | 250 | .containsExactly("BUILD", "bar", "first.js", "foo", "second.js", "second.txt"); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 254 | public void testSingleFileExclude_starStar() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 255 | assertThat( |
| 256 | cache.globUnsorted( |
| 257 | list("**"), list("first.txt"), Globber.Operation.FILES_AND_DIRS, true)) |
laurentlb | b340fb0 | 2019-04-29 09:38:19 -0700 | [diff] [blame] | 258 | .containsExactly( |
| 259 | "BUILD", |
| 260 | "bar", |
| 261 | "bar/first.js", |
| 262 | "bar/second.js", |
| 263 | "first.js", |
| 264 | "foo", |
| 265 | "foo/first.js", |
| 266 | "foo/second.js", |
| 267 | "second.js", |
| 268 | "second.txt"); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 272 | public void testExcludeAll_star() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 273 | assertThat(cache.globUnsorted(list("*"), list("*"), Globber.Operation.FILES_AND_DIRS, true)) |
| 274 | .isEmpty(); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 278 | public void testExcludeAll_star_noMatchesAnyway() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 279 | assertThat(cache.globUnsorted(list("nope"), list("*"), Globber.Operation.FILES_AND_DIRS, true)) |
| 280 | .isEmpty(); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 284 | public void testExcludeAll_starStar() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 285 | assertThat(cache.globUnsorted(list("**"), list("**"), Globber.Operation.FILES_AND_DIRS, true)) |
| 286 | .isEmpty(); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 290 | public void testExcludeAll_manual() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 291 | assertThat( |
| 292 | cache.globUnsorted( |
| 293 | list("**"), list("*", "*/*", "*/*/*"), Globber.Operation.FILES_AND_DIRS, true)) |
| 294 | .isEmpty(); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Test |
| 298 | public void testSingleFileExcludeDoesntMatch() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 299 | assertThat( |
| 300 | cache.globUnsorted( |
| 301 | list("first.txt"), list("nope.txt"), Globber.Operation.FILES_AND_DIRS, true)) |
laurentlb | b340fb0 | 2019-04-29 09:38:19 -0700 | [diff] [blame] | 302 | .containsExactly("first.txt"); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | @Test |
| 306 | public void testExcludeDirectory() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 307 | assertThat(cache.globUnsorted(list("foo/*"), NONE, Globber.Operation.FILES, true)) |
laurentlb | b340fb0 | 2019-04-29 09:38:19 -0700 | [diff] [blame] | 308 | .containsExactly("foo/first.js", "foo/second.js"); |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 309 | assertThat( |
| 310 | cache.globUnsorted(list("foo/*"), list("foo"), Globber.Operation.FILES_AND_DIRS, true)) |
laurentlb | b340fb0 | 2019-04-29 09:38:19 -0700 | [diff] [blame] | 311 | .containsExactly("foo/first.js", "foo/second.js"); |
| 312 | } |
| 313 | |
| 314 | @Test |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 315 | public void testChildGlobWithChildExclude() throws Exception { |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 316 | assertThat( |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 317 | cache.globUnsorted( |
| 318 | list("foo/*"), list("foo/*"), Globber.Operation.FILES_AND_DIRS, true)) |
laurentlb | b340fb0 | 2019-04-29 09:38:19 -0700 | [diff] [blame] | 319 | .isEmpty(); |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 320 | assertThat( |
| 321 | cache.globUnsorted( |
| 322 | list("foo/first.js", "foo/second.js"), |
| 323 | list("foo/*"), |
| 324 | Globber.Operation.FILES_AND_DIRS, |
| 325 | true)) |
laurentlb | b340fb0 | 2019-04-29 09:38:19 -0700 | [diff] [blame] | 326 | .isEmpty(); |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 327 | assertThat( |
| 328 | cache.globUnsorted( |
| 329 | list("foo/first.js"), list("foo/first.js"), Globber.Operation.FILES_AND_DIRS, true)) |
| 330 | .isEmpty(); |
| 331 | assertThat( |
| 332 | cache.globUnsorted( |
| 333 | list("foo/first.js"), list("*/first.js"), Globber.Operation.FILES_AND_DIRS, true)) |
| 334 | .isEmpty(); |
| 335 | assertThat( |
| 336 | cache.globUnsorted( |
| 337 | list("foo/first.js"), list("*/*"), Globber.Operation.FILES_AND_DIRS, true)) |
| 338 | .isEmpty(); |
| 339 | } |
| 340 | |
| 341 | @Test |
Googler | ef758fa | 2023-12-18 09:13:50 -0800 | [diff] [blame] | 342 | public void testSubpackages_noWildcard() throws Exception { |
| 343 | assertThat(cache.globUnsorted(list("sub/sub.js"), list(), Globber.Operation.SUBPACKAGES, true)) |
| 344 | .isEmpty(); |
| 345 | } |
| 346 | |
| 347 | @Test |
| 348 | public void testSubpackages_simpleDoubleStar() throws Exception { |
kkress | 7dbabb4 | 2022-01-11 14:24:38 -0800 | [diff] [blame] | 349 | assertThat(cache.globUnsorted(list("**"), list(), Globber.Operation.SUBPACKAGES, true)) |
| 350 | .containsExactly("sub"); |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Googler | ef758fa | 2023-12-18 09:13:50 -0800 | [diff] [blame] | 353 | @Test |
| 354 | public void testSubpackages_onlySub() throws Exception { |
| 355 | assertThat(cache.globUnsorted(list("sub"), list(), Globber.Operation.SUBPACKAGES, true)) |
| 356 | .containsExactly("sub"); |
| 357 | } |
| 358 | |
| 359 | @Test |
| 360 | public void testSubpackages_singleStarsAfterSub() throws Exception { |
| 361 | assertThat(cache.globUnsorted(list("sub/*"), list(), Globber.Operation.SUBPACKAGES, true)) |
| 362 | .isEmpty(); |
| 363 | } |
| 364 | |
| 365 | @Test |
| 366 | public void testSubpackages_doubleStarsAfterSub() throws Exception { |
| 367 | assertThat(cache.globUnsorted(list("sub/**"), list(), Globber.Operation.SUBPACKAGES, true)) |
| 368 | .containsExactly("sub"); |
| 369 | } |
| 370 | |
| 371 | @Test |
| 372 | public void testSubpackages_twoDoubleStarsAfterSub() throws Exception { |
| 373 | // Both `**`s are considered to match no path fragments. |
| 374 | assertThat(cache.globUnsorted(list("sub/**/**"), list(), Globber.Operation.SUBPACKAGES, true)) |
| 375 | .containsExactly("sub"); |
| 376 | } |
| 377 | |
| 378 | @Test |
| 379 | public void testSubpackages_doubleStarsAndOtherPathAfterSub() throws Exception { |
| 380 | assertThat(cache.globUnsorted(list("sub/**/foo"), list(), Globber.Operation.SUBPACKAGES, true)) |
| 381 | .isEmpty(); |
| 382 | } |
| 383 | |
| 384 | @Test |
| 385 | public void testSubpackages_doubleStarWithTrailingPattern() throws Exception { |
| 386 | assertThat(cache.globUnsorted(list("**/bar"), list(), Globber.Operation.SUBPACKAGES, true)) |
| 387 | .isEmpty(); |
| 388 | } |
| 389 | |
ajmichael | edcd915 | 2017-05-01 19:56:06 +0200 | [diff] [blame] | 390 | private void assertEmpty(Collection<?> glob) { |
| 391 | assertThat(glob).isEmpty(); |
| 392 | } |
| 393 | |
| 394 | private void assertPathsAre(List<Path> paths, String... strings) { |
| 395 | List<String> pathStrings = new ArrayList<>(); |
| 396 | for (Path path : paths) { |
| 397 | pathStrings.add(path.getPathString()); |
| 398 | } |
| 399 | assertThat(pathStrings).containsExactlyElementsIn(Arrays.asList(strings)); |
| 400 | } |
| 401 | |
| 402 | /* syntactic shorthand for Lists.newArrayList(strings) */ |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 403 | private List<String> list(String... strings) { |
| 404 | return Lists.newArrayList(strings); |
| 405 | } |
| 406 | } |