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; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 19 | import com.google.devtools.build.lib.testutil.MoreAsserts; |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.vfs.WindowsOsPathPolicy.ShortPathResolver; |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 21 | import java.util.HashMap; |
| 22 | import java.util.Map; |
| 23 | import org.junit.Test; |
| 24 | import org.junit.runner.RunWith; |
| 25 | import org.junit.runners.JUnit4; |
| 26 | |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 27 | /** Tests windows-specific parts of {@link Path} */ |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 28 | @RunWith(JUnit4.class) |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 29 | public class WindowsPathTest extends PathAbstractTest { |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 30 | |
| 31 | private static final class MockShortPathResolver implements ShortPathResolver { |
| 32 | // Full path to resolved child mapping. |
| 33 | private Map<String, String> resolutions = new HashMap<>(); |
| 34 | |
| 35 | @Override |
| 36 | public String resolveShortPath(String path) { |
| 37 | String[] segments = path.split("[\\\\/]+"); |
| 38 | String result = ""; |
| 39 | for (int i = 0; i < segments.length; ) { |
| 40 | String segment = segments[i]; |
| 41 | String queryString = (result + segment).toLowerCase(); |
| 42 | segment = resolutions.getOrDefault(queryString, segment); |
| 43 | result = result + segment; |
| 44 | ++i; |
| 45 | if (i != segments.length) { |
| 46 | result += "/"; |
| 47 | } |
| 48 | } |
| 49 | return result; |
| 50 | } |
| 51 | } |
| 52 | |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 53 | @Test |
| 54 | public void testEqualsAndHashcodeWindows() { |
| 55 | new EqualsTester() |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 56 | .addEqualityGroup(create("/a/b"), create("/A/B")) |
| 57 | .addEqualityGroup(create("c:/a/b"), create("C:\\A\\B")) |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 58 | .addEqualityGroup(create("C:/something/else")) |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 59 | .testEquals(); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testCaseIsPreserved() { |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 64 | assertThat(create("C:/a/B").getPathString()).isEqualTo("C:/a/B"); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testNormalizeWindows() { |
| 69 | assertThat(create("C:/")).isEqualTo(create("C:/")); |
| 70 | assertThat(create("c:/")).isEqualTo(create("C:/")); |
| 71 | assertThat(create("c:\\")).isEqualTo(create("C:/")); |
| 72 | assertThat(create("c:\\foo\\..\\bar\\")).isEqualTo(create("C:/bar")); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testStartsWithWindows() { |
| 77 | assertThat(create("C:/").startsWith(create("C:/"))).isTrue(); |
| 78 | assertThat(create("C:/foo").startsWith(create("C:/"))).isTrue(); |
| 79 | assertThat(create("C:/foo").startsWith(create("D:/"))).isFalse(); |
| 80 | |
| 81 | // Case insensitivity test |
| 82 | assertThat(create("C:/foo/bar").startsWith(create("C:/FOO"))).isTrue(); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testGetParentDirectoryWindows() { |
| 87 | assertThat(create("C:/foo").getParentDirectory()).isEqualTo(create("C:/")); |
| 88 | assertThat(create("C:/").getParentDirectory()).isNull(); |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 89 | assertThat(create("/").getParentDirectory()).isNull(); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Test |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 93 | public void testParentOfRootIsRootWindows() { |
| 94 | assertThat(create("C:/..")).isEqualTo(create("C:/")); |
| 95 | assertThat(create("C:/../../../../../..")).isEqualTo(create("C:/")); |
| 96 | assertThat(create("C:/../../../foo")).isEqualTo(create("C:/foo")); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testRelativeToWindows() { |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 101 | assertThat(create("C:/foo").relativeTo(create("C:/")).getPathString()).isEqualTo("foo"); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 102 | // Case insensitivity test |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 103 | assertThat(create("C:/foo/bar").relativeTo(create("C:/FOO")).getPathString()).isEqualTo("bar"); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 104 | MoreAsserts.assertThrows( |
| 105 | IllegalArgumentException.class, () -> create("D:/foo").relativeTo(create("C:/"))); |
| 106 | } |
| 107 | |
| 108 | @Test |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 109 | public void testResolvesShortenedPaths() { |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 110 | MockShortPathResolver shortPathResolver = new MockShortPathResolver(); |
| 111 | WindowsOsPathPolicy osPathPolicy = new WindowsOsPathPolicy(shortPathResolver); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 112 | shortPathResolver.resolutions.put("d:/progra~1", "program files"); |
| 113 | shortPathResolver.resolutions.put("d:/program files/micros~1", "microsoft something"); |
| 114 | shortPathResolver.resolutions.put( |
| 115 | "d:/program files/microsoft something/foo/~bar~1", "~bar_hello"); |
| 116 | |
| 117 | // Assert normal shortpath resolution. |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 118 | assertThat(normalize(osPathPolicy, "d:/progra~1/micros~1/foo/~bar~1/baz")) |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 119 | .isEqualTo("D:/program files/microsoft something/foo/~bar_hello/baz"); |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 120 | assertThat(normalize(osPathPolicy, "d:/progra~1/micros~1/foo/will~1.exi/bar")) |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 121 | .isEqualTo("D:/program files/microsoft something/foo/will~1.exi/bar"); |
| 122 | |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 123 | assertThat(normalize(osPathPolicy, "d:/progra~1/micros~1")) |
| 124 | .isEqualTo("D:/program files/microsoft something"); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 125 | |
| 126 | // Pretend that a path we already failed to resolve once came into existence. |
| 127 | shortPathResolver.resolutions.put( |
| 128 | "d:/program files/microsoft something/foo/will~1.exi", "will.exist"); |
| 129 | |
| 130 | // Assert that this time we can resolve the previously non-existent path. |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 131 | // The path string has an upper-case drive letter because that's how path printing works. |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 132 | assertThat(normalize(osPathPolicy, "d:/progra~1/micros~1/foo/will~1.exi/bar")) |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 133 | .isEqualTo("D:/program files/microsoft something/foo/will.exist/bar"); |
| 134 | |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 135 | // Check needsToNormalized |
| 136 | assertThat(osPathPolicy.needsToNormalize("d:/progra~1/micros~1/foo/will~1.exi/bar")) |
| 137 | .isEqualTo(WindowsOsPathPolicy.NEEDS_SHORT_PATH_NORMALIZATION); |
| 138 | assertThat(osPathPolicy.needsToNormalize("will~1.exi")) |
| 139 | .isEqualTo(WindowsOsPathPolicy.NEEDS_SHORT_PATH_NORMALIZATION); |
| 140 | assertThat(osPathPolicy.needsToNormalize("d:/no-normalization")) |
| 141 | .isEqualTo(WindowsOsPathPolicy.NORMALIZED); // Sanity check |
| 142 | } |
| 143 | |
| 144 | private static String normalize(OsPathPolicy osPathPolicy, String str) { |
| 145 | return osPathPolicy.normalize(str, osPathPolicy.needsToNormalize(str)); |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 146 | } |
tomlu | 4a2f2c5 | 2017-12-12 12:32:22 -0800 | [diff] [blame] | 147 | } |