| 1 |
use super::AutoCfg; |
| 2 |
use std::env; |
| 3 |
use std::path::Path; |
| 4 |
|
| 5 |
impl AutoCfg { |
| 6 |
fn core_std(&self, path: &str) -> String { |
| 7 |
let krate = if self.no_std { "core" } else { "std" }; |
| 8 |
format!("{}::{}", krate, path) |
| 9 |
} |
| 10 |
|
| 11 |
fn assert_std(&self, probe_result: bool) { |
| 12 |
assert_eq!(!self.no_std, probe_result); |
| 13 |
} |
| 14 |
|
| 15 |
fn assert_min(&self, major: usize, minor: usize, probe_result: bool) { |
| 16 |
assert_eq!(self.probe_rustc_version(major, minor), probe_result); |
| 17 |
} |
| 18 |
|
| 19 |
fn for_test() -> Result<Self, super::error::Error> { |
| 20 |
match env::var_os("TESTS_TARGET_DIR") { |
| 21 |
Some(d) => Self::with_dir(d), |
| 22 |
None => Self::with_dir("target"), |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
#[test] |
| 28 |
fn autocfg_version() { |
| 29 |
let ac = AutoCfg::for_test().unwrap(); |
| 30 |
println!("version: {:?}", ac.rustc_version); |
| 31 |
assert!(ac.probe_rustc_version(1, 0)); |
| 32 |
} |
| 33 |
|
| 34 |
#[test] |
| 35 |
fn version_cmp() { |
| 36 |
use super::version::Version; |
| 37 |
let v123 = Version::new(1, 2, 3); |
| 38 |
|
| 39 |
assert!(Version::new(1, 0, 0) < v123); |
| 40 |
assert!(Version::new(1, 2, 2) < v123); |
| 41 |
assert!(Version::new(1, 2, 3) == v123); |
| 42 |
assert!(Version::new(1, 2, 4) > v123); |
| 43 |
assert!(Version::new(1, 10, 0) > v123); |
| 44 |
assert!(Version::new(2, 0, 0) > v123); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn probe_add() { |
| 49 |
let ac = AutoCfg::for_test().unwrap(); |
| 50 |
let add = ac.core_std("ops::Add"); |
| 51 |
let add_rhs = add.clone() + "<i32>"; |
| 52 |
let add_rhs_output = add.clone() + "<i32, Output = i32>"; |
| 53 |
let dyn_add_rhs_output = "dyn ".to_string() + &*add_rhs_output; |
| 54 |
assert!(ac.probe_path(&add)); |
| 55 |
assert!(ac.probe_trait(&add)); |
| 56 |
assert!(ac.probe_trait(&add_rhs)); |
| 57 |
assert!(ac.probe_trait(&add_rhs_output)); |
| 58 |
ac.assert_min(1, 27, ac.probe_type(&dyn_add_rhs_output)); |
| 59 |
} |
| 60 |
|
| 61 |
#[test] |
| 62 |
fn probe_as_ref() { |
| 63 |
let ac = AutoCfg::for_test().unwrap(); |
| 64 |
let as_ref = ac.core_std("convert::AsRef"); |
| 65 |
let as_ref_str = as_ref.clone() + "<str>"; |
| 66 |
let dyn_as_ref_str = "dyn ".to_string() + &*as_ref_str; |
| 67 |
assert!(ac.probe_path(&as_ref)); |
| 68 |
assert!(ac.probe_trait(&as_ref_str)); |
| 69 |
assert!(ac.probe_type(&as_ref_str)); |
| 70 |
ac.assert_min(1, 27, ac.probe_type(&dyn_as_ref_str)); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn probe_i128() { |
| 75 |
let ac = AutoCfg::for_test().unwrap(); |
| 76 |
let i128_path = ac.core_std("i128"); |
| 77 |
ac.assert_min(1, 26, ac.probe_path(&i128_path)); |
| 78 |
ac.assert_min(1, 26, ac.probe_type("i128")); |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn probe_sum() { |
| 83 |
let ac = AutoCfg::for_test().unwrap(); |
| 84 |
let sum = ac.core_std("iter::Sum"); |
| 85 |
let sum_i32 = sum.clone() + "<i32>"; |
| 86 |
let dyn_sum_i32 = "dyn ".to_string() + &*sum_i32; |
| 87 |
ac.assert_min(1, 12, ac.probe_path(&sum)); |
| 88 |
ac.assert_min(1, 12, ac.probe_trait(&sum)); |
| 89 |
ac.assert_min(1, 12, ac.probe_trait(&sum_i32)); |
| 90 |
ac.assert_min(1, 12, ac.probe_type(&sum_i32)); |
| 91 |
ac.assert_min(1, 27, ac.probe_type(&dyn_sum_i32)); |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn probe_std() { |
| 96 |
let ac = AutoCfg::for_test().unwrap(); |
| 97 |
ac.assert_std(ac.probe_sysroot_crate("std")); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn probe_alloc() { |
| 102 |
let ac = AutoCfg::for_test().unwrap(); |
| 103 |
ac.assert_min(1, 36, ac.probe_sysroot_crate("alloc")); |
| 104 |
} |
| 105 |
|
| 106 |
#[test] |
| 107 |
fn probe_bad_sysroot_crate() { |
| 108 |
let ac = AutoCfg::for_test().unwrap(); |
| 109 |
assert!(!ac.probe_sysroot_crate("doesnt_exist")); |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn probe_no_std() { |
| 114 |
let ac = AutoCfg::for_test().unwrap(); |
| 115 |
assert!(ac.probe_type("i32")); |
| 116 |
assert!(ac.probe_type("[i32]")); |
| 117 |
ac.assert_std(ac.probe_type("Vec<i32>")); |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn probe_expression() { |
| 122 |
let ac = AutoCfg::for_test().unwrap(); |
| 123 |
assert!(ac.probe_expression(r#""test".trim_left()"#)); |
| 124 |
ac.assert_min(1, 30, ac.probe_expression(r#""test".trim_start()"#)); |
| 125 |
ac.assert_std(ac.probe_expression("[1, 2, 3].to_vec()")); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn probe_constant() { |
| 130 |
let ac = AutoCfg::for_test().unwrap(); |
| 131 |
assert!(ac.probe_constant("1 + 2 + 3")); |
| 132 |
ac.assert_min(1, 33, ac.probe_constant("{ let x = 1 + 2 + 3; x * x }")); |
| 133 |
ac.assert_min(1, 39, ac.probe_constant(r#""test".len()"#)); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn probe_raw() { |
| 138 |
let ac = AutoCfg::for_test().unwrap(); |
| 139 |
let prefix = if ac.no_std { "#![no_std]\n" } else { "" }; |
| 140 |
let f = |s| format!("{}{}", prefix, s); |
| 141 |
|
| 142 |
// This attribute **must** be used at the crate level. |
| 143 |
assert!(ac.probe_raw(&f("#![no_builtins]")).is_ok()); |
| 144 |
|
| 145 |
assert!(ac.probe_raw(&f("#![deny(dead_code)] fn x() {}")).is_err()); |
| 146 |
assert!(ac.probe_raw(&f("#![allow(dead_code)] fn x() {}")).is_ok()); |
| 147 |
assert!(ac |
| 148 |
.probe_raw(&f("#![deny(dead_code)] pub fn x() {}")) |
| 149 |
.is_ok()); |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn dir_does_not_contain_target() { |
| 154 |
assert!(!super::dir_contains_target( |
| 155 |
&Some("x86_64-unknown-linux-gnu".into()), |
| 156 |
Path::new("/project/target/debug/build/project-ea75983148559682/out"), |
| 157 |
None, |
| 158 |
)); |
| 159 |
} |
| 160 |
|
| 161 |
#[test] |
| 162 |
fn dir_does_contain_target() { |
| 163 |
assert!(super::dir_contains_target( |
| 164 |
&Some("x86_64-unknown-linux-gnu".into()), |
| 165 |
Path::new( |
| 166 |
"/project/target/x86_64-unknown-linux-gnu/debug/build/project-0147aca016480b9d/out" |
| 167 |
), |
| 168 |
None, |
| 169 |
)); |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn dir_does_not_contain_target_with_custom_target_dir() { |
| 174 |
assert!(!super::dir_contains_target( |
| 175 |
&Some("x86_64-unknown-linux-gnu".into()), |
| 176 |
Path::new("/project/custom/debug/build/project-ea75983148559682/out"), |
| 177 |
Some("custom".into()), |
| 178 |
)); |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn dir_does_contain_target_with_custom_target_dir() { |
| 183 |
assert!(super::dir_contains_target( |
| 184 |
&Some("x86_64-unknown-linux-gnu".into()), |
| 185 |
Path::new( |
| 186 |
"/project/custom/x86_64-unknown-linux-gnu/debug/build/project-0147aca016480b9d/out" |
| 187 |
), |
| 188 |
Some("custom".into()), |
| 189 |
)); |
| 190 |
} |