• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 extern crate autocfg;
2 
3 use std::env;
4 
5 mod support;
6 
7 /// Tests that autocfg uses the RUSTC_WRAPPER and/or RUSTC_WORKSPACE_WRAPPER
8 /// environment variables when running rustc.
9 #[test]
10 #[cfg(unix)] // we're using system binaries as wrappers
test_wrappers()11 fn test_wrappers() {
12     fn set(name: &str, value: Option<bool>) {
13         match value {
14             Some(true) => env::set_var(name, "/usr/bin/env"),
15             Some(false) => env::set_var(name, "/bin/false"),
16             None => env::remove_var(name),
17         }
18     }
19 
20     let out = support::out_dir();
21 
22     // This is used as a heuristic to detect rust-lang/cargo#9601.
23     env::set_var("CARGO_ENCODED_RUSTFLAGS", "");
24 
25     // No wrapper, a good pass-through wrapper, and a bad wrapper.
26     let variants = [None, Some(true), Some(false)];
27 
28     for &workspace in &variants {
29         for &rustc in &variants {
30             set("RUSTC_WRAPPER", rustc);
31             set("RUSTC_WORKSPACE_WRAPPER", workspace);
32 
33             let ac = autocfg::AutoCfg::with_dir(out.as_ref()).unwrap();
34             if rustc == Some(false) || workspace == Some(false) {
35                 // Everything should fail with bad wrappers.
36                 assert!(!ac.probe_type("usize"));
37             } else {
38                 // Try known good and bad types for the wrapped rustc.
39                 assert!(ac.probe_type("usize"));
40                 assert!(!ac.probe_type("mesize"));
41             }
42             // Either way, we should have found the inner rustc version.
43             assert!(ac.probe_rustc_version(1, 0));
44         }
45     }
46 
47     // Finally, make sure that `RUSTC_WRAPPER` is applied outermost
48     // by using something that doesn't pass through at all.
49     env::set_var("RUSTC_WRAPPER", "./tests/wrap_ignored");
50     env::set_var("RUSTC_WORKSPACE_WRAPPER", "/bin/false");
51     let ac = autocfg::AutoCfg::with_dir(out.as_ref()).unwrap();
52     assert!(ac.probe_type("mesize")); // anything goes!
53 
54     // Make sure we also got the version from that wrapper.
55     assert!(ac.probe_rustc_version(12345, 6789));
56 }
57