From 36bc3ebc14bb80e1faae0bf5a217debb1beef1cb Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 4 Nov 2015 22:25:43 +0100 Subject: [PATCH] [tests] symlink-related tests fixed on OS X --- .../vfs/impl/local/CanonicalPathMapTest.java | 6 +- .../vfs/local/SymlinkHandlingTest.java | 4 +- .../testFramework/rules/TempDirectory.java | 72 +++++++++++++++++++ 3 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 platform/testFramework/src/com/intellij/testFramework/rules/TempDirectory.java diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/impl/local/CanonicalPathMapTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/impl/local/CanonicalPathMapTest.java index 002bc48b351b..14720a7ec2b0 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/impl/local/CanonicalPathMapTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/impl/local/CanonicalPathMapTest.java @@ -16,9 +16,9 @@ package com.intellij.openapi.vfs.impl.local; import com.intellij.openapi.util.io.IoTestUtil; +import com.intellij.testFramework.rules.TempDirectory; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.TemporaryFolder; import java.io.File; import java.util.Collection; @@ -28,7 +28,7 @@ import static com.intellij.openapi.util.Pair.pair; import static org.assertj.core.api.Assertions.assertThat; public class CanonicalPathMapTest { - @Rule public TemporaryFolder myTempDir = new TemporaryFolder(); + @Rule public TempDirectory myTempDir = new TempDirectory(); @Test public void testRemappedSymLinkReportsOriginalWatchedPath() throws Exception { @@ -41,7 +41,7 @@ public class CanonicalPathMapTest { CanonicalPathMap pathMap = new CanonicalPathMap(Collections.singletonList(symLink.getPath()), Collections.emptyList()); // REMAP from native file watcher: /?/root/mapped -> /?/root/real - pathMap.addMapping(Collections.singletonList(pair(mappedDir.getPath(), realDir.getCanonicalPath()))); + pathMap.addMapping(Collections.singletonList(pair(mappedDir.getPath(), realDir.getPath()))); Collection watchedPaths = pathMap.getWatchedPaths(new File(mappedDir, "file.txt").getPath(), true, false); assertThat(watchedPaths).containsExactly(new File(symLink, "file.txt").getPath()); diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/SymlinkHandlingTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/SymlinkHandlingTest.java index 99c16b3129c4..3f0839ae2e5c 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/SymlinkHandlingTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/SymlinkHandlingTest.java @@ -22,12 +22,12 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.*; import com.intellij.testFramework.fixtures.BareTestFixtureTestCase; +import com.intellij.testFramework.rules.TempDirectory; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.TemporaryFolder; import java.io.File; import java.util.Set; @@ -40,7 +40,7 @@ import static org.junit.Assert.*; import static org.junit.Assume.assumeTrue; public class SymlinkHandlingTest extends BareTestFixtureTestCase { - @Rule public TemporaryFolder myTempDir = new TemporaryFolder(); + @Rule public TempDirectory myTempDir = new TempDirectory(); @Before public void setUp() { diff --git a/platform/testFramework/src/com/intellij/testFramework/rules/TempDirectory.java b/platform/testFramework/src/com/intellij/testFramework/rules/TempDirectory.java new file mode 100644 index 000000000000..62230a90d34b --- /dev/null +++ b/platform/testFramework/src/com/intellij/testFramework/rules/TempDirectory.java @@ -0,0 +1,72 @@ +/* + * Copyright 2000-2015 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.testFramework.rules; + +import com.intellij.openapi.util.io.FileUtil; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.assertTrue; + +/** + * A clone of {@link TemporaryFolder} with no symlinks in a temporary directory path and better directory name. + */ +public class TempDirectory extends TemporaryFolder { + private String myName = null; + private File myRoot = null; + + @Override + public Statement apply(Statement base, Description description) { + myName = FileUtil.sanitizeFileName(description.getMethodName(), false); + return super.apply(base, description); + } + + @Override + protected void before() throws IOException { + if (myName == null) { + throw new IllegalStateException("apply() was not called"); + } + + @SuppressWarnings("SSBasedInspection") File dir = File.createTempFile("junit_" + myName + "_", ""); + assertTrue("Cannot delete: " + dir.getPath(), dir.delete() || !dir.exists()); + assertTrue("Cannot create: " + dir.getPath(), dir.mkdir() || dir.isDirectory()); + myRoot = dir.getCanonicalFile(); + } + + @Override + protected void after() { + if (myRoot == null) { + throw new IllegalStateException("before() was not called"); + } + + FileUtil.delete(myRoot); + myRoot = null; + myName = null; + } + + @Override + public File getRoot() { + if (myRoot == null) { + throw new IllegalStateException("before() was not called"); + } + + return myRoot; + } +} \ No newline at end of file