PY-18970 Check that imports in "project" group don't belong to any library root
[idea/community.git] / python / testSrc / com / jetbrains / python / PyOptimizeImportsTest.java
index 668b53d841869186ce65e9ad04d0c027838d58e3..c812c354c0168a4a90bb0e1f72ed3db61d466f35 100644 (file)
@@ -17,7 +17,13 @@ package com.jetbrains.python;
 
 import com.intellij.codeInsight.actions.OptimizeImportsAction;
 import com.intellij.ide.DataManager;
+import com.intellij.openapi.application.WriteAction;
+import com.intellij.openapi.projectRoots.Sdk;
+import com.intellij.openapi.projectRoots.SdkModificator;
+import com.intellij.openapi.roots.OrderRootType;
+import com.intellij.openapi.vfs.VirtualFile;
 import com.jetbrains.python.fixtures.PyTestCase;
+import com.jetbrains.python.sdk.PythonSdkType;
 
 /**
  * @author yole
@@ -95,6 +101,38 @@ public class PyOptimizeImportsTest extends PyTestCase {
     doTest();
   }
 
+  // PY-18970
+  public void testLibraryRootInsideProject() {
+    final String testName = getTestName(true);
+    myFixture.copyDirectoryToProject(testName, "");
+    final VirtualFile libDir = myFixture.findFileInTempDir("lib");
+    assertNotNull(libDir);
+
+    final Sdk sdk = PythonSdkType.findPythonSdk(myFixture.getModule());
+    assertNotNull(sdk);
+    WriteAction.run(() -> {
+      final SdkModificator modificator = sdk.getSdkModificator();
+      assertNotNull(modificator);
+      modificator.addRoot(libDir, OrderRootType.CLASSES);
+      modificator.commitChanges();
+    });
+
+    try {
+      myFixture.configureByFile("main.py");
+      OptimizeImportsAction.actionPerformedImpl(DataManager.getInstance().getDataContext(myFixture.getEditor().getContentComponent()));
+      myFixture.checkResultByFile(testName + "/main.after.py");
+    }
+    finally {
+      //noinspection ThrowFromFinallyBlock
+      WriteAction.run(() -> {
+        final SdkModificator modificator = sdk.getSdkModificator();
+        assertNotNull(modificator);
+        modificator.removeRoot(libDir, OrderRootType.CLASSES);
+        modificator.commitChanges();
+      });
+    }
+  }
+
   private void doTest() {
     myFixture.configureByFile(getTestName(true) + ".py");
     OptimizeImportsAction.actionPerformedImpl(DataManager.getInstance().getDataContext(myFixture.getEditor().getContentComponent()));