/src/tools/clippy/tests/integration.rs

https://gitlab.com/rust-lang/rust · Rust · 89 lines · 77 code · 9 blank · 3 comment · 12 complexity · 1d6a01acd2326bc1b3cb2ca77898b22b MD5 · raw file

  1. #![cfg(feature = "integration")]
  2. #![cfg_attr(feature = "deny-warnings", deny(warnings))]
  3. #![warn(rust_2018_idioms, unused_lifetimes)]
  4. use std::env;
  5. use std::ffi::OsStr;
  6. use std::process::Command;
  7. #[cfg_attr(feature = "integration", test)]
  8. fn integration_test() {
  9. let repo_name = env::var("INTEGRATION").expect("`INTEGRATION` var not set");
  10. let repo_url = format!("https://github.com/{}", repo_name);
  11. let crate_name = repo_name
  12. .split('/')
  13. .nth(1)
  14. .expect("repo name should have format `<org>/<name>`");
  15. let mut repo_dir = tempfile::tempdir().expect("couldn't create temp dir").into_path();
  16. repo_dir.push(crate_name);
  17. let st = Command::new("git")
  18. .args(&[
  19. OsStr::new("clone"),
  20. OsStr::new("--depth=1"),
  21. OsStr::new(&repo_url),
  22. OsStr::new(&repo_dir),
  23. ])
  24. .status()
  25. .expect("unable to run git");
  26. assert!(st.success());
  27. let root_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
  28. let target_dir = std::path::Path::new(&root_dir).join("target");
  29. let clippy_binary = target_dir.join(env!("PROFILE")).join("cargo-clippy");
  30. let output = Command::new(clippy_binary)
  31. .current_dir(repo_dir)
  32. .env("RUST_BACKTRACE", "full")
  33. .env("CARGO_TARGET_DIR", target_dir)
  34. .args(&[
  35. "clippy",
  36. "--all-targets",
  37. "--all-features",
  38. "--",
  39. "--cap-lints",
  40. "warn",
  41. "-Wclippy::pedantic",
  42. "-Wclippy::nursery",
  43. ])
  44. .output()
  45. .expect("unable to run clippy");
  46. let stderr = String::from_utf8_lossy(&output.stderr);
  47. if stderr.contains("internal compiler error") {
  48. let backtrace_start = stderr
  49. .find("thread 'rustc' panicked at")
  50. .expect("start of backtrace not found");
  51. let backtrace_end = stderr
  52. .rfind("error: internal compiler error")
  53. .expect("end of backtrace not found");
  54. panic!(
  55. "internal compiler error\nBacktrace:\n\n{}",
  56. &stderr[backtrace_start..backtrace_end]
  57. );
  58. } else if stderr.contains("query stack during panic") {
  59. panic!("query stack during panic in the output");
  60. } else if stderr.contains("E0463") {
  61. // Encountering E0463 (can't find crate for `x`) did _not_ cause the build to fail in the
  62. // past. Even though it should have. That's why we explicitly panic here.
  63. // See PR #3552 and issue #3523 for more background.
  64. panic!("error: E0463");
  65. } else if stderr.contains("E0514") {
  66. panic!("incompatible crate versions");
  67. } else if stderr.contains("failed to run `rustc` to learn about target-specific information") {
  68. panic!("couldn't find librustc_driver, consider setting `LD_LIBRARY_PATH`");
  69. } else {
  70. assert!(
  71. !stderr.contains("toolchain") || !stderr.contains("is not installed"),
  72. "missing required toolchain"
  73. );
  74. }
  75. match output.status.code() {
  76. Some(0) => println!("Compilation successful"),
  77. Some(code) => eprintln!("Compilation failed. Exit code: {}", code),
  78. None => panic!("Process terminated by signal"),
  79. }
  80. }