compile.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use std::{
  2. fs,
  3. ffi::OsStr,
  4. io,
  5. path::Path,
  6. };
  7. use walkdir::WalkDir;
  8. #[test]
  9. fn fail() {
  10. prepare_stderr_files("tests/compile-fail").unwrap();
  11. let t = trybuild::TestCases::new();
  12. t.compile_fail("tests/compile-fail/**/*.rs");
  13. }
  14. #[test]
  15. fn pass() {
  16. let t = trybuild::TestCases::new();
  17. t.pass("tests/compile-pass/**/*.rs");
  18. }
  19. // Compiler messages may change between versions
  20. // We don't want to have to track these too closely for `bitflags`, but
  21. // having some message to check makes sure user-facing errors are sensical.
  22. //
  23. // The approach we use is to run the test on all compilers, but only check stderr
  24. // output on beta (which is the next stable release). We do this by default ignoring
  25. // any `.stderr` files in the `compile-fail` directory, and copying `.stderr.beta` files
  26. // when we happen to be running on a beta compiler.
  27. fn prepare_stderr_files(path: impl AsRef<Path>) -> io::Result<()> {
  28. for entry in WalkDir::new(path) {
  29. let entry = entry?;
  30. if entry.path().extension().and_then(OsStr::to_str) == Some("beta") {
  31. let renamed = entry.path().with_extension("");
  32. // Unconditionally remove a corresponding `.stderr` file for a `.stderr.beta`
  33. // file if it exists. On `beta` compilers, we'll recreate it. On other compilers,
  34. // we don't want to end up checking it anyways.
  35. if renamed.exists() {
  36. fs::remove_file(&renamed)?;
  37. }
  38. rename_beta_stderr(entry.path(), renamed)?;
  39. }
  40. }
  41. Ok(())
  42. }
  43. #[rustversion::beta]
  44. fn rename_beta_stderr(from: impl AsRef<Path>, to: impl AsRef<Path>) -> io::Result<()> {
  45. fs::copy(from, to)?;
  46. Ok(())
  47. }
  48. #[rustversion::not(beta)]
  49. fn rename_beta_stderr(_: impl AsRef<Path>, _: impl AsRef<Path>) -> io::Result<()> {
  50. Ok(())
  51. }