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

Commit f708683

Browse files
author
Aleksi Salmela
committed
Change all the login command tests into integration tests.
1 parent f0914e2 commit f708683

2 files changed

Lines changed: 56 additions & 33 deletions

File tree

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
@Command(name = "download", desc = "Download exercises for a specific course")
2525
public class DownloadExercisesCommand extends AbstractCommand {
2626

27+
private boolean showAll;
28+
2729
@Override
2830
public void getOptions(Options options) {
2931
options.addOption("a", "all", false,
@@ -36,12 +38,14 @@ public void getOptions(Options options) {
3638
@Override
3739
public void run(CommandLine args, Io io) {
3840
String[] stringArgs = args.getArgs();
39-
if (stringArgs.length == 0) {
41+
if (stringArgs.length == 0 || stringArgs.length > 1) {
4042
io.println("You must give course name as an argument.");
4143
io.println("Usage: tmc download COURSE");
4244
return;
4345
}
4446

47+
showAll = args.hasOption("a");
48+
4549
Application app = getApp();
4650
TmcCore core = app.getTmcCore();
4751
WorkDir workDir = getApp().getWorkDir();
@@ -54,12 +58,13 @@ public void run(CommandLine args, Io io) {
5458
return;
5559
}
5660

57-
Course course = TmcUtil.findCourse(core, stringArgs[0]);
61+
String courseName = stringArgs[0];
62+
Course course = TmcUtil.findCourse(core, courseName);
5863
if (course == null) {
5964
io.println("Course doesn't exist.");
6065
return;
6166
}
62-
List<Exercise> filtered = getFilteredExercises(course, args);
67+
List<Exercise> filtered = getFilteredExercises(course);
6368
// todo: If -c switch, use core.downloadCompletedExercises() to download user's old
6469
// submissions. Not yet implemented in tmc-core.
6570

@@ -71,24 +76,38 @@ public void run(CommandLine args, Io io) {
7176
if (exercises == null) {
7277
io.println("Failed to download exercises");
7378
return;
74-
} else if (exercises.isEmpty()) {
75-
io.println("Course doesn't have any exercises.");
79+
}
7680

77-
} else if (exercises.size() != filtered.size()) {
78-
io.println(Color.colorString("Some exercises could not be downloaded",
79-
Color.AnsiColor.ANSI_RED));
81+
if (course.getExercises().isEmpty()) {
82+
io.println("The '" + courseName + "' course doesn't have any exercises.");
83+
} else {
84+
io.println("The '" + courseName + "' course has " +
85+
course.getExercises().size() + " exercises");
86+
87+
int failedCount = (filtered.size() - exercises.size());
88+
if (failedCount > 0) {
89+
io.println(" from which " +
90+
exercises.size() + " exercises were succesfully downloaded");
91+
io.println(Color.colorString(" and of which " + failedCount + " failed.",
92+
Color.AnsiColor.ANSI_RED));
93+
//TODO we could print the names of the not downloaded exercises here
94+
} else {
95+
io.println(" from which " +
96+
exercises.size() + " exercises were downloaded.");
97+
}
98+
io.println("Use -a flag to download also your completed exercises.");
8099
}
81100

82101
Path configFile = app.getWorkDir().getWorkingDirectory()
83-
.resolve(stringArgs[0])
102+
.resolve(courseName)
84103
.resolve(CourseInfoIo.COURSE_CONFIG);
85104
CourseInfo info = app.createCourseInfo(course);
86105
info.setExercises(course.getExercises());
87106
CourseInfoIo.save(info, configFile);
88107
}
89108

90-
protected List<Exercise> getFilteredExercises(Course course, CommandLine args) {
91-
if (args.hasOption("a")) {
109+
private List<Exercise> getFilteredExercises(Course course) {
110+
if (showAll) {
92111
return course.getExercises();
93112
}
94113

src/test/java/fi/helsinki/cs/tmc/cli/command/DownloadExercisesCommandTest.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,23 @@ public void filtersCompletedExercisesByDefault() throws ParseException {
127127
completed1.setCompleted(true);
128128
completed2.setCompleted(true);
129129

130-
Course course = new Course("test-course");
130+
List<Exercise> filteredExercises = Arrays.asList(notCompleted);
131+
132+
Course course = new Course("course1");
131133
course.setExercises(Arrays.asList(completed1, notCompleted, completed2));
132134

133-
/*TODO somehow get rid of this parser stuff */
134-
GnuParser parser = new GnuParser();
135-
CommandLine args = parser.parse(new Options(), new String[]{});
135+
when(TmcUtil.findCourse(eq(mockCore), eq("course1"))).thenReturn(course);
136+
when(TmcUtil.downloadExercises(eq(mockCore), anyListOf(Exercise.class),
137+
any(ProgressObserver.class))).thenReturn(filteredExercises);
138+
139+
Settings settings = new Settings("server", "user", "password");
140+
settings.setTmcProjectDirectory(tempDir);
141+
app.setSettings(settings);
136142

137-
DownloadExercisesCommand dlCommand = new DownloadExercisesCommand();
138-
List<Exercise> filtered = dlCommand.getFilteredExercises(course, args);
143+
String[] args = {"download", "course1"};
144+
app.run(args);
139145

140-
assertTrue(filtered.size() == 1);
141-
assertEquals(notCompleted, filtered.get(0));
146+
io.assertContains("which 1 exercises were downloaded");
142147
}
143148

144149
@Test
@@ -151,23 +156,22 @@ public void getsAllExercisesWithAllSwitch() throws ParseException {
151156
completed1.setCompleted(true);
152157
completed2.setCompleted(true);
153158

154-
List<Exercise> exercises = new ArrayList<>();
155-
exercises.add(completed1);
156-
exercises.add(notCompleted);
157-
exercises.add(completed2);
158-
159-
Course course = new Course("test-course");
159+
List<Exercise> exercises = Arrays.asList(completed1, notCompleted,
160+
completed2);
161+
Course course = new Course("course1");
160162
course.setExercises(exercises);
161163

162-
/*TODO somehow get rid of this parser stuff */
163-
GnuParser parser = new GnuParser();
164-
Options options = new Options();
165-
options.addOption("a", "all", false, "");
166-
CommandLine args = parser.parse(options, new String[]{"-a"});
164+
when(TmcUtil.findCourse(eq(mockCore), eq("course1"))).thenReturn(course);
165+
when(TmcUtil.downloadExercises(eq(mockCore), anyListOf(Exercise.class),
166+
any(ProgressObserver.class))).thenReturn(exercises);
167+
168+
Settings settings = new Settings("server", "user", "password");
169+
settings.setTmcProjectDirectory(tempDir);
170+
app.setSettings(settings);
167171

168-
DownloadExercisesCommand dlCommand = new DownloadExercisesCommand();
169-
List<Exercise> filtered = dlCommand.getFilteredExercises(course, args);
172+
String[] args = {"download", "-a", "course1"};
173+
app.run(args);
170174

171-
assertTrue(filtered.size() == 3);
175+
io.assertContains("which 3 exercises were downloaded");
172176
}
173177
}

0 commit comments

Comments
 (0)