1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright 2018 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.runfiles;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import javax.annotation.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link Runfiles}. */
@RunWith(JUnit4.class)
public final class RunfilesTest {
private static boolean isWindows() {
return File.separatorChar == '\\';
}
private void assertRlocationArg(Runfiles runfiles, String path, @Nullable String error)
throws Exception {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> runfiles.rlocation(path));
if (error != null) {
assertThat(e).hasMessageThat().contains(error);
}
}
@Test
public void testRlocationArgumentValidation() throws Exception {
Path dir =
Files.createTempDirectory(
FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR")), null);
Runfiles r = Runfiles.create(ImmutableMap.of("RUNFILES_DIR", dir.toString()));
assertRlocationArg(r, null, null);
assertRlocationArg(r, "", null);
assertRlocationArg(r, "../foo", "is not normalized");
assertRlocationArg(r, "foo/..", "is not normalized");
assertRlocationArg(r, "foo/../bar", "is not normalized");
assertRlocationArg(r, "./foo", "is not normalized");
assertRlocationArg(r, "foo/.", "is not normalized");
assertRlocationArg(r, "foo/./bar", "is not normalized");
assertRlocationArg(r, "//foobar", "is not normalized");
assertRlocationArg(r, "foo//", "is not normalized");
assertRlocationArg(r, "foo//bar", "is not normalized");
assertRlocationArg(r, "\\foo", "path is absolute without a drive letter");
}
@Test
public void testCreatesManifestBasedRunfiles() throws Exception {
try (MockFile mf = new MockFile(ImmutableList.of("a/b c/d"))) {
Runfiles r =
Runfiles.create(
ImmutableMap.of(
"RUNFILES_MANIFEST_ONLY", "1",
"RUNFILES_MANIFEST_FILE", mf.path.toString(),
"RUNFILES_DIR", "ignored when RUNFILES_MANIFEST_ONLY=1",
"JAVA_RUNFILES", "ignored when RUNFILES_DIR has a value",
"TEST_SRCDIR", "should always be ignored"));
assertThat(r.rlocation("a/b")).isEqualTo("c/d");
assertThat(r.rlocation("foo")).isNull();
if (isWindows()) {
assertThat(r.rlocation("c:/foo")).isEqualTo("c:/foo");
assertThat(r.rlocation("c:\\foo")).isEqualTo("c:\\foo");
} else {
assertThat(r.rlocation("/foo")).isEqualTo("/foo");
}
}
}
@Test
public void testCreatesDirectoryBasedRunfiles() throws Exception {
Path dir =
Files.createTempDirectory(
FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR")), null);
Runfiles r =
Runfiles.create(
ImmutableMap.of(
"RUNFILES_MANIFEST_FILE", "ignored when RUNFILES_MANIFEST_ONLY is not set to 1",
"RUNFILES_DIR", dir.toString(),
"JAVA_RUNFILES", "ignored when RUNFILES_DIR has a value",
"TEST_SRCDIR", "should always be ignored"));
assertThat(r.rlocation("a/b")).endsWith("/a/b");
assertThat(r.rlocation("foo")).endsWith("/foo");
r =
Runfiles.create(
ImmutableMap.of(
"RUNFILES_MANIFEST_FILE", "ignored when RUNFILES_MANIFEST_ONLY is not set to 1",
"RUNFILES_DIR", "",
"JAVA_RUNFILES", dir.toString(),
"TEST_SRCDIR", "should always be ignored"));
assertThat(r.rlocation("a/b")).endsWith("/a/b");
assertThat(r.rlocation("foo")).endsWith("/foo");
}
@Test
public void testIgnoresTestSrcdirWhenJavaRunfilesIsUndefinedAndJustFails() throws Exception {
Path dir =
Files.createTempDirectory(
FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR")), null);
Runfiles.create(
ImmutableMap.of(
"RUNFILES_DIR", dir.toString(),
"RUNFILES_MANIFEST_FILE", "ignored when RUNFILES_MANIFEST_ONLY is not set to 1",
"TEST_SRCDIR", "should always be ignored"));
Runfiles.create(
ImmutableMap.of(
"JAVA_RUNFILES", dir.toString(),
"RUNFILES_MANIFEST_FILE", "ignored when RUNFILES_MANIFEST_ONLY is not set to 1",
"TEST_SRCDIR", "should always be ignored"));
IOException e =
assertThrows(
IOException.class,
() ->
Runfiles.create(
ImmutableMap.of(
"RUNFILES_DIR", "",
"JAVA_RUNFILES", "",
"RUNFILES_MANIFEST_FILE",
"ignored when RUNFILES_MANIFEST_ONLY is not set to 1",
"TEST_SRCDIR", "should always be ignored")));
assertThat(e).hasMessageThat().contains("$RUNFILES_DIR and $JAVA_RUNFILES");
}
@Test
public void testFailsToCreateManifestBasedBecauseManifestDoesNotExist() throws Exception {
IOException e =
assertThrows(
IOException.class,
() ->
Runfiles.create(
ImmutableMap.of(
"RUNFILES_MANIFEST_ONLY", "1",
"RUNFILES_MANIFEST_FILE", "non-existing path")));
assertThat(e).hasMessageThat().contains("non-existing path");
}
@Test
public void testManifestBasedEnvVars() throws Exception {
Path dir =
Files.createTempDirectory(
FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR")), null);
Path mf = dir.resolve("MANIFEST");
Files.write(mf, Collections.emptyList(), StandardCharsets.UTF_8);
Map<String, String> envvars =
Runfiles.create(
ImmutableMap.of(
"RUNFILES_MANIFEST_ONLY", "1",
"RUNFILES_MANIFEST_FILE", mf.toString(),
"RUNFILES_DIR", "ignored when RUNFILES_MANIFEST_ONLY=1",
"JAVA_RUNFILES", "ignored when RUNFILES_DIR has a value",
"TEST_SRCDIR", "should always be ignored"))
.getEnvVars();
assertThat(envvars.keySet())
.containsExactly(
"RUNFILES_MANIFEST_ONLY", "RUNFILES_MANIFEST_FILE", "RUNFILES_DIR", "JAVA_RUNFILES");
assertThat(envvars.get("RUNFILES_MANIFEST_ONLY")).isEqualTo("1");
assertThat(envvars.get("RUNFILES_MANIFEST_FILE")).isEqualTo(mf.toString());
assertThat(envvars.get("RUNFILES_DIR")).isEqualTo(dir.toString());
assertThat(envvars.get("JAVA_RUNFILES")).isEqualTo(dir.toString());
Path rfDir = dir.resolve("foo.runfiles");
Files.createDirectories(rfDir);
mf = dir.resolve("foo.runfiles_manifest");
Files.write(mf, Collections.emptyList(), StandardCharsets.UTF_8);
envvars =
Runfiles.create(
ImmutableMap.of(
"RUNFILES_MANIFEST_ONLY", "1",
"RUNFILES_MANIFEST_FILE", mf.toString(),
"RUNFILES_DIR", "ignored when RUNFILES_MANIFEST_ONLY=1",
"JAVA_RUNFILES", "ignored when RUNFILES_DIR has a value",
"TEST_SRCDIR", "should always be ignored"))
.getEnvVars();
assertThat(envvars.get("RUNFILES_MANIFEST_ONLY")).isEqualTo("1");
assertThat(envvars.get("RUNFILES_MANIFEST_FILE")).isEqualTo(mf.toString());
assertThat(envvars.get("RUNFILES_DIR")).isEqualTo(rfDir.toString());
assertThat(envvars.get("JAVA_RUNFILES")).isEqualTo(rfDir.toString());
}
@Test
public void testDirectoryBasedEnvVars() throws Exception {
Path dir =
Files.createTempDirectory(
FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR")), null);
Map<String, String> envvars =
Runfiles.create(
ImmutableMap.of(
"RUNFILES_MANIFEST_FILE",
"ignored when RUNFILES_MANIFEST_ONLY is not set to 1",
"RUNFILES_DIR",
dir.toString(),
"JAVA_RUNFILES",
"ignored when RUNFILES_DIR has a value",
"TEST_SRCDIR",
"should always be ignored"))
.getEnvVars();
assertThat(envvars.keySet()).containsExactly("RUNFILES_DIR", "JAVA_RUNFILES");
assertThat(envvars.get("RUNFILES_DIR")).isEqualTo(dir.toString());
assertThat(envvars.get("JAVA_RUNFILES")).isEqualTo(dir.toString());
}
@Test
public void testDirectoryBasedRlocation() throws Exception {
// The DirectoryBased implementation simply joins the runfiles directory and the runfile's path
// on a "/". DirectoryBased does not perform any normalization, nor does it check that the path
// exists.
File dir = new File(System.getenv("TEST_TMPDIR"), "mock/runfiles");
assertThat(dir.mkdirs()).isTrue();
Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString());
// Escaping for "\": once for string and once for regex.
assertThat(r.rlocation("arg")).matches(".*[/\\\\]mock[/\\\\]runfiles[/\\\\]arg");
}
@Test
public void testManifestBasedRlocation() throws Exception {
try (MockFile mf =
new MockFile(
ImmutableList.of(
"Foo/runfile1 C:/Actual Path\\runfile1",
"Foo/Bar/runfile2 D:\\the path\\run file 2.txt"))) {
Runfiles r = Runfiles.createManifestBasedForTesting(mf.path.toString());
assertThat(r.rlocation("Foo/runfile1")).isEqualTo("C:/Actual Path\\runfile1");
assertThat(r.rlocation("Foo/Bar/runfile2")).isEqualTo("D:\\the path\\run file 2.txt");
assertThat(r.rlocation("unknown")).isNull();
}
}
@Test
public void testDirectoryBasedCtorArgumentValidation() throws Exception {
assertThrows(
IllegalArgumentException.class, () -> Runfiles.createDirectoryBasedForTesting(null));
assertThrows(IllegalArgumentException.class, () -> Runfiles.createDirectoryBasedForTesting(""));
assertThrows(
IllegalArgumentException.class,
() -> Runfiles.createDirectoryBasedForTesting("non-existent directory is bad"));
Runfiles.createDirectoryBasedForTesting(System.getenv("TEST_TMPDIR"));
}
@Test
public void testManifestBasedCtorArgumentValidation() throws Exception {
assertThrows(
IllegalArgumentException.class, () -> Runfiles.createManifestBasedForTesting(null));
assertThrows(IllegalArgumentException.class, () -> Runfiles.createManifestBasedForTesting(""));
try (MockFile mf = new MockFile(ImmutableList.of("a b"))) {
Runfiles.createManifestBasedForTesting(mf.path.toString());
}
}
}
|