Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit d4cd827

Browse files
authored
Merge branch 'master' into exerciseinfoupdating-matike
2 parents ee39124 + 20655f9 commit d4cd827

8 files changed

Lines changed: 239 additions & 71 deletions

File tree

src/main/java/fi/helsinki/cs/tmc/cli/command/DownloadExercisesCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void run(CommandLine args, Io io) {
7474
.resolve(stringArgs[0])
7575
.resolve(CourseInfoIo.COURSE_CONFIG);
7676
CourseInfo info = app.createCourseInfo(course);
77-
info.setExercises(exercises);
77+
info.setExercises(course.getExercises());
7878
CourseInfoIo.save(info, configFile);
7979
}
8080

src/main/java/fi/helsinki/cs/tmc/cli/command/SubmitCommand.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
import fi.helsinki.cs.tmc.cli.tmcstuff.WorkDir;
1515
import fi.helsinki.cs.tmc.core.TmcCore;
1616
import fi.helsinki.cs.tmc.core.domain.Course;
17+
import fi.helsinki.cs.tmc.core.domain.Exercise;
1718
import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult;
1819

1920
import org.apache.commons.cli.CommandLine;
2021
import org.apache.commons.cli.Options;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324

25+
import java.nio.file.Path;
2426
import java.util.List;
2527

2628
@Command(name = "submit", desc = "Submit exercises")
@@ -63,17 +65,13 @@ public void run(CommandLine args, Io io) {
6365

6466
List<String> exerciseNames = workDir.getExerciseNames();
6567
if (exerciseNames.isEmpty()) {
66-
io.println("You have to be in a course directory to"
67-
+ " submit");
68+
io.println("You have to be in a course directory to submit");
6869
return;
6970
}
7071

7172
CourseInfo info = CourseInfoIo.load(workDir.getConfigFile());
72-
String courseName = info.getCourseName();
73-
Course course = TmcUtil.findCourse(core, courseName);
74-
75-
if (course == null) {
76-
io.println("Could not fetch course info from server.");
73+
Course currentCourse = info.getCourse();
74+
if (currentCourse == null) {
7775
return;
7876
}
7977

@@ -85,10 +83,12 @@ public void run(CommandLine args, Io io) {
8583
Color.AnsiColor color1 = app.getColor("testresults-left");
8684
Color.AnsiColor color2 = app.getColor("testresults-right");
8785

88-
for (String exerciseName : exerciseNames) {
89-
io.println(Color.colorString("Submitting: " + exerciseName,
86+
List<Exercise> submitExercises = info.getExercises(exerciseNames);
87+
88+
for (Exercise exercise : submitExercises) {
89+
io.println(Color.colorString("Submitting: " + exercise.getName(),
9090
Color.AnsiColor.ANSI_YELLOW));
91-
SubmissionResult result = TmcUtil.submitExercise(core, course, exerciseName);
91+
SubmissionResult result = TmcUtil.submitExercise(core, exercise);
9292
if (result == null) {
9393
io.println("Submission failed.");
9494
} else {
@@ -103,10 +103,34 @@ public void run(CommandLine args, Io io) {
103103
io.println("Total tests passed: " + passed + "/" + total);
104104
io.println(TmcCliProgressObserver.getPassedTestsBar(passed, total, color1, color2));
105105
}
106-
checkForExerciseUpdates(core, course);
106+
107+
updateCourseJson(core, submitExercises, info, workDir.getConfigFile());
108+
checkForExerciseUpdates(core, currentCourse);
109+
}
110+
111+
protected void updateCourseJson(TmcCore core, List<Exercise> submittedExercises,
112+
CourseInfo courseInfo, Path courseInfoFile) {
113+
114+
Course updatedCourse = TmcUtil.findCourse(core, courseInfo.getCourseName());
115+
if (updatedCourse == null) {
116+
io.println("Failed to update .tmc.json file for course " + courseInfo.getCourseName());
117+
return;
118+
}
119+
120+
for (Exercise submitted : submittedExercises) {
121+
Exercise updatedEx = TmcUtil.findExercise(updatedCourse, submitted.getName());
122+
if (updatedEx == null) {
123+
// Does this reaaally ever happen?
124+
io.println("Failed to update .tmc.json file for exercise " + submitted.getName()
125+
+ ". The exercise doesn't exist in server anymore.");
126+
continue;
127+
}
128+
courseInfo.replaceOldExercise(updatedEx);
129+
}
130+
CourseInfoIo.save(courseInfo, courseInfoFile);
107131
}
108132

109-
public void checkForExerciseUpdates(TmcCore core, Course course) {
133+
protected void checkForExerciseUpdates(TmcCore core, Course course) {
110134
ExerciseUpdater exerciseUpdater = new ExerciseUpdater(core, course);
111135
if (!exerciseUpdater.updatesAvailable()) {
112136
return;

src/main/java/fi/helsinki/cs/tmc/cli/io/ResultPrinter.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package fi.helsinki.cs.tmc.cli.io;
22

3-
import fi.helsinki.cs.tmc.cli.Application;
43
import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult;
54
import fi.helsinki.cs.tmc.langs.domain.RunResult;
5+
import fi.helsinki.cs.tmc.langs.domain.SpecialLogs;
66
import fi.helsinki.cs.tmc.langs.domain.TestResult;
77

88
import java.util.List;
@@ -56,9 +56,27 @@ public void printSubmissionResult(SubmissionResult result, Boolean printProgress
5656

5757
printTestResults(result.getTestCases());
5858

59-
if (result.getStatus() == SubmissionResult.Status.ERROR) {
60-
io.println("");
61-
io.println(result.getError());
59+
switch (result.getStatus()) {
60+
case ERROR:
61+
io.println("");
62+
io.println(result.getError());
63+
break;
64+
case FAIL:
65+
String valgrind = result.getValgrind();
66+
if (valgrind != null && !valgrind.isEmpty()) {
67+
io.println(Color.colorString("Valgrind error:",
68+
Color.AnsiColor.ANSI_RED));
69+
io.println(valgrind);
70+
return;
71+
}
72+
break;
73+
case PROCESSING:
74+
io.println("PROCESSING");
75+
break;
76+
case OK:
77+
//io.println("OK");
78+
break;
79+
default:
6280
}
6381

6482
if (printProgressBar && this.total > 0) {
@@ -85,7 +103,6 @@ public void printSubmissionResult(SubmissionResult result, Boolean printProgress
85103
if (msg != null) {
86104
io.println(msg);
87105
}
88-
return;
89106
}
90107

91108
public void printRunResult(RunResult result, Boolean printProgressBar,
@@ -111,6 +128,12 @@ public void printRunResult(RunResult result, Boolean printProgressBar,
111128
case COMPILE_FAILED:
112129
msg = ResultPrinter.COMPILE_ERROR_MESSAGE;
113130
break;
131+
case TESTRUN_INTERRUPTED:
132+
msg = "Testrun interrupted";
133+
break;
134+
case GENERIC_ERROR:
135+
msg = new String(result.logs.get(SpecialLogs.GENERIC_ERROR_MESSAGE));
136+
break;
114137
default:
115138
}
116139
if (msg != null) {

src/main/java/fi/helsinki/cs/tmc/cli/tmcstuff/CourseInfo.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ public List<Exercise> getExercises() {
5757
return this.course.getExercises();
5858
}
5959

60+
/**
61+
* Get a list of exercises by their names.
62+
*/
63+
public List<Exercise> getExercises(List<String> exerciseNames) {
64+
List<Exercise> exercises = new ArrayList<>();
65+
for (String exerciseName : exerciseNames) {
66+
Exercise exercise = getExercise(exerciseName);
67+
if (exercise != null) {
68+
exercises.add(exercise);
69+
}
70+
}
71+
return exercises;
72+
}
73+
6074
public List<String> getExerciseNames() {
6175
List<String> names = new ArrayList<>();
6276
for (Exercise ex : this.course.getExercises()) {
@@ -78,6 +92,37 @@ public void setExercises(List<Exercise> exercises) {
7892
this.course.setExercises(exercises);
7993
}
8094

95+
/**
96+
* Replaces an old identically named exercise with a new one. Adds if no old exercise is found.
97+
*/
98+
public void replaceOldExercise(Exercise newExercise) {
99+
List<Exercise> exercises = getExercises();
100+
String exerciseName = newExercise.getName();
101+
102+
Exercise oldExercise = null;
103+
for (Exercise exercise : exercises) {
104+
if (exercise.getName().equals(exerciseName)) {
105+
oldExercise = exercise;
106+
break;
107+
}
108+
}
109+
110+
if (oldExercise == null) {
111+
exercises.add(newExercise);
112+
} else {
113+
int index = exercises.indexOf(oldExercise);
114+
exercises.set(index, newExercise);
115+
}
116+
117+
setExercises(exercises);
118+
}
119+
120+
public void replaceOldExercises(List<Exercise> newExercises) {
121+
for (Exercise newExercise : newExercises) {
122+
replaceOldExercise(newExercise);
123+
}
124+
}
125+
81126
public void removeProperty(String prop) {
82127
this.properties.remove(prop);
83128
}

src/main/java/fi/helsinki/cs/tmc/cli/tmcstuff/TmcUtil.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package fi.helsinki.cs.tmc.cli.tmcstuff;
22

3-
import fi.helsinki.cs.tmc.cli.io.TmcCliProgressObserver;
43
import fi.helsinki.cs.tmc.core.TmcCore;
54
import fi.helsinki.cs.tmc.core.commands.GetUpdatableExercises.UpdateResult;
65
import fi.helsinki.cs.tmc.core.domain.Course;
@@ -65,7 +64,7 @@ public static Exercise findExercise(Course course, String name) {
6564
}
6665

6766
public static List<Exercise> downloadExercises(TmcCore core, List<Exercise> exercises,
68-
ProgressObserver progobs) {
67+
ProgressObserver progobs) {
6968
try {
7069
return core.downloadOrUpdateExercises(progobs, exercises).call();
7170
} catch (Exception e) {
@@ -75,7 +74,7 @@ public static List<Exercise> downloadExercises(TmcCore core, List<Exercise> exer
7574
}
7675

7776
public static List<Exercise> downloadAllExercises(TmcCore core, Course course,
78-
ProgressObserver progobs) {
77+
ProgressObserver progobs) {
7978
if (!course.isExercisesLoaded()) {
8079
course = getDetails(core, course);
8180
}
@@ -84,17 +83,15 @@ public static List<Exercise> downloadAllExercises(TmcCore core, Course course,
8483
}
8584

8685
public static SubmissionResult submitExercise(TmcCore core, Course course, String name) {
87-
// Exercise has directories separated with a - but core needs them to be separated with a / for submission
8886
Exercise exercise = TmcUtil.findExercise(course, name);
89-
//String fixedName = exercise.getName().replace("-", File.separator);
90-
//exercise.setName(fixedName);
87+
return submitExercise(core, exercise);
88+
}
89+
90+
public static SubmissionResult submitExercise(TmcCore core, Exercise exercise) {
9191
try {
92-
// TODO: replace null-observer with a real observer
93-
// once it shows up correctly in core/langs
94-
return core.submit(ProgressObserver.NULL_OBSERVER,
95-
exercise).call();
96-
} catch (Exception e) {
97-
logger.warn("Failed to submit the exercise", e);
92+
return core.submit(ProgressObserver.NULL_OBSERVER, exercise).call();
93+
} catch (Exception ex) {
94+
logger.warn("Failed to submit the exercise", ex);
9895
return null;
9996
}
10097
}

0 commit comments

Comments
 (0)