Hard links saving test

This commit is contained in:
Roman Shevchenko
2011-12-13 13:12:32 +01:00
parent 480c01f960
commit e6a53e5482
2 changed files with 57 additions and 59 deletions
@@ -1,15 +1,29 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* 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.intellij.openapi.vfs;
import com.intellij.idea.Bombed;
import com.intellij.execution.util.ExecUtil;
import com.intellij.ide.GeneralSettings;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.vfs.impl.win32.Win32LocalFileSystem;
import com.intellij.openapi.vfs.newvfs.ManagingFS;
import com.intellij.openapi.vfs.newvfs.persistent.PersistentFS;
import com.intellij.testFramework.IdeaTestCase;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.PlatformTestUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
@@ -18,16 +32,18 @@ import java.nio.charset.Charset;
import java.util.Arrays;
public class LocalFileSystemTest extends IdeaTestCase{
private static final String KEY = "filesystem.useNative";
public static void setContentOnDisk(File file, byte[] bom, String content, Charset charset) throws IOException {
FileOutputStream stream = new FileOutputStream(file);
if (bom != null) {
stream.write(bom);
}
OutputStreamWriter writer = new OutputStreamWriter(stream, charset);
writer.write(content);
writer.close();
try {
writer.write(content);
}
finally {
writer.close();
}
}
public static VirtualFile createTempFile(@NonNls String ext, @Nullable byte[] bom, @NonNls String content, Charset charset) throws IOException {
@@ -35,7 +51,9 @@ public class LocalFileSystemTest extends IdeaTestCase{
setContentOnDisk(temp, bom, content, charset);
myFilesToDelete.add(temp);
return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(temp);
final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(temp);
assert file != null : temp;
return file;
}
public void testChildrenAccessedButNotCached() throws Exception{
@@ -178,7 +196,7 @@ public class LocalFileSystemTest extends IdeaTestCase{
final String newName = "dir";
final VirtualFile dirCopy = dirToCopy.copy(null, toVDir, newName);
assertEquals(newName, dirCopy.getName());
IdeaTestUtil.assertDirectoriesEqual(toVDir, fromVDir, null);
PlatformTestUtil.assertDirectoriesEqual(toVDir, fromVDir, null);
}
catch(Exception e){
LOG.error(e);
@@ -193,7 +211,7 @@ public class LocalFileSystemTest extends IdeaTestCase{
final String name = "te\u00dft123123123.txt";
final File childFile = new File(dirFile, name);
childFile.createNewFile();
assert childFile.createNewFile() || childFile.exists() : childFile;
ApplicationManager.getApplication().runWriteAction(
new Runnable() {
@@ -214,35 +232,9 @@ public class LocalFileSystemTest extends IdeaTestCase{
}
);
assertTrue(childFile.delete());
}
public void _testSymLinks() throws Exception {
ApplicationManager.getApplication().runWriteAction(
new Runnable() {
@Override
public void run() {
boolean b = Registry.get(KEY).asBoolean();
try{
Registry.get(KEY).setValue(false);
final VirtualFile dir = LocalFileSystem.getInstance().refreshAndFindFileByPath("C:/Documents and Settings");
Win32LocalFileSystem system = Win32LocalFileSystem.getWin32Instance();
system.exists(dir);
String[] strings = system.list(dir);
System.out.println(Arrays.asList(strings));
}
catch(Exception e){
fail(e.getMessage());
}
finally {
Registry.get(KEY).setValue(b);
}
}
}
);
}
public void testFindRoot() {
VirtualFile file = LocalFileSystem.getInstance().findFileByPath("wrong_path");
assertNull(file);
@@ -266,7 +258,6 @@ public class LocalFileSystemTest extends IdeaTestCase{
}
public void testFileLength() throws Exception {
File file = FileUtil.createTempFile("test", "txt");
FileUtil.writeToFile(file, "hello");
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
@@ -282,24 +273,38 @@ public class LocalFileSystemTest extends IdeaTestCase{
assertEquals(11, virtualFile.getLength());
}
@Bombed(month = 12, day = 13, user = "roman.shevchenko")
public void testHardLinks() throws Exception {
if (SystemInfo.isWindows) {
File dir = FileUtil.createTempDirectory("hardlinks", "");
File oldfile = new File(dir, "oldfile");
assertTrue(oldfile.createNewFile());
File newfile = new File(dir, "newfile");
Process process = Runtime.getRuntime().exec(
new String[]{"fsutil", "hardlink", "create", '"' + newfile.getPath() + '"', '"' + oldfile.getPath() + '"'});
InputStream stream = process.getInputStream();
System.out.println(new String(FileUtil.loadBytes(stream)));
process.waitFor();
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(oldfile);
if (!SystemInfo.isWindows && !SystemInfo.isUnix) return;
final boolean safeWrite = GeneralSettings.getInstance().isUseSafeWrite();
final File dir = FileUtil.createTempDirectory("hardlinks", "");
try {
GeneralSettings.getInstance().setUseSafeWrite(false);
final File targetFile = new File(dir, "targetFile");
assertTrue(targetFile.createNewFile());
final File hardLinkFile = new File(dir, "hardLinkFile");
if (SystemInfo.isWindows) {
assertEquals("target=" + targetFile + " link=" + hardLinkFile,
0, ExecUtil.execAndGetResult("fsutil", "hardlink", "create", hardLinkFile.getPath(), targetFile.getPath()));
}
else if (SystemInfo.isUnix) {
assertEquals("target=" + targetFile + " link=" + hardLinkFile,
0, ExecUtil.execAndGetResult("ln", targetFile.getPath(), hardLinkFile.getPath()));
}
final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(targetFile);
assertNotNull(file);
file.setBinaryContent("hello".getBytes(), 0, 0, new SafeWriteRequestor() {});
VirtualFile check = LocalFileSystem.getInstance().findFileByIoFile(newfile);
final VirtualFile check = LocalFileSystem.getInstance().findFileByIoFile(hardLinkFile);
assertNotNull(check);
assertEquals("hello", VfsUtil.loadText(check));
}
finally {
GeneralSettings.getInstance().setUseSafeWrite(safeWrite);
FileUtil.delete(dir);
}
}
}
@@ -48,7 +48,7 @@ public class ExecUtil {
public static String loadTemplate(@NotNull final ClassLoader loader,
@NotNull final String templateName,
@Nullable final Map<String, String> variables) throws IOException {
final InputStream stream = loader.getResourceAsStream(templateName);
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed") final InputStream stream = loader.getResourceAsStream(templateName);
if (stream == null) {
throw new IOException("Template '" + templateName + "' not found by " + loader);
}
@@ -91,13 +91,6 @@ public class ExecUtil {
public static int sudoAndGetResult(@NotNull final String scriptPath,
@NotNull final String prompt) throws IOException, ExecutionException, ScriptException, InterruptedException {
if (SystemInfo.isMac) {
/*
final ScriptEngine engine = new ScriptEngineManager(null).getEngineByName("AppleScript");
if (engine == null) {
throw new ExecutionException("Could not find AppleScript engine");
}
engine.eval("do shell script \"" + scriptPath + "\" with administrator privileges");
*/
final String script = "do shell script \"" + scriptPath + "\" with administrator privileges";
Runtime runtime = Runtime.getRuntime();
String[] args = {getOsascriptPath(), "-e", script};