Skip to content

Commit

Permalink
port style.rs to syn and add tests for the style checker
Browse files Browse the repository at this point in the history
(backport <rust-lang#4220>)
(cherry picked from commit 56e8210)
  • Loading branch information
rmehri01 authored and tgross35 committed Feb 19, 2025
1 parent 32f65a8 commit 48bd1a0
Show file tree
Hide file tree
Showing 5 changed files with 816 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
target
Cargo.lock
*~
style
16 changes: 16 additions & 0 deletions libc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ path = ".."
version = "0.2.169"
default-features = false

[dev-dependencies]
syn = { version = "2.0.91", features = ["full", "visit"] }
proc-macro2 = { version = "1.0.92", features = ["span-locations"] }
glob = "0.3.2"
annotate-snippets = { version = "0.11.5", features = ["testing-colors"] }

[build-dependencies]
cc = "1.0.83"
# FIXME: Use fork ctest until the maintainer gets back.
Expand Down Expand Up @@ -93,3 +99,13 @@ harness = false
name = "primitive_types"
path = "test/primitive_types.rs"
harness = true

[[test]]
name = "style"
path = "test/check_style.rs"
harness = true

[[test]]
name = "style_tests"
path = "test/style_tests.rs"
harness = true
50 changes: 50 additions & 0 deletions libc-test/test/check_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! Simple script to verify the coding style of this library.
//!
//! ## How to run
//!
//! The first argument to this script is the directory to run on, so running
//! this script should be as simple as:
//!
//! ```notrust
//! cargo test --test style
//! ```
pub mod style;

use std::env;
use std::path::Path;

use style::{Result, StyleChecker};

#[test]
fn check_style() {
let root_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../src");
walk(&root_dir).unwrap();
eprintln!("good style!");
}

fn walk(root_dir: &Path) -> Result<()> {
let mut style_checker = StyleChecker::new();

for entry in glob::glob(&format!(
"{}/**/*.rs",
root_dir.to_str().expect("dir should be valid UTF-8")
))? {
let entry = entry?;

let name = entry
.file_name()
.expect("file name should not end in ..")
.to_str()
.expect("file name should be valid UTF-8");
if let "lib.rs" | "macros.rs" = &name[..] {
continue;
}

let path = entry.as_path();
style_checker.check_file(path)?;
style_checker.reset_state();
}

style_checker.finalize()
}
Loading

0 comments on commit 48bd1a0

Please sign in to comment.