exclude test files for future subtasks
authorLiana.Bakradze <liana.bakradze@jetbrains.com>
Thu, 27 Oct 2016 14:50:41 +0000 (17:50 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 17 Nov 2016 14:08:37 +0000 (17:08 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateCourseArchive.java
python/educational-core/student/src/com/jetbrains/edu/learning/StudySubtaskUtils.java

index 64182506bcbb24655ffda92b5a995245da661ebb..c8efdd55af3b4d1fa3c42d6219ab7966dd48be0a 100644 (file)
@@ -14,10 +14,12 @@ import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.DialogWrapper;
 import com.intellij.openapi.ui.Messages;
+import com.intellij.openapi.util.Condition;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.VfsUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.VirtualFileManager;
+import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.io.ZipUtil;
 import com.jetbrains.edu.coursecreator.CCLanguageManager;
 import com.jetbrains.edu.coursecreator.CCUtils;
@@ -32,6 +34,8 @@ import com.jetbrains.edu.learning.statistics.EduUsagesCollector;
 import org.jetbrains.annotations.NotNull;
 
 import java.io.*;
+import java.util.Arrays;
+import java.util.List;
 import java.util.zip.ZipOutputStream;
 
 public class CCCreateCourseArchive extends DumbAwareAction {
@@ -127,6 +131,9 @@ public class CCCreateCourseArchive extends DumbAwareAction {
             if (srcDir != null) {
               studentFileDir = srcDir;
             }
+            if (task.hasSubtasks()) {
+              transformSubtaskTestsToTextFiles(studentFileDir);
+            }
             for (String taskFile : task.getTaskFiles().keySet()) {
               VirtualFile answerFile = taskDir.findChild(taskFile);
               if (answerFile == null) {
@@ -134,9 +141,23 @@ public class CCCreateCourseArchive extends DumbAwareAction {
               }
               EduUtils.createStudentFile(this, project, answerFile, studentFileDir, task, 0);
             }
+          }
+        }
+      }
+
+      private void transformSubtaskTestsToTextFiles(VirtualFile studentFileDir) {
+        Condition<VirtualFile> isSubtaskTestFile =
+          file -> CCUtils.isTestsFile(project, file) && file.getName().contains(EduNames.SUBTASK_MARKER);
+        List<VirtualFile> subtaskTests = ContainerUtil.filter(Arrays.asList(studentFileDir.getChildren()), isSubtaskTestFile);
+        for (VirtualFile subtaskTest : subtaskTests) {
+          try {
+            subtaskTest.rename(this, subtaskTest.getNameWithoutExtension() + ".txt");
+          }
+          catch (IOException e) {
+            LOG.error(e);
+          }
         }
       }
-    }
     });
   }
 
index 9faa6125f2b83e1e11a7ab9b3eb12de9584a7db9..705c70dfd1872bd7da2ad8ffaf6b4161c5b7b0bc 100644 (file)
@@ -1,26 +1,30 @@
 package com.jetbrains.edu.learning;
 
 import com.intellij.ide.projectView.ProjectView;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.io.FileUtil;
+import com.intellij.openapi.util.io.FileUtilRt;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.problems.WolfTheProblemSolver;
 import com.intellij.ui.EditorNotifications;
 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.core.EduUtils;
-import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
-import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
-import com.jetbrains.edu.learning.courseFormat.Task;
-import com.jetbrains.edu.learning.courseFormat.TaskFile;
+import com.jetbrains.edu.learning.courseFormat.*;
 import com.jetbrains.edu.learning.ui.StudyToolWindow;
 import org.jetbrains.annotations.NotNull;
 
-import java.util.*;
+import java.io.IOException;
+import java.util.Map;
 
 public class StudySubtaskUtils {
+  private static final Logger LOG = Logger.getInstance(StudySubtaskUtils.class);
+
   private StudySubtaskUtils() {
   }
 
@@ -58,11 +62,38 @@ public class StudySubtaskUtils {
         taskFile.setHighlightErrors(false);
       }
     }
+    transformTestFile(project, toSubtaskIndex, taskDir);
     task.setActiveSubtaskIndex(toSubtaskIndex);
-    update(project, task, taskDir);
+    updateUI(project, task, taskDir);
+  }
+
+  private static void transformTestFile(@NotNull Project project, int toSubtaskIndex, VirtualFile taskDir) {
+    Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null) {
+      return;
+    }
+    StudyLanguageManager manager = StudyUtils.getLanguageManager(course);
+    if (manager == null) {
+      return;
+    }
+    String defaultTestFileName = manager.getTestFileName();
+    String nameWithoutExtension = FileUtil.getNameWithoutExtension(defaultTestFileName);
+    String extension = FileUtilRt.getExtension(defaultTestFileName);
+    String subtaskTestFileName = nameWithoutExtension + EduNames.SUBTASK_MARKER + toSubtaskIndex;
+    VirtualFile subtaskTestFile = taskDir.findChild(subtaskTestFileName + ".txt");
+    if (subtaskTestFile != null) {
+      ApplicationManager.getApplication().runWriteAction(() -> {
+        try {
+          subtaskTestFile.rename(project, subtaskTestFileName + "." + extension);
+        }
+        catch (IOException e) {
+          LOG.error(e);
+        }
+      });
+    }
   }
 
-  private static void update(@NotNull Project project, @NotNull Task task, VirtualFile taskDir) {
+  private static void updateUI(@NotNull Project project, @NotNull Task task, VirtualFile taskDir) {
     StudyCheckUtils.drawAllPlaceholders(project, task, taskDir);
     ProjectView.getInstance(project).refresh();
     StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project);