EDU-655 Run Study Tests configuration doesn't work if one of task files is image
[idea/community.git] / python / educational-python / course-creator-python / src / com / jetbrains / edu / coursecreator / run / PyCCCommandLineState.java
index f4678a97a665e606122b483e5e8fa720f4324d13..bb2264dbc18d042bd0069140f62582253b25d620 100644 (file)
@@ -10,6 +10,7 @@ import com.intellij.execution.process.ProcessEvent;
 import com.intellij.execution.process.ProcessHandler;
 import com.intellij.execution.runners.ExecutionEnvironment;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.execution.process.ProcessHandler;
 import com.intellij.execution.runners.ExecutionEnvironment;
 import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.LocalFileSystem;
@@ -22,11 +23,13 @@ import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.Task;
 import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.Task;
+import com.jetbrains.edu.learning.courseFormat.TaskFile;
 import com.jetbrains.python.run.CommandLinePatcher;
 import com.jetbrains.python.run.PythonCommandLineState;
 import com.jetbrains.python.run.CommandLinePatcher;
 import com.jetbrains.python.run.PythonCommandLineState;
-import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
 import java.io.File;
 
 import java.io.File;
+import java.util.Map;
 
 public class PyCCCommandLineState extends PythonCommandLineState {
   private final PyCCRunTestConfiguration myRunConfiguration;
 
 public class PyCCCommandLineState extends PythonCommandLineState {
   private final PyCCRunTestConfiguration myRunConfiguration;
@@ -58,16 +61,36 @@ public class PyCCCommandLineState extends PythonCommandLineState {
     group.addParameter(myRunConfiguration.getPathToTest());
     group.addParameter(new File(course.getCourseDirectory()).getPath());
 
     group.addParameter(myRunConfiguration.getPathToTest());
     group.addParameter(new File(course.getCourseDirectory()).getPath());
 
-    group.addParameter(getFirstTaskFilePath());
+    String path = getFirstTaskFilePath();
+    if (path != null) {
+      group.addParameter(path);
+    }
   }
 
   }
 
-  @NotNull
+  @Nullable
   private String getFirstTaskFilePath() {
   private String getFirstTaskFilePath() {
-    String firstTaskFileName = StudyUtils.getFirst(myTask.getTaskFiles().keySet());
+    for (Map.Entry<String, TaskFile> entry : myTask.getTaskFiles().entrySet()) {
+      String path = getTaskFilePath(entry.getKey());
+      if (!entry.getValue().getAnswerPlaceholders().isEmpty()) {
+        return path;
+      }
+      VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path);
+      if (virtualFile == null) {
+        continue;
+      }
+      if (TextEditorProvider.isTextFile(virtualFile)) {
+        return path;
+      }
+    }
+    return null;
+  }
+
+
+  private String getTaskFilePath(String name) {
     String taskDirPath = FileUtil.toSystemDependentName(myTaskDir.getPath());
     return myTaskDir.findChild(EduNames.SRC) != null ?
     String taskDirPath = FileUtil.toSystemDependentName(myTaskDir.getPath());
     return myTaskDir.findChild(EduNames.SRC) != null ?
-           FileUtil.join(taskDirPath, EduNames.SRC, firstTaskFileName) :
-           FileUtil.join(taskDirPath, firstTaskFileName);
+           FileUtil.join(taskDirPath, EduNames.SRC, name) :
+           FileUtil.join(taskDirPath, name);
   }
 
   @Override
   }
 
   @Override