Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | import static org.junit.Assert.assertEquals; |
| 18 | import static org.junit.Assert.assertFalse; |
| 19 | import static org.junit.Assert.assertSame; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.junit.Assert.fail; |
| 22 | |
| 23 | import com.google.common.collect.Lists; |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 24 | import com.google.common.testing.EqualsTester; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.testutil.TestUtils; |
| 26 | import com.google.devtools.build.lib.vfs.util.FileSystems; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import java.io.File; |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 28 | import java.io.FileOutputStream; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.io.OutputStream; |
| 32 | import java.util.Arrays; |
| 33 | import java.util.Collection; |
| 34 | import java.util.HashSet; |
| 35 | import java.util.List; |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | import org.junit.runners.JUnit4; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 41 | /** Tests for {@link Path} in combination with the native file system for the current platform. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | @RunWith(JUnit4.class) |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 43 | public class NativePathTest { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 45 | private FileSystem fs; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | private File aDirectory; |
| 47 | private File aFile; |
| 48 | private File anotherFile; |
| 49 | private File tmpDir; |
| 50 | |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 51 | protected FileSystem getNativeFileSystem() { |
Yun Peng | 22eb332 | 2016-06-21 14:38:12 +0000 | [diff] [blame] | 52 | return FileSystems.getNativeFileSystem(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Before |
Florian Weikert | 99ac0a2 | 2015-12-01 15:05:35 +0000 | [diff] [blame] | 56 | public final void createFiles() throws Exception { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 57 | fs = getNativeFileSystem(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | tmpDir = new File(TestUtils.tmpDir(), "tmpDir"); |
| 59 | tmpDir.mkdirs(); |
| 60 | aDirectory = new File(tmpDir, "a_directory"); |
| 61 | aDirectory.mkdirs(); |
| 62 | aFile = new File(tmpDir, "a_file"); |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 63 | new FileOutputStream(aFile).close(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | anotherFile = new File(aDirectory, "another_file.txt"); |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 65 | new FileOutputStream(anotherFile).close(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testExists() { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 70 | assertTrue(fs.getPath(aDirectory.getPath()).exists()); |
| 71 | assertTrue(fs.getPath(aFile.getPath()).exists()); |
| 72 | assertFalse(fs.getPath("/does/not/exist").exists()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testDirectoryEntriesForDirectory() throws IOException { |
| 77 | Collection<Path> entries = |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 78 | fs.getPath(tmpDir.getPath()).getDirectoryEntries(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | List<Path> expectedEntries = Arrays.asList( |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 80 | fs.getPath(tmpDir.getPath() + "/a_file"), |
| 81 | fs.getPath(tmpDir.getPath() + "/a_directory")); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | |
| 83 | assertEquals(new HashSet<Object>(expectedEntries), |
| 84 | new HashSet<Object>(entries)); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testDirectoryEntriesForFileThrowsException() { |
| 89 | try { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 90 | fs.getPath(aFile.getPath()).getDirectoryEntries(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | fail("No exception thrown."); |
| 92 | } catch (IOException x) { |
| 93 | // The expected result. |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testIsFileIsTrueForFile() { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 99 | assertTrue(fs.getPath(aFile.getPath()).isFile()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testIsFileIsFalseForDirectory() { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 104 | assertFalse(fs.getPath(aDirectory.getPath()).isFile()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testBaseName() { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 109 | assertEquals("base", fs.getPath("/foo/base").getBaseName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testBaseNameRunsAfterDotDotInterpretation() { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 114 | assertEquals("base", fs.getPath("/base/foo/..").getBaseName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void testParentOfRootIsRoot() { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 119 | assertEquals(fs.getPath("/"), fs.getPath("/..")); |
| 120 | assertEquals(fs.getPath("/"), fs.getPath("/../../../../../..")); |
| 121 | assertEquals(fs.getPath("/foo"), fs.getPath("/../../../foo")); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testIsDirectory() { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 126 | assertTrue(fs.getPath(aDirectory.getPath()).isDirectory()); |
| 127 | assertFalse(fs.getPath(aFile.getPath()).isDirectory()); |
| 128 | assertFalse(fs.getPath("/does/not/exist").isDirectory()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testListNonExistingDirectoryThrowsException() { |
| 133 | try { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 134 | fs.getPath("/does/not/exist").getDirectoryEntries(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 135 | fail("No exception thrown."); |
| 136 | } catch (IOException ex) { |
| 137 | // success! |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private void assertPathSet(Collection<Path> actual, String... expected) { |
| 142 | List<String> actualStrings = Lists.newArrayListWithCapacity(actual.size()); |
| 143 | |
| 144 | for (Path path : actual) { |
| 145 | actualStrings.add(path.getPathString()); |
| 146 | } |
| 147 | |
| 148 | assertThat(actualStrings).containsExactlyElementsIn(Arrays.asList(expected)); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testGlob() throws Exception { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 153 | Collection<Path> textFiles = UnixGlob.forPath(fs.getPath(tmpDir.getPath())) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 154 | .addPattern("*/*.txt") |
| 155 | .globInterruptible(); |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 156 | assertThat(textFiles).hasSize(1); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 157 | Path onlyFile = textFiles.iterator().next(); |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 158 | assertEquals(fs.getPath(anotherFile.getPath()), onlyFile); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 159 | |
| 160 | Collection<Path> onlyFiles = |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 161 | UnixGlob.forPath(fs.getPath(tmpDir.getPath())) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 162 | .addPattern("*") |
| 163 | .setExcludeDirectories(true) |
| 164 | .globInterruptible(); |
| 165 | assertPathSet(onlyFiles, aFile.getPath()); |
| 166 | |
| 167 | Collection<Path> directoriesToo = |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 168 | UnixGlob.forPath(fs.getPath(tmpDir.getPath())) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 169 | .addPattern("*") |
| 170 | .setExcludeDirectories(false) |
| 171 | .globInterruptible(); |
| 172 | assertPathSet(directoriesToo, aFile.getPath(), aDirectory.getPath()); |
| 173 | } |
| 174 | |
| 175 | @Test |
| 176 | public void testGetRelative() { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 177 | Path relative = fs.getPath("/foo").getChild("bar"); |
| 178 | Path expected = fs.getPath("/foo/bar"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 179 | assertEquals(expected, relative); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void testEqualsAndHash() { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 184 | Path path = fs.getPath("/foo/bar"); |
| 185 | Path equalPath = fs.getPath("/foo/bar"); |
| 186 | Path differentPath = fs.getPath("/foo/bar/baz"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 187 | Object differentType = new Object(); |
| 188 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 189 | new EqualsTester().addEqualityGroup(path, equalPath).testEquals(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 190 | assertFalse(path.equals(differentPath)); |
| 191 | assertFalse(path.equals(differentType)); |
| 192 | } |
| 193 | |
| 194 | @Test |
| 195 | public void testLatin1ReadAndWrite() throws IOException { |
| 196 | char[] allLatin1Chars = new char[256]; |
| 197 | for (int i = 0; i < 256; i++) { |
| 198 | allLatin1Chars[i] = (char) i; |
| 199 | } |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 200 | Path path = fs.getPath(aFile.getPath()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 201 | String latin1String = new String(allLatin1Chars); |
| 202 | FileSystemUtils.writeContentAsLatin1(path, latin1String); |
| 203 | String fileContent = new String(FileSystemUtils.readContentAsLatin1(path)); |
| 204 | assertEquals(fileContent, latin1String); |
| 205 | } |
| 206 | |
| 207 | /** |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 208 | * Verify that the encoding implemented by {@link |
| 209 | * com.google.devtools.build.lib.vfs.FileSystemUtils#writeContentAsLatin1(Path, String)} really is |
| 210 | * 8859-1 (latin1). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 211 | */ |
| 212 | @Test |
| 213 | public void testVerifyLatin1() throws IOException { |
| 214 | char[] allLatin1Chars = new char[256]; |
| 215 | for( int i = 0; i < 256; i++) { |
| 216 | allLatin1Chars[i] = (char)i; |
| 217 | } |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 218 | Path path = fs.getPath(aFile.getPath()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 219 | String latin1String = new String(allLatin1Chars); |
| 220 | FileSystemUtils.writeContentAsLatin1(path, latin1String); |
| 221 | byte[] bytes = FileSystemUtils.readContent(path); |
| 222 | assertEquals(new String(bytes, "ISO-8859-1"), latin1String); |
| 223 | } |
| 224 | |
| 225 | @Test |
| 226 | public void testBytesReadAndWrite() throws IOException { |
| 227 | byte[] bytes = new byte[] { (byte) 0xdeadbeef, (byte) 0xdeadbeef>>8, |
| 228 | (byte) 0xdeadbeef>>16, (byte) 0xdeadbeef>>24 }; |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 229 | Path path = fs.getPath(aFile.getPath()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 230 | FileSystemUtils.writeContent(path, bytes); |
| 231 | byte[] content = FileSystemUtils.readContent(path); |
| 232 | assertEquals(bytes.length, content.length); |
| 233 | for (int i = 0; i < bytes.length; i++) { |
| 234 | assertEquals(bytes[i], content[i]); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | @Test |
| 239 | public void testInputOutputStreams() throws IOException { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 240 | Path path = fs.getPath(aFile.getPath()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 241 | OutputStream out = path.getOutputStream(); |
| 242 | for (int i = 0; i < 256; i++) { |
| 243 | out.write(i); |
| 244 | } |
| 245 | out.close(); |
| 246 | InputStream in = path.getInputStream(); |
| 247 | for (int i = 0; i < 256; i++) { |
| 248 | assertEquals(i, in.read()); |
| 249 | } |
| 250 | assertEquals(-1, in.read()); |
| 251 | in.close(); |
| 252 | } |
| 253 | |
| 254 | @Test |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 255 | public void testDerivedSegmentEquality() { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 256 | Path absoluteSegment = fs.getRootDirectory(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 257 | |
| 258 | Path derivedNode = absoluteSegment.getChild("derivedSegment"); |
| 259 | Path otherDerivedNode = absoluteSegment.getChild("derivedSegment"); |
| 260 | |
| 261 | assertSame(derivedNode, otherDerivedNode); |
| 262 | } |
| 263 | } |