tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [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 | package com.google.devtools.build.lib.vfs; |
| 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; |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 18 | |
| 19 | import com.google.common.testing.EqualsTester; |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | import org.junit.runner.RunWith; |
| 22 | import org.junit.runners.JUnit4; |
| 23 | |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 24 | /** Tests the unix implementation of {@link Path}. */ |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 25 | @RunWith(JUnit4.class) |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 26 | public class UnixPathTest extends PathAbstractTest { |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 27 | |
| 28 | @Test |
| 29 | public void testEqualsAndHashCodeUnix() { |
| 30 | new EqualsTester() |
| 31 | .addEqualityGroup(create("/something/else")) |
| 32 | .addEqualityGroup(create("/"), create("//////")) |
| 33 | .testEquals(); |
| 34 | } |
| 35 | |
| 36 | @Test |
| 37 | public void testRelativeToUnix() { |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 38 | assertThat(create("/").relativeTo(create("/")).getPathString()).isEmpty(); |
| 39 | assertThat(create("/foo").relativeTo(create("/foo")).getPathString()).isEmpty(); |
| 40 | assertThat(create("/foo/bar/baz").relativeTo(create("/foo")).getPathString()) |
| 41 | .isEqualTo("bar/baz"); |
| 42 | assertThat(create("/foo/bar/baz").relativeTo(create("/foo/bar")).getPathString()) |
| 43 | .isEqualTo("baz"); |
| 44 | assertThat(create("/foo").relativeTo(create("/")).getPathString()).isEqualTo("foo"); |
| 45 | assertThrows( |
| 46 | IllegalArgumentException.class, () -> create("/foo/bar/baz").relativeTo(create("foo"))); |
| 47 | assertThrows( |
| 48 | IllegalArgumentException.class, () -> create("/foo").relativeTo(create("/foo/bar/baz"))); |
| 49 | } |
| 50 | |
| 51 | @Test |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 52 | public void testGetRelativeUnix() { |
| 53 | assertThat(create("/a").getRelative("b").getPathString()).isEqualTo("/a/b"); |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 54 | assertThat(create("/a/b").getRelative("c/d").getPathString()).isEqualTo("/a/b/c/d"); |
| 55 | assertThat(create("/c/d").getRelative("/a/b").getPathString()).isEqualTo("/a/b"); |
| 56 | assertThat(create("/a").getRelative("").getPathString()).isEqualTo("/a"); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 57 | assertThat(create("/").getRelative("").getPathString()).isEqualTo("/"); |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 58 | assertThat(create("/a/b").getRelative("../foo").getPathString()).isEqualTo("/a/foo"); |
| 59 | |
| 60 | // Make sure any fast path of Path#getRelative(PathFragment) works |
| 61 | assertThat(create("/a/b").getRelative(PathFragment.create("../foo")).getPathString()) |
| 62 | .isEqualTo("/a/foo"); |
| 63 | |
| 64 | // Make sure any fast path of Path#getRelative(PathFragment) works |
| 65 | assertThat(create("/c/d").getRelative(PathFragment.create("/a/b")).getPathString()) |
| 66 | .isEqualTo("/a/b"); |
| 67 | |
| 68 | // Test normalization |
| 69 | assertThat(create("/a").getRelative(".").getPathString()).isEqualTo("/a"); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void testEmptyPathToEmptyPathUnix() { |
| 74 | // compare string forms |
| 75 | assertThat(create("/").getPathString()).isEqualTo("/"); |
| 76 | // compare fragment forms |
| 77 | assertThat(create("/")).isEqualTo(create("/")); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testRedundantSlashes() { |
| 82 | // compare string forms |
| 83 | assertThat(create("///").getPathString()).isEqualTo("/"); |
| 84 | // compare fragment forms |
| 85 | assertThat(create("///")).isEqualTo(create("/")); |
| 86 | // compare string forms |
| 87 | assertThat(create("/foo///bar").getPathString()).isEqualTo("/foo/bar"); |
| 88 | // compare fragment forms |
| 89 | assertThat(create("/foo///bar")).isEqualTo(create("/foo/bar")); |
| 90 | // compare string forms |
| 91 | assertThat(create("////foo//bar").getPathString()).isEqualTo("/foo/bar"); |
| 92 | // compare fragment forms |
| 93 | assertThat(create("////foo//bar")).isEqualTo(create("/foo/bar")); |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testSimpleNameToSimpleNameUnix() { |
| 98 | // compare string forms |
| 99 | assertThat(create("/foo").getPathString()).isEqualTo("/foo"); |
| 100 | // compare fragment forms |
| 101 | assertThat(create("/foo")).isEqualTo(create("/foo")); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testSimplePathToSimplePathUnix() { |
| 106 | // compare string forms |
| 107 | assertThat(create("/foo/bar").getPathString()).isEqualTo("/foo/bar"); |
| 108 | // compare fragment forms |
| 109 | assertThat(create("/foo/bar")).isEqualTo(create("/foo/bar")); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testGetParentDirectoryUnix() { |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 114 | assertThat(create("/foo/bar/wiz").getParentDirectory()).isEqualTo(create("/foo/bar")); |
| 115 | assertThat(create("/foo/bar").getParentDirectory()).isEqualTo(create("/foo")); |
| 116 | assertThat(create("/foo").getParentDirectory()).isEqualTo(create("/")); |
| 117 | assertThat(create("/").getParentDirectory()).isNull(); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testBasenameUnix() throws Exception { |
| 122 | assertThat(create("/foo/bar").getBaseName()).isEqualTo("bar"); |
| 123 | assertThat(create("/foo/").getBaseName()).isEqualTo("foo"); |
| 124 | assertThat(create("/foo").getBaseName()).isEqualTo("foo"); |
| 125 | assertThat(create("/").getBaseName()).isEmpty(); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testStartsWithUnix() { |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 130 | Path foobar = create("/foo/bar"); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 131 | |
| 132 | // (path, prefix) => true |
| 133 | assertThat(foobar.startsWith(foobar)).isTrue(); |
| 134 | assertThat(foobar.startsWith(create("/"))).isTrue(); |
| 135 | assertThat(foobar.startsWith(create("/foo"))).isTrue(); |
| 136 | assertThat(foobar.startsWith(create("/foo/"))).isTrue(); |
| 137 | assertThat(foobar.startsWith(create("/foo/bar/"))).isTrue(); // Includes trailing slash. |
| 138 | |
| 139 | // (prefix, path) => false |
| 140 | assertThat(create("/foo").startsWith(foobar)).isFalse(); |
| 141 | assertThat(create("/").startsWith(foobar)).isFalse(); |
| 142 | |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 143 | // (path, sibling) => false |
| 144 | assertThat(create("/foo/wiz").startsWith(foobar)).isFalse(); |
| 145 | assertThat(foobar.startsWith(create("/foo/wiz"))).isFalse(); |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | public void testNormalizeUnix() { |
| 150 | assertThat(create("/a/b")).isEqualTo(create("/a/b")); |
| 151 | assertThat(create("/a/b/")).isEqualTo(create("/a/b")); |
| 152 | assertThat(create("/a/./b")).isEqualTo(create("/a/b")); |
| 153 | assertThat(create("/a/../b")).isEqualTo(create("/b")); |
| 154 | assertThat(create("/..")).isEqualTo(create("/..")); |
| 155 | } |
| 156 | |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 157 | @Test |
| 158 | public void testParentOfRootIsRootUnix() { |
| 159 | assertThat(create("/..")).isEqualTo(create("/")); |
| 160 | assertThat(create("/../../../../../..")).isEqualTo(create("/")); |
| 161 | assertThat(create("/../../../foo")).isEqualTo(create("/foo")); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 162 | } |
| 163 | } |