Skip to content

Commit

Permalink
Merge pull request #4220 from rmehri01/syn-style
Browse files Browse the repository at this point in the history
Rewrite `style.rs` using `syn`
  • Loading branch information
tgross35 authored Jan 12, 2025
2 parents 3f44946 + 94c2b14 commit 0f9f8c9
Show file tree
Hide file tree
Showing 13 changed files with 857 additions and 248 deletions.
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
19 changes: 13 additions & 6 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ if [ -n "${QEMU:-}" ]; then
fi

cmd="cargo test --target $target ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}"
test_flags="--skip check_style"

# Run tests in the `libc` crate
case "$target" in
Expand All @@ -101,25 +102,31 @@ if [ "$target" = "s390x-unknown-linux-gnu" ]; then
passed=0
until [ $n -ge $N ]; do
if [ "$passed" = "0" ]; then
if $cmd --no-default-features; then
# shellcheck disable=SC2086
if $cmd --no-default-features -- $test_flags; then
passed=$((passed+1))
continue
fi
elif [ "$passed" = "1" ]; then
if $cmd; then
# shellcheck disable=SC2086
if $cmd -- $test_flags; then
passed=$((passed+1))
continue
fi
elif [ "$passed" = "2" ]; then
if $cmd --features extra_traits; then
# shellcheck disable=SC2086
if $cmd --features extra_traits -- $test_flags; then
break
fi
fi
n=$((n+1))
sleep 1
done
else
$cmd --no-default-features
$cmd
$cmd --features extra_traits
# shellcheck disable=SC2086
$cmd --no-default-features -- $test_flags
# shellcheck disable=SC2086
$cmd -- $test_flags
# shellcheck disable=SC2086
$cmd --features extra_traits -- $test_flags
fi
20 changes: 11 additions & 9 deletions ci/runtest-android.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::env;
use std::process::Command;
use std::path::{Path, PathBuf};
use std::process::Command;

fn main() {
let args = env::args_os()
.skip(1)
.filter(|arg| arg != "--quiet")
.filter(|arg| arg != "--quiet" && arg != "--skip" && arg != "check_style")
.collect::<Vec<_>>();
assert_eq!(args.len(), 1);
let test = PathBuf::from(&args[0]);
Expand Down Expand Up @@ -36,14 +36,16 @@ fn main() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);

println!("status: {}\nstdout ---\n{}\nstderr ---\n{}",
output.status,
stdout,
stderr);
println!(
"status: {}\nstdout ---\n{}\nstderr ---\n{}",
output.status, stdout, stderr
);

if !stderr.lines().any(|l| (l.starts_with("PASSED ") && l.contains(" tests")) || l.starts_with("test result: ok"))
&& !stdout.lines().any(|l| (l.starts_with("PASSED ") && l.contains(" tests")) || l.starts_with("test result: ok"))
{
if !stderr.lines().any(|l| {
(l.starts_with("PASSED ") && l.contains(" tests")) || l.starts_with("test result: ok")
}) && !stdout.lines().any(|l| {
(l.starts_with("PASSED ") && l.contains(" tests")) || l.starts_with("test result: ok")
}) {
panic!("failed to find successful test run");
};
}
213 changes: 0 additions & 213 deletions ci/style.rs

This file was deleted.

2 changes: 1 addition & 1 deletion ci/style.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if [ -n "${CI:-}" ]; then
check="--check"
fi

rustc ci/style.rs && ./style src
cargo test --manifest-path libc-test/Cargo.toml --test style -- --nocapture

command -v rustfmt
rustfmt -V
Expand Down
16 changes: 16 additions & 0 deletions libc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ A test crate for the libc crate.
[dependencies]
libc = { path = "..", version = "1.0.0-alpha.1", 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 @@ -90,3 +96,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 0f9f8c9

Please sign in to comment.