mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 12:32:10 +01:00
predefined_quit and std_instant now run on a small Rust lexer instead of a masked line search: - Character and byte literals such as '"' no longer open a phantom string that hides the code after them. - A Quit call split by tabs or a line break around :: is reported, a different name such as MyPredefinedMenuItem or quitter is not, and a use muda::PredefinedMenuItem as X rename is followed. - std_instant reports std::time::Instant, <m>::Instant where a use binds m to std::time, and each use that imports std::time::Instant or std::time::*. A line that also names web_time is still reported, and an unrelated time::Instant is not. - cfg attributes are read from code, not from strings or comments, and evaluated for wasm32-unknown-unknown (all, any, not, unix, windows, target_* keys). An outer attribute covers the item after it, an inner one the rest of its module. A predicate that depends on a feature or test is checked. wgsl_float_literals skips integer literals, reports hexadecimal floats above f32::MAX, and checks an h-suffixed float against 65504. rust_sources does not follow symbolic links, skips folders that hold a CACHEDIR.TAG file, no longer drops every file when the root itself is under a folder named tests, and returns an error for a file over 8 MiB, more than 256 MiB in all or more than 50,000 files. rust_sources_with_limits takes other limits. The tests use a scratch folder that is never reused and is removed when a test panics. The crate is not published, and the root README lists it.
100 lines
3.4 KiB
Rust
100 lines
3.4 KiB
Rust
#![cfg(not(target_arch = "wasm32"))]
|
|
|
|
use craft_testkit::{SourceLimits, rust_sources_with_limits};
|
|
use std::io::ErrorKind;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// A scratch folder that this test made and removes on drop, also when the test panics.
|
|
struct ScratchDir(PathBuf);
|
|
|
|
impl ScratchDir {
|
|
fn new(tag: &str) -> Self {
|
|
let base = std::env::temp_dir();
|
|
for n in 0..1000u32 {
|
|
let path = base.join(format!("craft_testkit_limits_{tag}_{}_{n}", std::process::id()));
|
|
match std::fs::create_dir(&path) {
|
|
Ok(()) => return Self(path),
|
|
Err(e) if e.kind() == ErrorKind::AlreadyExists => continue,
|
|
Err(e) => panic!("create scratch folder {}: {e}", path.display()),
|
|
}
|
|
}
|
|
panic!("no free scratch folder name for {tag}");
|
|
}
|
|
|
|
fn path(&self) -> &Path {
|
|
&self.0
|
|
}
|
|
|
|
fn write(&self, relative: &str, content: &str) {
|
|
let path = self.0.join(relative);
|
|
if let Some(parent) = path.parent() {
|
|
std::fs::create_dir_all(parent).expect("create parent folder");
|
|
}
|
|
std::fs::write(path, content).expect("write file");
|
|
}
|
|
}
|
|
|
|
impl Drop for ScratchDir {
|
|
fn drop(&mut self) {
|
|
let _ = std::fs::remove_dir_all(&self.0);
|
|
}
|
|
}
|
|
|
|
fn three_files(tag: &str) -> ScratchDir {
|
|
let dir = ScratchDir::new(tag);
|
|
dir.write("src/a.rs", "0123456789");
|
|
dir.write("src/b.rs", "0123456789");
|
|
dir.write("src/c.rs", "0123456789");
|
|
dir
|
|
}
|
|
|
|
#[test]
|
|
fn defaults_are_documented_values() {
|
|
let limits = SourceLimits::default();
|
|
assert_eq!(limits.max_file_bytes, 8 << 20);
|
|
assert_eq!(limits.max_total_bytes, 256 << 20);
|
|
assert_eq!(limits.max_files, 50_000);
|
|
}
|
|
|
|
#[test]
|
|
fn files_within_the_limits_are_read() {
|
|
let dir = three_files("within");
|
|
let limits = SourceLimits { max_file_bytes: 10, max_total_bytes: 30, max_files: 3 };
|
|
let sources = rust_sources_with_limits(dir.path(), limits).expect("within limits");
|
|
assert_eq!(sources.len(), 3);
|
|
assert!(sources.iter().all(|(_, content)| content == "0123456789"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_file_over_the_size_limit_is_an_error() {
|
|
let dir = three_files("file");
|
|
let limits = SourceLimits { max_file_bytes: 9, ..SourceLimits::default() };
|
|
let err = rust_sources_with_limits(dir.path(), limits).expect_err("file over the limit");
|
|
assert_eq!(err.kind(), ErrorKind::FileTooLarge);
|
|
}
|
|
|
|
#[test]
|
|
fn files_over_the_total_limit_are_an_error() {
|
|
let dir = three_files("total");
|
|
let limits = SourceLimits { max_total_bytes: 29, ..SourceLimits::default() };
|
|
let err = rust_sources_with_limits(dir.path(), limits).expect_err("total over the limit");
|
|
assert_eq!(err.kind(), ErrorKind::QuotaExceeded);
|
|
}
|
|
|
|
#[test]
|
|
fn more_files_than_the_count_limit_is_an_error() {
|
|
let dir = three_files("count");
|
|
let limits = SourceLimits { max_files: 2, ..SourceLimits::default() };
|
|
let err = rust_sources_with_limits(dir.path(), limits).expect_err("too many files");
|
|
assert_eq!(err.kind(), ErrorKind::QuotaExceeded);
|
|
}
|
|
|
|
#[test]
|
|
fn files_the_filter_skips_do_not_count_against_the_limits() {
|
|
let dir = three_files("skipped");
|
|
dir.write("src/tests.rs", &"x".repeat(100));
|
|
dir.write("tests/big.rs", &"x".repeat(100));
|
|
let limits = SourceLimits { max_file_bytes: 10, max_total_bytes: 30, max_files: 3 };
|
|
assert_eq!(rust_sources_with_limits(dir.path(), limits).expect("skipped files are not read").len(), 3);
|
|
}
|