extern crate tempdir;
extern crate toml_edit;
use std::ffi::OsStr;
use std::io::prelude::*;
use std::{fs, process};
pub fn clone_out_test(source: &str) -> (tempdir::TempDir, String) {
let tmpdir =
tempdir::TempDir::new("cargo-edit-test").expect("failed to construct temporary directory");
fs::copy(source, tmpdir.path().join("Cargo.toml"))
.unwrap_or_else(|err| panic!("could not copy test manifest: {}", err));
let path = tmpdir
.path()
.join("Cargo.toml")
.to_str()
.unwrap()
.to_string()
.clone();
(tmpdir, path)
}
pub fn execute_command<S>(command: &[S], manifest: &str)
where
S: AsRef<OsStr>,
{
let subcommand_name = &command[0].as_ref().to_str().unwrap();
let call = process::Command::new(&format!("target/debug/cargo-{}", subcommand_name))
.args(command)
.arg(format!("--manifest-path={}", manifest))
.env("CARGO_IS_TEST", "1")
.output()
.unwrap();
if !call.status.success() {
println!("Status code: {:?}", call.status);
println!("STDOUT: {}", String::from_utf8_lossy(&call.stdout));
println!("STDERR: {}", String::from_utf8_lossy(&call.stderr));
panic!("cargo-{} failed to execute", subcommand_name)
}
}
pub fn get_toml(manifest_path: &str) -> toml_edit::Document {
let mut f = fs::File::open(manifest_path).unwrap();
let mut s = String::new();
f.read_to_string(&mut s).unwrap();
s.parse().expect("toml parse error")
}