aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/vfs/PathFragmentWindowsTest.java
blob: b2a8a52977dbd065fea1b3b2f04200d509e82fbe (plain)
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
// Copyright 2014 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.lib.vfs;

import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.vfs.PathFragment.create;

import com.google.devtools.build.lib.testutil.MoreAsserts;
import java.io.File;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * This class tests the functionality of the PathFragment.
 */
@RunWith(JUnit4.class)
public class PathFragmentWindowsTest {

  @Test
  public void testWindowsSeparator() {
    assertThat(create("bar\\baz").toString()).isEqualTo("bar/baz");
    assertThat(create("c:\\bar\\baz").toString()).isEqualTo("C:/bar/baz");
  }

  @Test
  public void testIsAbsoluteWindows() {
    assertThat(create("C:/").isAbsolute()).isTrue();
    assertThat(create("C:/").isAbsolute()).isTrue();
    assertThat(create("C:/foo").isAbsolute()).isTrue();
    assertThat(create("d:/foo/bar").isAbsolute()).isTrue();

    assertThat(create("*:/").isAbsolute()).isFalse();
  }

  @Test
  public void testAbsoluteAndAbsoluteLookingPaths() {
    assertThat(create("/c").isAbsolute()).isTrue();
    assertThat(create("/c").getSegments()).containsExactly("c");

    assertThat(create("/c/").isAbsolute()).isTrue();
    assertThat(create("/c/").getSegments()).containsExactly("c");

    assertThat(create("C:/").isAbsolute()).isTrue();
    assertThat(create("C:/").getSegments()).isEmpty();

    PathFragment p5 = create("/c:");
    assertThat(p5.isAbsolute()).isTrue();
    assertThat(p5.getSegments()).containsExactly("c:");
    assertThat(create("C:").isAbsolute()).isFalse();

    assertThat(create("/c:").isAbsolute()).isTrue();
    assertThat(create("/c:").getSegments()).containsExactly("c:");

    assertThat(create("/c")).isEqualTo(create("/c/"));
    assertThat(create("/c")).isNotEqualTo(create("C:/"));
    assertThat(create("/c")).isNotEqualTo(create("C:"));
    assertThat(create("/c")).isNotEqualTo(create("/c:"));
    assertThat(create("C:/")).isNotEqualTo(create("C:"));
    assertThat(create("C:/")).isNotEqualTo(create("/c:"));
  }

  @Test
  public void testIsAbsoluteWindowsBackslash() {
    assertThat(create(new File("C:\\blah").getPath()).isAbsolute()).isTrue();
    assertThat(create(new File("C:\\").getPath()).isAbsolute()).isTrue();
    assertThat(create(new File("\\blah").getPath()).isAbsolute()).isTrue();
    assertThat(create(new File("\\").getPath()).isAbsolute()).isTrue();
  }

  @Test
  public void testRootNodeReturnsRootStringWindows() {
    assertThat(create("C:/").getPathString()).isEqualTo("C:/");
  }

  @Test
  public void testGetRelativeWindows() {
    assertThat(create("C:/a").getRelative("b").getPathString()).isEqualTo("C:/a/b");
    assertThat(create("C:/a/b").getRelative("c/d").getPathString()).isEqualTo("C:/a/b/c/d");
    assertThat(create("C:/a").getRelative("C:/b").getPathString()).isEqualTo("C:/b");
    assertThat(create("C:/a/b").getRelative("C:/c/d").getPathString()).isEqualTo("C:/c/d");
    assertThat(create("a").getRelative("C:/b").getPathString()).isEqualTo("C:/b");
    assertThat(create("a/b").getRelative("C:/c/d").getPathString()).isEqualTo("C:/c/d");
  }

  @Test
  public void testGetRelativeMixed() throws Exception {
    assertThat(create("a").getRelative("b")).isEqualTo(create("a/b"));
    assertThat(create("a").getRelative("/b")).isEqualTo(create("/b"));
    assertThat(create("a").getRelative("E:/b")).isEqualTo(create("E:/b"));

    assertThat(create("/a").getRelative("b")).isEqualTo(create("/a/b"));
    assertThat(create("/a").getRelative("/b")).isEqualTo(create("/b"));
    assertThat(create("/a").getRelative("E:/b")).isEqualTo(create("E:/b"));

    assertThat(create("D:/a").getRelative("b")).isEqualTo(create("D:/a/b"));
    assertThat(create("D:/a").getRelative("/b")).isEqualTo(create("/b"));
    assertThat(create("D:/a").getRelative("E:/b")).isEqualTo(create("E:/b"));
  }

  @Test
  public void testRelativeTo() throws Exception {
    assertThat(create("").relativeTo("").getPathString()).isEqualTo("");
    MoreAsserts.expectThrows(IllegalArgumentException.class, () -> create("").relativeTo("a"));

    assertThat(create("a").relativeTo("")).isEqualTo(create("a"));
    assertThat(create("a").relativeTo("a").getPathString()).isEqualTo("");
    MoreAsserts.expectThrows(IllegalArgumentException.class, () -> create("a").relativeTo("b"));
    assertThat(create("a/b").relativeTo("a")).isEqualTo(create("b"));

    MoreAsserts.expectThrows(IllegalArgumentException.class, () -> create("C:/").relativeTo(""));
    assertThat(create("C:/").relativeTo("C:/").getPathString()).isEqualTo("");
  }

  @Test
  public void testGetChildWorks() {
    assertThat(create("../some/path").getChild("hi")).isEqualTo(create("../some/path/hi"));
  }

  @Test
  public void testEmptyPathToEmptyPathWindows() {
    assertThat(create("C:/")).isEqualTo(create("C:/"));
  }

  @Test
  public void testWindowsVolumeUppercase() {
    assertThat(create("C:/")).isEqualTo(create("c:/"));
  }

  @Test
  public void testRedundantSlashesWindows() {
    assertThat(create("C:/")).isEqualTo(create("C:///"));
    assertThat(create("C:/foo/bar")).isEqualTo(create("C:/foo///bar"));
    assertThat(create("C:/foo/bar")).isEqualTo(create("C:////foo//bar"));
  }

  @Test
  public void testSimpleNameToSimpleNameWindows() {
    assertThat(create("C:/foo")).isEqualTo(create("C:/foo"));
  }

  @Test
  public void testStripsTrailingSlashWindows() {
    assertThat(create("C:/foo/bar")).isEqualTo(create("C:/foo/bar/"));
  }

  @Test
  public void testGetParentDirectoryWindows() {
    assertThat(create("C:/foo/bar/wiz").getParentDirectory()).isEqualTo(create("C:/foo/bar"));
    assertThat(create("C:/foo/bar").getParentDirectory()).isEqualTo(create("C:/foo"));
    assertThat(create("C:/foo").getParentDirectory()).isEqualTo(create("C:/"));
    assertThat(create("C:/").getParentDirectory()).isNull();
  }

  @Test
  public void testSegmentsCountWindows() {
    assertThat(create("C:/foo").segmentCount()).isEqualTo(1);
    assertThat(create("C:/").segmentCount()).isEqualTo(0);
  }

  @Test
  public void testGetSegmentWindows() {
    assertThat(create("C:/foo/bar").getSegment(0)).isEqualTo("foo");
    assertThat(create("C:/foo/bar").getSegment(1)).isEqualTo("bar");
    assertThat(create("C:/foo/").getSegment(0)).isEqualTo("foo");
    assertThat(create("C:/foo").getSegment(0)).isEqualTo("foo");
  }

  @Test
  public void testBasenameWindows() throws Exception {
    assertThat(create("C:/foo/bar").getBaseName()).isEqualTo("bar");
    assertThat(create("C:/foo").getBaseName()).isEqualTo("foo");
    // Never return the drive name as a basename.
    assertThat(create("C:/").getBaseName()).isEmpty();
  }

  @Test
  public void testReplaceNameWindows() throws Exception {
    assertThat(create("C:/foo/bar").replaceName("baz").getPathString()).isEqualTo("C:/foo/baz");
    assertThat(create("C:/").replaceName("baz")).isNull();
  }

  @Test
  public void testStartsWithWindows() {
    assertThat(create("C:/foo/bar").startsWith(create("C:/foo"))).isTrue();
    assertThat(create("C:/foo/bar").startsWith(create("C:/"))).isTrue();
    assertThat(create("C:/").startsWith(create("C:/"))).isTrue();

    // The first path is absolute, the second is not.
    assertThat(create("C:/foo/bar").startsWith(create("C:"))).isFalse();
    assertThat(create("C:/").startsWith(create("C:"))).isFalse();
  }

  @Test
  public void testEndsWithWindows() {
    assertThat(create("C:/foo/bar").endsWith(create("bar"))).isTrue();
    assertThat(create("C:/foo/bar").endsWith(create("foo/bar"))).isTrue();
    assertThat(create("C:/foo/bar").endsWith(create("C:/foo/bar"))).isTrue();
    assertThat(create("C:/").endsWith(create("C:/"))).isTrue();
  }

  @Test
  public void testGetSafePathStringWindows() {
    assertThat(create("C:/").getSafePathString()).isEqualTo("C:/");
    assertThat(create("C:/abc").getSafePathString()).isEqualTo("C:/abc");
    assertThat(create("C:/abc/def").getSafePathString()).isEqualTo("C:/abc/def");
  }

  @Test
  public void testNormalizeWindows() {
    assertThat(create("C:/a/b")).isEqualTo(create("C:/a/b"));
    assertThat(create("C:/a/./b")).isEqualTo(create("C:/a/b"));
    assertThat(create("C:/a/../b")).isEqualTo(create("C:/b"));
    assertThat(create("C:/../b")).isEqualTo(create("C:/../b"));
  }

  @Test
  public void testWindowsDriveRelativePaths() throws Exception {
    // On Windows, paths that look like "C:foo" mean "foo relative to the current directory
    // of drive C:\".
    // Bazel doesn't resolve such paths, and just takes them literally like normal path segments.
    // If the user attempts to open files under such paths, the file system API will give an error.
    assertThat(create("C:").isAbsolute()).isFalse();
    assertThat(create("C:").getSegments()).containsExactly("C:");
  }

  @Test
  public void testToRelative() {
    assertThat(create("C:/foo/bar").toRelative()).isEqualTo(create("foo/bar"));
    assertThat(create("C:/").toRelative()).isEqualTo(create(""));
    MoreAsserts.expectThrows(IllegalArgumentException.class, () -> create("foo").toRelative());
  }

  @Test
  public void testGetDriveStr() {
    assertThat(create("C:/foo/bar").getDriveStr()).isEqualTo("C:/");
    assertThat(create("C:/").getDriveStr()).isEqualTo("C:/");
    MoreAsserts.expectThrows(IllegalArgumentException.class, () -> create("foo").getDriveStr());
  }
}