Skip to content

Commit

Permalink
Add ci test crate for check-cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
Techcable authored and cuviper committed Sep 27, 2024
1 parent 0b4a761 commit ac78be8
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
matrix:
rust: [1.0.0, 1.5.0, 1.10.0, 1.15.0, 1.20.0, 1.25.0, 1.30.0, 1.35.0,
1.40.0, 1.45.0, 1.50.0, 1.55.0, 1.60.0, 1.65.0, 1.70.0, 1.75.0,
1.80.0, # first version with rustc-check-cfg
stable, beta, nightly]
steps:
- uses: actions/checkout@v4
Expand All @@ -22,6 +23,8 @@ jobs:
toolchain: ${{ matrix.rust }}
- run: cargo build --verbose
- run: cargo test --verbose
- run: cargo run
working-directory: ./ci/verify-check-cfg

# try probing a target that doesn't have std at all
no_std:
Expand Down
3 changes: 3 additions & 0 deletions ci/verify-check-cfg/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Rust
/target
/Cargo.lock
12 changes: 12 additions & 0 deletions ci/verify-check-cfg/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
# NOTE: Cannot be in workspace because of rustc 1.0 support
name = "autocfg-verify-check-cfg"
description = "A dummy crate to verify autocfg is emitting check-cfg directives"
version = "0.1.0"
edition = "2021"
# only for testing
publish = false

[build-dependencies]
autocfg = { path = "../.." }

43 changes: 43 additions & 0 deletions ci/verify-check-cfg/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pub fn main() {
let cfg = autocfg::AutoCfg::new().unwrap();

//
// tests
//

// always true
cfg.emit_rustc_version(1, 0);
// should always be false
cfg.emit_rustc_version(7, u32::MAX as usize);

// always true
cfg.emit_has_path("std::vec::Vec");
cfg.emit_path_cfg("std::vec::Vec", "has_path_std_vec");
// always false
cfg.emit_has_path("dummy::DummyPath");
cfg.emit_path_cfg("dummy::DummyPath", "has_path_dummy");

// always true
cfg.emit_has_trait("std::ops::Add");
cfg.emit_trait_cfg("std::ops::Add", "has_trait_add");
// always false
cfg.emit_has_trait("dummy::DummyTrait");
cfg.emit_trait_cfg("dummy::DummyTrait", "has_trait_dummy");

// always true
cfg.emit_has_type("i32");
cfg.emit_type_cfg("i32", "has_type_i32");
// always false
cfg.emit_has_type("i7billion");
cfg.emit_type_cfg("i7billion", "has_type_i7billion");

// always true
cfg.emit_expression_cfg("3 + 7", "has_working_addition");
// always false
cfg.emit_expression_cfg("3 ^^^^^ 12", "has_working_5xor");

// always true
cfg.emit_constant_cfg("7", "has_const_7");
// false - Opening file should never be `const`
cfg.emit_constant_cfg("std::fs::File::open(\"foo.txt\")", "has_const_file_open");
}
42 changes: 42 additions & 0 deletions ci/verify-check-cfg/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![deny(unexpected_cfgs)]

macro_rules! test_cfgs {
($($cfg:ident,)*) => {$({
let cfg_desc = format!("cfg!({})", stringify!($cfg));
if cfg!($cfg) {
println!("Enabled: {}", cfg_desc);
} else {
println!("Disabled: {}", cfg_desc);
}
})*};

}

pub fn main() {
test_cfgs!(
// emit_rustc_version
rustc_1_0,
rustc_7_4294967295,
// emit_has_path, emit_path_cfg
has_std__vec__Vec,
has_path_std_vec,
has_dummy__DummyPath,
has_path_dummy,
// emit_has_trait, emit_trait_cfg
has_std__ops__Add,
has_trait_add,
has_dummy__DummyTrait,
has_trait_dummy,
// emit_has_type, has_type_i32
has_i32,
has_type_i32,
has_i7billion,
has_type_i7billion,
// emit_expression_cfg
has_working_addition,
has_working_5xor,
// emit_constant_cfg
has_const_7,
has_const_file_open,
);
}

0 comments on commit ac78be8

Please sign in to comment.