blob: d96b7d80e2df83124a110f13b1878074af60ec44 [file] [log] [blame]
Dave Borowitzbcd753d2013-02-08 11:10:19 -08001// Copyright 2012 Google Inc. 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
15package com.google.gitiles;
16
Dave Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
Dave Borowitzcfc1c532015-02-18 13:41:19 -080018import static com.google.gitiles.PathUtil.simplifyPathUpToRoot;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070019
20import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080021import org.junit.runner.RunWith;
22import org.junit.runners.JUnit4;
Dave Borowitzbcd753d2013-02-08 11:10:19 -080023
Dave Borowitzcfc1c532015-02-18 13:41:19 -080024/** Tests for {@link PathUtil}. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080025@RunWith(JUnit4.class)
Dave Borowitzd40bdf12014-04-19 19:33:56 -070026public class PathsTest {
27 @Test
28 public void simplifyPathUpToRootSimplifiesPath() throws Exception {
Dave Borowitzbcd753d2013-02-08 11:10:19 -080029 String root = "a/b/c";
Dave Borowitzfde41fd2015-09-16 15:14:38 -040030 assertThat(simplifyPathUpToRoot("/foo", root)).isNull();
31 assertThat(simplifyPathUpToRoot("../../", root)).isEqualTo("a");
32 assertThat(simplifyPathUpToRoot(".././../", root)).isEqualTo("a");
33 assertThat(simplifyPathUpToRoot("..//../", root)).isEqualTo("a");
34 assertThat(simplifyPathUpToRoot("../../d", root)).isEqualTo("a/d");
35 assertThat(simplifyPathUpToRoot("../../..", root)).isEqualTo("");
36 assertThat(simplifyPathUpToRoot("../../d/e", root)).isEqualTo("a/d/e");
37 assertThat(simplifyPathUpToRoot("../d/../e/../", root)).isEqualTo("a/b");
38 assertThat(simplifyPathUpToRoot("../../../../", root)).isNull();
39 assertThat(simplifyPathUpToRoot("../../a/../../..", root)).isNull();
Dave Borowitzbcd753d2013-02-08 11:10:19 -080040 }
41
Dave Borowitzd40bdf12014-04-19 19:33:56 -070042 @Test
43 public void simplifyPathUpToNullRootDetectsNullRoot() throws Exception {
Dave Borowitzfde41fd2015-09-16 15:14:38 -040044 assertThat(simplifyPathUpToRoot("/foo", null)).isNull();
45 assertThat(simplifyPathUpToRoot("../", null)).isNull();
46 assertThat(simplifyPathUpToRoot("../../", null)).isNull();
47 assertThat(simplifyPathUpToRoot(".././../", null)).isNull();
48 assertThat(simplifyPathUpToRoot("a/b", null)).isEqualTo("a/b");
49 assertThat(simplifyPathUpToRoot("a/b/../c", null)).isEqualTo("a/c");
Dave Borowitzbcd753d2013-02-08 11:10:19 -080050 }
51}