fix check utilities
[idea/community.git] / python / educational-python / course-creator-python / src / com / jetbrains / edu / coursecreator / run / PyCCRunTestsConfigurationProducer.java
1 package com.jetbrains.edu.coursecreator.run;
2
3 import com.intellij.execution.Location;
4 import com.intellij.execution.actions.ConfigurationContext;
5 import com.intellij.execution.actions.RunConfigurationProducer;
6 import com.intellij.openapi.project.Project;
7 import com.intellij.openapi.util.Ref;
8 import com.intellij.openapi.util.io.FileUtil;
9 import com.intellij.openapi.vfs.LocalFileSystem;
10 import com.intellij.openapi.vfs.VirtualFile;
11 import com.intellij.psi.PsiElement;
12 import com.jetbrains.edu.coursecreator.CCUtils;
13 import com.jetbrains.edu.coursecreator.PyCCLanguageManager;
14 import com.jetbrains.edu.learning.StudyUtils;
15 import com.jetbrains.edu.learning.core.EduNames;
16 import com.jetbrains.edu.learning.courseFormat.Task;
17 import org.jetbrains.annotations.NotNull;
18 import org.jetbrains.annotations.Nullable;
19
20 public class PyCCRunTestsConfigurationProducer extends RunConfigurationProducer<PyCCRunTestConfiguration> {
21   protected PyCCRunTestsConfigurationProducer() {
22     super(PyCCRunTestsConfigurationType.getInstance());
23   }
24
25   @Override
26   protected boolean setupConfigurationFromContext(PyCCRunTestConfiguration configuration,
27                                                   ConfigurationContext context,
28                                                   Ref<PsiElement> sourceElement) {
29     Project project = context.getProject();
30     if (!CCUtils.isCourseCreator(project)) {
31       return false;
32     }
33
34     String testsPath = getTestPath(context);
35     if (testsPath == null) {
36       return false;
37     }
38     VirtualFile testsFile = LocalFileSystem.getInstance().findFileByPath(testsPath);
39     if (testsFile == null) {
40       return false;
41     }
42
43     String generatedName = generateName(testsFile, project);
44     if (generatedName == null) {
45       return false;
46     }
47
48     configuration.setPathToTest(testsPath);
49     configuration.setName(generatedName);
50     return true;
51   }
52
53   @Nullable
54   private static String generateName(@NotNull VirtualFile testsFile, @NotNull Project project) {
55     VirtualFile taskDir = StudyUtils.getTaskDir(testsFile);
56     if (taskDir == null) {
57       return null;
58     }
59     Task task = StudyUtils.getTask(project, taskDir);
60     if (task == null) {
61       return null;
62     }
63     String generatedName = task.getLesson().getName() + "/" + task.getName();
64     if (task.hasSubtasks()) {
65       int index = task.getActiveSubtaskIndex() + 1;
66       generatedName += " " + index;
67     }
68     return generatedName;
69   }
70
71   @Nullable
72   private static String getTestPath(@NotNull ConfigurationContext context) {
73     Location location = context.getLocation();
74     if (location == null) {
75       return null;
76     }
77     VirtualFile file = location.getVirtualFile();
78     if (file == null) {
79       return null;
80     }
81     VirtualFile taskDir = StudyUtils.getTaskDir(file);
82     if (taskDir == null) {
83       return null;
84     }
85
86     Task task = StudyUtils.getTask(location.getProject(), taskDir);
87     if (task == null) {
88       return null;
89     }
90     String testsFileName = PyCCLanguageManager.getSubtaskTestsFileName(task.getActiveSubtaskIndex());
91     String taskDirPath = FileUtil.toSystemDependentName(taskDir.getPath());
92     String testsPath = taskDir.findChild(EduNames.SRC) != null ?
93                        FileUtil.join(taskDirPath, EduNames.SRC, testsFileName) :
94                        FileUtil.join(taskDirPath, testsFileName);
95     String filePath = FileUtil.toSystemDependentName(file.getPath());
96     return filePath.equals(testsPath) ? testsPath : null;
97   }
98
99   @Override
100   public boolean isConfigurationFromContext(PyCCRunTestConfiguration configuration, ConfigurationContext context) {
101     String path = getTestPath(context);
102     if (path == null) {
103       return false;
104     }
105     return path.equals(configuration.getPathToTest());
106   }
107 }