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 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [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 | |
| 18 | import com.google.devtools.build.lib.testutil.TestUtils; |
| 19 | import com.google.devtools.build.lib.vfs.util.FileSystems; |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 20 | import java.io.IOException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.junit.runner.RunWith; |
| 25 | import org.junit.runners.JUnit4; |
| 26 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | /** |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 28 | * A test for {@link Path}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | */ |
| 30 | @RunWith(JUnit4.class) |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 31 | public class PathGetParentTest { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 33 | private FileSystem fs; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | private Path testRoot; |
| 35 | |
| 36 | @Before |
Florian Weikert | 99ac0a2 | 2015-12-01 15:05:35 +0000 | [diff] [blame] | 37 | public final void createTestRoot() throws Exception { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 38 | fs = FileSystems.getNativeFileSystem(); |
| 39 | testRoot = fs.getPath(TestUtils.tmpDir()).getRelative("UnixPathGetParentTest"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | FileSystemUtils.createDirectoryAndParents(testRoot); |
| 41 | } |
| 42 | |
| 43 | @After |
Florian Weikert | 99ac0a2 | 2015-12-01 15:05:35 +0000 | [diff] [blame] | 44 | public final void deleteTestRoot() throws Exception { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | FileSystemUtils.deleteTree(testRoot); // (comment out during debugging) |
| 46 | } |
| 47 | |
| 48 | private Path getParent(String path) { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 49 | return fs.getPath(path).getParentDirectory(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testAbsoluteRootHasNoParent() { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 54 | assertThat(getParent("/")).isNull(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testParentOfSimpleDirectory() { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 59 | assertThat(getParent("/foo/bar").getPathString()).isEqualTo("/foo"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testParentOfDotDotInMiddleOfPathname() { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 64 | assertThat(getParent("/foo/../bar").getPathString()).isEqualTo("/"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testGetPathDoesNormalizationWithoutIO() throws IOException { |
| 69 | Path tmp = testRoot.getChild("tmp"); |
| 70 | Path tmpWiz = tmp.getChild("wiz"); |
| 71 | |
| 72 | tmp.createDirectory(); |
| 73 | |
| 74 | // ln -sf /tmp /tmp/wiz |
| 75 | tmpWiz.createSymbolicLink(tmp); |
| 76 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 77 | assertThat(tmp.getParentDirectory()).isEqualTo(testRoot); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 79 | assertThat(tmpWiz.getParentDirectory()).isEqualTo(tmp); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 80 | |
| 81 | // Under UNIX, inode(/tmp/wiz/..) == inode(/). However getPath() does not |
| 82 | // perform I/O, only string operations, so it disagrees: |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 83 | assertThat(tmp.getRelative(PathFragment.create("wiz/.."))).isEqualTo(tmp); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | } |
| 85 | } |