#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;
pub const BIN: &str = env!("CARGO_BIN_EXE_atlassian-cli");
pub struct Sandbox {
home: TempDir,
}
impl Sandbox {
pub fn new() -> Self {
Self {
home: TempDir::new().expect("failed to create a scratch home"),
}
}
pub fn home(&self) -> &Path {
self.home.path()
}
pub fn config_dir(&self) -> PathBuf {
self.home.path().join("config-dir")
}
pub fn path(&self, rel: &str) -> PathBuf {
self.home.path().join(rel)
}
pub fn cli(&self) -> Command {
let mut cmd = self.clean_command();
cmd.env("ATLASSIAN_CLI_CONFIG_DIR", self.config_dir());
cmd
}
pub fn bare_cli(&self) -> Command {
self.clean_command()
}
fn clean_command(&self) -> Command {
const PREFIXES: [&str; 4] = ["ATLASSIAN_", "BITBUCKET_", "JIRA_", "CONFLUENCE_"];
let mut cmd = Command::new(BIN);
for (key, _) in std::env::vars_os() {
let name = key.to_string_lossy().to_uppercase();
if PREFIXES.iter().any(|p| name.starts_with(p)) {
cmd.env_remove(&key);
}
}
cmd.env("HOME", self.home.path())
.env_remove("XDG_CONFIG_HOME");
cmd
}
}
impl Default for Sandbox {
fn default() -> Self {
Self::new()
}
}