| Dave Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | cfc1c53 | 2015-02-18 13:41:19 -0800 | [diff] [blame] | 18 | import static com.google.gitiles.PathUtil.simplifyPathUpToRoot; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 19 | |
| 20 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 21 | import org.junit.runner.RunWith; |
| 22 | import org.junit.runners.JUnit4; |
| Dave Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 23 | |
| Dave Borowitz | cfc1c53 | 2015-02-18 13:41:19 -0800 | [diff] [blame] | 24 | /** Tests for {@link PathUtil}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 25 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 26 | public class PathsTest { |
| 27 | @Test |
| 28 | public void simplifyPathUpToRootSimplifiesPath() throws Exception { |
| Dave Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 29 | String root = "a/b/c"; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 30 | 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 Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 42 | @Test |
| 43 | public void simplifyPathUpToNullRootDetectsNullRoot() throws Exception { |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 44 | 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 Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 50 | } |
| 51 | } |