Extract calculate to its own fuction

This commit is contained in:
Graham Christensen 2019-01-01 17:00:41 -05:00
parent 31f4ec74b4
commit 293ca1f9a6
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C

View file

@ -1,4 +1,6 @@
use ofborg::nix::Nix;
use std::collections::HashMap;
use std::path::Path;
#[derive(Deserialize, Debug, Eq, PartialEq)]
struct ImpactedMaintainers(HashMap<Maintainer, Vec<Package>>);
@ -17,12 +19,34 @@ impl<'a> From<&'a str> for Package {
}
}
impl ImpactedMaintainers {
pub fn calculate(
nix: &Nix,
checkout: &Path,
paths: &[String],
attributes: &[Vec<&str>],
) -> ImpactedMaintainers {
let pathstr = serde_json::to_string(&paths).unwrap();
let attrstr = serde_json::to_string(&attributes).unwrap();
let mut argstrs: HashMap<&str, &str> = HashMap::new();
argstrs.insert("changedattrsjson", &attrstr);
argstrs.insert("changedpathsjson", &pathstr);
let ret = nix
.safely_evaluate_expr_cmd(&checkout, include_str!("./maintainers.nix"), argstrs)
.output()
.unwrap();
serde_json::from_str(&String::from_utf8(ret.stdout).unwrap()).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use checkout::cached_cloner;
use clone::GitClonable;
use ofborg::nix::Nix;
use ofborg::test_scratch::TestScratch;
use std::env;
use std::ffi::OsStr;
@ -76,26 +100,11 @@ mod tests {
working_co.checkout_ref(&OsStr::new(&hash)).unwrap();
let pathstr = serde_json::to_string(&paths).unwrap();
let attrstr = serde_json::to_string(&attributes).unwrap();
let mut argstrs: HashMap<&str, &str> = HashMap::new();
argstrs.insert("changedattrsjson", &attrstr);
argstrs.insert("changedpathsjson", &pathstr);
let remote = env::var("NIX_REMOTE").unwrap_or("".to_owned());
let nix = Nix::new("x86_64-linux".to_owned(), remote, 1800, None);
let ret = nix
.safely_evaluate_expr_cmd(
&working_co.clone_to(),
include_str!("./maintainers.nix"),
argstrs,
)
.output()
.expect(":)");
let parsed: ImpactedMaintainers =
serde_json::from_str(&String::from_utf8(ret.stdout).unwrap()).unwrap();
let parsed =
ImpactedMaintainers::calculate(&nix, &working_co.clone_to(), &paths, &attributes);
let mut expect = ImpactedMaintainers(HashMap::new());
expect.0.insert(