Skip to content

Commit 212e0c6

Browse files
committed
tests for cli api
1 parent 575e239 commit 212e0c6

File tree

3 files changed

+259
-40
lines changed

3 files changed

+259
-40
lines changed

tmc-client/src/tmc_client/api.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,8 @@ impl TmcClient {
142142
// todo use same code here and in json_res
143143
if !response.status().is_success() {
144144
let status = response.status();
145-
<<<<<<< HEAD
146145
if let Ok(err) = response.json::<ErrorResponse>() {
147146
// failed and got an error json
148-
=======
149-
let err: Result<ErrorResponse, _> = response.json();
150-
151-
if let Ok(err) = err {
152-
>>>>>>> temp
153147
let error = match (err.error, err.errors) {
154148
(Some(err), Some(errs)) => format!("{}, {}", err, errs.join(",")),
155149
(Some(err), None) => err,
@@ -163,18 +157,11 @@ impl TmcClient {
163157
obsolete_client: err.obsolete_client,
164158
})
165159
} else {
166-
<<<<<<< HEAD
167160
// failed and failed to parse error json, return generic HTTP error
168161
Err(ClientError::HttpError {
169162
url,
170163
status,
171164
error: status.to_string(),
172-
=======
173-
Err(ClientError::HttpError {
174-
url,
175-
status,
176-
error: "HTTP error".to_string(),
177-
>>>>>>> temp
178165
obsolete_client: false,
179166
})
180167
}

tmc-langs-cli/src/main.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,19 @@ fn run_app(matches: ArgMatches, pretty: bool, warnings: &mut Vec<anyhow::Error>)
344344
let exercise_path = Path::new(exercise_path);
345345

346346
let output_path = matches.value_of("output-path");
347+
347348
let output_path = output_path.map(Path::new);
348349

349-
file_util::lock!(exercise_path);
350+
let exercises = {
351+
file_util::lock!(exercise_path);
350352

351-
let exercises =
352353
task_executor::find_exercise_directories(exercise_path).with_context(|| {
353354
format!(
354355
"Failed to find exercise directories in {}",
355356
exercise_path.display(),
356357
)
357-
})?;
358+
})?
359+
};
358360

359361
if let Some(output_path) = output_path {
360362
file_util::lock!(output_path);
@@ -478,16 +480,16 @@ fn run_app(matches: ArgMatches, pretty: bool, warnings: &mut Vec<anyhow::Error>)
478480
print_output(&output, pretty, &warnings)?
479481
}
480482
("prepare-submission", Some(matches)) => {
483+
let clone_path = matches.value_of("clone-path").unwrap();
484+
let clone_path = Path::new(clone_path);
485+
481486
let output_format = match matches.value_of("output-format") {
482487
Some("tar") => OutputFormat::Tar,
483488
Some("zip") => OutputFormat::Zip,
484489
Some("zstd") => OutputFormat::TarZstd,
485490
_ => unreachable!("validation error"),
486491
};
487492

488-
let clone_path = matches.value_of("clone-path").unwrap();
489-
let clone_path = Path::new(clone_path);
490-
491493
let output_path = matches.value_of("output-path").unwrap();
492494
let output_path = Path::new(output_path);
493495

@@ -552,24 +554,26 @@ fn run_app(matches: ArgMatches, pretty: bool, warnings: &mut Vec<anyhow::Error>)
552554
print_output(&output, pretty, &warnings)?
553555
}
554556
("refresh-course", Some(matches)) => {
555-
let course_name = matches.value_of("course-name").unwrap();
556557
let cache_path = matches.value_of("cache-path").unwrap();
558+
let cache_root = matches.value_of("cache-root").unwrap();
559+
let chgrp_uid = matches.value_of("chgrp-uid");
560+
let chmod_bits = matches.value_of("chmod-bits");
557561
let clone_path = matches.value_of("clone-path").unwrap();
558-
let stub_path = matches.value_of("stub-path").unwrap();
559-
let stub_zip_path = matches.value_of("stub-zip-path").unwrap();
560-
let solution_path = matches.value_of("solution-path").unwrap();
561-
let solution_zip_path = matches.value_of("solution-zip-path").unwrap();
562+
let course_name = matches.value_of("course-name").unwrap();
563+
562564
let exercise_args = matches.values_of("exercise");
563-
let source_backend = matches.value_of("source-backend").unwrap();
564-
let source_url = matches.value_of("source-url").unwrap();
565565
let git_branch = matches.value_of("git-branch").unwrap();
566-
let no_directory_changes = matches.is_present("no-directory-changes");
567566
let no_background_operations = matches.is_present("no-background-operations");
568-
let chmod_bits = matches.value_of("chmod-bits");
569-
let chgrp_uid = matches.value_of("chgrp-uid");
570-
let cache_root = matches.value_of("cache-root").unwrap();
567+
let no_directory_changes = matches.is_present("no-directory-changes");
571568
let rails_root = matches.value_of("rails-root").unwrap();
572569

570+
let solution_path = matches.value_of("solution-path").unwrap();
571+
let solution_zip_path = matches.value_of("solution-zip-path").unwrap();
572+
let source_backend = matches.value_of("source-backend").unwrap();
573+
let source_url = matches.value_of("source-url").unwrap();
574+
let stub_path = matches.value_of("stub-path").unwrap();
575+
let stub_zip_path = matches.value_of("stub-zip-path").unwrap();
576+
573577
let mut exercises = vec![];
574578
if let Some(mut exercise_args) = exercise_args {
575579
while let Some(exercise_name) = exercise_args.next() {
Lines changed: 238 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,256 @@
11
use std::env;
22
use std::process::{Command, Output};
3-
use tempfile::tempdir;
3+
use tempfile::{tempdir, NamedTempFile};
44

55
pub fn run_cmd(args: &[&str]) -> Output {
66
let path = env::current_exe().unwrap().parent().unwrap().to_path_buf();
77
let path = path.parent().unwrap().join("tmc-langs-cli");
88
Command::new(path).args(args).output().unwrap()
99
}
1010

11-
fn test_dir(dir: &str) -> String {
12-
format!("tests/data/{}", dir)
11+
pub fn run_assert_success(args: &[&str]) {
12+
let out = run_cmd(args);
13+
let stdout = String::from_utf8(out.stdout).unwrap();
14+
let stderr = String::from_utf8(out.stderr).unwrap();
15+
println!("stdout {}", stdout);
16+
println!("stderr {}", stderr);
17+
// if there's an issue with the argument parsing, stdout will be empty and not parse as json
18+
let last_line = stdout.lines().last().unwrap();
19+
let _json: serde_json::Value = serde_json::from_str(&last_line).unwrap();
20+
assert!(stderr.is_empty());
21+
}
22+
23+
#[test]
24+
fn sanity() {
25+
let out = run_cmd(&["non-existent-command"]);
26+
assert!(out.stdout.is_empty());
27+
assert!(!out.stderr.is_empty());
28+
}
29+
30+
#[test]
31+
fn checkstyle() {
32+
let temp = NamedTempFile::new().unwrap();
33+
let path = temp.path().to_str().unwrap();
34+
run_assert_success(&[
35+
"checkstyle",
36+
"--exercise-path",
37+
path,
38+
"--locale",
39+
"fi",
40+
"--output-path",
41+
path,
42+
]);
43+
}
44+
45+
#[test]
46+
fn clean() {
47+
let temp = NamedTempFile::new().unwrap();
48+
run_assert_success(&["clean", "--exercise-path", temp.path().to_str().unwrap()]);
1349
}
1450

1551
#[test]
1652
fn compress_project() {
17-
let temp = tempdir().unwrap();
18-
let out = run_cmd(&[
53+
let temp = NamedTempFile::new().unwrap();
54+
let path = temp.path().to_str().unwrap();
55+
run_assert_success(&[
1956
"compress-project",
2057
"--exercise-path",
21-
&test_dir("project"),
58+
path,
59+
"--output-path",
60+
path,
61+
]);
62+
}
63+
64+
// core is in a separate file
65+
66+
#[test]
67+
fn disk_space() {
68+
let temp = NamedTempFile::new().unwrap();
69+
let path = temp.path().to_str().unwrap();
70+
run_assert_success(&["disk-space", "--path", path]);
71+
}
72+
73+
#[test]
74+
fn extract_project() {
75+
let temp = NamedTempFile::new().unwrap();
76+
let path = temp.path().to_str().unwrap();
77+
run_assert_success(&[
78+
"extract-project",
79+
"--archive-path",
80+
path,
81+
"--output-path",
82+
path,
83+
]);
84+
}
85+
86+
#[test]
87+
fn fast_available_points() {
88+
let temp = NamedTempFile::new().unwrap();
89+
let path = temp.path().to_str().unwrap();
90+
run_assert_success(&["fast-available-points", "--exercise-path", path]);
91+
}
92+
93+
#[test]
94+
fn find_exercises() {
95+
let temp = tempdir().unwrap();
96+
let path = temp.path().to_str().unwrap();
97+
run_assert_success(&[
98+
"find-exercises",
99+
"--exercise-path",
100+
path,
101+
"--output-path",
102+
path,
103+
]);
104+
}
105+
106+
#[test]
107+
fn get_exercise_packaging_configuration() {
108+
let temp = NamedTempFile::new().unwrap();
109+
let path = temp.path().to_str().unwrap();
110+
run_assert_success(&[
111+
"get-exercise-packaging-configuration",
112+
"--exercise-path",
113+
path,
114+
"--output-path",
115+
path,
116+
]);
117+
}
118+
119+
#[test]
120+
fn list_local_course_exercises() {
121+
run_assert_success(&[
122+
"list-local-course-exercises",
123+
"--client-name",
124+
"client",
125+
"--course-slug",
126+
"slug",
127+
]);
128+
}
129+
130+
#[test]
131+
fn prepare_solutions() {
132+
let temp = NamedTempFile::new().unwrap();
133+
let path = temp.path().to_str().unwrap();
134+
run_assert_success(&[
135+
"prepare-solutions",
136+
"--exercise-path",
137+
path,
138+
"--output-path",
139+
path,
140+
]);
141+
}
142+
143+
#[test]
144+
fn prepare_stubs() {
145+
let temp = NamedTempFile::new().unwrap();
146+
let path = temp.path().to_str().unwrap();
147+
run_assert_success(&[
148+
"prepare-stubs",
149+
"--exercise-path",
150+
path,
151+
"--output-path",
152+
path,
153+
]);
154+
}
155+
156+
#[test]
157+
fn prepare_submission() {
158+
let temp = NamedTempFile::new().unwrap();
159+
let path = temp.path().to_str().unwrap();
160+
run_assert_success(&[
161+
"prepare-submission",
162+
"--clone-path",
163+
path,
164+
"--output-format",
165+
"tar",
166+
"--output-path",
167+
path,
168+
"--stub-zip-path",
169+
path,
170+
"--submission-path",
171+
path,
172+
"--tmc-param",
173+
"a=b",
174+
"--tmc-param",
175+
"c=d",
176+
]);
177+
}
178+
179+
#[test]
180+
fn refresh_course() {
181+
let temp = NamedTempFile::new().unwrap();
182+
let path = temp.path().to_str().unwrap();
183+
run_assert_success(&[
184+
"refresh-course",
185+
"--cache-path",
186+
path,
187+
"--cache-root",
188+
path,
189+
"--chgrp-uid",
190+
"1234",
191+
"--chmod-bits",
192+
"1234",
193+
"--clone-path",
194+
path,
195+
"--course-name",
196+
"name",
197+
"--exercise",
198+
"name",
199+
path,
200+
"10,11,12",
201+
"--exercise",
202+
"second",
203+
path,
204+
"20,21,22",
205+
"--git-branch",
206+
"main",
207+
"--no-background-operations",
208+
"--no-directory-changes",
209+
"--rails-root",
210+
path,
211+
"--solution-path",
212+
path,
213+
"--solution-zip-path",
214+
path,
215+
"--source-backend",
216+
"git",
217+
"--source-url",
218+
"example.com",
219+
"--stub-path",
220+
path,
221+
"--stub-zip-path",
222+
path,
223+
]);
224+
}
225+
226+
#[test]
227+
fn run_tests() {
228+
let temp = NamedTempFile::new().unwrap();
229+
let path = temp.path().to_str().unwrap();
230+
run_assert_success(&[
231+
"run-tests",
232+
"--checkstyle-output-path",
233+
path,
234+
"--exercise-path",
235+
path,
236+
"--locale",
237+
"fi",
238+
"--output-path",
239+
path,
240+
]);
241+
}
242+
243+
// settings in a separate file
244+
245+
#[test]
246+
fn scan_exercise() {
247+
let temp = NamedTempFile::new().unwrap();
248+
let path = temp.path().to_str().unwrap();
249+
run_assert_success(&[
250+
"scan-exercise",
251+
"--exercise-path",
252+
path,
22253
"--output-path",
23-
temp.path().join("zip.zip").to_str().unwrap(),
254+
path,
24255
]);
25-
log::debug!("out:\n{}", String::from_utf8(out.stdout).unwrap());
26-
log::debug!("err:\n{}", String::from_utf8(out.stderr).unwrap());
27-
// TODO
28256
}

0 commit comments

Comments
 (0)