Skip to content

Commit 00ca24d

Browse files
committed
ci: Add tests to list, remove, resolve and init
Signed-off-by: Jarsop <[email protected]>
1 parent 7ff8291 commit 00ca24d

File tree

6 files changed

+237
-5
lines changed

6 files changed

+237
-5
lines changed

src/config.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use anyhow::{bail, Context, Result};
2+
#[cfg(test)]
3+
use serial_test::serial;
24
use std::env;
35
use std::fs;
46
use std::path::PathBuf;
@@ -40,3 +42,36 @@ pub fn rad_resolve_symlinks() -> bool {
4042
None => false,
4143
}
4244
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use super::*;
49+
50+
#[test]
51+
#[serial]
52+
fn echo() {
53+
assert!(!rad_no_echo());
54+
}
55+
56+
#[test]
57+
#[serial]
58+
fn no_echo() {
59+
std::env::set_var("_RAD_NO_ECHO", "1");
60+
assert!(rad_no_echo());
61+
std::env::set_var("_RAD_NO_ECHO", "");
62+
}
63+
64+
#[test]
65+
#[serial]
66+
fn resolve_symlinks() {
67+
std::env::set_var("_RAD_RESOLVE_SYMLINKS", "1");
68+
assert!(rad_resolve_symlinks());
69+
std::env::set_var("_RAD_RESOLVE_SYMLINKS", "0");
70+
}
71+
72+
#[test]
73+
#[serial]
74+
fn no_resolve_symlinks() {
75+
assert!(!rad_resolve_symlinks());
76+
}
77+
}

src/subcommand/add.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl RadSubCmdRunnable for Add {
4141
}
4242

4343
#[cfg(test)]
44-
mod tests_add {
44+
mod tests {
4545
use super::*;
4646

4747
#[test]

src/subcommand/init/mod.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
mod bash;
22
mod zsh;
33

4+
#[cfg(test)]
5+
use crate::fixture;
6+
use crate::subcommand::RadSubCmdRunnable;
47
use anyhow::{Context, Result};
8+
#[cfg(test)]
9+
use serial_test::serial;
510
use structopt::clap::arg_enum;
611
use structopt::StructOpt;
712

@@ -19,7 +24,7 @@ pub struct Init {
1924
cmd: String,
2025
}
2126

22-
impl super::RadSubCmdRunnable for Init {
27+
impl RadSubCmdRunnable for Init {
2328
fn run(&self) -> Result<String> {
2429
let stdout = io::stdout();
2530
let mut handle = stdout.lock();
@@ -41,3 +46,30 @@ arg_enum! {
4146
zsh,
4247
}
4348
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use super::*;
53+
54+
#[test]
55+
#[serial]
56+
fn zsh() {
57+
let subcmd = fixture::create_subcmd(Init {
58+
shell: Shell::zsh,
59+
cmd: String::from("rad"),
60+
});
61+
let res = subcmd.run();
62+
assert!(res.is_ok());
63+
}
64+
65+
#[test]
66+
#[serial]
67+
fn bash() {
68+
let subcmd = fixture::create_subcmd(Init {
69+
shell: Shell::bash,
70+
cmd: String::from("rad"),
71+
});
72+
let res = subcmd.run();
73+
assert!(res.is_ok());
74+
}
75+
}

src/subcommand/list.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
use crate::config;
2+
#[cfg(test)]
3+
use crate::fixture;
4+
use crate::subcommand::RadSubCmdRunnable;
25
use anyhow::{Context, Result};
36
use rualdlib::Aliases;
7+
#[cfg(test)]
8+
use serial_test::serial;
49
use structopt::StructOpt;
510

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

10-
impl super::RadSubCmdRunnable for List {
15+
impl RadSubCmdRunnable for List {
1116
fn run(&self) -> Result<String> {
1217
let aliases_dir = config::rad_aliases_dir().with_context(|| "fail to list aliases")?;
1318
let aliases = Aliases::open(aliases_dir).with_context(|| "fail to list aliases")?;
@@ -19,3 +24,43 @@ impl super::RadSubCmdRunnable for List {
1924
Ok(res)
2025
}
2126
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[test]
33+
#[serial]
34+
fn no_aliases() {
35+
let subcmd = fixture::create_subcmd(List {});
36+
let res = subcmd.run();
37+
assert!(res.is_ok());
38+
assert_eq!(res.unwrap(), "No aliases found\n");
39+
}
40+
41+
#[test]
42+
#[serial]
43+
fn alias() {
44+
let mut subcmd = fixture::create_subcmd(List {});
45+
subcmd.use_config(toml::toml![test = "test"]);
46+
let res = subcmd.run();
47+
assert!(res.is_ok());
48+
assert_eq!(res.unwrap(), "Aliases:\n\n\t'test' => 'test'\n");
49+
}
50+
51+
#[test]
52+
#[serial]
53+
fn aliases() {
54+
let mut subcmd = fixture::create_subcmd(List {});
55+
subcmd.use_config(toml::toml![
56+
test = "test"
57+
test2 = "test2"
58+
]);
59+
let res = subcmd.run();
60+
assert!(res.is_ok());
61+
assert_eq!(
62+
res.unwrap(),
63+
"Aliases:\n\n\t'test' => 'test'\n\t'test2' => 'test2'\n"
64+
);
65+
}
66+
}

src/subcommand/remove.rs

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use crate::config;
2+
#[cfg(test)]
3+
use crate::fixture;
4+
use crate::subcommand::RadSubCmdRunnable;
25
use anyhow::{Context, Result};
36
use rualdlib::Aliases;
7+
#[cfg(test)]
8+
use serial_test::serial;
49
use structopt::StructOpt;
510

611
/// Remove alias
@@ -10,7 +15,7 @@ pub struct Remove {
1015
pub alias: Vec<String>,
1116
}
1217

13-
impl super::RadSubCmdRunnable for Remove {
18+
impl RadSubCmdRunnable for Remove {
1419
fn run(&self) -> Result<String> {
1520
let aliases_dir = config::rad_aliases_dir().with_context(|| "fail to remove alias")?;
1621
let mut aliases = Aliases::open(aliases_dir).with_context(|| "fail to remove alias")?;
@@ -25,3 +30,46 @@ impl super::RadSubCmdRunnable for Remove {
2530
Ok("".into())
2631
}
2732
}
33+
34+
#[cfg(test)]
35+
mod tests {
36+
use super::*;
37+
38+
#[test]
39+
#[serial]
40+
fn not_existing_alias() {
41+
let subcmd = fixture::create_subcmd(Remove {
42+
alias: vec![String::from("test")],
43+
});
44+
let res = subcmd.run();
45+
assert!(res.is_err());
46+
assert_eq!(res.unwrap_err().to_string(), "fail to remove alias 'test'");
47+
}
48+
49+
#[test]
50+
#[serial]
51+
fn existing_alias() {
52+
let mut subcmd = fixture::create_subcmd(Remove {
53+
alias: vec![String::from("test")],
54+
});
55+
subcmd.use_config(toml::toml!(test = "test"));
56+
let res = subcmd.run();
57+
assert!(res.is_ok());
58+
assert_eq!(res.unwrap(), "");
59+
}
60+
61+
#[test]
62+
#[serial]
63+
fn existing_aliases() {
64+
let mut subcmd = fixture::create_subcmd(Remove {
65+
alias: vec![String::from("test"), String::from("test2")],
66+
});
67+
subcmd.use_config(toml::toml!(
68+
test = "test"
69+
test2 = "test2"
70+
));
71+
let res = subcmd.run();
72+
assert!(res.is_ok());
73+
assert_eq!(res.unwrap(), "");
74+
}
75+
}

src/subcommand/resolve.rs

+73-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
use crate::config;
2+
#[cfg(test)]
3+
use crate::fixture;
4+
use crate::subcommand::RadSubCmdRunnable;
25
use crate::utils;
36
use anyhow::{Context, Result};
47
use rualdlib::Aliases;
8+
#[cfg(test)]
9+
use serial_test::serial;
510
use std::ffi::OsStr;
611
use std::path::{Path, PathBuf};
12+
#[cfg(test)]
13+
use std::str::FromStr;
714
use structopt::StructOpt;
815

916
/// Resolve alias
@@ -13,7 +20,7 @@ pub struct Resolve {
1320
pub path: PathBuf,
1421
}
1522

16-
impl super::RadSubCmdRunnable for Resolve {
23+
impl RadSubCmdRunnable for Resolve {
1724
fn run(&self) -> Result<String> {
1825
let aliases_dir = config::rad_aliases_dir()
1926
.with_context(|| format!("fail to resolve alias path '{}'", self.path.display()))?;
@@ -62,3 +69,68 @@ fn resolve_alias<P: AsRef<Path>>(path: P, aliases: Aliases) -> Result<PathBuf> {
6269
};
6370
Ok(result)
6471
}
72+
73+
#[cfg(test)]
74+
mod tests {
75+
use super::*;
76+
77+
#[test]
78+
#[serial]
79+
fn existing_alias() {
80+
let current_dir = std::env::current_dir().unwrap();
81+
let mut subcmd = fixture::create_subcmd(Resolve {
82+
path: PathBuf::from_str("test").unwrap(),
83+
});
84+
subcmd.use_config(toml::toml![test = "not-existing-path"]);
85+
let res = subcmd.run();
86+
let expected = format!(
87+
"could not resolve path: {}/not-existing-path",
88+
current_dir.to_str().unwrap()
89+
);
90+
assert!(res.is_err());
91+
assert_eq!(res.unwrap_err().to_string(), expected);
92+
}
93+
94+
#[test]
95+
#[serial]
96+
fn existing_path_without_alias() {
97+
let current_dir = std::env::current_dir().unwrap();
98+
let subcmd = fixture::create_subcmd(Resolve {
99+
path: PathBuf::from_str(current_dir.to_str().unwrap()).unwrap(),
100+
});
101+
let res = subcmd.run();
102+
let expected = format!("{}\n", current_dir.to_str().unwrap());
103+
assert!(res.is_ok());
104+
assert_eq!(res.unwrap(), expected);
105+
}
106+
107+
#[test]
108+
#[serial]
109+
fn not_existing_path_without_alias() {
110+
let current_dir = std::env::current_dir().unwrap();
111+
let subcmd = fixture::create_subcmd(Resolve {
112+
path: PathBuf::from_str("test").unwrap(),
113+
});
114+
let res = subcmd.run();
115+
let expected = format!(
116+
"could not resolve path: {}/test",
117+
current_dir.to_str().unwrap()
118+
);
119+
assert!(res.is_err());
120+
assert_eq!(res.unwrap_err().to_string(), expected);
121+
}
122+
123+
#[test]
124+
#[serial]
125+
fn tild_alias() {
126+
let home_dir = std::env::var("HOME").unwrap();
127+
let mut subcmd = fixture::create_subcmd(Resolve {
128+
path: PathBuf::from_str("home").unwrap(),
129+
});
130+
subcmd.use_config(toml::toml![home = "~"]);
131+
let res = subcmd.run();
132+
let expected = format!("{}\n", home_dir);
133+
assert!(res.is_ok());
134+
assert_eq!(res.unwrap(), expected);
135+
}
136+
}

0 commit comments

Comments
 (0)