From efb81b1e37ed5ef990e0e3c7ded4bb72aa775073 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 26 Jul 2019 14:38:08 +0200 Subject: [PATCH] [test framework] prevents accidental copying of local files into temp. FS by absolute paths GitOrigin-RevId: 570914c80338caa33a6e22e5578be4eb8957d201 --- .../impl/CodeInsightTestFixtureImpl.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java index df381056e7a5..d6aa38b29bf4 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java @@ -55,6 +55,7 @@ import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.components.ServiceManager; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorFactory; @@ -135,6 +136,7 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.intellij.openapi.util.io.FileUtil.toSystemDependentName; import static org.junit.Assert.*; /** @@ -327,16 +329,29 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig @Override public VirtualFile copyFileToProject(@NotNull String sourcePath, @NotNull String targetPath) { String testDataPath = getTestDataPath(); - File sourceFile = FileUtil.findFirstThatExist(testDataPath + '/' + sourcePath, sourcePath); + File sourceFile = new File(testDataPath, toSystemDependentName(sourcePath)); + if (!sourceFile.exists()) { + File candidate = new File(sourcePath); + if (candidate.isAbsolute()) { + sourceFile = candidate; + if (targetPath == sourcePath) { + targetPath = PathUtil.getFileName(sourcePath); + Logger.getInstance(getClass()).warn("Absolute target path '" + sourcePath + "' trimmed to '" + targetPath + "'"); + } + } + } + + targetPath = FileUtil.toSystemIndependentName(targetPath); + VirtualFile targetFile = myTempDirFixture.getFile(targetPath); - if (sourceFile == null && targetFile != null && targetPath.equals(sourcePath)) { + if (!sourceFile.exists() && targetFile != null && targetPath.equals(sourcePath)) { return targetFile; } assertFileEndsWithCaseSensitivePath(sourceFile); - assertNotNull("Cannot find source file: " + sourcePath + "; test data path: " + testDataPath, sourceFile); + assertTrue("Cannot find source file: " + sourcePath + "; test data path: " + testDataPath, sourceFile.exists()); assertTrue("Not a file: " + sourceFile, sourceFile.isFile()); if (targetFile == null) {