blob: 640f8c4c91b7aefdb3c6530b810601c9b4769b50 [file] [log] [blame]
tomluee6a6862018-01-17 14:36:26 -08001// Copyright 2018 The Bazel Authors. 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.
14package com.google.devtools.build.lib.vfs;
15
16import static com.google.common.truth.Truth.assertThat;
17
tomlu4c9fafd2018-01-18 10:29:11 -080018import com.google.common.collect.Lists;
tomluee6a6862018-01-17 14:36:26 -080019import com.google.common.testing.EqualsTester;
20import com.google.devtools.build.lib.clock.BlazeClock;
shahan20f35b42018-02-28 15:57:33 -080021import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester;
tomlu4c9fafd2018-01-18 10:29:11 -080022import com.google.devtools.build.lib.testutil.MoreAsserts;
tomluee6a6862018-01-17 14:36:26 -080023import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
tomlu4c9fafd2018-01-18 10:29:11 -080024import java.util.Comparator;
25import java.util.List;
tomluee6a6862018-01-17 14:36:26 -080026import org.junit.Before;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.JUnit4;
30
31/** Tests for {@link RootTest}. */
32@RunWith(JUnit4.class)
33public class RootTest {
34 private FileSystem fs;
35
36 @Before
37 public final void initializeFileSystem() throws Exception {
ccalvarinc9efd062018-07-27 12:46:46 -070038 fs = new InMemoryFileSystem(BlazeClock.instance());
tomluee6a6862018-01-17 14:36:26 -080039 }
40
41 @Test
42 public void testEqualsAndHashCodeContract() throws Exception {
ccalvarinc9efd062018-07-27 12:46:46 -070043 FileSystem otherFs = new InMemoryFileSystem(BlazeClock.instance());
tomluee6a6862018-01-17 14:36:26 -080044 new EqualsTester()
tomlu4c9fafd2018-01-18 10:29:11 -080045 .addEqualityGroup(Root.absoluteRoot(fs), Root.absoluteRoot(fs))
46 .addEqualityGroup(Root.absoluteRoot(otherFs), Root.absoluteRoot(otherFs))
tomluee6a6862018-01-17 14:36:26 -080047 .addEqualityGroup(Root.fromPath(fs.getPath("/foo")), Root.fromPath(fs.getPath("/foo")))
48 .testEquals();
49 }
50
51 @Test
52 public void testPathRoot() throws Exception {
53 Root root = Root.fromPath(fs.getPath("/foo"));
54 assertThat(root.asPath()).isEqualTo(fs.getPath("/foo"));
55 assertThat(root.contains(fs.getPath("/foo/bar"))).isTrue();
56 assertThat(root.contains(fs.getPath("/boo/bar"))).isFalse();
tomlu4c9fafd2018-01-18 10:29:11 -080057 assertThat(root.contains(PathFragment.create("/foo/bar"))).isTrue();
58 assertThat(root.contains(PathFragment.create("foo/bar"))).isFalse();
tomluee6a6862018-01-17 14:36:26 -080059 assertThat(root.getRelative(PathFragment.create("bar"))).isEqualTo(fs.getPath("/foo/bar"));
60 assertThat(root.getRelative("bar")).isEqualTo(fs.getPath("/foo/bar"));
tomlu4c9fafd2018-01-18 10:29:11 -080061 assertThat(root.getRelative(PathFragment.create("/bar"))).isEqualTo(fs.getPath("/bar"));
tomluee6a6862018-01-17 14:36:26 -080062 assertThat(root.relativize(fs.getPath("/foo/bar"))).isEqualTo(PathFragment.create("bar"));
tomlu4c9fafd2018-01-18 10:29:11 -080063 assertThat(root.relativize(PathFragment.create("/foo/bar")))
64 .isEqualTo(PathFragment.create("bar"));
cushon978cb002018-02-24 14:05:37 -080065 MoreAsserts.assertThrows(
tomlu4c9fafd2018-01-18 10:29:11 -080066 IllegalArgumentException.class, () -> root.relativize(PathFragment.create("foo")));
tomluee6a6862018-01-17 14:36:26 -080067 }
68
69 @Test
felly89d85ea2018-06-06 19:31:23 -070070 public void testFilesystemTransform() throws Exception {
ccalvarinc9efd062018-07-27 12:46:46 -070071 FileSystem fs2 = new InMemoryFileSystem(BlazeClock.instance());
felly89d85ea2018-06-06 19:31:23 -070072 Root root = Root.fromPath(fs.getPath("/foo"));
73 Root root2 = Root.toFileSystem(root, fs2);
74 assertThat(root2.asPath().getFileSystem()).isSameAs(fs2);
75 assertThat(root2.asPath().asFragment()).isEqualTo(PathFragment.create("/foo"));
76 assertThat(root.isAbsolute()).isFalse();
77 }
78
79 @Test
tomlu4c9fafd2018-01-18 10:29:11 -080080 public void testFileSystemAbsoluteRoot() throws Exception {
81 Root root = Root.absoluteRoot(fs);
82 assertThat(root.asPath()).isNull();
tomluee6a6862018-01-17 14:36:26 -080083 assertThat(root.contains(fs.getPath("/foo"))).isTrue();
tomlu4c9fafd2018-01-18 10:29:11 -080084 assertThat(root.contains(PathFragment.create("/foo/bar"))).isTrue();
85 assertThat(root.contains(PathFragment.create("foo/bar"))).isFalse();
86 assertThat(root.getRelative("/foo")).isEqualTo(fs.getPath("/foo"));
87 assertThat(root.relativize(fs.getPath("/foo"))).isEqualTo(PathFragment.create("/foo"));
88 assertThat(root.relativize(PathFragment.create("/foo"))).isEqualTo(PathFragment.create("/foo"));
89
cushon978cb002018-02-24 14:05:37 -080090 MoreAsserts.assertThrows(
tomlu4c9fafd2018-01-18 10:29:11 -080091 IllegalArgumentException.class, () -> root.getRelative(PathFragment.create("foo")));
cushon978cb002018-02-24 14:05:37 -080092 MoreAsserts.assertThrows(
tomlu4c9fafd2018-01-18 10:29:11 -080093 IllegalArgumentException.class, () -> root.getRelative(PathFragment.create("foo")));
cushon978cb002018-02-24 14:05:37 -080094 MoreAsserts.assertThrows(
tomlu4c9fafd2018-01-18 10:29:11 -080095 IllegalArgumentException.class, () -> root.relativize(PathFragment.create("foo")));
96 }
97
98 @Test
99 public void testCompareTo() throws Exception {
100 Root a = Root.fromPath(fs.getPath("/a"));
101 Root b = Root.fromPath(fs.getPath("/b"));
102 Root root = Root.absoluteRoot(fs);
103 List<Root> list = Lists.newArrayList(a, root, b);
104 list.sort(Comparator.naturalOrder());
105 assertThat(list).containsExactly(root, a, b).inOrder();
tomluee6a6862018-01-17 14:36:26 -0800106 }
107
108 @Test
109 public void testSerialization() throws Exception {
shahan20f35b42018-02-28 15:57:33 -0800110 new SerializationTester(Root.absoluteRoot(fs), Root.fromPath(fs.getPath("/foo")))
shahanfae34b92018-02-13 10:08:47 -0800111 .addDependency(FileSystem.class, fs)
shahan20f35b42018-02-28 15:57:33 -0800112 .runTests();
tomluee6a6862018-01-17 14:36:26 -0800113 }
114}