rustflags.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. extern crate autocfg;
  2. use std::env;
  3. /// Tests that autocfg uses the RUSTFLAGS or CARGO_ENCODED_RUSTFLAGS
  4. /// environment variables when running rustc.
  5. #[test]
  6. fn test_with_sysroot() {
  7. // Use the same path as this test binary.
  8. let dir = env::current_exe().unwrap().parent().unwrap().to_path_buf();
  9. env::set_var("OUT_DIR", &format!("{}", dir.display()));
  10. // If we have encoded rustflags, they take precedence, even if empty.
  11. env::set_var("CARGO_ENCODED_RUSTFLAGS", "");
  12. env::set_var("RUSTFLAGS", &format!("-L {}", dir.display()));
  13. let ac = autocfg::AutoCfg::new().unwrap();
  14. assert!(ac.probe_sysroot_crate("std"));
  15. assert!(!ac.probe_sysroot_crate("autocfg"));
  16. // Now try again with useful encoded args.
  17. env::set_var(
  18. "CARGO_ENCODED_RUSTFLAGS",
  19. &format!("-L\x1f{}", dir.display()),
  20. );
  21. let ac = autocfg::AutoCfg::new().unwrap();
  22. assert!(ac.probe_sysroot_crate("autocfg"));
  23. // Try the old-style RUSTFLAGS, ensuring HOST != TARGET.
  24. env::remove_var("CARGO_ENCODED_RUSTFLAGS");
  25. env::set_var("HOST", "lol");
  26. let ac = autocfg::AutoCfg::new().unwrap();
  27. assert!(ac.probe_sysroot_crate("autocfg"));
  28. }