From e73ffbf1e59eb05fe8ffe73ce4e1833295c588a5 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 3 Feb 2021 22:01:09 +0800 Subject: Add cargo file tidy test --- xtask/src/lib.rs | 33 +++++++++++++++++++++------------ xtask/tests/tidy.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 12 deletions(-) (limited to 'xtask') diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index 16b06b853..b19985fb2 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -38,19 +38,13 @@ pub fn rust_files() -> impl Iterator { rust_files_in(&project_root().join("crates")) } -pub fn rust_files_in(path: &Path) -> impl Iterator { - let iter = WalkDir::new(path); - return iter - .into_iter() - .filter_entry(|e| !is_hidden(e)) - .map(|e| e.unwrap()) - .filter(|e| !e.file_type().is_dir()) - .map(|e| e.into_path()) - .filter(|path| path.extension().map(|it| it == "rs").unwrap_or(false)); +pub fn cargo_files() -> impl Iterator { + files_in(&project_root(), "toml") + .filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false)) +} - fn is_hidden(entry: &DirEntry) -> bool { - entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false) - } +pub fn rust_files_in(path: &Path) -> impl Iterator { + files_in(path, "rs") } pub fn run_rustfmt(mode: Mode) -> Result<()> { @@ -120,3 +114,18 @@ fn date_iso() -> Result { fn is_release_tag(tag: &str) -> bool { tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit()) } + +fn files_in(path: &Path, ext: &'static str) -> impl Iterator { + let iter = WalkDir::new(path); + return iter + .into_iter() + .filter_entry(|e| !is_hidden(e)) + .map(|e| e.unwrap()) + .filter(|e| !e.file_type().is_dir()) + .map(|e| e.into_path()) + .filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false)); + + fn is_hidden(entry: &DirEntry) -> bool { + entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false) + } +} diff --git a/xtask/tests/tidy.rs b/xtask/tests/tidy.rs index 9a6933b09..cb83e07fd 100644 --- a/xtask/tests/tidy.rs +++ b/xtask/tests/tidy.rs @@ -5,6 +5,7 @@ use std::{ use xshell::{cmd, read_file}; use xtask::{ + cargo_files, codegen::{self, Mode}, project_root, run_rustfmt, rust_files, }; @@ -93,6 +94,32 @@ fn rust_files_are_tidy() { tidy_docs.finish(); } +#[test] +fn cargo_files_are_tidy() { + for cargo in cargo_files() { + let mut section = None; + for (line_no, text) in read_file(&cargo).unwrap().lines().enumerate() { + let text = text.trim(); + if text.starts_with("[") { + section = Some(text); + continue; + } + if !section.map(|it| it.starts_with("[dependencies")).unwrap_or(false) { + continue; + } + let text: String = text.split_whitespace().collect(); + if text.contains("path=") && !text.contains("version") { + panic!( + "\ncargo internal dependencies should have version.\n\ + {}:{}\n", + cargo.display(), + line_no + 1 + ) + } + } + } +} + #[test] fn check_merge_commits() { let stdout = cmd!("git rev-list --merges --invert-grep --author 'bors\\[bot\\]' HEAD~19..") -- cgit v1.2.3