Han-Wen Nienhuys | 5afe76c | 2015-10-16 13:59:03 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All Rights Reserved. |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 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. |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 14 | package com.google.devtools.build.lib.cmdline; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 15 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 17 | import static org.junit.Assert.assertEquals; |
| 18 | import static org.junit.Assert.assertFalse; |
Brian Silverman | 22f287f | 2016-02-09 12:13:21 +0000 | [diff] [blame] | 19 | import static org.junit.Assert.assertTrue; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 20 | import static org.junit.Assert.fail; |
| 21 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.testutil.TestUtils; |
| 23 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 24 | |
| 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | import org.junit.runners.JUnit4; |
| 28 | |
| 29 | import java.util.regex.Pattern; |
| 30 | |
| 31 | /** |
| 32 | * Tests for {@link Label}. |
| 33 | */ |
| 34 | @RunWith(JUnit4.class) |
| 35 | public class LabelTest { |
| 36 | |
Han-Wen Nienhuys | 5afe76c | 2015-10-16 13:59:03 +0000 | [diff] [blame] | 37 | private static final String BAD_PACKAGE_CHARS = "package names may contain only"; |
| 38 | private static final String INVALID_TARGET_NAME = "invalid target name"; |
| 39 | private static final String INVALID_PACKAGE_NAME = "invalid package name"; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 40 | |
| 41 | @Test |
| 42 | public void testAbsolute() throws Exception { |
| 43 | { |
| 44 | Label l = Label.parseAbsolute("//foo/bar:baz"); |
| 45 | assertEquals("foo/bar", l.getPackageName()); |
| 46 | assertEquals("baz", l.getName()); |
| 47 | } |
| 48 | { |
| 49 | Label l = Label.parseAbsolute("//foo/bar"); |
| 50 | assertEquals("foo/bar", l.getPackageName()); |
| 51 | assertEquals("bar", l.getName()); |
| 52 | } |
Kristina Chodorow | 5284f32 | 2015-03-06 19:17:00 +0000 | [diff] [blame] | 53 | { |
| 54 | Label l = Label.parseAbsolute("//:bar"); |
| 55 | assertEquals("", l.getPackageName()); |
| 56 | assertEquals("bar", l.getName()); |
| 57 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 60 | private static String parseCommandLine(String label, String prefix) throws LabelSyntaxException { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 61 | return Label.parseCommandLineLabel(label, new PathFragment(prefix)).toString(); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testLabelResolution() throws Exception { |
| 66 | assertEquals("//absolute:label", parseCommandLine("//absolute:label", "")); |
| 67 | assertEquals("//absolute:label", parseCommandLine("//absolute:label", "absolute")); |
| 68 | assertEquals("//absolute:label", parseCommandLine(":label", "absolute")); |
| 69 | assertEquals("//absolute:label", parseCommandLine("label", "absolute")); |
| 70 | assertEquals("//absolute:label", parseCommandLine("absolute:label", "")); |
| 71 | assertEquals("//absolute/path:label", parseCommandLine("path:label", "absolute")); |
| 72 | assertEquals("//absolute/path:label/path", parseCommandLine("path:label/path", "absolute")); |
| 73 | assertEquals("//absolute:label/path", parseCommandLine("label/path", "absolute")); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testLabelResolutionAbsolutePath() throws Exception { |
| 78 | try { |
| 79 | parseCommandLine("//absolute:label", "/absolute"); |
| 80 | fail(); |
| 81 | } catch (IllegalArgumentException e) { |
| 82 | // Expected exception |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void testLabelResolutionBadSyntax() throws Exception { |
| 88 | try { |
| 89 | parseCommandLine("//absolute:A+bad%syntax", ""); |
| 90 | fail(); |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 91 | } catch (LabelSyntaxException e) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 92 | // Expected exception |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @Test |
Kristina Chodorow | b6fbab7 | 2016-01-28 14:38:31 +0000 | [diff] [blame] | 97 | public void testGetRelativeWithAbsoluteLabel() throws Exception { |
| 98 | Label base = Label.parseAbsolute("//foo/bar:baz"); |
| 99 | Label l = base.getRelative("//p1/p2:target"); |
| 100 | assertEquals("p1/p2", l.getPackageName()); |
| 101 | assertEquals("target", l.getName()); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testGetRelativeWithRelativeLabel() throws Exception { |
| 106 | Label base = Label.parseAbsolute("//foo/bar:baz"); |
| 107 | Label l = base.getRelative(":quux"); |
| 108 | assertEquals("foo/bar", l.getPackageName()); |
| 109 | assertEquals("quux", l.getName()); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testGetRelativeWithIllegalLabel() throws Exception { |
| 114 | Label base = Label.parseAbsolute("//foo/bar:baz"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 115 | try { |
| 116 | base.getRelative("/p1/p2:target"); |
| 117 | fail(); |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 118 | } catch (LabelSyntaxException e) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 119 | /* ok */ |
| 120 | } |
| 121 | try { |
| 122 | base.getRelative("quux:"); |
| 123 | fail(); |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 124 | } catch (LabelSyntaxException e) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 125 | /* ok */ |
| 126 | } |
| 127 | try { |
| 128 | base.getRelative(":"); |
| 129 | fail(); |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 130 | } catch (LabelSyntaxException e) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 131 | /* ok */ |
| 132 | } |
| 133 | try { |
| 134 | base.getRelative("::"); |
| 135 | fail(); |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 136 | } catch (LabelSyntaxException e) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 137 | /* ok */ |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @Test |
Kristina Chodorow | b6fbab7 | 2016-01-28 14:38:31 +0000 | [diff] [blame] | 142 | public void testGetRelativeWithDifferentRepo() throws Exception { |
| 143 | PackageIdentifier packageId = PackageIdentifier.create("@repo", new PathFragment("foo")); |
| 144 | Label base = Label.create(packageId, "bar"); |
| 145 | |
| 146 | Label relative = base.getRelative("@remote//x:y"); |
| 147 | |
| 148 | assertEquals(RepositoryName.create("@remote"), relative.getPackageIdentifier().getRepository()); |
| 149 | assertEquals(new PathFragment("x"), relative.getPackageFragment()); |
| 150 | assertEquals("y", relative.getName()); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void testGetRelativeWithRepoLocalAbsoluteLabel() throws Exception { |
| 155 | PackageIdentifier packageId = PackageIdentifier.create("@repo", new PathFragment("foo")); |
| 156 | Label base = Label.create(packageId, "bar"); |
| 157 | |
| 158 | Label relative = base.getRelative("//x:y"); |
| 159 | |
| 160 | assertEquals(packageId.getRepository(), relative.getPackageIdentifier().getRepository()); |
| 161 | assertEquals(new PathFragment("x"), relative.getPackageFragment()); |
| 162 | assertEquals("y", relative.getName()); |
| 163 | } |
| 164 | |
| 165 | @Test |
| 166 | public void testGetRelativeWithLocalRepoRelativeLabel() throws Exception { |
| 167 | PackageIdentifier packageId = PackageIdentifier.create("@repo", new PathFragment("foo")); |
| 168 | Label base = Label.create(packageId, "bar"); |
| 169 | |
| 170 | Label relative = base.getRelative(":y"); |
| 171 | |
| 172 | assertEquals(packageId.getRepository(), relative.getPackageIdentifier().getRepository()); |
| 173 | assertEquals(new PathFragment("foo"), relative.getPackageFragment()); |
| 174 | assertEquals("y", relative.getName()); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void testGetRelativeWithRepoAndReservedPackage() throws Exception { |
| 179 | PackageIdentifier packageId = PackageIdentifier.create("@repo", new PathFragment("foo")); |
| 180 | Label base = Label.create(packageId, "bar"); |
| 181 | |
| 182 | Label relative = base.getRelative("//conditions:default"); |
| 183 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 184 | PackageIdentifier expected = PackageIdentifier.createInMainRepo("conditions"); |
Kristina Chodorow | b6fbab7 | 2016-01-28 14:38:31 +0000 | [diff] [blame] | 185 | assertEquals(expected.getRepository(), relative.getPackageIdentifier().getRepository()); |
| 186 | assertEquals(expected.getPackageFragment(), relative.getPackageFragment()); |
| 187 | assertEquals("default", relative.getName()); |
| 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void testGetRelativeWithRemoteRepoToDefaultRepo() throws Exception { |
| 192 | PackageIdentifier packageId = PackageIdentifier.create("@repo", new PathFragment("foo")); |
| 193 | Label base = Label.create(packageId, "bar"); |
| 194 | |
| 195 | Label relative = base.getRelative("@//x:y"); |
| 196 | |
| 197 | assertEquals(RepositoryName.create("@"), relative.getPackageIdentifier().getRepository()); |
| 198 | assertEquals(new PathFragment("x"), relative.getPackageFragment()); |
| 199 | assertEquals("y", relative.getName()); |
| 200 | } |
| 201 | |
| 202 | @Test |
Brian Silverman | 22f287f | 2016-02-09 12:13:21 +0000 | [diff] [blame] | 203 | public void testGetRepositoryRelative() throws Exception { |
| 204 | Label defaultBase = Label.parseAbsolute("//foo/bar:baz"); |
| 205 | Label repoBase = Label.parseAbsolute("@repo//foo/bar:baz"); |
| 206 | Label mainBase = Label.parseAbsolute("@//foo/bar:baz"); |
| 207 | Label externalTarget = Label.parseAbsolute("//external:target"); |
| 208 | Label l = defaultBase.resolveRepositoryRelative(externalTarget); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 209 | assertTrue(l.getPackageIdentifier().getRepository().isMain()); |
Brian Silverman | 22f287f | 2016-02-09 12:13:21 +0000 | [diff] [blame] | 210 | assertEquals("external", l.getPackageName()); |
| 211 | assertEquals("target", l.getName()); |
| 212 | assertEquals(l, repoBase.resolveRepositoryRelative(externalTarget)); |
| 213 | assertEquals(l, mainBase.resolveRepositoryRelative(externalTarget)); |
| 214 | } |
| 215 | |
| 216 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 217 | public void testFactory() throws Exception { |
| 218 | Label l = Label.create("foo/bar", "quux"); |
| 219 | assertEquals("foo/bar", l.getPackageName()); |
| 220 | assertEquals("quux", l.getName()); |
| 221 | } |
| 222 | |
| 223 | @Test |
| 224 | public void testIdentities() throws Exception { |
| 225 | |
| 226 | Label l1 = Label.parseAbsolute("//foo/bar:baz"); |
| 227 | Label l2 = Label.parseAbsolute("//foo/bar:baz"); |
| 228 | Label l3 = Label.parseAbsolute("//foo/bar:quux"); |
| 229 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 230 | assertEquals(l1, l1); |
| 231 | assertEquals(l1, l2); |
| 232 | assertEquals(l2, l1); |
| 233 | assertEquals(l1, l2); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 234 | |
| 235 | assertFalse(l3.equals(l1)); |
| 236 | assertFalse(l1.equals(l3)); |
| 237 | |
| 238 | assertEquals(l1.hashCode(), l2.hashCode()); |
| 239 | } |
| 240 | |
| 241 | @Test |
| 242 | public void testToString() throws Exception { |
| 243 | { |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 244 | String s = "@//foo/bar:baz"; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 245 | Label l = Label.parseAbsolute(s); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 246 | assertEquals("//foo/bar:baz", l.toString()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 247 | } |
| 248 | { |
| 249 | Label l = Label.parseAbsolute("//foo/bar"); |
| 250 | assertEquals("//foo/bar:bar", l.toString()); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | @Test |
| 255 | public void testDotDot() throws Exception { |
| 256 | Label.parseAbsolute("//foo/bar:baz..gif"); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Asserts that creating a label throws a SyntaxException. |
| 261 | * @param label the label to create. |
| 262 | */ |
| 263 | private static void assertSyntaxError(String expectedError, String label) { |
| 264 | try { |
| 265 | Label.parseAbsolute(label); |
| 266 | fail("Label '" + label + "' did not contain a syntax error"); |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 267 | } catch (LabelSyntaxException e) { |
Googler | 2fa3ccf | 2015-11-10 14:30:39 +0000 | [diff] [blame] | 268 | assertThat(e.getMessage()).containsMatch(Pattern.quote(expectedError)); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | @Test |
| 273 | public void testBadCharacters() throws Exception { |
| 274 | assertSyntaxError("package names may contain only", |
| 275 | "//foo/bar baz"); |
| 276 | assertSyntaxError("target names may not contain ':'", |
| 277 | "//foo:bar:baz"); |
| 278 | assertSyntaxError("target names may not contain ':'", |
| 279 | "//foo:bar:"); |
| 280 | assertSyntaxError("target names may not contain ':'", |
| 281 | "//foo/bar::"); |
| 282 | assertSyntaxError("target names may not contain '&'", |
| 283 | "//foo:bar&"); |
| 284 | assertSyntaxError("target names may not contain '$'", |
| 285 | "//foo/bar:baz$a"); |
| 286 | assertSyntaxError("target names may not contain '('", |
| 287 | "//foo/bar:baz(foo)"); |
| 288 | assertSyntaxError("target names may not contain ')'", |
| 289 | "//foo/bar:bazfoo)"); |
Janak Ramakrishnan | e60d55d | 2015-12-16 03:34:24 +0000 | [diff] [blame] | 290 | // Warning: if these assertions are false, tools that assume that they can safely quote labels |
| 291 | // may need to be fixed. Please consult with bazel-dev before loosening these restrictions. |
| 292 | assertSyntaxError("target names may not contain '''", "//foo/bar:baz'foo"); |
| 293 | assertSyntaxError("target names may not contain '\"'", "//foo/bar:baz\"foo"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Test |
| 297 | public void testUplevelReferences() throws Exception { |
Han-Wen Nienhuys | 5afe76c | 2015-10-16 13:59:03 +0000 | [diff] [blame] | 298 | assertSyntaxError(INVALID_PACKAGE_NAME, "//foo/bar/..:baz"); |
| 299 | assertSyntaxError(INVALID_PACKAGE_NAME, "//foo/../baz:baz"); |
| 300 | assertSyntaxError(INVALID_PACKAGE_NAME, "//../bar/baz:baz"); |
| 301 | assertSyntaxError(INVALID_PACKAGE_NAME, "//..:foo"); |
| 302 | assertSyntaxError(INVALID_TARGET_NAME, "//foo:bar/../baz"); |
| 303 | assertSyntaxError(INVALID_TARGET_NAME, "//foo:../bar/baz"); |
| 304 | assertSyntaxError(INVALID_TARGET_NAME, "//foo:bar/baz/.."); |
| 305 | assertSyntaxError(INVALID_TARGET_NAME, "//foo:.."); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Test |
| 309 | public void testDotAsAPathSegment() throws Exception { |
Han-Wen Nienhuys | 5afe76c | 2015-10-16 13:59:03 +0000 | [diff] [blame] | 310 | assertSyntaxError(INVALID_PACKAGE_NAME, "//foo/bar/.:baz"); |
| 311 | assertSyntaxError(INVALID_PACKAGE_NAME, "//foo/./baz:baz"); |
| 312 | assertSyntaxError(INVALID_PACKAGE_NAME, "//./bar/baz:baz"); |
| 313 | assertSyntaxError(INVALID_TARGET_NAME, "//foo:bar/./baz"); |
| 314 | assertSyntaxError(INVALID_TARGET_NAME, "//foo:./bar/baz"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 315 | // TODO(bazel-team): enable when we have removed the "Workaround" in Label |
| 316 | // that rewrites broken Labels by removing the trailing '.' |
Han-Wen Nienhuys | 5afe76c | 2015-10-16 13:59:03 +0000 | [diff] [blame] | 317 | //assertSyntaxError(INVALID_PACKAGE_NAME, |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 318 | // "//foo:bar/baz/."); |
Han-Wen Nienhuys | 5afe76c | 2015-10-16 13:59:03 +0000 | [diff] [blame] | 319 | //assertSyntaxError(INVALID_PACKAGE_NAME, |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 320 | // "//foo:."); |
| 321 | } |
| 322 | |
| 323 | @Test |
| 324 | public void testTrailingDotSegment() throws Exception { |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 325 | assertEquals(Label |
| 326 | .parseAbsolute("//foo:dir/."), Label |
| 327 | .parseAbsolute("//foo:dir")); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Test |
| 331 | public void testSomeOtherBadLabels() throws Exception { |
| 332 | assertSyntaxError("package names may not end with '/'", |
| 333 | "//foo/:bar"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 334 | assertSyntaxError("package names may not start with '/'", "///p:foo"); |
| 335 | assertSyntaxError("package names may not contain '//' path separators", |
| 336 | "//a//b:foo"); |
| 337 | } |
| 338 | |
| 339 | @Test |
| 340 | public void testSomeGoodLabels() throws Exception { |
| 341 | Label.parseAbsolute("//foo:..bar"); |
| 342 | Label.parseAbsolute("//Foo:..bar"); |
| 343 | Label.parseAbsolute("//-Foo:..bar"); |
| 344 | Label.parseAbsolute("//00:..bar"); |
| 345 | Label.parseAbsolute("//package:foo+bar"); |
| 346 | Label.parseAbsolute("//package:foo_bar"); |
| 347 | Label.parseAbsolute("//package:foo=bar"); |
| 348 | Label.parseAbsolute("//package:foo-bar"); |
| 349 | Label.parseAbsolute("//package:foo.bar"); |
| 350 | Label.parseAbsolute("//package:foo@bar"); |
| 351 | Label.parseAbsolute("//package:foo~bar"); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Regression test: we previously expanded the set of characters which are considered label chars |
| 356 | * to include "@" (see test above). An unexpected side-effect is that "@D" in genrule(cmd) was |
| 357 | * considered to be a valid relative label! The fix is to forbid "@x" in package names. |
| 358 | */ |
| 359 | @Test |
| 360 | public void testAtVersionIsIllegal() throws Exception { |
| 361 | assertSyntaxError(BAD_PACKAGE_CHARS, "//foo/bar@123:baz"); |
| 362 | } |
| 363 | |
| 364 | @Test |
| 365 | public void testDoubleSlashPathSeparator() throws Exception { |
| 366 | assertSyntaxError("package names may not contain '//' path separators", |
| 367 | "//foo//bar:baz"); |
| 368 | assertSyntaxError("target names may not contain '//' path separator", |
| 369 | "//foo:bar//baz"); |
| 370 | } |
| 371 | |
| 372 | @Test |
| 373 | public void testNonPrintableCharacters() throws Exception { |
| 374 | assertSyntaxError( |
| 375 | "target names may not contain non-printable characters: '\\x02'", |
| 376 | "//foo:..\002bar"); |
| 377 | } |
| 378 | |
| 379 | /** Make sure that control characters - such as CR - are escaped on output. */ |
| 380 | @Test |
| 381 | public void testInvalidLineEndings() throws Exception { |
| 382 | assertSyntaxError("invalid target name '..bar\\r': " |
| 383 | + "target names may not end with carriage returns", "//foo:..bar\r"); |
| 384 | } |
| 385 | |
| 386 | @Test |
| 387 | public void testEmptyName() throws Exception { |
| 388 | assertSyntaxError("invalid target name '': empty target name", "//foo/bar:"); |
| 389 | } |
| 390 | |
| 391 | @Test |
| 392 | public void testSerializationSimple() throws Exception { |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 393 | checkSerialization("//a", 93); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | @Test |
| 397 | public void testSerializationNested() throws Exception { |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 398 | checkSerialization("//foo/bar:baz", 101); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | @Test |
| 402 | public void testSerializationWithoutTargetName() throws Exception { |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 403 | checkSerialization("//foo/bar", 101); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | private void checkSerialization(String labelString, int expectedSize) throws Exception { |
| 407 | Label a = Label.parseAbsolute(labelString); |
| 408 | byte[] sa = TestUtils.serializeObject(a); |
| 409 | assertEquals(expectedSize, sa.length); |
| 410 | |
| 411 | Label a2 = (Label) TestUtils.deserializeObject(sa); |
| 412 | assertEquals(a, a2); |
| 413 | } |
| 414 | |
| 415 | @Test |
| 416 | public void testRepoLabel() throws Exception { |
Kristina Chodorow | 0f85e10 | 2015-05-22 14:03:02 +0000 | [diff] [blame] | 417 | Label label = Label.parseAbsolute("@foo//bar/baz:bat/boo"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 418 | assertEquals("@foo//bar/baz:bat/boo", label.toString()); |
| 419 | } |
| 420 | |
| 421 | @Test |
| 422 | public void testNoRepo() throws Exception { |
Kristina Chodorow | 0f85e10 | 2015-05-22 14:03:02 +0000 | [diff] [blame] | 423 | Label label = Label.parseAbsolute("//bar/baz:bat/boo"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 424 | assertEquals("//bar/baz:bat/boo", label.toString()); |
| 425 | } |
| 426 | |
| 427 | @Test |
| 428 | public void testInvalidRepo() throws Exception { |
| 429 | try { |
Kristina Chodorow | 0f85e10 | 2015-05-22 14:03:02 +0000 | [diff] [blame] | 430 | Label.parseAbsolute("foo//bar/baz:bat/boo"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 431 | fail(); |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 432 | } catch (LabelSyntaxException e) { |
Kristina Chodorow | e76dd5e | 2015-09-18 13:45:55 +0000 | [diff] [blame] | 433 | assertThat(e).hasMessage( |
| 434 | "invalid repository name 'foo': workspace names must start with '@'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
Damien Martin-Guillerez | 7751d43 | 2016-01-15 11:22:31 +0000 | [diff] [blame] | 437 | |
| 438 | @Test |
| 439 | public void testGetWorkspaceRoot() throws Exception { |
| 440 | Label label = Label.parseAbsolute("//bar/baz"); |
| 441 | assertThat(label.getWorkspaceRoot()).isEmpty(); |
| 442 | label = Label.parseAbsolute("@repo//bar/baz"); |
Kristina Chodorow | bdfd58a | 2016-06-16 20:30:57 +0000 | [diff] [blame^] | 443 | assertThat(label.getWorkspaceRoot()).isEqualTo("../repo"); |
Damien Martin-Guillerez | 7751d43 | 2016-01-15 11:22:31 +0000 | [diff] [blame] | 444 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 445 | } |