use super::execute;
const EXAMPLE_DIR: &'static str = "./example";
const SHELL: &'static str = "bash";
fn _vec_str(args: Vec<&str>) -> Vec<String> {
args.iter().map(|s| s.to_string()).collect()
}
#[test]
fn test_exec_simple_script() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"exec",
"file_example"
])),
Ok(format!("'{}/file_example'", EXAMPLE_DIR))
);
}
#[test]
fn test_exec_recursive_simple_script() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"exec",
"exec",
"file_example"
])),
Ok(format!("'{}/file_example'", EXAMPLE_DIR))
);
}
#[test]
fn test_simple_script() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"file_example"
])),
Ok(format!("'{}/file_example'", EXAMPLE_DIR))
);
}
#[test]
fn test_simple_script_with_args() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"file_example",
"x"
])),
Ok(format!("'{}/file_example' 'x'", EXAMPLE_DIR))
);
}
#[test]
fn test_simple_script_completion() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-complete",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"file_example",
])),
Ok(String::from("file autocomplete example"))
);
}
#[test]
fn test_script_completion_no_args() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-complete",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"dir_example",
"baz",
])),
Ok(String::from("--bar1 --bar2 --not-bar\n"))
);
}
#[test]
fn test_script_completion_with_args() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-complete",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"dir_example",
"baz",
"foo",
"bar",
])),
Ok(String::from("completed with: foo bar\n"))
);
}
#[test]
fn test_simple_script_no_completion() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-complete",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"test_files",
"file_example_no_completion",
])),
Ok(String::from(""))
);
}
#[test]
fn test_source() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"source_example",
])),
Ok(format!(
"set --; '.' '{}/source_example.source'",
EXAMPLE_DIR
))
);
}
#[test]
fn test_source_completion() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-complete",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"source_example",
])),
Ok(String::from("foo baz\n"))
);
}
#[test]
fn test_directory_completion() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-complete",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"dir_example",
])),
Ok("bar baz foo".to_string())
);
}
#[test]
fn test_root_directory_completion() {
assert_eq!(
execute(_vec_str(vec!["tome", "command-complete", "-s", "bash", EXAMPLE_DIR])),
Ok("commands dir_example exec file_example help practical_examples quote read-from-root source_example source_example_fish test_files tome use-arg".to_string())
);
}
#[test]
fn test_tome_completion() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-complete",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"tome",
])),
Ok(String::from(""))
);
}
#[test]
fn test_script_in_directory() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"dir_example",
"foo"
])),
Ok(format!("'{}/dir_example/foo'", EXAMPLE_DIR))
);
}
#[test]
fn test_script_in_directory_not_found() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"dir_example",
"foo-nonexistent",
"baz"
])),
Err(format!(
"command foo-nonexistent not found in directory {}/dir_example",
EXAMPLE_DIR
))
);
}
#[test]
fn test_script_directory_argument() {
let result = execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"dir_example",
]))
.unwrap();
assert!(result.contains("list of dir_example commands available are:"));
assert!(result.contains("foo:"));
assert!(result.contains("bar:"));
}
#[test]
fn test_use_arg() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"use-arg"
])),
Ok(format!("set --; '.' '{}/use-arg.source'", EXAMPLE_DIR))
);
}
#[test]
fn test_dangerous_characters_quoted() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"use-arg"
])),
Ok(format!("set --; '.' '{}/use-arg.source'", EXAMPLE_DIR))
);
}
#[test]
fn test_execute_with_single_quotes() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
SHELL,
EXAMPLE_DIR,
"--",
"file_example",
"I'm the developer",
"'hello'",
])),
Ok(format!(
"'{}/file_example' 'I'\\''m the developer' ''\\''hello'\\'''",
EXAMPLE_DIR
))
);
}
#[test]
fn test_execute_help() {
let result = execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"help",
]))
.unwrap();
assert_is_help_text(&result)
}
#[test]
fn test_execute_commands() {
let result = execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"commands",
]))
.unwrap();
assert_is_help_text(&result)
}
#[test]
fn test_help_page() {
let result = execute(_vec_str(vec!["tome", "command-help", EXAMPLE_DIR])).unwrap();
assert_is_help_text(&result)
}
#[test]
fn test_help_page_when_execute_no_args() {
let result = execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
]))
.unwrap();
assert_is_help_text(&result)
}
#[test]
fn test_empty_arg_in_middle_of_path() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"",
"file_example"
])),
Err(format!("command not found in directory {}", EXAMPLE_DIR))
);
}
#[test]
fn test_source_arg_count() {
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"source_example",
])),
Ok(format!(
"set --; '.' '{}/source_example.source'",
EXAMPLE_DIR
))
);
assert_eq!(
execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"source_example",
""
])),
Ok(format!("'.' '{}/source_example.source' ''", EXAMPLE_DIR))
);
}
fn assert_is_help_text(result: &str) {
assert_eq!(result.matches("'\\'").count(), 2);
assert_eq!(result.matches("'").count(), 8);
assert!(result.contains("echo -e"));
assert!(result.contains("commands:"));
assert!(result.contains("exec:"));
assert!(result.contains("help:"));
assert!(result.contains("tome:"));
}
#[test]
fn test_help_for_script() {
let result = execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"help",
"file_example",
]))
.unwrap();
assert!(result.contains("file_example"));
assert!(result.contains("hey"));
assert!(result.contains("this is an example of a simple script"));
}
#[test]
fn test_help_page_for_script() {
let result = execute(_vec_str(vec![
"tome",
"command-help",
EXAMPLE_DIR,
"--",
"file_example",
]))
.unwrap();
assert!(result.contains("file_example"));
assert!(result.contains("hey"));
assert!(result.contains("this is an example of a simple script"));
}
#[test]
fn test_help_for_directory() {
let result = execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"help",
"dir_example",
]))
.unwrap();
assert!(result.contains("foo"));
assert!(result.contains("bar"));
}
#[test]
fn test_help_unknown_command_is_eval_safe() {
let result = execute(_vec_str(vec![
"tome",
"command-execute",
"-s",
"bash",
EXAMPLE_DIR,
"--",
"help",
"nonexistent_command",
]))
.unwrap();
assert!(
result.starts_with("echo '"),
"expected echo-wrapped output, got: {}",
result
);
assert!(result.contains(">&2"));
}