diff options
author | Matan Kushner <hello@matchai.me> | 2019-06-06 13:18:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-06 13:18:00 +0100 |
commit | 8239fbd12befad1126e677fa083ce73947d74d8c (patch) | |
tree | f2d7a82f92c5f696e77a70fea39c7b56ac6b3875 /src | |
parent | bb220bb5a056b28c6457d2f5c75c53184a2cbe77 (diff) |
Refactor integration tests (#71)
- Create subcommands to be able to print modules independently
- `starship prompt` will print the full prompt
- `starship module <MODULE_NAME>` will print a specific module
e.g. `starship module python`
- Added `--path` flag to print the prompt or modules without being in a specific directory
- Added `--status` flag to provide the status of the last command, instead of requiring it as an argument
- Refactored integration tests to be end-to-end tests, since there was no way in integration tests to set the environment variables for a specific command, which was required for the `username` module
- Moved e2e tests to `tests/testsuite` to allow for a single binary to be built
- Tests will build/run faster
- No more false positives for unused functions
- Added tests for `username`
- Removed codecov + tarpaulin 😢
Diffstat (limited to 'src')
-rw-r--r-- | src/context.rs | 10 | ||||
-rw-r--r-- | src/main.rs | 67 | ||||
-rw-r--r-- | src/modules/character.rs | 2 | ||||
-rw-r--r-- | src/print.rs | 11 |
4 files changed, 73 insertions, 17 deletions
diff --git a/src/context.rs b/src/context.rs index b86bd0708..e0e7e286c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -15,11 +15,15 @@ pub struct Context<'a> { impl<'a> Context<'a> { pub fn new(arguments: ArgMatches) -> Context { - let current_dir = env::current_dir().expect("Unable to identify current directory."); - Context::new_with_dir(arguments, current_dir) + // Retreive the "path" flag. If unavailable, use the current directory instead. + let path = arguments + .value_of("path") + .map(From::from) + .unwrap_or_else(|| env::current_dir().expect("Unable to identify current directory.")); + + Context::new_with_dir(arguments, path) } - #[allow(dead_code)] pub fn new_with_dir<T>(arguments: ArgMatches, dir: T) -> Context where T: Into<PathBuf>, diff --git a/src/main.rs b/src/main.rs index 1f69d4ed9..4707cd816 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,36 +1,77 @@ #[macro_use] extern crate clap; -extern crate ansi_term; -extern crate battery; -extern crate dirs; -extern crate git2; -extern crate pretty_env_logger; - mod context; mod module; mod modules; mod print; mod segment; -use clap::{App, Arg}; +use clap::{App, Arg, SubCommand}; fn main() { pretty_env_logger::init(); - let args = App::new("Starship") + let matches = App::new("Starship") .about("The cross-shell prompt for astronauts. ✨🚀") // pull the version number from Cargo.toml .version(crate_version!()) // pull the authors from Cargo.toml .author(crate_authors!()) .after_help("https://github.com/matchai/starship") - .arg( - Arg::with_name("status_code") - .help("The status code of the previously run command") - .required(true), + .subcommand( + SubCommand::with_name("prompt") + .about("Prints the full starship prompt") + .arg( + Arg::with_name("status_code") + .short("s") + .long("status") + .value_name("STATUS_CODE") + .help("The status code of the previously run command") + .takes_value(true), + ) + .arg( + Arg::with_name("path") + .short("p") + .long("path") + .value_name("PATH") + .help("The path that the prompt should render for ($PWD by default)") + .takes_value(true), + ), + ) + .subcommand( + SubCommand::with_name("module") + .about("Prints a specific prompt module") + .arg( + Arg::with_name("name") + .help("The name of the module to be printed") + .required(true), + ) + .arg( + Arg::with_name("status_code") + .short("s") + .long("status") + .value_name("STATUS_CODE") + .help("The status code of the previously run command") + .takes_value(true), + ) + .arg( + Arg::with_name("path") + .short("p") + .long("path") + .value_name("PATH") + .help("The path the prompt should render for ($PWD by default)") + .takes_value(true), + ), ) .get_matches(); - print::prompt(args); + match matches.subcommand() { + ("prompt", Some(sub_m)) => print::prompt(sub_m.clone()), + ("module", Some(sub_m)) => { + let module_name = sub_m.value_of("name").expect("Module name missing."); + print::module(module_name, sub_m.clone()); + } + _ => {} + } } diff --git a/src/modules/character.rs b/src/modules/character.rs index 2a9334894..1dfc27991 100644 --- a/src/modules/character.rs +++ b/src/modules/character.rs @@ -20,7 +20,7 @@ pub fn segment(context: &Context) -> Option<Module> { let symbol = module.new_segment("symbol", PROMPT_CHAR); let arguments = &context.arguments; - if arguments.value_of("status_code").unwrap() == "0" { + if arguments.value_of("status_code").unwrap_or("0") == "0" { symbol.set_style(color_success.bold()); } else { symbol.set_style(color_failure.bold()); diff --git a/src/print.rs b/src/print.rs index 53442f23f..71d453e34 100644 --- a/src/print.rs +++ b/src/print.rs @@ -46,3 +46,14 @@ pub fn prompt(args: ArgMatches) { // Print all remaining modules printable.for_each(|module| write!(handle, "{}", module).unwrap()); } + +pub fn module(module_name: &str, args: ArgMatches) { + let context = Context::new(args); + + // If the module returns `None`, print an empty string + let module = modules::handle(module_name, &context) + .map(|m| m.to_string()) + .unwrap_or_default(); + + print!("{}", module); +} |