blob: 1cd3e85239d1796494ea88452ec026fe072e14f1 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
Ulf Adamscceeac62016-05-18 14:09:43 +000016import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertSame;
19
Laszlo Csomorca99bb72016-10-25 13:15:55 +000020import com.google.common.base.Predicate;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021import com.google.devtools.build.lib.util.BlazeClock;
Laszlo Csomorca99bb72016-10-25 13:15:55 +000022import com.google.devtools.build.lib.vfs.Path.PathFactory;
23import com.google.devtools.build.lib.vfs.WindowsFileSystem.WindowsPathFactory;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
Laszlo Csomorca99bb72016-10-25 13:15:55 +000025import java.util.ArrayList;
26import java.util.List;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.junit.runners.JUnit4;
31
32/**
33 * A test for windows aspects of {@link Path}.
34 */
35@RunWith(JUnit4.class)
36public class PathWindowsTest {
37 private FileSystem filesystem;
38 private Path root;
39
40 @Before
Florian Weikert99ac0a22015-12-01 15:05:35 +000041 public final void initializeFileSystem() throws Exception {
Laszlo Csomorca99bb72016-10-25 13:15:55 +000042 filesystem =
43 new InMemoryFileSystem(BlazeClock.instance()) {
44 @Override
45 protected PathFactory getPathFactory() {
46 return WindowsPathFactory.INSTANCE;
47 }
48 };
49 root = filesystem.getRootDirectory().getRelative("C:/");
50 root.createDirectory();
51
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010052 Path first = root.getChild("first");
53 first.createDirectory();
54 }
55
56 private void assertAsFragmentWorks(String expected) {
57 assertEquals(new PathFragment(expected), filesystem.getPath(expected).asFragment());
58 }
59
60 @Test
61 public void testWindowsPath() {
62 Path p = filesystem.getPath("C:/foo/bar");
63 assertEquals("C:/foo/bar", p.getPathString());
64 assertEquals("C:/foo/bar", p.toString());
65 }
66
67 @Test
68 public void testAsFragmentWindows() {
69 assertAsFragmentWorks("C:/");
70 assertAsFragmentWorks("C://");
71 assertAsFragmentWorks("C:/first");
72 assertAsFragmentWorks("C:/first/x/y");
73 assertAsFragmentWorks("C:/first/x/y.foo");
74 }
75
76 @Test
77 public void testGetRelativeWithFragmentWindows() {
78 Path dir = filesystem.getPath("C:/first/x");
79 assertEquals("C:/first/x/y",
80 dir.getRelative(new PathFragment("y")).toString());
81 assertEquals("C:/first/x/x",
82 dir.getRelative(new PathFragment("./x")).toString());
83 assertEquals("C:/first/y",
84 dir.getRelative(new PathFragment("../y")).toString());
85 assertEquals("C:/first/y",
86 dir.getRelative(new PathFragment("../y")).toString());
87 assertEquals("C:/y",
88 dir.getRelative(new PathFragment("../../../y")).toString());
89 }
90
91 @Test
92 public void testGetRelativeWithAbsoluteFragmentWindows() {
93 Path root = filesystem.getPath("C:/first/x");
94 assertEquals("C:/x/y",
95 root.getRelative(new PathFragment("C:/x/y")).toString());
96 }
97
98 @Test
99 public void testGetRelativeWithAbsoluteStringWorksWindows() {
100 Path root = filesystem.getPath("C:/first/x");
101 assertEquals("C:/x/y", root.getRelative("C:/x/y").toString());
102 }
103
104 @Test
105 public void testParentOfRootIsRootWindows() {
106 assertSame(root, root.getRelative(".."));
107
108 assertSame(root.getRelative("dots"),
109 root.getRelative("broken/../../dots"));
110 }
Yun Peng352f7e72016-05-09 11:08:25 +0000111
112 @Test
Laszlo Csomorca99bb72016-10-25 13:15:55 +0000113 public void testAbsoluteUnixPathIsRelativeToWindowsUnixRoot() {
114 Path actual = root.getRelative("/foo/bar");
115 Path expected = root.getRelative("C:/fake/msys/foo/bar");
116 assertThat(actual.getPathString()).isEqualTo(expected.getPathString());
117 assertThat(actual).isEqualTo(expected);
118 }
119
120 @Test
121 public void testAbsoluteUnixPathReferringToDriveIsRecognized() {
122 Path actual = root.getRelative("/c/foo");
123 Path expected = root.getRelative("C:/foo");
Laszlo Csomorff8fcf02016-10-26 16:20:27 +0000124 Path weird = root.getRelative("/c:");
Laszlo Csomorca99bb72016-10-25 13:15:55 +0000125 assertThat(actual.getPathString()).isEqualTo(expected.getPathString());
126 assertThat(actual).isEqualTo(expected);
Laszlo Csomorff8fcf02016-10-26 16:20:27 +0000127 assertThat(weird).isNotEqualTo(expected);
Laszlo Csomorca99bb72016-10-25 13:15:55 +0000128 }
129
130 @Test
Yun Peng352f7e72016-05-09 11:08:25 +0000131 public void testStartsWithWorksOnWindows() {
132 assertStartsWithReturnsOnWindows(true, "C:/first/x", "C:/first/x/y");
133 assertStartsWithReturnsOnWindows(true, "c:/first/x", "C:/FIRST/X/Y");
134 assertStartsWithReturnsOnWindows(true, "C:/FIRST/X", "c:/first/x/y");
Laszlo Csomorca99bb72016-10-25 13:15:55 +0000135 assertStartsWithReturnsOnWindows(true, "/", "C:/");
136 assertStartsWithReturnsOnWindows(false, "C:/", "/");
137 assertStartsWithReturnsOnWindows(false, "C:/", "D:/");
138 assertStartsWithReturnsOnWindows(false, "C:/", "D:/foo");
Yun Peng352f7e72016-05-09 11:08:25 +0000139 }
140
Ulf Adamscceeac62016-05-18 14:09:43 +0000141 @Test
142 public void testGetRelative() {
143 Path root = filesystem.getPath("C:\\first\\x");
144 Path other = root.getRelative("a\\b\\c");
145 assertThat(other.asFragment().getPathString()).isEqualTo("C:/first/x/a/b/c");
146 }
147
Yun Peng352f7e72016-05-09 11:08:25 +0000148 private void assertStartsWithReturnsOnWindows(boolean expected,
149 String ancestor,
150 String descendant) {
151 FileSystem windowsFileSystem = new WindowsFileSystem();
152 Path parent = windowsFileSystem.getPath(ancestor);
153 Path child = windowsFileSystem.getPath(descendant);
154 assertEquals(expected, child.startsWith(parent));
155 }
Laszlo Csomorca99bb72016-10-25 13:15:55 +0000156
157 @Test
158 public void testChildRegistrationWithTranslatedPaths() {
159 // Ensure the Path to "/usr" (actually "C:/fake/msys/usr") is created, path parents/children
160 // properly registered.
161 Path usrPath = root.getRelative("/usr");
162
163 // Assert that "usr" is not registered as a child of "/".
164 final List<String> children = new ArrayList<>(2);
165 root.applyToChildren(
166 new Predicate<Path>() {
167 @Override
168 public boolean apply(Path input) {
169 children.add(input.getPathString());
170 return true;
171 }
172 });
173 assertThat(children).containsExactly("C:/fake", "C:/first");
174
175 // Assert that "usr" is registered as a child of "C:/fake/msys/".
176 children.clear();
177 root.getRelative("C:/fake/msys")
178 .applyToChildren(
179 new Predicate<Path>() {
180 @Override
181 public boolean apply(Path input) {
182 children.add(input.getPathString());
183 return true;
184 }
185 });
186 assertThat(children).containsExactly("C:/fake/msys/usr");
187
188 assertThat(usrPath).isEqualTo(root.getRelative("C:/fake/msys/usr"));
189 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100190}