Skip to content

Commit 0231865

Browse files
committed
ci: adding test cases
adding test cases.
1 parent 500a11a commit 0231865

5 files changed

Lines changed: 115 additions & 10 deletions

File tree

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct CommandOption {
2525
}
2626

2727
// Function to get the path of the config file; else create it
28-
pub(crate) fn get_config_file_path() -> Result<PathBuf, String> {
28+
pub fn get_config_file_path() -> Result<PathBuf, String> {
2929
let base_dirs = BaseDirs::new().ok_or("Could not get base directories")?;
3030

3131
// Get the config directory and append the file name

src/csv.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ pub fn import_commands(config: &mut Config, changes_made: &mut bool) {
6767
}
6868

6969
// Function to read commands from a CSV file
70-
pub(crate) fn read_commands_from_csv<P: AsRef<Path>>(
71-
path: P,
72-
) -> anyhow::Result<Vec<CommandOption>> {
70+
pub fn read_commands_from_csv<P: AsRef<Path>>(path: P) -> anyhow::Result<Vec<CommandOption>> {
7371
let mut reader = csv::Reader::from_path(path)?;
7472
let mut commands = Vec::new();
7573

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod config;
2+
pub mod csv;
3+
pub mod menu_edit;
4+
pub mod menu_main;
5+
pub mod utils;

src/main.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
mod config;
2-
mod csv;
3-
mod menu_edit;
4-
mod menu_main;
5-
mod utils;
1+
use shell_command_menu::{config, menu_main, utils};
62

73
fn main() {
84
// Print the version
9-
let version = crate::utils::get_version();
5+
let version = utils::get_version();
106
println!("Welcome to CLI_Menu v{version}!");
117
// Execute the config::get_config_file_path function to get the config file path and load it; else create it
128
let config_path = match config::get_config_file_path() {

tests/test.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use shell_command_menu::{
2+
config::{CommandOption, Config, load_config, save_config, validate_json},
3+
csv::read_commands_from_csv,
4+
menu_edit::clear_all_commands,
5+
menu_main::{generate_menu, prompt_or_return},
6+
utils::get_version,
7+
};
8+
9+
#[test]
10+
fn config_roundtrip_save_load() {
11+
let temp = tempfile::NamedTempFile::new().expect("temp file");
12+
let path = temp.path().to_path_buf();
13+
14+
let original = Config {
15+
commands: vec![CommandOption {
16+
display_name: "List".into(),
17+
command: "ls -la".into(),
18+
}],
19+
cmd_sound: Some("sound.mp3".into()),
20+
window_title_support: true,
21+
window_title: Some("CLI Menu".into()),
22+
};
23+
24+
save_config(&path, &original);
25+
let loaded = load_config(&path).expect("load config");
26+
27+
assert_eq!(loaded.commands.len(), 1);
28+
assert_eq!(loaded.commands[0].display_name, "List");
29+
assert_eq!(loaded.cmd_sound, original.cmd_sound);
30+
assert_eq!(loaded.window_title_support, original.window_title_support);
31+
assert_eq!(loaded.window_title, original.window_title);
32+
}
33+
34+
#[test]
35+
fn config_validate_default_json() {
36+
let config = Config::default();
37+
assert!(validate_json(&config));
38+
}
39+
40+
#[test]
41+
fn csv_read_commands_fixture() {
42+
let commands = read_commands_from_csv("tests/fixtures/commands.csv").expect("parse csv");
43+
assert_eq!(commands.len(), 2);
44+
assert_eq!(commands[0].display_name, "List Files");
45+
assert_eq!(commands[0].command, "ls -la");
46+
}
47+
48+
#[test]
49+
fn menu_generate_menu_strikes_selected() {
50+
let commands = vec![
51+
CommandOption {
52+
display_name: "One".into(),
53+
command: "echo 1".into(),
54+
},
55+
CommandOption {
56+
display_name: "Two".into(),
57+
command: "echo 2".into(),
58+
},
59+
];
60+
61+
let rendered = generate_menu(&commands, &[2]);
62+
assert_eq!(rendered.len(), 2);
63+
assert_eq!(rendered[0], "1. One");
64+
assert_eq!(rendered[1], "2. T\u{0336}w\u{0336}o\u{0336}");
65+
}
66+
67+
#[test]
68+
fn menu_prompt_or_return_ok() {
69+
let value = prompt_or_return(|| Ok::<_, inquire::error::InquireError>(123));
70+
assert_eq!(value, Some(123));
71+
}
72+
73+
#[test]
74+
fn menu_prompt_or_return_cancelled() {
75+
let value: Option<i32> =
76+
prompt_or_return(|| Err::<i32, _>(inquire::error::InquireError::OperationCanceled));
77+
assert_eq!(value, None);
78+
}
79+
80+
#[test]
81+
fn menu_edit_clear_all_commands() {
82+
let mut config = Config {
83+
commands: vec![
84+
CommandOption {
85+
display_name: "A".into(),
86+
command: "echo a".into(),
87+
},
88+
CommandOption {
89+
display_name: "B".into(),
90+
command: "echo b".into(),
91+
},
92+
],
93+
..Default::default()
94+
};
95+
let mut changed = false;
96+
97+
clear_all_commands(&mut config, &mut changed);
98+
99+
assert!(config.commands.is_empty());
100+
assert!(changed);
101+
}
102+
103+
#[test]
104+
fn utils_version_matches_package() {
105+
assert_eq!(get_version(), env!("CARGO_PKG_VERSION"));
106+
}

0 commit comments

Comments
 (0)