|
| 1 | +//! This checks the output of `--print=check-cfg` |
| 2 | +
|
| 3 | +extern crate run_make_support; |
| 4 | + |
| 5 | +use std::collections::HashSet; |
| 6 | +use std::iter::FromIterator; |
| 7 | +use std::ops::Deref; |
| 8 | + |
| 9 | +use run_make_support::rustc; |
| 10 | + |
| 11 | +fn main() { |
| 12 | + check( |
| 13 | + /*args*/ &[], |
| 14 | + /*has_any*/ false, |
| 15 | + /*has_any_any*/ true, |
| 16 | + /*contains*/ &[], |
| 17 | + ); |
| 18 | + check( |
| 19 | + /*args*/ &["--check-cfg=cfg()"], |
| 20 | + /*has_any*/ false, |
| 21 | + /*has_any_any*/ false, |
| 22 | + /*contains*/ &["unix", "miri"], |
| 23 | + ); |
| 24 | + check( |
| 25 | + /*args*/ &["--check-cfg=cfg(any())"], |
| 26 | + /*has_any*/ true, |
| 27 | + /*has_any_any*/ false, |
| 28 | + /*contains*/ &["windows", "test"], |
| 29 | + ); |
| 30 | + check( |
| 31 | + /*args*/ &["--check-cfg=cfg(feature)"], |
| 32 | + /*has_any*/ false, |
| 33 | + /*has_any_any*/ false, |
| 34 | + /*contains*/ &["unix", "miri", "feature"], |
| 35 | + ); |
| 36 | + check( |
| 37 | + /*args*/ &[r#"--check-cfg=cfg(feature, values(none(), "", "test", "lol"))"#], |
| 38 | + /*has_any*/ false, |
| 39 | + /*has_any_any*/ false, |
| 40 | + /*contains*/ &["feature", "feature=\"\"", "feature=\"test\"", "feature=\"lol\""], |
| 41 | + ); |
| 42 | + check( |
| 43 | + /*args*/ &[ |
| 44 | + r#"--check-cfg=cfg(feature, values(any()))"#, |
| 45 | + r#"--check-cfg=cfg(feature, values("tmp"))"# |
| 46 | + ], |
| 47 | + /*has_any*/ false, |
| 48 | + /*has_any_any*/ false, |
| 49 | + /*contains*/ &["unix", "miri", "feature=any()"], |
| 50 | + ); |
| 51 | + check( |
| 52 | + /*args*/ &[ |
| 53 | + r#"--check-cfg=cfg(has_foo, has_bar)"#, |
| 54 | + r#"--check-cfg=cfg(feature, values("tmp"))"#, |
| 55 | + r#"--check-cfg=cfg(feature, values("tmp"))"# |
| 56 | + ], |
| 57 | + /*has_any*/ false, |
| 58 | + /*has_any_any*/ false, |
| 59 | + /*contains*/ &["has_foo", "has_bar", "feature=\"tmp\""], |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +fn check(args: &[&str], has_any: bool, has_any_any: bool, contains: &[&str]) { |
| 64 | + let output = rustc() |
| 65 | + .input("lib.rs") |
| 66 | + .arg("-Zunstable-options") |
| 67 | + .arg("--print=check-cfg") |
| 68 | + .args(&*args) |
| 69 | + .run(); |
| 70 | + |
| 71 | + let stdout = String::from_utf8(output.stdout).unwrap(); |
| 72 | + |
| 73 | + let mut found_any = false; |
| 74 | + let mut found_any_any = false; |
| 75 | + let mut found = HashSet::<String>::new(); |
| 76 | + let mut recorded = HashSet::<String>::new(); |
| 77 | + |
| 78 | + for l in stdout.lines() { |
| 79 | + assert!(l == l.trim()); |
| 80 | + if l == "any()" { |
| 81 | + found_any = true; |
| 82 | + } else if l == "any()=any()" { |
| 83 | + found_any_any = true; |
| 84 | + } else if let Some((left, right)) = l.split_once('=') { |
| 85 | + if right != "any()" && right != "" { |
| 86 | + assert!(right.starts_with("\"")); |
| 87 | + assert!(right.ends_with("\"")); |
| 88 | + } |
| 89 | + assert!(!left.contains("\"")); |
| 90 | + } else { |
| 91 | + assert!(!l.contains("\"")); |
| 92 | + } |
| 93 | + assert!(recorded.insert(l.to_string()), "{}", &l); |
| 94 | + if contains.contains(&l) { |
| 95 | + assert!(found.insert(l.to_string()), "{}", &l); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + let should_found = HashSet::<String>::from_iter(contains.iter().map(|s| s.to_string())); |
| 100 | + let diff: Vec<_> = should_found.difference(&found).collect(); |
| 101 | + |
| 102 | + assert_eq!(found_any, has_any); |
| 103 | + assert_eq!(found_any_any, has_any_any); |
| 104 | + assert_eq!(found_any_any, recorded.len() == 1); |
| 105 | + assert!(diff.is_empty(), "{:?} != {:?} (~ {:?})", &should_found, &found, &diff); |
| 106 | +} |
0 commit comments