Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add probe_sysroot_crate and emit_sysroot_crate #9

Merged
merged 3 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <cuviper@gmail.com>"]
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