|
1 | 1 | use std::env; |
2 | 2 | use std::process::{Command, Output}; |
3 | | -use tempfile::tempdir; |
| 3 | +use tempfile::{tempdir, NamedTempFile}; |
4 | 4 |
|
5 | 5 | pub fn run_cmd(args: &[&str]) -> Output { |
6 | 6 | let path = env::current_exe().unwrap().parent().unwrap().to_path_buf(); |
7 | 7 | let path = path.parent().unwrap().join("tmc-langs-cli"); |
8 | 8 | Command::new(path).args(args).output().unwrap() |
9 | 9 | } |
10 | 10 |
|
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()]); |
13 | 49 | } |
14 | 50 |
|
15 | 51 | #[test] |
16 | 52 | 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(&[ |
19 | 56 | "compress-project", |
20 | 57 | "--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, |
22 | 253 | "--output-path", |
23 | | - temp.path().join("zip.zip").to_str().unwrap(), |
| 254 | + path, |
24 | 255 | ]); |
25 | | - log::debug!("out:\n{}", String::from_utf8(out.stdout).unwrap()); |
26 | | - log::debug!("err:\n{}", String::from_utf8(out.stderr).unwrap()); |
27 | | - // TODO |
28 | 256 | } |
0 commit comments