diff options
Diffstat (limited to 'tests/common/mod.rs')
| -rw-r--r-- | tests/common/mod.rs | 86 |
1 files changed, 27 insertions, 59 deletions
diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 23049ea..fa0beea 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -7,6 +7,7 @@ #![allow(dead_code)] use std::collections::HashMap; +use std::fmt::Write as _; use std::path::{Path, PathBuf}; use std::sync::{LazyLock, Mutex, OnceLock}; @@ -22,11 +23,7 @@ pub struct Env { impl Env { pub fn new() -> Self { let tmp = tempfile::tempdir().expect("tempdir"); - let cfg = Config { - root: tmp.path().to_path_buf(), - signing_key_id: "TESTKEY".into(), - ..Config::default() - }; + let cfg = Config { root: tmp.path().to_path_buf(), signing_key_id: "TESTKEY".into(), ..Config::default() }; Self::with_cfg(tmp, cfg) } @@ -164,8 +161,7 @@ impl Env { }) .collect(); use rusqlite::OptionalExtension; - Ok(c.query_row(&sql, rusqlite::params_from_iter(refs.iter()), |r| r.get::<_, i64>(0)) - .optional()?) + Ok(c.query_row(&sql, rusqlite::params_from_iter(refs.iter()), |r| r.get::<_, i64>(0)).optional()?) }) .unwrap() } @@ -183,8 +179,7 @@ impl Env { }) .collect(); use rusqlite::OptionalExtension; - Ok(c.query_row(&sql, rusqlite::params_from_iter(refs.iter()), |r| r.get::<_, String>(0)) - .optional()?) + Ok(c.query_row(&sql, rusqlite::params_from_iter(refs.iter()), |r| r.get::<_, String>(0)).optional()?) }) .unwrap() } @@ -229,10 +224,7 @@ pub fn corpus_root() -> PathBuf { } pub fn shim_dir() -> PathBuf { - Path::new(&env!("CARGO_BIN_EXE_makechrootpkg")) - .parent() - .unwrap() - .to_path_buf() + Path::new(&env!("CARGO_BIN_EXE_makechrootpkg")).parent().unwrap().to_path_buf() } pub fn env_with_fixture(name: &str) -> (Env, Fixture) { @@ -339,12 +331,7 @@ struct Corpus { fn read_corpus(name: &str) -> Corpus { let src = corpus_root().join(name); - let mut corpus = Corpus { - recipe_files: Vec::new(), - data_files: Vec::new(), - upstream: false, - upstream2: false, - }; + let mut corpus = Corpus { recipe_files: Vec::new(), data_files: Vec::new(), upstream: false, upstream2: false }; for entry in std::fs::read_dir(&src).unwrap() { let path = entry.unwrap().path(); let fname = path.file_name().unwrap().to_string_lossy().to_string(); @@ -380,14 +367,11 @@ static PRISTINE: LazyLock<Mutex<HashMap<String, Pristine>>> = LazyLock::new(|| M static CACHE_ROOT: OnceLock<tempfile::TempDir> = OnceLock::new(); fn pristine_fixture(name: &str) -> Pristine { - let mut cache = PRISTINE.lock().unwrap_or_else(|e| e.into_inner()); + let mut cache = PRISTINE.lock().unwrap_or_else(std::sync::PoisonError::into_inner); if let Some(found) = cache.get(name) { return found.clone(); } - let root = CACHE_ROOT - .get_or_init(|| tempfile::tempdir().expect("fixture cache")) - .path() - .to_path_buf(); + let root = CACHE_ROOT.get_or_init(|| tempfile::tempdir().expect("fixture cache")).path().to_path_buf(); let corpus = read_corpus(name); let data = root.join(format!("{name}/fixture")); @@ -411,21 +395,15 @@ fn pristine_fixture(name: &str) -> Pristine { } if corpus.upstream { make_git_repo(&upstream, &[("README", "upstream\n")], "upstream: initial"); - if std::fs::read_to_string(corpus_root().join(name).join("PKGBUILD")) - .unwrap_or_default() - .contains("#tag=") - { + if std::fs::read_to_string(corpus_root().join(name).join("PKGBUILD")).unwrap_or_default().contains("#tag=") { git(&upstream, &["tag", "v1"]); } } if corpus.upstream2 { make_git_repo(&upstream2, &[("README", "second upstream\n")], "upstream: initial"); } - let recipe: Vec<(&str, String)> = corpus - .recipe_files - .iter() - .map(|(n, b)| (n.as_str(), substitute(b, &subs))) - .collect(); + let recipe: Vec<(&str, String)> = + corpus.recipe_files.iter().map(|(n, b)| (n.as_str(), substitute(b, &subs))).collect(); make_git_repo(&aur, &recipe.iter().map(|(n, b)| (*n, b.as_str())).collect::<Vec<_>>(), "initial"); let pristine = Pristine { @@ -460,9 +438,8 @@ fn copy_tree(from: &Path, to: &Path) { if entry.file_type().unwrap().is_dir() { copy_tree(&path, &target); } else { - std::fs::copy(&path, &target).unwrap_or_else(|e| { - panic!("copying {} to {}: {e}", path.display(), target.display()) - }); + std::fs::copy(&path, &target) + .unwrap_or_else(|e| panic!("copying {} to {}: {e}", path.display(), target.display())); } } } @@ -485,7 +462,9 @@ impl Fixture { for line in text.lines() { let key = line.split('=').next().unwrap_or("").to_string(); match changes.iter().find(|(k, _)| *k == key) { - Some((_, v)) => out.push_str(&format!("{key}={v}\n")), + Some((_, v)) => { + let _ = writeln!(out, "{key}={v}"); + } None => out.push_str(line), } out.push('\n'); @@ -583,11 +562,7 @@ pub fn git(dir: &Path, args: &[&str]) -> String { .env("GIT_COMMITTER_EMAIL", "t@example.invalid") .output() .expect("git"); - assert!( - out.status.success(), - "git {args:?} failed: {}", - String::from_utf8_lossy(&out.stderr) - ); + assert!(out.status.success(), "git {args:?} failed: {}", String::from_utf8_lossy(&out.stderr)); String::from_utf8_lossy(&out.stdout).trim().to_string() } @@ -626,7 +601,7 @@ impl Web { .0 .iter() .find(|(k, _)| k == "set-cookie") - .and_then(|(_, v)| v.split(';').next().map(|s| s.to_string())) + .and_then(|(_, v)| v.split(';').next().map(std::string::ToString::to_string)) .unwrap_or_default(); let body = web.get("/"); web.csrf = extract(&body.body, "name=\"csrf\" content=\"", "\"").unwrap_or_default(); @@ -660,7 +635,7 @@ impl Web { Err(ureq::Error::Status(_, r)) => r, Err(e) => panic!("GET {path}: {e}"), }; - let cookie = r.header("set-cookie").map(|s| s.to_string()).unwrap_or_default(); + let cookie = r.header("set-cookie").map_or_default(std::string::ToString::to_string); let mut headers = vec![("set-cookie".to_string(), cookie)]; for name in ["content-type", "content-encoding", "location"] { if let Some(v) = r.header(name) { @@ -672,17 +647,17 @@ impl Web { pub fn get(&self, path: &str) -> Page { let resp = ureq::get(&self.url(path)).set("Cookie", &self.cookie).call(); - self.finish(resp, path) + Self::finish(resp, path) } pub fn post(&self, path: &str, form: &[(&str, &str)]) -> Page { let resp = ureq::post(&self.url(path)).set("Cookie", &self.cookie).send_form(form); - self.finish(resp, path) + Self::finish(resp, path) } pub fn post_bare(&self, path: &str, form: &[(&str, &str)]) -> Page { let resp = ureq::post(&self.url(path)).set("Cookie", &self.cookie).send_form(form); - self.finish(resp, path) + Self::finish(resp, path) } pub fn raw_request(&self, method: &str, path: &str, headers: &[(&str, String)], body: Option<&str>) -> Raw { @@ -691,12 +666,12 @@ impl Web { let mut sock = std::net::TcpStream::connect(&addr).unwrap(); let mut req = format!("{method} {path} HTTP/1.1\r\nHost: {addr}\r\nConnection: close\r\n"); for (k, v) in headers { - req.push_str(&format!("{k}: {v}\r\n")); + let _ = writeln!(req, "{k}: {v}\r"); } match body { Some(b) => { req.push_str("Content-Type: application/x-www-form-urlencoded\r\n"); - req.push_str(&format!("Content-Length: {}\r\n", b.len())); + let _ = writeln!(req, "Content-Length: {}\r", b.len()); req.push_str("\r\n"); req.push_str(b); } @@ -718,7 +693,7 @@ impl Web { raw } - fn finish(&self, resp: Result<ureq::Response, ureq::Error>, path: &str) -> Page { + fn finish(resp: Result<ureq::Response, ureq::Error>, path: &str) -> Page { match resp { Ok(r) => Page { status: r.status(), body: r.into_string().unwrap_or_default() }, Err(ureq::Error::Status(code, r)) => Page { status: code, body: r.into_string().unwrap_or_default() }, @@ -735,10 +710,7 @@ pub struct Raw { impl Raw { fn parse(raw: &[u8]) -> Self { - let split = raw - .windows(4) - .position(|w| w == b"\r\n\r\n") - .unwrap_or(raw.len()); + let split = raw.windows(4).position(|w| w == b"\r\n\r\n").unwrap_or(raw.len()); let head = String::from_utf8_lossy(&raw[..split]).to_string(); let mut lines = head.lines(); let status = lines.next().and_then(|l| l.split_whitespace().nth(1)).and_then(|s| s.parse().ok()).unwrap_or(0); @@ -778,9 +750,5 @@ pub fn extract(hay: &str, open: &str, close: &str) -> Option<String> { } fn unescape(s: &str) -> String { - s.replace("&", "&") - .replace(""", "\"") - .replace("<", "<") - .replace(">", ">") - .replace("'", "'") + s.replace("&", "&").replace(""", "\"").replace("<", "<").replace(">", ">").replace("'", "'") } |
