blob: 04def1c0862084b8e562d3b7ce7f5e320c4b944f [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
lberkiaea56b32017-05-30 12:35:33 +020016import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017
18import com.google.devtools.build.lib.testutil.TestUtils;
19import com.google.devtools.build.lib.vfs.util.FileSystems;
Ulf Adams8afbd3c2017-02-28 10:42:48 +000020import java.io.IOException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.junit.runner.RunWith;
25import org.junit.runners.JUnit4;
26
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027/**
Ulf Adams8afbd3c2017-02-28 10:42:48 +000028 * A test for {@link Path}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029 */
30@RunWith(JUnit4.class)
Ulf Adams8afbd3c2017-02-28 10:42:48 +000031public class PathGetParentTest {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010032
Ulf Adams8afbd3c2017-02-28 10:42:48 +000033 private FileSystem fs;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034 private Path testRoot;
35
36 @Before
Florian Weikert99ac0a22015-12-01 15:05:35 +000037 public final void createTestRoot() throws Exception {
Ulf Adams8afbd3c2017-02-28 10:42:48 +000038 fs = FileSystems.getNativeFileSystem();
39 testRoot = fs.getPath(TestUtils.tmpDir()).getRelative("UnixPathGetParentTest");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040 FileSystemUtils.createDirectoryAndParents(testRoot);
41 }
42
43 @After
Florian Weikert99ac0a22015-12-01 15:05:35 +000044 public final void deleteTestRoot() throws Exception {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045 FileSystemUtils.deleteTree(testRoot); // (comment out during debugging)
46 }
47
48 private Path getParent(String path) {
Ulf Adams8afbd3c2017-02-28 10:42:48 +000049 return fs.getPath(path).getParentDirectory();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010050 }
51
52 @Test
53 public void testAbsoluteRootHasNoParent() {
lberkiaea56b32017-05-30 12:35:33 +020054 assertThat(getParent("/")).isNull();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010055 }
56
57 @Test
58 public void testParentOfSimpleDirectory() {
lberkiaea56b32017-05-30 12:35:33 +020059 assertThat(getParent("/foo/bar").getPathString()).isEqualTo("/foo");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010060 }
61
62 @Test
63 public void testParentOfDotDotInMiddleOfPathname() {
lberkiaea56b32017-05-30 12:35:33 +020064 assertThat(getParent("/foo/../bar").getPathString()).isEqualTo("/");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065 }
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
lberkiaea56b32017-05-30 12:35:33 +020077 assertThat(tmp.getParentDirectory()).isEqualTo(testRoot);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010078
lberkiaea56b32017-05-30 12:35:33 +020079 assertThat(tmpWiz.getParentDirectory()).isEqualTo(tmp);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010080
81 // Under UNIX, inode(/tmp/wiz/..) == inode(/). However getPath() does not
82 // perform I/O, only string operations, so it disagrees:
lberkiaea56b32017-05-30 12:35:33 +020083 assertThat(tmp.getRelative(PathFragment.create("wiz/.."))).isEqualTo(tmp);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010084 }
85}