Skip to content

Commit

Permalink
Merge pull request #276 from tmc-cli/new-update-command-juha
Browse files Browse the repository at this point in the history
Update to core 0.5.2 & new update command
  • Loading branch information
Salmela authored Jun 16, 2016
2 parents 3e73d34 + 1da0c82 commit f0f4a4a
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>fi.helsinki.cs.tmc</groupId>
<artifactId>core</artifactId>
<version>0.5.1-SNAPSHOT</version>
<version>0.5.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
Expand Down
44 changes: 32 additions & 12 deletions src/main/java/fi/helsinki/cs/tmc/cli/command/UpdateCommand.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package fi.helsinki.cs.tmc.cli.command;

import fi.helsinki.cs.tmc.cli.Application;
import fi.helsinki.cs.tmc.cli.command.core.AbstractCommand;
import fi.helsinki.cs.tmc.cli.command.core.Command;
import fi.helsinki.cs.tmc.cli.io.Io;
import fi.helsinki.cs.tmc.cli.io.TmcCliProgressObserver;
import fi.helsinki.cs.tmc.cli.tmcstuff.CourseInfo;
import fi.helsinki.cs.tmc.cli.tmcstuff.CourseInfoIo;
import fi.helsinki.cs.tmc.cli.tmcstuff.TmcUtil;
import fi.helsinki.cs.tmc.cli.tmcstuff.WorkDir;
import fi.helsinki.cs.tmc.core.TmcCore;
import fi.helsinki.cs.tmc.core.commands.GetUpdatableExercises;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.core.domain.Exercise;

Expand All @@ -25,6 +24,7 @@ public class UpdateCommand extends AbstractCommand {
public void getOptions(Options options) {
}

// flag --all
@Override
public void run(CommandLine args, Io io) {
String[] stringArgs = args.getArgs();
Expand All @@ -50,25 +50,45 @@ public void run(CommandLine args, Io io) {

CourseInfo info = CourseInfoIo.load(workDir.getConfigFile());
Course course = info.getCourse();
List<Exercise> exercises;

try {
exercises = core.getExerciseUpdates(new TmcCliProgressObserver(io), course).call();
} catch (Exception e) {
System.out.println(e);
GetUpdatableExercises.UpdateResult result;
result = TmcUtil.getUpdatableExercises(core, course);
if (result == null) {
return;
}

if (exercises.isEmpty()) {
boolean hasNewExercises = !result.getNewExercises().isEmpty();
boolean hasUpdatedExercises = !result.getUpdatedExercises().isEmpty();

if (!hasNewExercises && !hasUpdatedExercises) {
io.println("All exercises are up-to-date");
return;
}

io.println("Updates available for:");
for (Exercise exercise : exercises) {
io.println(exercise.getName());
if (hasNewExercises) {
io.println("New exercises:");
}
for (Exercise exercise : result.getNewExercises()) {
io.println(" " + exercise.getName());
}

if (hasUpdatedExercises) {
io.println("Modified exercises:");
}
for (Exercise exercise : result.getUpdatedExercises()) {
io.println(" " + exercise.getName());
}

System.out.println(TmcUtil.downloadExercises(core, exercises));
io.println("");

List<Exercise> exercises = result.getNewExercises();
exercises.addAll(result.getUpdatedExercises());
//exercises.addAll(Deleted exercises ???); todo

TmcUtil.downloadExercises(core, exercises);

info.setExercises(TmcUtil.findCourse(core, course.getName()).getExercises());

CourseInfoIo.save(info, workDir.getConfigFile());
}
}
13 changes: 13 additions & 0 deletions src/main/java/fi/helsinki/cs/tmc/cli/tmcstuff/TmcUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fi.helsinki.cs.tmc.cli.io.TmcCliProgressObserver;
import fi.helsinki.cs.tmc.core.TmcCore;
import fi.helsinki.cs.tmc.core.commands.GetUpdatableExercises;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.core.domain.Exercise;
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
Expand All @@ -15,6 +16,7 @@
import java.util.concurrent.Callable;

public class TmcUtil {

private static final Logger logger = LoggerFactory.getLogger(TmcUtil.class);

public static List<Course> listCourses(TmcCore core) {
Expand Down Expand Up @@ -95,4 +97,15 @@ public static SubmissionResult submitExercise(TmcCore core, Course course, Strin
return null;
}
}

public static GetUpdatableExercises.UpdateResult getUpdatableExercises(
TmcCore core, Course course) {
try {
return core.getExerciseUpdates(ProgressObserver.NULL_OBSERVER, course)
.call();
} catch (Exception e) {
System.out.println(e);
return null;
}
}
}
126 changes: 103 additions & 23 deletions src/test/java/fi/helsinki/cs/tmc/cli/command/UpdateCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
import fi.helsinki.cs.tmc.cli.io.TestIo;
import fi.helsinki.cs.tmc.cli.tmcstuff.WorkDir;
import fi.helsinki.cs.tmc.core.TmcCore;

import fi.helsinki.cs.tmc.core.commands.GetUpdatableExercises;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.core.domain.Exercise;
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -33,6 +37,7 @@ public class UpdateCommandTest {
private static final String COURSE_NAME = "2016-aalto-c";

static Path pathToDummyCourse;
static Path tempDir;

private Application app;
private TestIo io;
Expand All @@ -44,14 +49,25 @@ public static void setUpClass() throws Exception {
pathToDummyCourse = Paths.get(SubmitCommandTest.class.getClassLoader()
.getResource("dummy-courses/" + COURSE_NAME).toURI());
assertNotNull(pathToDummyCourse);

tempDir = Paths.get(System.getProperty("java.io.tmpdir"))
.resolve("updateCommandTests");
assertNotNull(tempDir);
}

@Before
public void setUp() {
public void setUp() throws IOException {
io = new TestIo();
app = new Application(io);
mockCore = mock(TmcCore.class);
app.setTmcCore(mockCore);

FileUtils.copyDirectory(pathToDummyCourse.toFile(), tempDir.toFile());
}

@After
public void tearDown() throws IOException {
FileUtils.deleteDirectory(tempDir.toFile());
}

@Test
Expand Down Expand Up @@ -82,18 +98,36 @@ public void printsAnErrorMessageIfUsedOutsideCourseDirectory() {

@Test
public void worksRightIfAllExercisesAreUpToDate() {
Callable<List<Exercise>> callableExercise = new Callable<List<Exercise>>() {
@Override
public List<Exercise> call() throws Exception {
ArrayList<Exercise> tmp = new ArrayList<>();
return tmp;
}
};
// Callable<List<Exercise>> callableExercise = new Callable<List<Exercise>>() {
// @Override
// public List<Exercise> call() throws Exception {
// ArrayList<Exercise> tmp = new ArrayList<>();
// return tmp;
// }
// };
//
// when(mockCore.getExerciseUpdates(any(ProgressObserver.class), any(Course.class)))
// .thenReturn(callableExercise);

Callable<GetUpdatableExercises.UpdateResult> callableResult
= new Callable<GetUpdatableExercises.UpdateResult>() {

@Override
public GetUpdatableExercises.UpdateResult call() throws Exception {
GetUpdatableExercises.UpdateResult result = mock(
GetUpdatableExercises.UpdateResult.class);
when(result.getNewExercises()).thenReturn(
new ArrayList<Exercise>());
when(result.getUpdatedExercises()).thenReturn(
new ArrayList<Exercise>());
return result;
}
};

when(mockCore.getExerciseUpdates(any(ProgressObserver.class), any(Course.class)))
.thenReturn(callableExercise);
.thenReturn(callableResult);

workDir = new WorkDir(pathToDummyCourse);
workDir = new WorkDir(tempDir);
app.setWorkdir(workDir);

String[] args = {"update"};
Expand All @@ -102,27 +136,73 @@ public List<Exercise> call() throws Exception {
}

@Test
@Ignore
public void worksRightIfUpdatesAvailable() {
Callable<List<Exercise>> callableExercise = new Callable<List<Exercise>>() {
// Callable<List<Exercise>> callableExercise = new Callable<List<Exercise>>() {
// @Override
// public List<Exercise> call() throws Exception {
// ArrayList<Exercise> tmp = new ArrayList<>();
// tmp.add(new Exercise("exercise1"));
// tmp.add(new Exercise("exercise2"));
// return tmp;
// }
// };
//
// when(mockCore.getExerciseUpdates(any(ProgressObserver.class), any(Course.class)))
// .thenReturn(callableExercise);

final List<Exercise> unlockedList = new ArrayList<>();
unlockedList.add(new Exercise("unlocked_exercise"));

final List<Exercise> changedList = new ArrayList<>();
unlockedList.add(new Exercise("Module_1-02_intro"));

Callable<GetUpdatableExercises.UpdateResult> callableResult
= new Callable<GetUpdatableExercises.UpdateResult>() {
@Override
public GetUpdatableExercises.UpdateResult call() throws Exception {
GetUpdatableExercises.UpdateResult result = mock(
GetUpdatableExercises.UpdateResult.class);
when(result.getNewExercises()).thenReturn(unlockedList);
when(result.getUpdatedExercises()).thenReturn(changedList);
return result;
}
};
when(mockCore.getExerciseUpdates(any(ProgressObserver.class), any(Course.class)))
.thenReturn(callableResult);

Callable<List<Course>> callableCourseList = new Callable<List<Course>>() {
@Override
public List<Exercise> call() throws Exception {
ArrayList<Exercise> tmp = new ArrayList<>();
tmp.add(new Exercise("exercise1"));
tmp.add(new Exercise("exercise2"));
return tmp;
public List<Course> call() throws Exception {
return new ArrayList<>();
}
};
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableCourseList);

when(mockCore.getExerciseUpdates(any(ProgressObserver.class), any(Course.class)))
.thenReturn(callableExercise);
Callable<Course> callableCourse = new Callable<Course>() {
@Override
public Course call() throws Exception {
Course course = new Course("2016-aalto-c");
List<Exercise> exercises = new ArrayList<>();
exercises.add(new Exercise("Module_1-02_intro"));
exercises.add(new Exercise("unlocked_course"));
course.setExercises(exercises);
return course;
}
};

workDir = new WorkDir(pathToDummyCourse);
when(mockCore.getCourseDetails(any(ProgressObserver.class), any(Course.class)))
.thenReturn(callableCourse);

workDir = new WorkDir(tempDir);
app.setWorkdir(workDir);

String[] args = {"update"};
app.run(args);
assertTrue(io.getPrint().contains("Updates available for:"));
assertTrue(io.getPrint().contains("exercise1"));
assertTrue(io.getPrint().contains("exercise2"));
assertTrue(io.getPrint().contains("New exercises:"));
assertTrue(io.getPrint().contains("unlocked_exercise"));

//assertTrue(io.getPrint().contains("Modified exercises:"));
//assertTrue(io.getPrint().contains("Module_1-02_intro"));
}
}

0 comments on commit f0f4a4a

Please sign in to comment.