blob: 15a18428b5d1b189789db71fc227442c92e15d50 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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;
17import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertFalse;
19import static org.junit.Assert.assertSame;
20import static org.junit.Assert.assertTrue;
21import static org.junit.Assert.fail;
22
23import com.google.common.collect.Lists;
Ulf Adams795895a2015-03-06 15:58:35 +000024import com.google.common.testing.EqualsTester;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025import com.google.devtools.build.lib.testutil.TestUtils;
26import com.google.devtools.build.lib.vfs.util.FileSystems;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027import java.io.File;
Ulf Adams8afbd3c2017-02-28 10:42:48 +000028import java.io.FileOutputStream;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029import java.io.IOException;
30import java.io.InputStream;
31import java.io.OutputStream;
32import java.util.Arrays;
33import java.util.Collection;
34import java.util.HashSet;
35import java.util.List;
Laszlo Csomorca99bb72016-10-25 13:15:55 +000036import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.junit.runners.JUnit4;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040
Ulf Adams8afbd3c2017-02-28 10:42:48 +000041/** Tests for {@link Path} in combination with the native file system for the current platform. */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010042@RunWith(JUnit4.class)
Ulf Adams8afbd3c2017-02-28 10:42:48 +000043public class NativePathTest {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010044
Ulf Adams8afbd3c2017-02-28 10:42:48 +000045 private FileSystem fs;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010046 private File aDirectory;
47 private File aFile;
48 private File anotherFile;
49 private File tmpDir;
50
Ulf Adams8afbd3c2017-02-28 10:42:48 +000051 protected FileSystem getNativeFileSystem() {
Yun Peng22eb3322016-06-21 14:38:12 +000052 return FileSystems.getNativeFileSystem();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010053 }
54
55 @Before
Florian Weikert99ac0a22015-12-01 15:05:35 +000056 public final void createFiles() throws Exception {
Ulf Adams8afbd3c2017-02-28 10:42:48 +000057 fs = getNativeFileSystem();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010058 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 Adams8afbd3c2017-02-28 10:42:48 +000063 new FileOutputStream(aFile).close();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010064 anotherFile = new File(aDirectory, "another_file.txt");
Ulf Adams8afbd3c2017-02-28 10:42:48 +000065 new FileOutputStream(anotherFile).close();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010066 }
67
68 @Test
69 public void testExists() {
Ulf Adams8afbd3c2017-02-28 10:42:48 +000070 assertTrue(fs.getPath(aDirectory.getPath()).exists());
71 assertTrue(fs.getPath(aFile.getPath()).exists());
72 assertFalse(fs.getPath("/does/not/exist").exists());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073 }
74
75 @Test
76 public void testDirectoryEntriesForDirectory() throws IOException {
77 Collection<Path> entries =
Ulf Adams8afbd3c2017-02-28 10:42:48 +000078 fs.getPath(tmpDir.getPath()).getDirectoryEntries();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010079 List<Path> expectedEntries = Arrays.asList(
Ulf Adams8afbd3c2017-02-28 10:42:48 +000080 fs.getPath(tmpDir.getPath() + "/a_file"),
81 fs.getPath(tmpDir.getPath() + "/a_directory"));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010082
83 assertEquals(new HashSet<Object>(expectedEntries),
84 new HashSet<Object>(entries));
85 }
86
87 @Test
88 public void testDirectoryEntriesForFileThrowsException() {
89 try {
Ulf Adams8afbd3c2017-02-28 10:42:48 +000090 fs.getPath(aFile.getPath()).getDirectoryEntries();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010091 fail("No exception thrown.");
92 } catch (IOException x) {
93 // The expected result.
94 }
95 }
96
97 @Test
98 public void testIsFileIsTrueForFile() {
Ulf Adams8afbd3c2017-02-28 10:42:48 +000099 assertTrue(fs.getPath(aFile.getPath()).isFile());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100100 }
101
102 @Test
103 public void testIsFileIsFalseForDirectory() {
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000104 assertFalse(fs.getPath(aDirectory.getPath()).isFile());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100105 }
106
107 @Test
108 public void testBaseName() {
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000109 assertEquals("base", fs.getPath("/foo/base").getBaseName());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100110 }
111
112 @Test
113 public void testBaseNameRunsAfterDotDotInterpretation() {
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000114 assertEquals("base", fs.getPath("/base/foo/..").getBaseName());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100115 }
116
117 @Test
118 public void testParentOfRootIsRoot() {
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000119 assertEquals(fs.getPath("/"), fs.getPath("/.."));
120 assertEquals(fs.getPath("/"), fs.getPath("/../../../../../.."));
121 assertEquals(fs.getPath("/foo"), fs.getPath("/../../../foo"));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100122 }
123
124 @Test
125 public void testIsDirectory() {
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000126 assertTrue(fs.getPath(aDirectory.getPath()).isDirectory());
127 assertFalse(fs.getPath(aFile.getPath()).isDirectory());
128 assertFalse(fs.getPath("/does/not/exist").isDirectory());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100129 }
130
131 @Test
132 public void testListNonExistingDirectoryThrowsException() {
133 try {
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000134 fs.getPath("/does/not/exist").getDirectoryEntries();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100135 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 Adams8afbd3c2017-02-28 10:42:48 +0000153 Collection<Path> textFiles = UnixGlob.forPath(fs.getPath(tmpDir.getPath()))
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100154 .addPattern("*/*.txt")
155 .globInterruptible();
Ulf Adams795895a2015-03-06 15:58:35 +0000156 assertThat(textFiles).hasSize(1);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100157 Path onlyFile = textFiles.iterator().next();
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000158 assertEquals(fs.getPath(anotherFile.getPath()), onlyFile);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100159
160 Collection<Path> onlyFiles =
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000161 UnixGlob.forPath(fs.getPath(tmpDir.getPath()))
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100162 .addPattern("*")
163 .setExcludeDirectories(true)
164 .globInterruptible();
165 assertPathSet(onlyFiles, aFile.getPath());
166
167 Collection<Path> directoriesToo =
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000168 UnixGlob.forPath(fs.getPath(tmpDir.getPath()))
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100169 .addPattern("*")
170 .setExcludeDirectories(false)
171 .globInterruptible();
172 assertPathSet(directoriesToo, aFile.getPath(), aDirectory.getPath());
173 }
174
175 @Test
176 public void testGetRelative() {
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000177 Path relative = fs.getPath("/foo").getChild("bar");
178 Path expected = fs.getPath("/foo/bar");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100179 assertEquals(expected, relative);
180 }
181
182 @Test
183 public void testEqualsAndHash() {
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000184 Path path = fs.getPath("/foo/bar");
185 Path equalPath = fs.getPath("/foo/bar");
186 Path differentPath = fs.getPath("/foo/bar/baz");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100187 Object differentType = new Object();
188
Ulf Adams795895a2015-03-06 15:58:35 +0000189 new EqualsTester().addEqualityGroup(path, equalPath).testEquals();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100190 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 Adams8afbd3c2017-02-28 10:42:48 +0000200 Path path = fs.getPath(aFile.getPath());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100201 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 Adams8afbd3c2017-02-28 10:42:48 +0000208 * 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100211 */
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 Adams8afbd3c2017-02-28 10:42:48 +0000218 Path path = fs.getPath(aFile.getPath());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100219 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 Adams8afbd3c2017-02-28 10:42:48 +0000229 Path path = fs.getPath(aFile.getPath());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100230 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 Adams8afbd3c2017-02-28 10:42:48 +0000240 Path path = fs.getPath(aFile.getPath());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100241 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100255 public void testDerivedSegmentEquality() {
Ulf Adams8afbd3c2017-02-28 10:42:48 +0000256 Path absoluteSegment = fs.getRootDirectory();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100257
258 Path derivedNode = absoluteSegment.getChild("derivedSegment");
259 Path otherDerivedNode = absoluteSegment.getChild("derivedSegment");
260
261 assertSame(derivedNode, otherDerivedNode);
262 }
263}