Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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. |
| 14 | package com.google.devtools.build.lib.vfs; |
| 15 | |
Ulf Adams | cceeac6 | 2016-05-18 14:09:43 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import static org.junit.Assert.assertEquals; |
| 18 | import static org.junit.Assert.assertSame; |
| 19 | |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 20 | import com.google.common.base.Predicate; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.devtools.build.lib.util.BlazeClock; |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.vfs.Path.PathFactory; |
| 23 | import com.google.devtools.build.lib.vfs.WindowsFileSystem.WindowsPathFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.junit.runners.JUnit4; |
| 31 | |
| 32 | /** |
| 33 | * A test for windows aspects of {@link Path}. |
| 34 | */ |
| 35 | @RunWith(JUnit4.class) |
| 36 | public class PathWindowsTest { |
| 37 | private FileSystem filesystem; |
| 38 | private Path root; |
| 39 | |
| 40 | @Before |
Florian Weikert | 99ac0a2 | 2015-12-01 15:05:35 +0000 | [diff] [blame] | 41 | public final void initializeFileSystem() throws Exception { |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 42 | 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | 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 Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 111 | |
| 112 | @Test |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 113 | 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 Csomor | ff8fcf0 | 2016-10-26 16:20:27 +0000 | [diff] [blame^] | 124 | Path weird = root.getRelative("/c:"); |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 125 | assertThat(actual.getPathString()).isEqualTo(expected.getPathString()); |
| 126 | assertThat(actual).isEqualTo(expected); |
Laszlo Csomor | ff8fcf0 | 2016-10-26 16:20:27 +0000 | [diff] [blame^] | 127 | assertThat(weird).isNotEqualTo(expected); |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Test |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 131 | 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 Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 135 | assertStartsWithReturnsOnWindows(true, "/", "C:/"); |
| 136 | assertStartsWithReturnsOnWindows(false, "C:/", "/"); |
| 137 | assertStartsWithReturnsOnWindows(false, "C:/", "D:/"); |
| 138 | assertStartsWithReturnsOnWindows(false, "C:/", "D:/foo"); |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Ulf Adams | cceeac6 | 2016-05-18 14:09:43 +0000 | [diff] [blame] | 141 | @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 Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 148 | 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 Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 156 | |
| 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 190 | } |