replace run tests action with run configuration
[idea/community.git] / python / educational-python / course-creator-python / src / com / jetbrains / edu / coursecreator / run / PyCCRunTestConfiguration.java
1 package com.jetbrains.edu.coursecreator.run;
2
3 import com.intellij.execution.ExecutionException;
4 import com.intellij.execution.Executor;
5 import com.intellij.execution.configurations.ConfigurationFactory;
6 import com.intellij.execution.configurations.RunProfileState;
7 import com.intellij.execution.configurations.RuntimeConfigurationException;
8 import com.intellij.execution.runners.ExecutionEnvironment;
9 import com.intellij.openapi.options.SettingsEditor;
10 import com.intellij.openapi.project.Project;
11 import com.intellij.openapi.util.InvalidDataException;
12 import com.intellij.openapi.util.JDOMExternalizerUtil;
13 import com.intellij.openapi.util.WriteExternalException;
14 import com.intellij.openapi.vfs.LocalFileSystem;
15 import com.intellij.openapi.vfs.VirtualFile;
16 import com.jetbrains.edu.learning.StudyUtils;
17 import com.jetbrains.python.run.AbstractPythonRunConfiguration;
18 import org.jdom.Element;
19 import org.jetbrains.annotations.NotNull;
20 import org.jetbrains.annotations.Nullable;
21
22 class PyCCRunTestConfiguration extends AbstractPythonRunConfiguration<PyCCRunTestConfiguration> {
23
24   public static final String PATH_ATTR = "studyTest";
25   private Project myProject;
26   private String myPathToTest;
27
28   public PyCCRunTestConfiguration(Project project, ConfigurationFactory factory) {
29     super(project, factory);
30     myProject = project;
31   }
32
33   @Nullable
34   @Override
35   public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment environment) throws ExecutionException {
36     return new PyCCCommandLineState(this, environment);
37   }
38
39   @Override
40   protected SettingsEditor<PyCCRunTestConfiguration> createConfigurationEditor() {
41     return new PyCCSettingsEditor(myProject);
42   }
43
44   public String getPathToTest() {
45     return myPathToTest;
46   }
47
48   public void setPathToTest(String pathToTest) {
49     myPathToTest = pathToTest;
50   }
51
52   @Override
53   public void readExternal(Element element) throws InvalidDataException {
54     super.readExternal(element);
55     myPathToTest = JDOMExternalizerUtil.readField(element, PATH_ATTR);
56   }
57
58   @Override
59   public void writeExternal(Element element) throws WriteExternalException {
60     super.writeExternal(element);
61     JDOMExternalizerUtil.writeField(element, PATH_ATTR, myPathToTest);
62   }
63
64   @Override
65   public void checkConfiguration() throws RuntimeConfigurationException {
66     super.checkConfiguration();
67     String message = "Select valid path to the file with tests";
68     VirtualFile testsFile = LocalFileSystem.getInstance().findFileByPath(myPathToTest);
69     if (testsFile == null) {
70       throw new RuntimeConfigurationException(message);
71     }
72     VirtualFile taskDir = StudyUtils.getTaskDir(testsFile);
73     if (taskDir == null) {
74       throw new RuntimeConfigurationException(message);
75     }
76     if (StudyUtils.getTask(myProject, taskDir) == null) {
77       throw new RuntimeConfigurationException(message);
78     }
79   }
80 }