PY-23996 In PYTHONPATH keep the order in which source roots were added

It follows the behavior of the platform and IntelliJ IDEA in this regard.
This commit is contained in:
Mikhail Golubev
2017-04-27 15:24:00 +03:00
parent 498592e221
commit 05fa55791b
4 changed files with 68 additions and 2 deletions
@@ -15,6 +15,7 @@
*/
package com.jetbrains.python.run;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
@@ -433,10 +434,11 @@ public abstract class PythonCommandLineState extends CommandLineState {
}
}
protected static Collection<String> collectPythonPath(Project project, PythonRunParams config, boolean isDebug) {
@VisibleForTesting
public static Collection<String> collectPythonPath(Project project, PythonRunParams config, boolean isDebug) {
final Module module = getModule(project, config);
final HashSet<String> pythonPath =
Sets.newHashSet(collectPythonPath(module, config.shouldAddContentRoots(), config.shouldAddSourceRoots()));
Sets.newLinkedHashSet(collectPythonPath(module, config.shouldAddContentRoots(), config.shouldAddSourceRoots()));
if (isDebug && PythonSdkFlavor.getFlavor(config.getSdkHome()) instanceof JythonSdkFlavor) {
//that fixes Jython problem changing sys.argv on execfile, see PY-8164
@@ -0,0 +1,64 @@
/*
* Copyright 2000-2017 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.jetbrains.python.toolbox;
import com.intellij.execution.RunManager;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.PsiTestUtil;
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 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 ConfigurationFactory factory = PythonConfigurationType.getInstance().getConfigurationFactories()[0];
final RunnerAndConfigurationSettings settings = RunManager.getInstance(myFixture.getProject()).createRunConfiguration("test", factory);
final PythonRunConfiguration configuration = (PythonRunConfiguration)settings.getConfiguration();
configuration.setAddSourceRoots(true);
Collection<String> path = PythonCommandLineState.collectPythonPath(myFixture.getProject(), configuration, false);
assertContainsOrdered(path, "/src", "/src/src1", "/src/src2");
PsiTestUtil.removeSourceRoot(module, sourceRoot1);
PsiTestUtil.addSourceRoot(module, sourceRoot1);
path = PythonCommandLineState.collectPythonPath(myFixture.getProject(), configuration, false);
assertContainsOrdered(path, "/src", "/src/src2", "/src/src1");
}
@Override
protected String getTestDataPath() {
return super.getTestDataPath() + "/runConfig/";
}
}