2023-06-14 23:12:55 +02:00
|
|
|
use nu_protocol::{
|
|
|
|
engine::{EngineState, StateWorkingSet},
|
2023-12-15 07:32:37 +01:00
|
|
|
Category, PositionalArg, Span,
|
2023-06-14 23:12:55 +02:00
|
|
|
};
|
2021-07-01 00:47:56 +02:00
|
|
|
use quickcheck_macros::quickcheck;
|
2020-03-04 19:58:20 +01:00
|
|
|
|
|
|
|
mod commands;
|
|
|
|
mod format_conversions;
|
2021-07-01 00:47:56 +02:00
|
|
|
|
2023-06-14 23:12:55 +02:00
|
|
|
fn create_default_context() -> EngineState {
|
|
|
|
nu_command::add_shell_command_context(nu_cmd_lang::create_default_context())
|
|
|
|
}
|
|
|
|
|
2022-02-04 03:01:45 +01:00
|
|
|
#[quickcheck]
|
|
|
|
fn quickcheck_parse(data: String) -> bool {
|
|
|
|
let (tokens, err) = nu_parser::lex(data.as_bytes(), 0, b"", b"", true);
|
|
|
|
|
2022-11-18 22:46:48 +01:00
|
|
|
if err.is_none() {
|
2022-07-14 16:09:27 +02:00
|
|
|
let context = create_default_context();
|
2022-02-04 03:01:45 +01:00
|
|
|
{
|
|
|
|
let mut working_set = StateWorkingSet::new(&context);
|
2023-04-09 22:55:47 +02:00
|
|
|
let _ = working_set.add_file("quickcheck".into(), data.as_bytes());
|
2022-02-04 03:01:45 +01:00
|
|
|
|
2023-04-21 21:00:33 +02:00
|
|
|
let _ =
|
|
|
|
nu_parser::parse_block(&mut working_set, &tokens, Span::new(0, 0), false, false);
|
2022-02-04 03:01:45 +01:00
|
|
|
}
|
2021-07-01 00:47:56 +02:00
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2022-08-24 11:16:47 +02:00
|
|
|
|
2023-12-15 07:32:37 +01:00
|
|
|
#[test]
|
|
|
|
fn arguments_end_period() {
|
|
|
|
fn ends_period(cmd_name: &str, ty: &str, arg: PositionalArg, failures: &mut Vec<String>) {
|
|
|
|
let arg_name = arg.name;
|
|
|
|
let desc = arg.desc;
|
|
|
|
if !desc.ends_with('.') {
|
|
|
|
failures.push(format!(
|
|
|
|
"{cmd_name} {ty} argument \"{arg_name}\": \"{desc}\""
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ctx = crate::create_default_context();
|
|
|
|
let decls = ctx.get_decls_sorted(true);
|
|
|
|
let mut failures = Vec::new();
|
|
|
|
|
|
|
|
for (name_bytes, decl_id) in decls {
|
|
|
|
let cmd = ctx.get_decl(decl_id);
|
|
|
|
let cmd_name = String::from_utf8_lossy(&name_bytes);
|
|
|
|
let signature = cmd.signature();
|
|
|
|
|
|
|
|
for arg in signature.required_positional {
|
|
|
|
ends_period(&cmd_name, "required", arg, &mut failures);
|
|
|
|
}
|
|
|
|
|
|
|
|
for arg in signature.optional_positional {
|
|
|
|
ends_period(&cmd_name, "optional", arg, &mut failures);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(arg) = signature.rest_positional {
|
|
|
|
ends_period(&cmd_name, "rest", arg, &mut failures);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
failures.is_empty(),
|
|
|
|
"Command argument description does not end with a period:\n{}",
|
|
|
|
failures.join("\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arguments_start_uppercase() {
|
|
|
|
fn starts_uppercase(cmd_name: &str, ty: &str, arg: PositionalArg, failures: &mut Vec<String>) {
|
|
|
|
let arg_name = arg.name;
|
|
|
|
let desc = arg.desc;
|
|
|
|
|
|
|
|
// Check lowercase to allow usage to contain syntax like:
|
|
|
|
//
|
|
|
|
// "`as` keyword …"
|
|
|
|
if desc.starts_with(|u: char| u.is_lowercase()) {
|
|
|
|
failures.push(format!(
|
|
|
|
"{cmd_name} {ty} argument \"{arg_name}\": \"{desc}\""
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ctx = crate::create_default_context();
|
|
|
|
let decls = ctx.get_decls_sorted(true);
|
|
|
|
let mut failures = Vec::new();
|
|
|
|
|
|
|
|
for (name_bytes, decl_id) in decls {
|
|
|
|
let cmd = ctx.get_decl(decl_id);
|
|
|
|
let cmd_name = String::from_utf8_lossy(&name_bytes);
|
|
|
|
let signature = cmd.signature();
|
|
|
|
|
|
|
|
for arg in signature.required_positional {
|
|
|
|
starts_uppercase(&cmd_name, "required", arg, &mut failures);
|
|
|
|
}
|
|
|
|
|
|
|
|
for arg in signature.optional_positional {
|
|
|
|
starts_uppercase(&cmd_name, "optional", arg, &mut failures);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(arg) = signature.rest_positional {
|
|
|
|
starts_uppercase(&cmd_name, "rest", arg, &mut failures);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
failures.is_empty(),
|
|
|
|
"Command argument description does not end with a period:\n{}",
|
|
|
|
failures.join("\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-24 11:16:47 +02:00
|
|
|
#[test]
|
|
|
|
fn signature_name_matches_command_name() {
|
2023-06-14 23:12:55 +02:00
|
|
|
let ctx = create_default_context();
|
2022-12-30 16:44:37 +01:00
|
|
|
let decls = ctx.get_decls_sorted(true);
|
2022-08-24 11:16:47 +02:00
|
|
|
let mut failures = Vec::new();
|
|
|
|
|
2022-12-30 16:44:37 +01:00
|
|
|
for (name_bytes, decl_id) in decls {
|
2022-08-24 11:16:47 +02:00
|
|
|
let cmd = ctx.get_decl(decl_id);
|
2022-12-30 16:44:37 +01:00
|
|
|
let cmd_name = String::from_utf8_lossy(&name_bytes);
|
2022-08-24 11:16:47 +02:00
|
|
|
let sig_name = cmd.signature().name;
|
|
|
|
let category = cmd.signature().category;
|
|
|
|
|
|
|
|
if cmd_name != sig_name {
|
|
|
|
failures.push(format!(
|
|
|
|
"{cmd_name} ({category:?}): Signature name \"{sig_name}\" is not equal to the command name \"{cmd_name}\""
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
failures.is_empty(),
|
|
|
|
"Name mismatch:\n{}",
|
|
|
|
failures.join("\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-21 20:20:46 +01:00
|
|
|
#[test]
|
|
|
|
fn commands_declare_input_output_types() {
|
2023-06-14 23:12:55 +02:00
|
|
|
let ctx = create_default_context();
|
2022-12-30 16:44:37 +01:00
|
|
|
let decls = ctx.get_decls_sorted(true);
|
2022-12-21 20:20:46 +01:00
|
|
|
let mut failures = Vec::new();
|
|
|
|
|
2022-12-30 16:44:37 +01:00
|
|
|
for (_, decl_id) in decls {
|
2022-12-21 20:20:46 +01:00
|
|
|
let cmd = ctx.get_decl(decl_id);
|
|
|
|
let sig_name = cmd.signature().name;
|
|
|
|
let category = cmd.signature().category;
|
|
|
|
|
2024-05-20 19:22:08 +02:00
|
|
|
if matches!(category, Category::Removed) {
|
2023-08-14 21:17:31 +02:00
|
|
|
// Deprecated/Removed commands don't have to conform
|
2022-12-21 20:20:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.signature().input_output_types.is_empty() {
|
|
|
|
failures.push(format!(
|
|
|
|
"{sig_name} ({category:?}): No pipeline input/output type signatures found"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
failures.is_empty(),
|
|
|
|
"Command missing type annotations:\n{}",
|
|
|
|
failures.join("\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-24 11:16:47 +02:00
|
|
|
#[test]
|
|
|
|
fn no_search_term_duplicates() {
|
|
|
|
let ctx = crate::create_default_context();
|
2022-12-30 16:44:37 +01:00
|
|
|
let decls = ctx.get_decls_sorted(true);
|
2022-08-24 11:16:47 +02:00
|
|
|
let mut failures = Vec::new();
|
|
|
|
|
2022-12-30 16:44:37 +01:00
|
|
|
for (name_bytes, decl_id) in decls {
|
2022-08-24 11:16:47 +02:00
|
|
|
let cmd = ctx.get_decl(decl_id);
|
2022-12-30 16:44:37 +01:00
|
|
|
let cmd_name = String::from_utf8_lossy(&name_bytes);
|
2022-08-24 11:16:47 +02:00
|
|
|
let search_terms = cmd.search_terms();
|
|
|
|
let category = cmd.signature().category;
|
|
|
|
|
|
|
|
for search_term in search_terms {
|
|
|
|
if cmd_name.contains(search_term) {
|
|
|
|
failures.push(format!("{cmd_name} ({category:?}): Search term \"{search_term}\" is substring of command name \"{cmd_name}\""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
failures.is_empty(),
|
|
|
|
"Duplication in search terms:\n{}",
|
|
|
|
failures.join("\n")
|
|
|
|
);
|
|
|
|
}
|
2023-12-10 15:28:54 +01:00
|
|
|
|
|
|
|
#[test]
|
2024-08-22 12:02:08 +02:00
|
|
|
fn description_end_period() {
|
2023-12-10 15:28:54 +01:00
|
|
|
let ctx = crate::create_default_context();
|
|
|
|
let decls = ctx.get_decls_sorted(true);
|
|
|
|
let mut failures = Vec::new();
|
|
|
|
|
|
|
|
for (name_bytes, decl_id) in decls {
|
|
|
|
let cmd = ctx.get_decl(decl_id);
|
|
|
|
let cmd_name = String::from_utf8_lossy(&name_bytes);
|
2024-08-22 12:02:08 +02:00
|
|
|
let description = cmd.description();
|
2023-12-10 15:28:54 +01:00
|
|
|
|
2024-08-22 12:02:08 +02:00
|
|
|
if !description.ends_with('.') {
|
|
|
|
failures.push(format!("{cmd_name}: \"{description}\""));
|
2023-12-10 15:28:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
failures.is_empty(),
|
2024-08-22 12:02:08 +02:00
|
|
|
"Command description does not end with a period:\n{}",
|
2023-12-10 15:28:54 +01:00
|
|
|
failures.join("\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-08-22 12:02:08 +02:00
|
|
|
fn description_start_uppercase() {
|
2023-12-10 15:28:54 +01:00
|
|
|
let ctx = crate::create_default_context();
|
|
|
|
let decls = ctx.get_decls_sorted(true);
|
|
|
|
let mut failures = Vec::new();
|
|
|
|
|
|
|
|
for (name_bytes, decl_id) in decls {
|
|
|
|
let cmd = ctx.get_decl(decl_id);
|
|
|
|
let cmd_name = String::from_utf8_lossy(&name_bytes);
|
2024-08-22 12:02:08 +02:00
|
|
|
let description = cmd.description();
|
2023-12-10 15:28:54 +01:00
|
|
|
|
2024-08-22 12:02:08 +02:00
|
|
|
// Check lowercase to allow description to contain syntax like:
|
2023-12-10 15:28:54 +01:00
|
|
|
//
|
2024-03-27 14:41:02 +01:00
|
|
|
// "`$env.FOO = ...`"
|
2024-08-22 12:02:08 +02:00
|
|
|
if description.starts_with(|u: char| u.is_lowercase()) {
|
|
|
|
failures.push(format!("{cmd_name}: \"{description}\""));
|
2023-12-10 15:28:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
failures.is_empty(),
|
2024-08-22 12:02:08 +02:00
|
|
|
"Command description does not start with an uppercase letter:\n{}",
|
2023-12-10 15:28:54 +01:00
|
|
|
failures.join("\n")
|
|
|
|
);
|
|
|
|
}
|