Skip to content

Commit

Permalink
ci: Add tests to list, remove, resolve and init
Browse files Browse the repository at this point in the history
Signed-off-by: Jarsop <[email protected]>
  • Loading branch information
Jarsop committed Sep 28, 2020
1 parent 7ff8291 commit 00ca24d
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 5 deletions.
35 changes: 35 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::{bail, Context, Result};
#[cfg(test)]
use serial_test::serial;
use std::env;
use std::fs;
use std::path::PathBuf;
Expand Down Expand Up @@ -40,3 +42,36 @@ pub fn rad_resolve_symlinks() -> bool {
None => false,
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[serial]
fn echo() {
assert!(!rad_no_echo());
}

#[test]
#[serial]
fn no_echo() {
std::env::set_var("_RAD_NO_ECHO", "1");
assert!(rad_no_echo());
std::env::set_var("_RAD_NO_ECHO", "");
}

#[test]
#[serial]
fn resolve_symlinks() {
std::env::set_var("_RAD_RESOLVE_SYMLINKS", "1");
assert!(rad_resolve_symlinks());
std::env::set_var("_RAD_RESOLVE_SYMLINKS", "0");
}

#[test]
#[serial]
fn no_resolve_symlinks() {
assert!(!rad_resolve_symlinks());
}
}
2 changes: 1 addition & 1 deletion src/subcommand/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl RadSubCmdRunnable for Add {
}

#[cfg(test)]
mod tests_add {
mod tests {
use super::*;

#[test]
Expand Down
34 changes: 33 additions & 1 deletion src/subcommand/init/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
mod bash;
mod zsh;

#[cfg(test)]
use crate::fixture;
use crate::subcommand::RadSubCmdRunnable;
use anyhow::{Context, Result};
#[cfg(test)]
use serial_test::serial;
use structopt::clap::arg_enum;
use structopt::StructOpt;

Expand All @@ -19,7 +24,7 @@ pub struct Init {
cmd: String,
}

impl super::RadSubCmdRunnable for Init {
impl RadSubCmdRunnable for Init {
fn run(&self) -> Result<String> {
let stdout = io::stdout();
let mut handle = stdout.lock();
Expand All @@ -41,3 +46,30 @@ arg_enum! {
zsh,
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[serial]
fn zsh() {
let subcmd = fixture::create_subcmd(Init {
shell: Shell::zsh,
cmd: String::from("rad"),
});
let res = subcmd.run();
assert!(res.is_ok());
}

#[test]
#[serial]
fn bash() {
let subcmd = fixture::create_subcmd(Init {
shell: Shell::bash,
cmd: String::from("rad"),
});
let res = subcmd.run();
assert!(res.is_ok());
}
}
47 changes: 46 additions & 1 deletion src/subcommand/list.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use crate::config;
#[cfg(test)]
use crate::fixture;
use crate::subcommand::RadSubCmdRunnable;
use anyhow::{Context, Result};
use rualdlib::Aliases;
#[cfg(test)]
use serial_test::serial;
use structopt::StructOpt;

/// Add new path alias
#[derive(Debug, StructOpt)]
pub struct List {}

impl super::RadSubCmdRunnable for List {
impl RadSubCmdRunnable for List {
fn run(&self) -> Result<String> {
let aliases_dir = config::rad_aliases_dir().with_context(|| "fail to list aliases")?;
let aliases = Aliases::open(aliases_dir).with_context(|| "fail to list aliases")?;
Expand All @@ -19,3 +24,43 @@ impl super::RadSubCmdRunnable for List {
Ok(res)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[serial]
fn no_aliases() {
let subcmd = fixture::create_subcmd(List {});
let res = subcmd.run();
assert!(res.is_ok());
assert_eq!(res.unwrap(), "No aliases found\n");
}

#[test]
#[serial]
fn alias() {
let mut subcmd = fixture::create_subcmd(List {});
subcmd.use_config(toml::toml![test = "test"]);
let res = subcmd.run();
assert!(res.is_ok());
assert_eq!(res.unwrap(), "Aliases:\n\n\t'test' => 'test'\n");
}

#[test]
#[serial]
fn aliases() {
let mut subcmd = fixture::create_subcmd(List {});
subcmd.use_config(toml::toml![
test = "test"
test2 = "test2"
]);
let res = subcmd.run();
assert!(res.is_ok());
assert_eq!(
res.unwrap(),
"Aliases:\n\n\t'test' => 'test'\n\t'test2' => 'test2'\n"
);
}
}
50 changes: 49 additions & 1 deletion src/subcommand/remove.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use crate::config;
#[cfg(test)]
use crate::fixture;
use crate::subcommand::RadSubCmdRunnable;
use anyhow::{Context, Result};
use rualdlib::Aliases;
#[cfg(test)]
use serial_test::serial;
use structopt::StructOpt;

/// Remove alias
Expand All @@ -10,7 +15,7 @@ pub struct Remove {
pub alias: Vec<String>,
}

impl super::RadSubCmdRunnable for Remove {
impl RadSubCmdRunnable for Remove {
fn run(&self) -> Result<String> {
let aliases_dir = config::rad_aliases_dir().with_context(|| "fail to remove alias")?;
let mut aliases = Aliases::open(aliases_dir).with_context(|| "fail to remove alias")?;
Expand All @@ -25,3 +30,46 @@ impl super::RadSubCmdRunnable for Remove {
Ok("".into())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[serial]
fn not_existing_alias() {
let subcmd = fixture::create_subcmd(Remove {
alias: vec![String::from("test")],
});
let res = subcmd.run();
assert!(res.is_err());
assert_eq!(res.unwrap_err().to_string(), "fail to remove alias 'test'");
}

#[test]
#[serial]
fn existing_alias() {
let mut subcmd = fixture::create_subcmd(Remove {
alias: vec![String::from("test")],
});
subcmd.use_config(toml::toml!(test = "test"));
let res = subcmd.run();
assert!(res.is_ok());
assert_eq!(res.unwrap(), "");
}

#[test]
#[serial]
fn existing_aliases() {
let mut subcmd = fixture::create_subcmd(Remove {
alias: vec![String::from("test"), String::from("test2")],
});
subcmd.use_config(toml::toml!(
test = "test"
test2 = "test2"
));
let res = subcmd.run();
assert!(res.is_ok());
assert_eq!(res.unwrap(), "");
}
}
74 changes: 73 additions & 1 deletion src/subcommand/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use crate::config;
#[cfg(test)]
use crate::fixture;
use crate::subcommand::RadSubCmdRunnable;
use crate::utils;
use anyhow::{Context, Result};
use rualdlib::Aliases;
#[cfg(test)]
use serial_test::serial;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
#[cfg(test)]
use std::str::FromStr;
use structopt::StructOpt;

/// Resolve alias
Expand All @@ -13,7 +20,7 @@ pub struct Resolve {
pub path: PathBuf,
}

impl super::RadSubCmdRunnable for Resolve {
impl RadSubCmdRunnable for Resolve {
fn run(&self) -> Result<String> {
let aliases_dir = config::rad_aliases_dir()
.with_context(|| format!("fail to resolve alias path '{}'", self.path.display()))?;
Expand Down Expand Up @@ -62,3 +69,68 @@ fn resolve_alias<P: AsRef<Path>>(path: P, aliases: Aliases) -> Result<PathBuf> {
};
Ok(result)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[serial]
fn existing_alias() {
let current_dir = std::env::current_dir().unwrap();
let mut subcmd = fixture::create_subcmd(Resolve {
path: PathBuf::from_str("test").unwrap(),
});
subcmd.use_config(toml::toml![test = "not-existing-path"]);
let res = subcmd.run();
let expected = format!(
"could not resolve path: {}/not-existing-path",
current_dir.to_str().unwrap()
);
assert!(res.is_err());
assert_eq!(res.unwrap_err().to_string(), expected);
}

#[test]
#[serial]
fn existing_path_without_alias() {
let current_dir = std::env::current_dir().unwrap();
let subcmd = fixture::create_subcmd(Resolve {
path: PathBuf::from_str(current_dir.to_str().unwrap()).unwrap(),
});
let res = subcmd.run();
let expected = format!("{}\n", current_dir.to_str().unwrap());
assert!(res.is_ok());
assert_eq!(res.unwrap(), expected);
}

#[test]
#[serial]
fn not_existing_path_without_alias() {
let current_dir = std::env::current_dir().unwrap();
let subcmd = fixture::create_subcmd(Resolve {
path: PathBuf::from_str("test").unwrap(),
});
let res = subcmd.run();
let expected = format!(
"could not resolve path: {}/test",
current_dir.to_str().unwrap()
);
assert!(res.is_err());
assert_eq!(res.unwrap_err().to_string(), expected);
}

#[test]
#[serial]
fn tild_alias() {
let home_dir = std::env::var("HOME").unwrap();
let mut subcmd = fixture::create_subcmd(Resolve {
path: PathBuf::from_str("home").unwrap(),
});
subcmd.use_config(toml::toml![home = "~"]);
let res = subcmd.run();
let expected = format!("{}\n", home_dir);
assert!(res.is_ok());
assert_eq!(res.unwrap(), expected);
}
}

0 comments on commit 00ca24d

Please sign in to comment.