|
| 1 | +use duct::{cmd, Expression}; |
| 2 | +use failure::Error; |
| 3 | +use std::{ |
| 4 | + collections::HashSet, |
| 5 | + path::{Path, PathBuf}, |
| 6 | +}; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn static_api() -> Result<(), Error> { |
| 10 | + let dir_output = dir_valid().join("_output"); |
| 11 | + let dir_expected = dir_valid().join("_expected"); |
| 12 | + |
| 13 | + if dir_output.exists() { |
| 14 | + std::fs::remove_dir_all(&dir_output)?; |
| 15 | + } |
| 16 | + |
| 17 | + step("checking whether the data is valid"); |
| 18 | + cmd!(bin(), "check", "--skip", "validate_github_usernames") |
| 19 | + .dir(dir_valid()) |
| 20 | + .assert_success()?; |
| 21 | + |
| 22 | + step("generating the static api contents"); |
| 23 | + cmd!(bin(), "static-api", &dir_output) |
| 24 | + .dir(dir_valid()) |
| 25 | + .assert_success()?; |
| 26 | + |
| 27 | + step("checking whether the output matched the expected one"); |
| 28 | + |
| 29 | + // Collect all the files present in either the output or expected dirs |
| 30 | + let mut files = HashSet::new(); |
| 31 | + for dir in &[&dir_output, &dir_expected] { |
| 32 | + for entry in walkdir::WalkDir::new(dir) { |
| 33 | + let entry = entry?; |
| 34 | + if !entry.file_type().is_file() { |
| 35 | + continue; |
| 36 | + } |
| 37 | + files.insert(entry.path().strip_prefix(dir)?.to_path_buf()); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Check whether any file is different |
| 42 | + let mut failed = false; |
| 43 | + for file in &files { |
| 44 | + let expected = std::fs::read_to_string(dir_expected.join(file)) |
| 45 | + .ok() |
| 46 | + .unwrap_or_default(); |
| 47 | + let output = std::fs::read_to_string(dir_output.join(file)) |
| 48 | + .ok() |
| 49 | + .unwrap_or_default(); |
| 50 | + |
| 51 | + let changeset = difference::Changeset::new(&expected, &output, "\n"); |
| 52 | + if changeset.distance != 0 { |
| 53 | + failed = true; |
| 54 | + println!( |
| 55 | + "{} {} {}", |
| 56 | + ansi_term::Color::Red.bold().paint("!!! the file"), |
| 57 | + ansi_term::Color::White |
| 58 | + .bold() |
| 59 | + .paint(&file.to_str().unwrap().to_string()), |
| 60 | + ansi_term::Color::Red.bold().paint("does not match"), |
| 61 | + ); |
| 62 | + println!("{}", changeset); |
| 63 | + } |
| 64 | + } |
| 65 | + if failed { |
| 66 | + println!( |
| 67 | + "{} {}", |
| 68 | + ansi_term::Color::Cyan |
| 69 | + .bold() |
| 70 | + .paint("==> If you believe the new content is right, run:"), |
| 71 | + ansi_term::Color::White.bold().paint("tests/bless.sh") |
| 72 | + ); |
| 73 | + println!(); |
| 74 | + panic!("there were differences in the expected output"); |
| 75 | + } |
| 76 | + |
| 77 | + Ok(()) |
| 78 | +} |
| 79 | + |
| 80 | +fn bin() -> &'static str { |
| 81 | + env!("CARGO_BIN_EXE_rust-team") |
| 82 | +} |
| 83 | + |
| 84 | +fn dir_valid() -> PathBuf { |
| 85 | + Path::new(env!("CARGO_MANIFEST_DIR")) |
| 86 | + .join("tests") |
| 87 | + .join("static-api") |
| 88 | +} |
| 89 | + |
| 90 | +fn step(name: &str) { |
| 91 | + println!( |
| 92 | + "{}", |
| 93 | + ansi_term::Color::White |
| 94 | + .bold() |
| 95 | + .paint(&format!("==> {}", name)) |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +trait ExpressionExt { |
| 100 | + fn assert_success(self) -> Result<(), Error>; |
| 101 | +} |
| 102 | + |
| 103 | +impl ExpressionExt for Expression { |
| 104 | + fn assert_success(mut self) -> Result<(), Error> { |
| 105 | + // If the environment variable is not passed colors will never be shown. |
| 106 | + if atty::is(atty::Stream::Stdout) { |
| 107 | + self = self.env("RUST_TEAM_FORCE_COLORS", "1"); |
| 108 | + } |
| 109 | + |
| 110 | + // Redirects stderr to stdout to be able to print the output in the correct order. |
| 111 | + let res = self.stderr_to_stdout().stdout_capture().unchecked().run()?; |
| 112 | + print!("{}", String::from_utf8_lossy(&res.stdout)); |
| 113 | + |
| 114 | + if !res.status.success() { |
| 115 | + failure::bail!("command returned a non-zero exit code!"); |
| 116 | + } |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | +} |
0 commit comments