Skip to content

Commit

Permalink
Merge pull request #9 from leo60228/sysroot
Browse files Browse the repository at this point in the history
Add probe_sysroot_crate and emit_sysroot_crate
  • Loading branch information
cuviper authored Aug 19, 2019
2 parents 81ea9f3 + db12127 commit 0f87a09
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ matrix:
before_script:
- rustup target add $TARGET
script:
- cargo test --verbose --lib -- no_std
- cargo test --verbose --lib
# we don't even need an installed target for version checks
- name: "missing_target"
rust: stable
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "autocfg"
version = "0.1.5"
version = "0.1.6"
authors = ["Josh Stone <[email protected]>"]
license = "Apache-2.0/MIT"
repository = "https://github.com/cuviper/autocfg"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ should only be used when the compiler supports it.

## Release Notes

- 0.1.6 (2019-08-19)
- Add `probe`/`emit_sysroot_crate`, by @leo60228

- 0.1.5 (2019-07-16)
- Mask some warnings from newer rustc.

Expand Down
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,25 @@ impl AutoCfg {
Ok(status.success())
}

/// Tests whether the given sysroot crate can be used.
///
/// The test code is subject to change, but currently looks like:
///
/// ```ignore
/// extern crate CRATE as probe;
/// ```
pub fn probe_sysroot_crate(&self, name: &str) -> bool {
self.probe(format!("extern crate {} as probe;", name)) // `as _` wasn't stabilized until Rust 1.33
.unwrap_or(false)
}

/// Emits a config value `has_CRATE` if `probe_sysroot_crate` returns true.
pub fn emit_sysroot_crate(&self, name: &str) {
if self.probe_sysroot_crate(name) {
emit(&format!("has_{}", mangle(name)));
}
}

/// Tests whether the given path can be used.
///
/// The test code is subject to change, but currently looks like:
Expand Down
60 changes: 47 additions & 13 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use super::AutoCfg;

impl AutoCfg {
fn core_std(&self, path: &str) -> String {
let krate = if self.no_std { "core" } else { "std" };
format!("{}::{}", krate, path)
}
}

#[test]
fn autocfg_version() {
let ac = AutoCfg::with_dir("target").unwrap();
Expand All @@ -23,37 +30,64 @@ fn version_cmp() {
#[test]
fn probe_add() {
let ac = AutoCfg::with_dir("target").unwrap();
assert!(ac.probe_path("std::ops::Add"));
assert!(ac.probe_trait("std::ops::Add"));
assert!(ac.probe_trait("std::ops::Add<i32>"));
assert!(ac.probe_trait("std::ops::Add<i32, Output = i32>"));
assert!(ac.probe_type("std::ops::Add<i32, Output = i32>"));
let add = ac.core_std("ops::Add");
let add_rhs = ac.core_std("ops::Add<i32>");
let add_rhs_output = ac.core_std("ops::Add<i32, Output = i32>");
assert!(ac.probe_path(&add));
assert!(ac.probe_trait(&add));
assert!(ac.probe_trait(&add_rhs));
assert!(ac.probe_trait(&add_rhs_output));
assert!(ac.probe_type(&add_rhs_output));
}

#[test]
fn probe_as_ref() {
let ac = AutoCfg::with_dir("target").unwrap();
assert!(ac.probe_path("std::convert::AsRef"));
assert!(ac.probe_trait("std::convert::AsRef<str>"));
assert!(ac.probe_type("std::convert::AsRef<str>"));
let as_ref = ac.core_std("convert::AsRef");
let as_ref_str = ac.core_std("convert::AsRef<str>");
assert!(ac.probe_path(&as_ref));
assert!(ac.probe_trait(&as_ref_str));
assert!(ac.probe_type(&as_ref_str));
}

#[test]
fn probe_i128() {
let ac = AutoCfg::with_dir("target").unwrap();
let missing = !ac.probe_rustc_version(1, 26);
assert!(missing ^ ac.probe_path("std::i128"));
let i128_path = ac.core_std("i128");
assert!(missing ^ ac.probe_path(&i128_path));
assert!(missing ^ ac.probe_type("i128"));
}

#[test]
fn probe_sum() {
let ac = AutoCfg::with_dir("target").unwrap();
let missing = !ac.probe_rustc_version(1, 12);
assert!(missing ^ ac.probe_path("std::iter::Sum"));
assert!(missing ^ ac.probe_trait("std::iter::Sum"));
assert!(missing ^ ac.probe_trait("std::iter::Sum<i32>"));
assert!(missing ^ ac.probe_type("std::iter::Sum<i32>"));
let sum = ac.core_std("iter::Sum");
let sum_i32 = ac.core_std("iter::Sum<i32>");
assert!(missing ^ ac.probe_path(&sum));
assert!(missing ^ ac.probe_trait(&sum));
assert!(missing ^ ac.probe_trait(&sum_i32));
assert!(missing ^ ac.probe_type(&sum_i32));
}

#[test]
fn probe_std() {
let ac = AutoCfg::with_dir("target").unwrap();
assert_eq!(ac.probe_sysroot_crate("std"), !ac.no_std);
}

#[test]
fn probe_alloc() {
let ac = AutoCfg::with_dir("target").unwrap();
let missing = !ac.probe_rustc_version(1, 36);
assert!(missing ^ ac.probe_sysroot_crate("alloc"));
}

#[test]
fn probe_bad_sysroot_crate() {
let ac = AutoCfg::with_dir("target").unwrap();
assert!(!ac.probe_sysroot_crate("doesnt_exist"));
}

#[test]
Expand Down

0 comments on commit 0f87a09

Please sign in to comment.