Add functionality to partition buildable / not buildable attributes

This commit is contained in:
Graham Christensen 2018-03-10 08:58:06 -05:00
parent 7045302a1f
commit 24763528b6
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C

View file

@ -97,6 +97,50 @@ impl Nix {
return n;
}
pub fn safely_partition_instantiable_attrs(
&self,
nixpkgs: &Path,
file: &str,
attrs: Vec<String>,
) -> (Vec<String>, Vec<String>) {
attrs
.into_iter()
.partition(|attr| {
self.safely_instantiate_attrs(
nixpkgs,
file,
vec![attr.clone()]
).is_ok()
})
}
pub fn safely_instantiate_attrs(
&self,
nixpkgs: &Path,
file: &str,
attrs: Vec<String>,
) -> Result<File, File> {
let cmd = self.safely_instantiate_attrs_cmd(nixpkgs, file, attrs);
return self.run(cmd, true);
}
pub fn safely_instantiate_attrs_cmd(
&self,
nixpkgs: &Path,
file: &str,
attrs: Vec<String>,
) -> Command {
let mut attrargs: Vec<String> = Vec::with_capacity(3 + (attrs.len() * 2));
attrargs.push(file.to_owned());
for attr in attrs {
attrargs.push(String::from("-A"));
attrargs.push(attr);
}
return self.safe_command(Operation::Instantiate, nixpkgs, attrargs);
}
pub fn safely_build_attrs(
&self,
nixpkgs: &Path,
@ -235,6 +279,12 @@ mod tests {
return cwd;
}
fn individual_eval_path() -> PathBuf {
let mut cwd = env::current_dir().unwrap();
cwd.push(Path::new("./test-srcs/eval-mixed-failure"));
return cwd;
}
#[derive(Debug)]
enum Expect {
Pass,
@ -509,6 +559,63 @@ mod tests {
);
}
#[test]
fn partition_instantiable_attributes() {
let nix = nix();
let ret: (Vec<String>, Vec<String>) = nix.safely_partition_instantiable_attrs(
individual_eval_path().as_path(),
"default.nix",
vec![
String::from("fails-instantiation"),
String::from("passes-instantiation"),
String::from("missing-attr"),
],
);
assert_eq!(ret.0, vec!["passes-instantiation"]);
assert_eq!(ret.1, vec!["fails-instantiation", "missing-attr"]);
}
#[test]
fn safely_instantiate_attrs_failure() {
let nix = nix();
let ret: Result<File, File> = nix.safely_instantiate_attrs(
individual_eval_path().as_path(),
"default.nix",
vec![String::from("fails-instantiation")],
);
assert_run(
ret,
Expect::Fail,
vec![
"You just can't",
"assertion failed",
],
);
}
#[test]
fn safely_instantiate_attrs_success() {
let nix = nix();
let ret: Result<File, File> = nix.safely_instantiate_attrs(
individual_eval_path().as_path(),
"default.nix",
vec![String::from("passes-instantiation")],
);
assert_run(
ret,
Expect::Pass,
vec![
"-passes-instantiation.drv"
],
);
}
#[test]
fn strict_sandboxing() {
let ret: Result<File, File> = nix().safely_build_attrs(