blob: 5e6224c998ff408fa3c798ccb173f2726462cb55 [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
16import static org.junit.Assert.assertEquals;
Ulf Adams795895a2015-03-06 15:58:35 +000017import static org.junit.Assert.assertNull;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018
19import com.google.devtools.build.lib.testutil.TestUtils;
20import com.google.devtools.build.lib.vfs.util.FileSystems;
Ulf Adams8afbd3c2017-02-28 10:42:48 +000021import java.io.IOException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
27
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028/**
Ulf Adams8afbd3c2017-02-28 10:42:48 +000029 * A test for {@link Path}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030 */
31@RunWith(JUnit4.class)
Ulf Adams8afbd3c2017-02-28 10:42:48 +000032public class PathGetParentTest {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033
Ulf Adams8afbd3c2017-02-28 10:42:48 +000034 private FileSystem fs;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010035 private Path testRoot;
36
37 @Before
Florian Weikert99ac0a22015-12-01 15:05:35 +000038 public final void createTestRoot() throws Exception {
Ulf Adams8afbd3c2017-02-28 10:42:48 +000039 fs = FileSystems.getNativeFileSystem();
40 testRoot = fs.getPath(TestUtils.tmpDir()).getRelative("UnixPathGetParentTest");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041 FileSystemUtils.createDirectoryAndParents(testRoot);
42 }
43
44 @After
Florian Weikert99ac0a22015-12-01 15:05:35 +000045 public final void deleteTestRoot() throws Exception {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010046 FileSystemUtils.deleteTree(testRoot); // (comment out during debugging)
47 }
48
49 private Path getParent(String path) {
Ulf Adams8afbd3c2017-02-28 10:42:48 +000050 return fs.getPath(path).getParentDirectory();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010051 }
52
53 @Test
54 public void testAbsoluteRootHasNoParent() {
Ulf Adams795895a2015-03-06 15:58:35 +000055 assertNull(getParent("/"));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010056 }
57
58 @Test
59 public void testParentOfSimpleDirectory() {
60 assertEquals("/foo", getParent("/foo/bar").getPathString());
61 }
62
63 @Test
64 public void testParentOfDotDotInMiddleOfPathname() {
65 assertEquals("/", getParent("/foo/../bar").getPathString());
66 }
67
68 @Test
69 public void testGetPathDoesNormalizationWithoutIO() throws IOException {
70 Path tmp = testRoot.getChild("tmp");
71 Path tmpWiz = tmp.getChild("wiz");
72
73 tmp.createDirectory();
74
75 // ln -sf /tmp /tmp/wiz
76 tmpWiz.createSymbolicLink(tmp);
77
78 assertEquals(testRoot, tmp.getParentDirectory());
79
80 assertEquals(tmp, tmpWiz.getParentDirectory());
81
82 // Under UNIX, inode(/tmp/wiz/..) == inode(/). However getPath() does not
83 // perform I/O, only string operations, so it disagrees:
84 assertEquals(tmp, tmp.getRelative(new PathFragment("wiz/..")));
85 }
86}