blob: 7d26be47af74239d446c8fb6aecb9e028de5edf2 [file] [log] [blame]
tomlu4a2f2c52017-12-12 12:32:22 -08001// 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.
14package com.google.devtools.build.lib.vfs;
15
16import static com.google.common.truth.Truth.assertThat;
michajlo660d17f2020-03-27 09:01:57 -070017import static org.junit.Assert.assertThrows;
tomlu4a2f2c52017-12-12 12:32:22 -080018
19import com.google.common.testing.EqualsTester;
tomlu4a2f2c52017-12-12 12:32:22 -080020import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.junit.runners.JUnit4;
23
tomlua729b9b2018-02-08 15:32:00 -080024/** Tests the unix implementation of {@link Path}. */
tomlu4a2f2c52017-12-12 12:32:22 -080025@RunWith(JUnit4.class)
tomlua729b9b2018-02-08 15:32:00 -080026public class UnixPathTest extends PathAbstractTest {
tomlu4a2f2c52017-12-12 12:32:22 -080027
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() {
tomlu4a2f2c52017-12-12 12:32:22 -080038 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
tomlu4a2f2c52017-12-12 12:32:22 -080052 public void testGetRelativeUnix() {
53 assertThat(create("/a").getRelative("b").getPathString()).isEqualTo("/a/b");
tomlua729b9b2018-02-08 15:32:00 -080054 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");
tomlu4a2f2c52017-12-12 12:32:22 -080057 assertThat(create("/").getRelative("").getPathString()).isEqualTo("/");
tomlua729b9b2018-02-08 15:32:00 -080058 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");
tomlu4a2f2c52017-12-12 12:32:22 -080070 }
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() {
tomlua729b9b2018-02-08 15:32:00 -0800114 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();
tomlu4a2f2c52017-12-12 12:32:22 -0800118 }
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() {
tomlua729b9b2018-02-08 15:32:00 -0800130 Path foobar = create("/foo/bar");
tomlu4a2f2c52017-12-12 12:32:22 -0800131
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
tomlu4a2f2c52017-12-12 12:32:22 -0800143 // (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
tomlua729b9b2018-02-08 15:32:00 -0800157 @Test
158 public void testParentOfRootIsRootUnix() {
159 assertThat(create("/..")).isEqualTo(create("/"));
160 assertThat(create("/../../../../../..")).isEqualTo(create("/"));
161 assertThat(create("/../../../foo")).isEqualTo(create("/foo"));
tomlu4a2f2c52017-12-12 12:32:22 -0800162 }
163}