// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.toolbox;

import com.intellij.execution.RunManager;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.PsiTestUtil;
import com.intellij.util.PathUtil;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.python.fixtures.PyTestCase;
import com.jetbrains.python.run.PythonCommandLineState;
import com.jetbrains.python.run.PythonConfigurationType;
import com.jetbrains.python.run.PythonRunConfiguration;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

/**
 * @author Mikhail Golubev
 */
public class PyRunConfigurationTest extends PyTestCase {

  public void testPythonPathPreservesAdditionOrderOfSourceRoots() {
    myFixture.copyDirectoryToProject(getTestName(false), "");
    final VirtualFile sourceRoot1 = myFixture.findFileInTempDir("src1");
    final VirtualFile sourceRoot2 = myFixture.findFileInTempDir("src2");

    final Module module = myFixture.getModule();
    PsiTestUtil.addSourceRoot(module, sourceRoot1);
    PsiTestUtil.addSourceRoot(module, sourceRoot2);

    final RunnerAndConfigurationSettings settings = RunManager.getInstance(myFixture.getProject()).createConfiguration("test", PythonConfigurationType.class);
    final PythonRunConfiguration configuration = (PythonRunConfiguration)settings.getConfiguration();
    configuration.setAddSourceRoots(true);

    Collection<String> path = collectProjectPythonPathEntries(configuration);
    assertContainsOrdered(path, "/src", "/src/src1", "/src/src2");

    PsiTestUtil.removeSourceRoot(module, sourceRoot1);
    PsiTestUtil.addSourceRoot(module, sourceRoot1);

    path = collectProjectPythonPathEntries(configuration);
    assertContainsOrdered(path, "/src", "/src/src2", "/src/src1");
  }

  @NotNull
  private Collection<String> collectProjectPythonPathEntries(@NotNull PythonRunConfiguration configuration) {
    final Collection<String> roots = PythonCommandLineState.collectPythonPath(myFixture.getProject(), configuration, false);
    return ContainerUtil.map(roots, PathUtil::toSystemIndependentName);
  }

  @Override
  protected String getTestDataPath() {
    return super.getTestDataPath() + "/runConfig/";
  }
}

@@ -408,24 +408,95 @@
 siTestUtil;%0A
+import com.intellij.testFramework.fixtures.CodeInsightFixtureTestCase;%0A
 import com.i
@@ -514,24 +514,24 @@
 l.PathUtil;%0A
-
 import com.i
@@ -573,57 +573,8 @@
 il;%0A
-import com.jetbrains.python.fixtures.PyTestCase;%0A
 impo
@@ -890,18 +890,34 @@
 extends 
-Py
+CodeInsightFixture
 TestCase
@@ -1046,24 +1046,70 @@
 alse), %22%22);%0A
+    String root = myFixture.getTempDirPath();%0A
     final Vi
@@ -1816,34 +1816,38 @@
 th, 
-%22/src%22, %22/src/src1%22, %22/src
+root, root + %22/src1%22, root + %22
 /src
@@ -2055,34 +2055,38 @@
 th, 
-%22/src%22, %22/src/src2%22, %22/src
+root, root + %22/src2%22, root + %22
 /src
@@ -2418,16 +2418,86 @@
 );%0A  %7D%0A%0A
+  @Override%0A  protected boolean isCommunity() %7B%0A    return true;%0A  %7D%0A%0A
   @Overr
@@ -2522,24 +2522,20 @@
 ring get
-TestData
+Base
 Path() %7B
@@ -2550,35 +2550,25 @@
 urn 
-super.getTestDataPath() + %22
+%22/python/testData
 /run

make PyRunConfigurationTest heavy because it changes shared project's roots