further test suite

This commit is contained in:
nora 2023-04-04 12:50:35 +02:00
parent c70d6fa26f
commit ff26987129
4 changed files with 76 additions and 10 deletions

31
Cargo.lock generated
View file

@ -2,6 +2,15 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "aho-corasick"
version = "0.7.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.65" version = "1.0.65"
@ -34,10 +43,12 @@ dependencies = [
"ctrlc", "ctrlc",
"fs_extra", "fs_extra",
"libc", "libc",
"once_cell",
"owo-colors", "owo-colors",
"prettyplease", "prettyplease",
"proc-macro2", "proc-macro2",
"quote", "quote",
"regex",
"rustfix", "rustfix",
"serde", "serde",
"serde_json", "serde_json",
@ -241,6 +252,12 @@ dependencies = [
"regex-automata", "regex-automata",
] ]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]] [[package]]
name = "nix" name = "nix"
version = "0.26.2" version = "0.26.2"
@ -265,9 +282,9 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.15.0" version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]] [[package]]
name = "os_str_bytes" name = "os_str_bytes"
@ -354,10 +371,12 @@ dependencies = [
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.6.0" version = "1.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d"
dependencies = [ dependencies = [
"aho-corasick",
"memchr",
"regex-syntax", "regex-syntax",
] ]
@ -372,9 +391,9 @@ dependencies = [
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.27" version = "0.6.29"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]] [[package]]
name = "remove_dir_all" name = "remove_dir_all"

View file

@ -33,3 +33,5 @@ libc = "0.2.138"
[dev-dependencies] [dev-dependencies]
fs_extra = "1.3.0" fs_extra = "1.3.0"
once_cell = "1.17.1"
regex = "1.7.3"

View file

@ -1,7 +1,7 @@
use std::collecions::HashMap; use std::collections::HashMap;
/// ~REQUIRE-DELETED /// ~REQUIRE-DELETED user-fn
fn user(map: HashMap<(), ()>) { fn user(mut map: HashMap<(), ()>) {
map.insert((), ()); map.insert((), ());
} }

View file

@ -1,4 +1,6 @@
use anyhow::{ensure, Context, Result}; use anyhow::{ensure, Context, Result};
use once_cell::sync::Lazy;
use regex::Regex;
use std::process::Command; use std::process::Command;
use std::{ use std::{
fs, fs,
@ -7,6 +9,7 @@ use std::{
}; };
#[test] #[test]
#[ignore = "unfinished"]
fn full_tests() -> Result<()> { fn full_tests() -> Result<()> {
let path = Path::new(file!()) let path = Path::new(file!())
.canonicalize()? .canonicalize()?
@ -40,6 +43,9 @@ fn build(path: &Path) -> Result<()> {
let proj_dir = tempdir.path().join(proj_dir).canonicalize()?; let proj_dir = tempdir.path().join(proj_dir).canonicalize()?;
let start_roots = get_roots(&proj_dir).context("getting initial roots")?;
eprintln!("Roots: {:?}", start_roots);
let mut cmd = Command::new(cargo_minimize); let mut cmd = Command::new(cargo_minimize);
cmd.current_dir(&proj_dir); cmd.current_dir(&proj_dir);
@ -48,7 +54,46 @@ fn build(path: &Path) -> Result<()> {
let out = cmd.output().context("spawning cargo-minimize")?; let out = cmd.output().context("spawning cargo-minimize")?;
let stderr = String::from_utf8(out.stderr).unwrap(); let stderr = String::from_utf8(out.stderr).unwrap();
// ensure!(out.status.success(), "Command failed:\n{stderr}",); ensure!(out.status.success(), "Command failed:\n{stderr}");
let required_deleted = get_required_deleted(&proj_dir)?;
ensure!(
required_deleted.len() > 0,
"Some REQUIRE-DELETED have not been deleted: {required_deleted:?}"
);
Ok(()) Ok(())
} }
fn get_roots(path: &Path) -> Result<Vec<String>> {
static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"~MINIMIZE-ROOT ([\w\-_]+)").unwrap());
grep(path, &REGEX)
}
fn get_required_deleted(path: &Path) -> Result<Vec<String>> {
static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"~REQUIRE-DELETED ([\w\-_]+)").unwrap());
grep(path, &REGEX)
}
fn grep(path: &Path, regex: &Regex) -> Result<Vec<String>> {
let mut results = Vec::new();
let walk = walkdir::WalkDir::new(path);
for entry in walk {
let entry = entry?;
if !entry.metadata()?.is_file() {
continue;
}
let src = fs::read_to_string(entry.path())?;
let captures = regex.captures_iter(&src);
for cap in captures {
let root_name = cap.get(1).unwrap();
results.push(root_name.as_str().to_owned());
}
}
Ok(results)
}