tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.vfs; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 19 | import com.google.common.testing.EqualsTester; |
| 20 | import com.google.devtools.build.lib.clock.BlazeClock; |
shahan | 20f35b4 | 2018-02-28 15:57:33 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester; |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 22 | import com.google.devtools.build.lib.testutil.MoreAsserts; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 24 | import java.util.Comparator; |
| 25 | import java.util.List; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.junit.runners.JUnit4; |
| 30 | |
| 31 | /** Tests for {@link RootTest}. */ |
| 32 | @RunWith(JUnit4.class) |
| 33 | public class RootTest { |
| 34 | private FileSystem fs; |
| 35 | |
| 36 | @Before |
| 37 | public final void initializeFileSystem() throws Exception { |
ccalvarin | c9efd06 | 2018-07-27 12:46:46 -0700 | [diff] [blame] | 38 | fs = new InMemoryFileSystem(BlazeClock.instance()); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | @Test |
| 42 | public void testEqualsAndHashCodeContract() throws Exception { |
ccalvarin | c9efd06 | 2018-07-27 12:46:46 -0700 | [diff] [blame] | 43 | FileSystem otherFs = new InMemoryFileSystem(BlazeClock.instance()); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 44 | new EqualsTester() |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 45 | .addEqualityGroup(Root.absoluteRoot(fs), Root.absoluteRoot(fs)) |
| 46 | .addEqualityGroup(Root.absoluteRoot(otherFs), Root.absoluteRoot(otherFs)) |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 47 | .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(); |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 57 | assertThat(root.contains(PathFragment.create("/foo/bar"))).isTrue(); |
| 58 | assertThat(root.contains(PathFragment.create("foo/bar"))).isFalse(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 59 | assertThat(root.getRelative(PathFragment.create("bar"))).isEqualTo(fs.getPath("/foo/bar")); |
| 60 | assertThat(root.getRelative("bar")).isEqualTo(fs.getPath("/foo/bar")); |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 61 | assertThat(root.getRelative(PathFragment.create("/bar"))).isEqualTo(fs.getPath("/bar")); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 62 | assertThat(root.relativize(fs.getPath("/foo/bar"))).isEqualTo(PathFragment.create("bar")); |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 63 | assertThat(root.relativize(PathFragment.create("/foo/bar"))) |
| 64 | .isEqualTo(PathFragment.create("bar")); |
cushon | 978cb00 | 2018-02-24 14:05:37 -0800 | [diff] [blame] | 65 | MoreAsserts.assertThrows( |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 66 | IllegalArgumentException.class, () -> root.relativize(PathFragment.create("foo"))); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Test |
felly | 89d85ea | 2018-06-06 19:31:23 -0700 | [diff] [blame] | 70 | public void testFilesystemTransform() throws Exception { |
ccalvarin | c9efd06 | 2018-07-27 12:46:46 -0700 | [diff] [blame] | 71 | FileSystem fs2 = new InMemoryFileSystem(BlazeClock.instance()); |
felly | 89d85ea | 2018-06-06 19:31:23 -0700 | [diff] [blame] | 72 | 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 |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 80 | public void testFileSystemAbsoluteRoot() throws Exception { |
| 81 | Root root = Root.absoluteRoot(fs); |
| 82 | assertThat(root.asPath()).isNull(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 83 | assertThat(root.contains(fs.getPath("/foo"))).isTrue(); |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 84 | 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 | |
cushon | 978cb00 | 2018-02-24 14:05:37 -0800 | [diff] [blame] | 90 | MoreAsserts.assertThrows( |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 91 | IllegalArgumentException.class, () -> root.getRelative(PathFragment.create("foo"))); |
cushon | 978cb00 | 2018-02-24 14:05:37 -0800 | [diff] [blame] | 92 | MoreAsserts.assertThrows( |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 93 | IllegalArgumentException.class, () -> root.getRelative(PathFragment.create("foo"))); |
cushon | 978cb00 | 2018-02-24 14:05:37 -0800 | [diff] [blame] | 94 | MoreAsserts.assertThrows( |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 95 | 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(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testSerialization() throws Exception { |
shahan | 20f35b4 | 2018-02-28 15:57:33 -0800 | [diff] [blame] | 110 | new SerializationTester(Root.absoluteRoot(fs), Root.fromPath(fs.getPath("/foo"))) |
shahan | fae34b9 | 2018-02-13 10:08:47 -0800 | [diff] [blame] | 111 | .addDependency(FileSystem.class, fs) |
shahan | 20f35b4 | 2018-02-28 15:57:33 -0800 | [diff] [blame] | 112 | .runTests(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 113 | } |
| 114 | } |