#![allow(dead_code)]
use std::path::{Path, PathBuf};
use assert_cmd::Command;
use tempfile::TempDir;
pub fn roba_in(dir: &Path) -> Command {
let mut cmd = Command::cargo_bin("roba").expect("cargo-built roba binary");
cmd.args([
"-C",
dir.to_str().expect("utf-8 tempdir path"),
"--model",
"haiku",
]);
cmd
}
pub fn fresh_dir() -> tempfile::TempDir {
tempfile::tempdir().expect("create test tempdir")
}
pub struct Project {
_root: TempDir,
_config_home: TempDir,
pub root: PathBuf,
pub config_home: PathBuf,
}
pub fn project() -> ProjectBuilder {
ProjectBuilder::default()
}
#[derive(Default)]
pub struct ProjectBuilder {
user_config: Option<String>,
project_toml: Option<String>,
files: Vec<(String, String)>,
}
impl ProjectBuilder {
pub fn user_config(mut self, toml: &str) -> Self {
self.user_config = Some(toml.to_string());
self
}
pub fn project_toml(mut self, toml: &str) -> Self {
self.project_toml = Some(toml.to_string());
self
}
pub fn file(mut self, rel: &str, content: &str) -> Self {
self.files.push((rel.to_string(), content.to_string()));
self
}
pub fn build(self) -> Project {
let root = tempfile::tempdir().expect("project root tempdir");
let config_home = tempfile::tempdir().expect("config home tempdir");
std::fs::create_dir_all(root.path().join(".git")).expect("mkdir .git");
if let Some(toml) = &self.user_config {
std::fs::write(config_home.path().join("roba.toml"), toml).expect("write user config");
}
if let Some(toml) = &self.project_toml {
std::fs::write(root.path().join("roba.toml"), toml).expect("write project roba.toml");
}
for (rel, content) in &self.files {
let p = root.path().join(rel);
if let Some(parent) = p.parent() {
std::fs::create_dir_all(parent).expect("mkdir for fixture file");
}
std::fs::write(&p, content).expect("write fixture file");
}
let root_path = root.path().to_path_buf();
let config_home_path = config_home.path().to_path_buf();
Project {
_root: root,
_config_home: config_home,
root: root_path,
config_home: config_home_path,
}
}
}
impl Project {
pub fn roba(&self) -> Command {
let mut cmd = Command::cargo_bin("roba").expect("cargo-built roba binary");
cmd.arg("-C")
.arg(&self.root)
.env("HOME", &self.config_home)
.env("XDG_CONFIG_HOME", &self.config_home)
.env_remove("ROBA_PROFILE");
cmd
}
}