PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/run-pass/backtrace.rs

https://gitlab.com/alx741/rust
Rust | 130 lines | 83 code | 16 blank | 31 comment | 21 complexity | 37b91412fbc3881667a40dfae80f1711 MD5 | raw file
  1. // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. // no-pretty-expanded FIXME #15189
  11. // ignore-android FIXME #17520
  12. // compile-flags:-g
  13. use std::env;
  14. use std::process::{Command, Stdio};
  15. use std::str;
  16. #[inline(never)]
  17. fn foo() {
  18. let _v = vec![1, 2, 3];
  19. if env::var_os("IS_TEST").is_some() {
  20. panic!()
  21. }
  22. }
  23. #[inline(never)]
  24. fn double() {
  25. struct Double;
  26. impl Drop for Double {
  27. fn drop(&mut self) { panic!("twice") }
  28. }
  29. let _d = Double;
  30. panic!("once");
  31. }
  32. fn template(me: &str) -> Command {
  33. let mut m = Command::new(me);
  34. m.env("IS_TEST", "1")
  35. .stdout(Stdio::piped())
  36. .stderr(Stdio::piped());
  37. return m;
  38. }
  39. fn expected(fn_name: &str) -> String {
  40. // FIXME(#32481)
  41. //
  42. // On windows, we read the function name from debuginfo using some
  43. // system APIs. For whatever reason, these APIs seem to use the
  44. // "name" field, which is only the "relative" name, not the full
  45. // name with namespace info, so we just see `foo` and not
  46. // `backtrace::foo` as we see on linux (which uses the linkage
  47. // name).
  48. if cfg!(windows) && cfg!(target_env = "msvc") {
  49. format!(" - {}", fn_name)
  50. } else {
  51. format!(" - backtrace::{}", fn_name)
  52. }
  53. }
  54. fn runtest(me: &str) {
  55. // Make sure that the stack trace is printed
  56. let p = template(me).arg("fail").env("RUST_BACKTRACE", "1").spawn().unwrap();
  57. let out = p.wait_with_output().unwrap();
  58. assert!(!out.status.success());
  59. let s = str::from_utf8(&out.stderr).unwrap();
  60. assert!(s.contains("stack backtrace") && s.contains(&expected("foo")),
  61. "bad output: {}", s);
  62. // Make sure the stack trace is *not* printed
  63. // (Remove RUST_BACKTRACE from our own environment, in case developer
  64. // is running `make check` with it on.)
  65. let p = template(me).arg("fail").env_remove("RUST_BACKTRACE").spawn().unwrap();
  66. let out = p.wait_with_output().unwrap();
  67. assert!(!out.status.success());
  68. let s = str::from_utf8(&out.stderr).unwrap();
  69. assert!(!s.contains("stack backtrace") && !s.contains(&expected("foo")),
  70. "bad output2: {}", s);
  71. // Make sure the stack trace is *not* printed
  72. // (RUST_BACKTRACE=0 acts as if it were unset from our own environment,
  73. // in case developer is running `make check` with it set.)
  74. let p = template(me).arg("fail").env("RUST_BACKTRACE","0").spawn().unwrap();
  75. let out = p.wait_with_output().unwrap();
  76. assert!(!out.status.success());
  77. let s = str::from_utf8(&out.stderr).unwrap();
  78. assert!(!s.contains("stack backtrace") && !s.contains(" - foo"),
  79. "bad output3: {}", s);
  80. // Make sure a stack trace is printed
  81. let p = template(me).arg("double-fail").spawn().unwrap();
  82. let out = p.wait_with_output().unwrap();
  83. assert!(!out.status.success());
  84. let s = str::from_utf8(&out.stderr).unwrap();
  85. // loosened the following from double::h to double:: due to
  86. // spurious failures on mac, 32bit, optimized
  87. assert!(s.contains("stack backtrace") && s.contains(&expected("double")),
  88. "bad output3: {}", s);
  89. // Make sure a stack trace isn't printed too many times
  90. let p = template(me).arg("double-fail")
  91. .env("RUST_BACKTRACE", "1").spawn().unwrap();
  92. let out = p.wait_with_output().unwrap();
  93. assert!(!out.status.success());
  94. let s = str::from_utf8(&out.stderr).unwrap();
  95. let mut i = 0;
  96. for _ in 0..2 {
  97. i += s[i + 10..].find("stack backtrace").unwrap() + 10;
  98. }
  99. assert!(s[i + 10..].find("stack backtrace").is_none(),
  100. "bad output4: {}", s);
  101. }
  102. fn main() {
  103. if cfg!(windows) && cfg!(target_arch = "x86") && cfg!(target_env = "gnu") {
  104. return
  105. }
  106. let args: Vec<String> = env::args().collect();
  107. if args.len() >= 2 && args[1] == "fail" {
  108. foo();
  109. } else if args.len() >= 2 && args[1] == "double-fail" {
  110. double();
  111. } else {
  112. runtest(&args[0]);
  113. }
  114. }