this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 397 lines 15 kB view raw
1use assert_cmd::prelude::*; 2use assert_fs::prelude::*; 3use predicates::prelude::*; 4use std::{ 5 fs::File, 6 process::{Command, Stdio}, 7}; 8 9mod common; 10use common::*; 11 12mod magic { 13 use super::*; 14 15 mod roundtrip { 16 use super::*; 17 18 /// Magic roundtrip using stdin 19 /// Compressing: input = stdin, output = test.txt.gz 20 /// Extracting: input = test.txt.gz, output = test.txt 21 /// 22 /// ``` bash 23 /// cat test.txt | cmprss test.txt.gz 24 /// cmprss gz --extract test.txt.gz out.txt 25 /// ``` 26 #[test] 27 fn stdin() -> Result<(), Box<dyn std::error::Error>> { 28 let file = create_test_file("test.txt", "garbage data for testing")?; 29 let working_dir = create_working_dir()?; 30 let archive = working_dir.child("test.txt.gz"); 31 archive.assert(predicate::path::missing()); 32 33 // Pipe file to stdin 34 let mut compress = Command::cargo_bin("cmprss")?; 35 compress 36 .current_dir(&working_dir) 37 .arg("--ignore-stdout") 38 .arg("test.txt.gz") 39 .stdin(Stdio::from(File::open(file.path())?)); 40 compress.assert().success(); 41 archive.assert(predicate::path::is_file()); 42 43 let mut extract = Command::cargo_bin("cmprss")?; 44 extract 45 .current_dir(&working_dir) 46 .arg("gz") 47 .arg("--ignore-pipes") 48 .arg("--extract") 49 .arg("test.txt.gz"); 50 extract.assert().success(); 51 52 // Assert the files are identical 53 assert_files_equal(file.path(), &working_dir.child("test.txt")); 54 55 Ok(()) 56 } 57 58 /// Magic roundtrip using files 59 /// Compressing: input = test.txt, output = test.txt.gz 60 /// Extracting: input = test.txt.gz, output = stdout 61 /// 62 /// ``` bash 63 /// cmprss test.txt test.txt.gz 64 /// cmprss test.txt.gz out.txt 65 /// ``` 66 #[test] 67 fn files() -> Result<(), Box<dyn std::error::Error>> { 68 let file = create_test_file("test.txt", "garbage data for testing")?; 69 let working_dir = create_working_dir()?; 70 let archive = working_dir.child("test.txt.gz"); 71 archive.assert(predicate::path::missing()); 72 73 // Compress file to an archive 74 let mut compress = Command::cargo_bin("cmprss")?; 75 compress 76 .current_dir(&working_dir) 77 .arg("--ignore-pipes") 78 .arg(file.path()) 79 .arg("test.txt.gz"); 80 compress.assert().success(); 81 archive.assert(predicate::path::is_file()); 82 83 // Extract file to given file 84 let mut extract = Command::cargo_bin("cmprss")?; 85 extract 86 .current_dir(&working_dir) 87 .arg("--ignore-pipes") 88 .arg("test.txt.gz") 89 .arg("out.txt"); 90 extract.assert().success(); 91 92 // Assert the files are identical 93 assert_files_equal(file.path(), &working_dir.child("out.txt")); 94 95 Ok(()) 96 } 97 98 /// Magic roundtrip using stdout decompression 99 /// Compressing: input = test.txt, output = test.txt.gz 100 /// Extracting: input = test.txt.gz, output = stdout 101 /// 102 /// ``` bash 103 /// cmprss test.txt test.txt.gz 104 /// cmprss test.txt.gz > out.txt 105 /// ``` 106 #[test] 107 fn stdout_decompression() -> Result<(), Box<dyn std::error::Error>> { 108 let file = create_test_file("test.txt", "garbage data for testing")?; 109 let working_dir = create_working_dir()?; 110 let archive = working_dir.child("test.txt.gz"); 111 archive.assert(predicate::path::missing()); 112 113 let out_file = working_dir.child("out.txt"); 114 115 // Compress file to an archive 116 let mut compress = Command::cargo_bin("cmprss")?; 117 compress 118 .current_dir(&working_dir) 119 .arg("--ignore-pipes") 120 .arg(file.path()) 121 .arg("test.txt.gz"); 122 compress.assert().success(); 123 archive.assert(predicate::path::is_file()); 124 125 // Extract file to stdout 126 let mut extract = Command::cargo_bin("cmprss")?; 127 extract 128 .current_dir(&working_dir) 129 .arg("--ignore-stdin") 130 .arg("test.txt.gz") 131 .stdout(Stdio::from(File::create(&out_file)?)); 132 extract.assert().success(); 133 134 // Assert the files are identical 135 assert_files_equal(file.path(), out_file.path()); 136 137 Ok(()) 138 } 139 140 /// Magic roundtrip using stdin compression 141 /// Compressing: input = stdin, output = test.txt.gz 142 /// Extracting: input = test.txt.gz, output = test.txt 143 /// 144 /// ``` bash 145 /// cat test.txt | cmprss test.txt.gz 146 /// cmprss test.txt.gz out.txt 147 /// ``` 148 #[test] 149 fn stdin_compression() -> Result<(), Box<dyn std::error::Error>> { 150 let file = create_test_file("test.txt", "garbage data for testing")?; 151 let working_dir = create_working_dir()?; 152 let archive = working_dir.child("test.txt.gz"); 153 archive.assert(predicate::path::missing()); 154 155 let out_file = working_dir.child("out.txt"); 156 157 // Compress stdin to an archive 158 let mut compress = Command::cargo_bin("cmprss")?; 159 compress 160 .current_dir(&working_dir) 161 .arg("--ignore-stdout") 162 .arg("test.txt.gz") 163 .stdin(Stdio::from(File::open(file.path())?)); 164 compress.assert().success(); 165 archive.assert(predicate::path::is_file()); 166 167 // Extract file to given file 168 let mut extract = Command::cargo_bin("cmprss")?; 169 extract 170 .current_dir(&working_dir) 171 .arg("--ignore-pipes") 172 .arg("test.txt.gz") 173 .arg(out_file.path()); 174 extract.assert().success(); 175 176 // Assert the files are identical 177 assert_files_equal(file.path(), out_file.path()); 178 179 Ok(()) 180 } 181 182 /// Magic roundtrip using default filenames 183 /// Compressing: input = test.txt, output = test.txt.gz 184 /// Extracting: input = test.txt.gz, output = <default> 185 /// 186 /// ``` bash 187 /// cmprss test.txt test.txt.gz 188 /// cmprss test.txt.gz 189 /// ``` 190 #[test] 191 fn default_filenames() -> Result<(), Box<dyn std::error::Error>> { 192 let file = create_test_file("test.txt", "garbage data for testing")?; 193 let working_dir = create_working_dir()?; 194 let archive = working_dir.child("test.txt.gz"); 195 archive.assert(predicate::path::missing()); 196 197 // Compress file to an archive 198 let mut compress = Command::cargo_bin("cmprss")?; 199 compress 200 .current_dir(&working_dir) 201 .arg("--ignore-pipes") 202 .arg(file.path()) 203 .arg("test.txt.gz"); 204 compress.assert().success(); 205 archive.assert(predicate::path::is_file()); 206 207 // Extract file to default filename 208 let mut extract = Command::cargo_bin("cmprss")?; 209 extract 210 .current_dir(&working_dir) 211 .arg("--ignore-pipes") 212 .arg("test.txt.gz"); 213 extract.assert().success(); 214 215 // Assert the files are identical 216 assert_files_equal(file.path(), &working_dir.child("test.txt")); 217 218 Ok(()) 219 } 220 221 /// Magic roundtrip using multiple files with tar 222 /// Compressing: input = test.txt/test2.txt, output = archive.tar 223 /// Extracting: input = archive.tar, output = <default> 224 /// 225 /// ``` bash 226 /// cmprss test.txt test2.txt archive.tar 227 /// cmprss archive.tar 228 /// ``` 229 #[test] 230 fn multiple_files_tar() -> Result<(), Box<dyn std::error::Error>> { 231 let file = create_test_file("test.txt", "garbage data for testing")?; 232 let file2 = create_test_file("test2.txt", "more garbage data for testing")?; 233 let working_dir = create_working_dir()?; 234 let archive = working_dir.child("archive.tar"); 235 archive.assert(predicate::path::missing()); 236 237 // Compress files to an archive 238 let mut compress = Command::cargo_bin("cmprss")?; 239 compress 240 .current_dir(&working_dir) 241 .arg("--ignore-pipes") 242 .arg(file.path()) 243 .arg(file2.path()) 244 .arg("archive.tar"); 245 compress.assert().success(); 246 archive.assert(predicate::path::is_file()); 247 248 // Extract file to default filename 249 let mut extract = Command::cargo_bin("cmprss")?; 250 extract 251 .current_dir(&working_dir) 252 .arg("--ignore-pipes") 253 .arg("archive.tar"); 254 extract.assert().success(); 255 256 // Assert the files are identical 257 assert_files_equal(file.path(), &working_dir.child("test.txt")); 258 assert_files_equal(file2.path(), &working_dir.child("test2.txt")); 259 260 Ok(()) 261 } 262 263 /// Magic roundtrip with tar.gz 264 /// Infer things as much as possible 265 /// Compressing: input = test.txt + test2.txt, output = test.tar.gz 266 /// Extracting: input = test.tar.gz, output = test.txt + test2.txt 267 /// 268 /// ``` bash 269 /// cmprss test.txt test2.txt archive.tar 270 /// cmprss archive.tar archive.tar.gz 271 /// cmprss archive.tar.gz archive.tar 272 /// cmprss archive.tar 273 /// ``` 274 #[test] 275 fn tar_gz() -> Result<(), Box<dyn std::error::Error>> { 276 let file = create_test_file("test.txt", "garbage data for testing")?; 277 let file2 = create_test_file("test2.txt", "more garbage data for testing")?; 278 let working_dir = create_working_dir()?; 279 let archive = working_dir.child("archive.tar"); 280 archive.assert(predicate::path::missing()); 281 let archive2 = working_dir.child("archive.tar.gz"); 282 archive2.assert(predicate::path::missing()); 283 284 let extract_dir = create_working_dir()?; 285 286 // Compress files to an archive 287 let mut compress = Command::cargo_bin("cmprss")?; 288 compress 289 .current_dir(&working_dir) 290 .arg("--ignore-pipes") 291 .arg(file.path()) 292 .arg(file2.path()) 293 .arg("archive.tar"); 294 compress.assert().success(); 295 archive.assert(predicate::path::is_file()); 296 297 // Compress tar to an archive 298 let mut compress2 = Command::cargo_bin("cmprss")?; 299 compress2 300 .current_dir(&working_dir) 301 .arg("--ignore-pipes") 302 .arg("archive.tar") 303 .arg("archive.tar.gz"); 304 compress2.assert().success(); 305 archive2.assert(predicate::path::is_file()); 306 307 // Extract file to default filename 308 let mut extract = Command::cargo_bin("cmprss")?; 309 extract 310 .current_dir(&extract_dir) 311 .arg("--ignore-pipes") 312 .arg(archive2.path()) 313 .arg("archive.tar"); 314 extract.assert().success(); 315 316 // Extract file to default filename 317 let mut extract2 = Command::cargo_bin("cmprss")?; 318 extract2 319 .current_dir(&extract_dir) 320 .arg("--ignore-pipes") 321 .arg("archive.tar"); 322 extract2.assert().success(); 323 324 // Assert the files are identical 325 assert_files_equal(file.path(), &extract_dir.child("test.txt")); 326 assert_files_equal(file2.path(), &extract_dir.child("test2.txt")); 327 328 Ok(()) 329 } 330 331 /// Magic roundtrip with tar.gz using pipes 332 /// Infer things as much as possible 333 /// Compressing: input = test.txt + test2.txt, output = test.tar.gz 334 /// Extracting: input = test.tar.gz, output = test.txt + test2.txt 335 /// 336 /// ``` bash 337 /// cmprss tar test.txt test2.txt | cmprss gzip | cmprss gzip --extract | cmprss tar --extract 338 /// ``` 339 #[test] 340 fn tar_gz_pipes() -> Result<(), Box<dyn std::error::Error>> { 341 let file = create_test_file("test.txt", "garbage data for testing")?; 342 let file2 = create_test_file("test2.txt", "more garbage data for testing")?; 343 let working_dir = create_working_dir()?; 344 let tee1 = working_dir.child("tee1"); 345 let tee2 = working_dir.child("tee2"); 346 let tee3 = working_dir.child("tee3"); 347 348 let extract_dir = create_working_dir()?; 349 350 let mut compress = Command::cargo_bin("cmprss")?; 351 compress 352 .current_dir(&working_dir) 353 .arg("tar") 354 .arg("--ignore-stdin") 355 .arg(file.path()) 356 .arg(file2.path()) 357 .stdout(Stdio::from(File::create(tee1.path())?)); 358 compress.assert().success(); 359 tee1.assert(predicate::path::is_file()); 360 361 let mut compress2 = Command::cargo_bin("cmprss")?; 362 compress2 363 .current_dir(&working_dir) 364 .arg("gzip") 365 .stdin(Stdio::from(File::open(tee1.path())?)) 366 .stdout(Stdio::from(File::create(tee2.path())?)); 367 compress2.assert().success(); 368 tee2.assert(predicate::path::is_file()); 369 370 let mut extract = Command::cargo_bin("cmprss")?; 371 extract 372 .current_dir(&working_dir) 373 .arg("gzip") 374 .arg("--extract") 375 .stdin(Stdio::from(File::open(tee2.path())?)) 376 .stdout(Stdio::from(File::create(tee3.path())?)); 377 extract.assert().success(); 378 tee3.assert(predicate::path::is_file()); 379 380 // Extract file to default filename 381 let mut extract2 = Command::cargo_bin("cmprss")?; 382 extract2 383 .current_dir(&extract_dir) 384 .arg("tar") 385 .arg("--ignore-stdout") 386 .arg("--extract") 387 .stdin(Stdio::from(File::open(tee3.path())?)); 388 extract2.assert().success(); 389 390 // Assert the files are identical 391 assert_files_equal(file.path(), &extract_dir.child("test.txt")); 392 assert_files_equal(file2.path(), &extract_dir.child("test2.txt")); 393 394 Ok(()) 395 } 396 } 397}