fixed run tests action
[idea/community.git] / python / edu / course-creator-python / src / com / jetbrains / edu / coursecreator / actions / PyCCRunTestsAction.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.jetbrains.edu.coursecreator.actions;
17
18 import com.intellij.execution.Executor;
19 import com.intellij.execution.ProgramRunnerUtil;
20 import com.intellij.execution.RunManager;
21 import com.intellij.execution.RunnerAndConfigurationSettings;
22 import com.intellij.execution.configurations.ConfigurationFactory;
23 import com.intellij.execution.executors.DefaultRunExecutor;
24 import com.intellij.ide.projectView.ProjectView;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.vfs.VirtualFile;
27 import com.intellij.openapi.vfs.VirtualFileManager;
28 import com.intellij.util.PathUtil;
29 import com.jetbrains.edu.coursecreator.CCProjectService;
30 import com.jetbrains.python.run.PythonConfigurationType;
31 import com.jetbrains.python.run.PythonRunConfiguration;
32 import org.jetbrains.annotations.NotNull;
33
34 public class PyCCRunTestsAction extends CCRunTestsAction {
35
36   @Override
37   protected void executeTests(@NotNull final Project project,
38                                    @NotNull final VirtualFile virtualFile,
39                                    @NotNull final VirtualFile taskDir,
40                                    @NotNull final VirtualFile testFile) {
41     final ConfigurationFactory factory = PythonConfigurationType.getInstance().getConfigurationFactories()[0];
42     final RunnerAndConfigurationSettings settings =
43       RunManager.getInstance(project).createRunConfiguration("test", factory);
44
45     final PythonRunConfiguration configuration = (PythonRunConfiguration)settings.getConfiguration();
46     configuration.setScriptName(testFile.getPath());
47     configuration.setWorkingDirectory(taskDir.getPath());
48     String taskFileName = CCProjectService.getRealTaskFileName(virtualFile.getName());
49     if (taskFileName == null) {
50       return;
51     }
52     VirtualFile userFile = taskDir.findChild(taskFileName);
53     if (userFile == null) {
54       return;
55     }
56     VirtualFile ideaDir = project.getBaseDir().findChild(".idea");
57     if (ideaDir == null) {
58       return;
59     }
60     VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);
61     ProjectView.getInstance(project).refresh();
62     configuration.setScriptParameters(PathUtil.toSystemDependentName(project.getBasePath()) + " " + PathUtil.toSystemDependentName(userFile.getPath()));
63     Executor executor = DefaultRunExecutor.getRunExecutorInstance();
64     ProgramRunnerUtil.executeConfiguration(project, settings, executor);
65   }
66 }