2019-07-02 20:12:53 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::{Path, PathBuf};
|
2019-09-04 04:20:22 +00:00
|
|
|
use std::process::Command;
|
2019-09-05 16:45:04 +00:00
|
|
|
use std::{env, fs, io, process};
|
2019-06-06 12:18:00 +00:00
|
|
|
|
2019-07-02 20:12:53 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref MANIFEST_DIR: &'static Path = Path::new(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
pub static ref FIXTURES_DIR: PathBuf = MANIFEST_DIR.join("tests/fixtures");
|
|
|
|
static ref EMPTY_CONFIG: PathBuf = MANIFEST_DIR.join("empty_config.toml");
|
|
|
|
}
|
|
|
|
|
2019-07-27 22:25:13 +00:00
|
|
|
/// Render the full starship prompt
|
|
|
|
pub fn render_prompt() -> process::Command {
|
2019-06-06 12:18:00 +00:00
|
|
|
let mut command = process::Command::new("./target/debug/starship");
|
2019-07-02 20:12:53 +00:00
|
|
|
|
|
|
|
command
|
|
|
|
.arg("prompt")
|
|
|
|
.env_clear()
|
|
|
|
.env("PATH", env!("PATH")) // Provide the $PATH variable so that external programs are runnable
|
|
|
|
.env("STARSHIP_CONFIG", EMPTY_CONFIG.as_os_str());
|
2019-06-06 12:18:00 +00:00
|
|
|
|
|
|
|
command
|
|
|
|
}
|
|
|
|
|
2019-07-27 22:25:13 +00:00
|
|
|
/// Render a specific starship module by name
|
2019-06-06 12:18:00 +00:00
|
|
|
pub fn render_module(module_name: &str) -> process::Command {
|
2019-09-05 16:45:04 +00:00
|
|
|
let binary = fs::canonicalize("./target/debug/starship").unwrap();
|
|
|
|
let mut command = process::Command::new(binary);
|
2019-07-02 20:12:53 +00:00
|
|
|
|
|
|
|
command
|
|
|
|
.arg("module")
|
|
|
|
.arg(module_name)
|
|
|
|
.env_clear()
|
|
|
|
.env("PATH", env!("PATH")) // Provide the $PATH variable so that external programs are runnable
|
|
|
|
.env("STARSHIP_CONFIG", EMPTY_CONFIG.as_os_str());
|
2019-06-06 12:18:00 +00:00
|
|
|
|
|
|
|
command
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a temporary directory with full access permissions (rwxrwxrwt).
|
|
|
|
pub fn new_tempdir() -> io::Result<tempfile::TempDir> {
|
|
|
|
// Using `tempfile::TempDir` directly creates files on macOS within
|
|
|
|
// "/var/folders", which provides us with restricted permissions (rwxr-xr-x)
|
|
|
|
tempfile::tempdir_in("/tmp")
|
|
|
|
}
|
2019-07-02 20:12:53 +00:00
|
|
|
|
2019-09-04 04:20:22 +00:00
|
|
|
/// Create a repo from the fixture to be used in git module tests
|
|
|
|
pub fn create_fixture_repo() -> io::Result<std::path::PathBuf> {
|
|
|
|
let fixture_repo_dir = new_tempdir()?.path().join("fixture");
|
|
|
|
let fixture = env::current_dir()?.join("tests/fixtures/rocket.bundle");
|
|
|
|
|
|
|
|
Command::new("git")
|
|
|
|
.args(&["config", "--global", "user.email", "starship@example.com"])
|
|
|
|
.output()?;
|
|
|
|
|
|
|
|
Command::new("git")
|
|
|
|
.args(&["config", "--global", "user.name", "starship"])
|
|
|
|
.output()?;
|
|
|
|
|
|
|
|
Command::new("git")
|
|
|
|
.args(&[
|
|
|
|
"clone",
|
|
|
|
"-b",
|
|
|
|
"master",
|
|
|
|
&fixture.to_str().unwrap(),
|
|
|
|
fixture_repo_dir.to_str().unwrap(),
|
|
|
|
])
|
|
|
|
.output()?;
|
|
|
|
|
|
|
|
Ok(fixture_repo_dir)
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:12:53 +00:00
|
|
|
/// Extends `std::process::Command` with methods for testing
|
|
|
|
pub trait TestCommand {
|
|
|
|
fn use_config(&mut self, toml: toml::value::Value) -> &mut process::Command;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestCommand for process::Command {
|
|
|
|
/// Create a configuration file with the provided TOML and use it
|
|
|
|
fn use_config(&mut self, toml: toml::value::Value) -> &mut process::Command {
|
|
|
|
// Create a persistent config file in a tempdir
|
|
|
|
let (mut config_file, config_path) =
|
|
|
|
tempfile::NamedTempFile::new().unwrap().keep().unwrap();
|
|
|
|
write!(config_file, "{}", toml.to_string()).unwrap();
|
|
|
|
|
|
|
|
// Set that newly-created file as the config for the prompt instance
|
|
|
|
self.env("STARSHIP_CONFIG", config_path)
|
|
|
|
}
|
|
|
|
}
|