summaryrefslogtreecommitdiffstats
path: root/vendor/sysinfo/tests/code_checkers
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/sysinfo/tests/code_checkers
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/sysinfo/tests/code_checkers')
-rw-r--r--vendor/sysinfo/tests/code_checkers/docs.rs135
-rw-r--r--vendor/sysinfo/tests/code_checkers/headers.rs59
-rw-r--r--vendor/sysinfo/tests/code_checkers/mod.rs49
-rw-r--r--vendor/sysinfo/tests/code_checkers/signals.rs64
-rw-r--r--vendor/sysinfo/tests/code_checkers/utils.rs49
5 files changed, 356 insertions, 0 deletions
diff --git a/vendor/sysinfo/tests/code_checkers/docs.rs b/vendor/sysinfo/tests/code_checkers/docs.rs
new file mode 100644
index 000000000..918e64bb0
--- /dev/null
+++ b/vendor/sysinfo/tests/code_checkers/docs.rs
@@ -0,0 +1,135 @@
+// Take a look at the license at the top of the repository in the LICENSE file.
+
+use super::utils::{show_error, TestResult};
+use std::ffi::OsStr;
+use std::path::Path;
+
+fn to_correct_name(s: &str) -> String {
+ let mut out = String::with_capacity(s.len());
+
+ for c in s.chars() {
+ if c.is_uppercase() {
+ if !out.is_empty() {
+ out.push('_');
+ }
+ out.push_str(c.to_lowercase().to_string().as_str());
+ } else {
+ out.push(c);
+ }
+ }
+ out
+}
+
+fn check_md_doc_path(p: &Path, md_line: &str, ty_line: &str) -> bool {
+ let parts = md_line.split("/").collect::<Vec<_>>();
+ if let Some(md_name) = parts.last().and_then(|n| n.split(".md").next()) {
+ if let Some(name) = ty_line
+ .split_whitespace()
+ .filter(|s| !s.is_empty())
+ .skip(2)
+ .next()
+ {
+ if let Some(name) = name
+ .split('<')
+ .next()
+ .and_then(|n| n.split('{').next())
+ .and_then(|n| n.split('(').next())
+ .and_then(|n| n.split(';').next())
+ {
+ let correct = to_correct_name(name);
+ if correct.as_str() == md_name {
+ return true;
+ }
+ show_error(
+ p,
+ &format!(
+ "Invalid markdown file name `{}`, should have been `{}`",
+ md_name, correct
+ ),
+ );
+ return false;
+ }
+ }
+ show_error(p, &format!("Cannot extract type name from `{}`", ty_line));
+ } else {
+ show_error(p, &format!("Cannot extract md name from `{}`", md_line));
+ }
+ return false;
+}
+
+fn check_doc_comments_before(p: &Path, lines: &[&str], start: usize) -> bool {
+ let mut found_docs = false;
+
+ for pos in (0..start).rev() {
+ let trimmed = lines[pos].trim();
+ if trimmed.starts_with("///") {
+ if !lines[start].trim().starts_with("pub enum ThreadStatus {") {
+ show_error(
+ p,
+ &format!(
+ "Types should use common documentation by using `#[doc = include_str!(` \
+ and by putting the markdown file in the `md_doc` folder instead of `{}`",
+ &lines[pos],
+ ),
+ );
+ return false;
+ }
+ return true;
+ } else if trimmed.starts_with("#[doc = include_str!(") {
+ found_docs = true;
+ if !check_md_doc_path(p, trimmed, lines[start]) {
+ return false;
+ }
+ } else if !trimmed.starts_with("#[") && !trimmed.starts_with("//") {
+ break;
+ }
+ }
+ if !found_docs {
+ show_error(
+ p,
+ &format!(
+ "Missing documentation for public item: `{}` (if it's not supposed to be a public \
+ item, use `pub(crate)` instead)",
+ lines[start],
+ ),
+ );
+ return false;
+ }
+ true
+}
+
+pub fn check_docs(content: &str, p: &Path) -> TestResult {
+ let mut res = TestResult {
+ nb_tests: 0,
+ nb_errors: 0,
+ };
+
+ // No need to check if we are in the `src` folder or if we are in a `ffi.rs` file.
+ if p.parent().unwrap().file_name().unwrap() == OsStr::new("src")
+ || p.file_name().unwrap() == OsStr::new("ffi.rs")
+ {
+ return res;
+ }
+ let lines = content.lines().collect::<Vec<_>>();
+
+ for pos in 1..lines.len() {
+ let line = lines[pos];
+ let trimmed = line.trim();
+ if trimmed.starts_with("//!") {
+ show_error(p, "There shouln't be inner doc comments (`//!`)");
+ res.nb_tests += 1;
+ res.nb_errors += 1;
+ continue;
+ } else if !line.starts_with("pub fn ")
+ && !trimmed.starts_with("pub struct ")
+ && !trimmed.starts_with("pub enum ")
+ {
+ continue;
+ }
+ res.nb_tests += 1;
+ if !check_doc_comments_before(p, &lines, pos) {
+ res.nb_errors += 1;
+ }
+ }
+ res
+}
diff --git a/vendor/sysinfo/tests/code_checkers/headers.rs b/vendor/sysinfo/tests/code_checkers/headers.rs
new file mode 100644
index 000000000..ac7a34ddb
--- /dev/null
+++ b/vendor/sysinfo/tests/code_checkers/headers.rs
@@ -0,0 +1,59 @@
+// Take a look at the license at the top of the repository in the LICENSE file.
+
+use super::utils::{show_error, TestResult};
+use std::path::Path;
+
+pub fn check_license_header(content: &str, p: &Path) -> TestResult {
+ let mut lines = content.lines();
+ let next = lines.next();
+ let header = "// Take a look at the license at the top of the repository in the LICENSE file.";
+
+ match next {
+ Some(s) if s == header => {
+ let next = lines.next();
+ match next {
+ Some("") => TestResult {
+ nb_tests: 1,
+ nb_errors: 0,
+ },
+ Some(s) => {
+ show_error(
+ p,
+ &format!("Expected empty line after license header, found `{}`", s),
+ );
+ TestResult {
+ nb_tests: 1,
+ nb_errors: 1,
+ }
+ }
+ None => {
+ show_error(p, "This file should very likely not exist...");
+ TestResult {
+ nb_tests: 1,
+ nb_errors: 1,
+ }
+ }
+ }
+ }
+ Some(s) => {
+ show_error(
+ p,
+ &format!(
+ "Expected license header at the top of the file (`{}`), found: `{}`",
+ header, s
+ ),
+ );
+ TestResult {
+ nb_tests: 1,
+ nb_errors: 1,
+ }
+ }
+ None => {
+ show_error(p, "This (empty?) file should very likely not exist...");
+ TestResult {
+ nb_tests: 1,
+ nb_errors: 1,
+ }
+ }
+ }
+}
diff --git a/vendor/sysinfo/tests/code_checkers/mod.rs b/vendor/sysinfo/tests/code_checkers/mod.rs
new file mode 100644
index 000000000..f6012da95
--- /dev/null
+++ b/vendor/sysinfo/tests/code_checkers/mod.rs
@@ -0,0 +1,49 @@
+// Take a look at the license at the top of the repository in the LICENSE file.
+
+mod docs;
+mod headers;
+mod signals;
+mod utils;
+
+use std::path::Path;
+use utils::TestResult;
+
+const CHECKS: &[(fn(&str, &Path) -> TestResult, &[&str])] = &[
+ (headers::check_license_header, &["src", "tests", "examples"]),
+ (signals::check_signals, &["src"]),
+ (docs::check_docs, &["src"]),
+];
+
+fn handle_tests(res: &mut [TestResult]) {
+ utils::read_dirs(
+ &["benches", "examples", "src", "tests"],
+ &mut |p: &Path, c: &str| {
+ if let Some(first) = p.iter().next().and_then(|first| first.to_str()) {
+ for (pos, (check, filter)) in CHECKS.iter().enumerate() {
+ if filter.contains(&first) {
+ res[pos] += check(c, p);
+ }
+ }
+ }
+ },
+ );
+}
+
+#[test]
+fn code_checks() {
+ let mut res = Vec::new();
+
+ for _ in CHECKS {
+ res.push(TestResult {
+ nb_tests: 0,
+ nb_errors: 0,
+ });
+ }
+
+ handle_tests(&mut res);
+
+ for r in res {
+ assert_eq!(r.nb_errors, 0);
+ assert_ne!(r.nb_tests, 0);
+ }
+}
diff --git a/vendor/sysinfo/tests/code_checkers/signals.rs b/vendor/sysinfo/tests/code_checkers/signals.rs
new file mode 100644
index 000000000..68e47f76e
--- /dev/null
+++ b/vendor/sysinfo/tests/code_checkers/signals.rs
@@ -0,0 +1,64 @@
+// Take a look at the license at the top of the repository in the LICENSE file.
+
+use super::utils::{show_error, TestResult};
+use std::path::Path;
+
+fn check_supported_signals_decl<'a>(lines: &mut impl Iterator<Item = &'a str>, p: &Path) -> usize {
+ for line in lines {
+ let trimmed = line.trim();
+ if trimmed.starts_with("const SUPPORTED_SIGNALS: &'static [Signal]") {
+ if trimmed != "const SUPPORTED_SIGNALS: &'static [Signal] = supported_signals();" {
+ show_error(
+ p,
+ "SystemExt::SUPPORTED_SIGNALS should be declared using `supported_signals()`",
+ );
+ return 1;
+ }
+ break;
+ }
+ }
+ 0
+}
+
+fn check_kill_decl<'a>(lines: &mut impl Iterator<Item = &'a str>, p: &Path) -> usize {
+ let mut errors = 0;
+
+ while let Some(line) = lines.next() {
+ let trimmed = line.trim();
+ if trimmed.starts_with("fn kill(") {
+ show_error(p, "`ProcessExt::kill` should not be reimplemented!");
+ errors += 1;
+ } else if trimmed.starts_with("fn kill_with(") {
+ if let Some(line) = lines.next() {
+ let trimmed = line.trim();
+ if trimmed.ends_with("::system::convert_signal(signal)?;") || trimmed == "None" {
+ continue;
+ } else {
+ show_error(p, "`ProcessExt::kill_with` should use `convert_signal`");
+ errors += 1;
+ }
+ }
+ }
+ }
+ errors
+}
+
+pub fn check_signals(content: &str, p: &Path) -> TestResult {
+ let mut lines = content.lines();
+ let mut res = TestResult {
+ nb_tests: 0,
+ nb_errors: 0,
+ };
+
+ while let Some(line) = lines.next() {
+ let trimmed = line.trim();
+ if trimmed.starts_with("impl SystemExt for System {") {
+ res.nb_tests += 1;
+ res.nb_errors += check_supported_signals_decl(&mut lines, p);
+ } else if trimmed.starts_with("impl ProcessExt for Process {") {
+ res.nb_tests += 1;
+ res.nb_errors += check_kill_decl(&mut lines, p);
+ }
+ }
+ res
+}
diff --git a/vendor/sysinfo/tests/code_checkers/utils.rs b/vendor/sysinfo/tests/code_checkers/utils.rs
new file mode 100644
index 000000000..dadbc0a54
--- /dev/null
+++ b/vendor/sysinfo/tests/code_checkers/utils.rs
@@ -0,0 +1,49 @@
+// Take a look at the license at the top of the repository in the LICENSE file.
+
+use std::fs::{self, File};
+use std::io::Read;
+use std::path::Path;
+
+pub struct TestResult {
+ pub nb_tests: usize,
+ pub nb_errors: usize,
+}
+
+impl std::ops::AddAssign for TestResult {
+ fn add_assign(&mut self, other: Self) {
+ self.nb_tests += other.nb_tests;
+ self.nb_errors += other.nb_errors;
+ }
+}
+
+pub fn read_dirs<P: AsRef<Path>, F: FnMut(&Path, &str)>(dirs: &[P], callback: &mut F) {
+ for dir in dirs {
+ read_dir(dir, callback);
+ }
+}
+
+fn read_dir<P: AsRef<Path>, F: FnMut(&Path, &str)>(dir: P, callback: &mut F) {
+ for entry in fs::read_dir(dir).expect("read_dir failed") {
+ let entry = entry.expect("entry failed");
+ let path = entry.path();
+ if path.is_dir() {
+ read_dir(path, callback);
+ } else {
+ let content = read_file(&path);
+ callback(&path, &content);
+ }
+ }
+}
+
+fn read_file<P: AsRef<Path>>(p: P) -> String {
+ let mut f = File::open(p).expect("read_file::open failed");
+ let mut content =
+ String::with_capacity(f.metadata().map(|m| m.len() as usize + 1).unwrap_or(0));
+ f.read_to_string(&mut content)
+ .expect("read_file::read_to_end failed");
+ content
+}
+
+pub fn show_error(p: &Path, err: &str) {
+ eprintln!("=> [{}]: {}", p.display(), err);
+}