aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/vfs/LocalPathAbstractTest.java
blob: 93869521ad3db91cf4239cfd0e69abc9fd84f7be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.vfs;

import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import static java.util.stream.Collectors.toList;

import com.google.common.collect.Lists;
import com.google.common.testing.EqualsTester;
import com.google.devtools.build.lib.vfs.LocalPath.OsPathPolicy;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;

/** Tests for {@link LocalPath}. */
public abstract class LocalPathAbstractTest {

  private OsPathPolicy os;

  @Before
  public void setup() {
    os = getFilePathOs();
  }

  @Test
  public void testEqualsAndHashCode() {
    new EqualsTester()
        .addEqualityGroup(
            create("../relative/path"), create("..").getRelative("relative").getRelative("path"))
        .addEqualityGroup(create("something/else"))
        .addEqualityGroup(create(""), LocalPath.EMPTY)
        .testEquals();
  }

  @Test
  public void testRelativeTo() {
    assertThat(create("").relativeTo(create("")).getPathString()).isEmpty();
    assertThat(create("foo").relativeTo(create("foo")).getPathString()).isEmpty();
    assertThat(create("foo/bar/baz").relativeTo(create("foo")).getPathString())
        .isEqualTo("bar/baz");
    assertThat(create("foo/bar/baz").relativeTo(create("foo/bar")).getPathString())
        .isEqualTo("baz");
    assertThat(create("foo").relativeTo(create("")).getPathString()).isEqualTo("foo");

    // Cannot relativize non-ancestors
    assertThrows(IllegalArgumentException.class, () -> create("foo/bar").relativeTo(create("fo")));

    // Make sure partial directory matches aren't reported
    assertThrows(
        IllegalArgumentException.class, () -> create("foo/bar").relativeTo(create("foo/ba")));
  }

  @Test
  public void testGetRelative() {
    assertThat(create("a").getRelative("b").getPathString()).isEqualTo("a/b");
    assertThat(create("a/b").getRelative("c/d").getPathString()).isEqualTo("a/b/c/d");
    assertThat(create("a").getRelative("").getPathString()).isEqualTo("a");
    assertThat(create("a/b").getRelative("../c").getPathString()).isEqualTo("a/c");
    assertThat(create("a/b").getRelative("..").getPathString()).isEqualTo("a");
  }

  @Test
  public void testEmptyPathToEmptyPath() {
    // compare string forms
    assertThat(create("").getPathString()).isEmpty();
    // compare fragment forms
    assertThat(create("")).isEqualTo(create(""));
  }

  @Test
  public void testSimpleNameToSimpleName() {
    // compare string forms
    assertThat(create("foo").getPathString()).isEqualTo("foo");
    // compare fragment forms
    assertThat(create("foo")).isEqualTo(create("foo"));
  }

  @Test
  public void testSimplePathToSimplePath() {
    // compare string forms
    assertThat(create("foo/bar").getPathString()).isEqualTo("foo/bar");
    // compare fragment forms
    assertThat(create("foo/bar")).isEqualTo(create("foo/bar"));
  }

  @Test
  public void testStripsTrailingSlash() {
    // compare string forms
    assertThat(create("foo/bar/").getPathString()).isEqualTo("foo/bar");
    // compare fragment forms
    assertThat(create("foo/bar/")).isEqualTo(create("foo/bar"));
  }

  @Test
  public void testGetParentDirectory() {
    LocalPath fooBarWiz = create("foo/bar/wiz");
    LocalPath fooBar = create("foo/bar");
    LocalPath foo = create("foo");
    LocalPath empty = create("");
    assertThat(fooBarWiz.getParentDirectory()).isEqualTo(fooBar);
    assertThat(fooBar.getParentDirectory()).isEqualTo(foo);
    assertThat(foo.getParentDirectory()).isEqualTo(empty);
    assertThat(empty.getParentDirectory()).isNull();
  }

  @Test
  public void testBasename() throws Exception {
    assertThat(create("foo/bar").getBaseName()).isEqualTo("bar");
    assertThat(create("foo/").getBaseName()).isEqualTo("foo");
    assertThat(create("foo").getBaseName()).isEqualTo("foo");
    assertThat(create("").getBaseName()).isEmpty();
  }

  @Test
  public void testStartsWith() {
    // (relative path, relative prefix) => true
    assertThat(create("foo/bar").startsWith(create("foo/bar"))).isTrue();
    assertThat(create("foo/bar").startsWith(create("foo"))).isTrue();
    assertThat(create("foot/bar").startsWith(create("foo"))).isFalse();
  }

  @Test
  public void testNormalize() {
    assertThat(create("a/b")).isEqualTo(create("a/b"));
    assertThat(create("a/../../b")).isEqualTo(create("../b"));
    assertThat(create("a/../..")).isEqualTo(create(".."));
    assertThat(create("a/../b")).isEqualTo(create("b"));
    assertThat(create("a/b/../b")).isEqualTo(create("a/b"));
  }

  @Test
  public void testNormalStringsDoNotAllocate() {
    String normal1 = "a/b/hello.txt";
    assertThat(create(normal1).getPathString()).isSameAs(normal1);

    // Sanity check our testing strategy
    String notNormal = "a/../b";
    assertThat(create(notNormal).getPathString()).isNotSameAs(notNormal);
  }

  @Test
  public void testComparableSortOrder() {
    List<LocalPath> list =
        Lists.newArrayList(
            create("zzz"),
            create("ZZZ"),
            create("ABC"),
            create("aBc"),
            create("AbC"),
            create("abc"));
    Collections.sort(list);
    List<String> result = list.stream().map(LocalPath::getPathString).collect(toList());

    if (os.isCaseSensitive()) {
      assertThat(result).containsExactly("ABC", "AbC", "ZZZ", "aBc", "abc", "zzz").inOrder();
    } else {
      // Partial ordering among case-insensitive items guaranteed by Collections.sort stability
      assertThat(result).containsExactly("ABC", "aBc", "AbC", "abc", "zzz", "ZZZ").inOrder();
    }
  }

  protected abstract OsPathPolicy getFilePathOs();

  protected LocalPath create(String path) {
    return LocalPath.createWithOs(path, os);
  }
}