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

Commit 45eb8bf

Browse files
authored
Merge branch 'master' into update-dont-dl-completed-juha
2 parents 9495932 + 21c020b commit 45eb8bf

2 files changed

Lines changed: 84 additions & 52 deletions

File tree

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

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,69 +26,94 @@
2626
public class CourseInfoCommand extends AbstractCommand {
2727
private Course course;
2828
private Exercise exercise;
29+
private CourseInfo info;
30+
private WorkDir workDir;
2931
private Io io;
3032

3133
@Override
3234
public void getOptions(Options options) {
33-
options.addOption("a", false, "Show all info for a specific course");
35+
options.addOption("a", false, "Show all information for a specific course");
36+
options.addOption("i", false, "Get the information from the server");
3437
}
3538

3639
@Override
3740
public void run(CommandLine args, Io io) {
3841
this.io = io;
39-
TmcCore core = getApp().getTmcCore();
40-
if (core == null) {
42+
workDir = getApp().getWorkDir();
43+
44+
if (!args.hasOption("i")) {
45+
printLocalCourseOrExercise(args);
4146
return;
4247
}
4348

44-
WorkDir workDir = getApp().getWorkDir();
4549
String[] stringArgs = args.getArgs();
46-
if (stringArgs.length == 0 && workDir.getConfigFile() == null) {
47-
io.println("You must give the course name as a parameter.");
50+
if (stringArgs.length == 0) {
51+
io.println("You must give a course as a parameter.");
4852
return;
4953
}
5054

51-
// if in course directory
55+
TmcCore core = getApp().getTmcCore();
56+
if (core == null) {
57+
return;
58+
}
59+
course = TmcUtil.findCourse(core, stringArgs[0]);
60+
if (course == null) {
61+
io.println("The course " + stringArgs[0] + " doesn't exist on this server.");
62+
return;
63+
}
64+
printCourse(args.hasOption("a"));
65+
}
66+
67+
private void printLocalCourseOrExercise(CommandLine args) {
5268
if (workDir.getConfigFile() != null) {
53-
CourseInfo info = CourseInfoIo.load(workDir.getConfigFile());
69+
info = CourseInfoIo.load(workDir.getConfigFile());
5470
course = info.getCourse();
71+
printCourseOrExercise(args);
72+
} else {
73+
this.io.println("You have to be in a course directory"
74+
+ " or use the -i option with the course name "
75+
+ "to get the information from the server.");
76+
}
77+
}
5578

56-
// if in exercise directory and no parameters given, print info for that exercise.
57-
// else if exercise or course name given as a parameter, check which one it is and print info for that
58-
if (workDir.getExerciseNames().size() == 1 && stringArgs.length == 0) {
59-
String currentExercise = workDir.getExerciseNames().get(0);
60-
exercise = info.getExercise(currentExercise);
61-
printOneExercise(args.hasOption("a"));
62-
return;
63-
64-
} else if (stringArgs.length != 0) {
65-
if (info.getExercise(stringArgs[0]) != null) {
66-
exercise = info.getExercise(stringArgs[0]);
67-
printOneExercise(args.hasOption("a"));
68-
return;
69-
70-
} else {
71-
course = TmcUtil.findCourse(core, stringArgs[0]);
72-
if (course != null) {
73-
printCourse(args.hasOption("a"));
74-
} else {
75-
io.println("No such course or exercise.");
76-
}
77-
return;
78-
}
79-
}
80-
printCourse(args.hasOption("a"));
79+
private void printCourseOrExercise(CommandLine args) {
80+
String[] stringArgs = args.getArgs();
81+
82+
// if in exercise directory and no parameters given, print info for that exercise.
83+
if (workDir.getExerciseNames().size() == 1 && stringArgs.length == 0) {
84+
String currentExercise = workDir.getExerciseNames().get(0);
85+
exercise = info.getExercise(currentExercise);
86+
printOneExercise(args.hasOption("a"));
8187
return;
8288
}
8389

84-
course = TmcUtil.findCourse(core, stringArgs[0]);
85-
if (course == null) {
86-
io.println("The course " + stringArgs[0] + " doesn't exist on this server.");
90+
if (stringArgs.length != 0) {
91+
printCourseOrExerciseFromParameters(args);
8792
return;
8893
}
8994
printCourse(args.hasOption("a"));
9095
}
9196

97+
private void printCourseOrExerciseFromParameters(CommandLine args) {
98+
String[] stringArgs = args.getArgs();
99+
// if parameter is given, check if it is an exercise or a course. If neither, print an error message.
100+
if (info.getExercise(stringArgs[0]) != null) {
101+
exercise = info.getExercise(stringArgs[0]);
102+
printOneExercise(args.hasOption("a"));
103+
return;
104+
105+
}
106+
course = info.getCourse();
107+
if (course != null && course.getName().equals(stringArgs[0])) {
108+
printCourse(args.hasOption("a"));
109+
} else {
110+
this.io.println("Wrong course directory."
111+
+ " Navigate to the correct course directory or"
112+
+ " use the -i option with the course name"
113+
+ " to get the information from the server.");
114+
}
115+
}
116+
92117
private void printCourse(boolean showAll) {
93118
printCourseShort();
94119
if (showAll) {

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,57 +87,64 @@ public void failIfCoreIsNull() {
8787
app = spy(app);
8888
doReturn(null).when(app).getTmcCore();
8989

90-
String[] args = {"info"};
90+
String[] args = {"info", "course", "-i"};
9191
app.run(args);
92-
assertFalse(io.out().contains("You must give"));
92+
assertFalse(io.out().contains("doesn't exist on this server."));
9393
}
9494

9595
@Test
9696
public void showMessageIfCourseIsNotGiven() {
9797
String[] args = {"info"};
9898
app.run(args);
99-
assertTrue(io.out().contains("You must give the course name as a parameter"));
99+
assertTrue(io.out().contains("You have to be in a course directory"));
100+
}
101+
102+
@Test
103+
public void showErrorMessageIfNoCourseGivenWithIOption() {
104+
String[] args = {"info", "-i"};
105+
app.run(args);
106+
assertTrue(io.out().contains("You must give a course as a parameter."));
100107
}
101108

102109
@Test
103-
public void showMessageIfCourseDoesNotExist() {
110+
public void showMessageIfCourseDoesNotExistOnTheServer() {
104111
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
105-
String[] args = {"info", "foo"};
112+
String[] args = {"info", "foo", "-i"};
106113
app.run(args);
107114
assertTrue(io.out().contains("The course foo doesn't exist on this server"));
108115
}
109116

110117
@Test
111-
public void printCourseWithoutOption() {
118+
public void printCourseWithOptionI() {
112119
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
113120
when(mockCore.getCourseDetails(any(ProgressObserver.class),
114121
any(Course.class))).thenReturn(callableCourse);
115122

116-
String[] args = {"info", "test-course123"};
123+
String[] args = {"info", "test-course123", "-i"};
117124
app.run(args);
118125
assertTrue(io.out().contains("Course name: test-course123"));
119126
assertFalse(io.out().contains("Statistics URLs"));
120127
}
121128

122129
@Test
123-
public void printCourseWithOption() {
130+
public void printCourseWithOptionsIAndA() {
124131
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
125132
when(mockCore.getCourseDetails(any(ProgressObserver.class),
126133
any(Course.class))).thenReturn(callableCourse);
127134

128-
String[] args = {"info", "test-course123", "-a"};
135+
String[] args = {"info", "test-course123", "-a", "-i"};
129136
app.run(args);
130137
assertTrue(io.out().contains("Statistics URLs"));
131138
}
132139

133140
@Test
134-
public void printCourseWithNoExercises() {
141+
public void printCourseWithNoExercisesFromTheServer() {
135142
course.setExercises(new ArrayList<Exercise>());
136143
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
137144
when(mockCore.getCourseDetails(any(ProgressObserver.class),
138145
any(Course.class))).thenReturn(callableCourse);
139146

140-
String[] args = {"info", "test-course123"};
147+
String[] args = {"info", "test-course123", "-i"};
141148
app.run(args);
142149
assertTrue(io.out().contains("Exercises: -"));
143150
}
@@ -180,7 +187,7 @@ public void printErrorMessageIfNotInCourseDirectoryAndCourseDoesntExist() {
180187
}
181188

182189
@Test
183-
public void printGivenCourseIfInCourseDirectoryAndGivenCourseName() {
190+
public void printGivenCourseFromTheServerIfInCourseDirectoryAndGivenCourseName() {
184191
workDir = new WorkDir(pathToDummyCourse);
185192
app.setWorkdir(workDir);
186193

@@ -189,23 +196,23 @@ public void printGivenCourseIfInCourseDirectoryAndGivenCourseName() {
189196
when(mockCore.getCourseDetails(any(ProgressObserver.class),
190197
any(Course.class))).thenReturn(callableCourse);
191198

192-
String[] args = {"info", "test-course123"};
199+
String[] args = {"info", "test-course123", "-i"};
193200
app.run(args);
194201
assertTrue(io.out().contains("test-course123"));
195202
}
196203

197204
@Test
198-
public void printsErrorIfInCourseDirectoryAndGivenCourseNameThatDoesntExist() {
205+
public void printsErrorIfInCourseDirectoryAndGivenCourseNameThatDoesntExistOnTheServer() {
199206
workDir = new WorkDir(pathToDummyCourse);
200207
app.setWorkdir(workDir);
201208

202209
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
203210
when(mockCore.getCourseDetails(any(ProgressObserver.class),
204211
any(Course.class))).thenReturn(callableCourse);
205212

206-
String[] args = {"info", "notacourse"};
213+
String[] args = {"info", "notacourse", "-i"};
207214
app.run(args);
208-
assertTrue(io.out().contains("exercise"));
215+
assertTrue(io.out().contains("doesn't exist on this server."));
209216
}
210217

211218
@Test

0 commit comments

Comments
 (0)