From f48c60611a5b0a2b71653a2b4c25f353f4c7f556 Mon Sep 17 00:00:00 2001 From: marcelarie Date: Mon, 14 Oct 2024 14:35:36 +0200 Subject: [PATCH] test: Add validation for alias content using nu-parser --- Cargo.lock | 5 ++-- Cargo.toml | 1 + src/syntax_tree/alias.rs | 8 ++++++- src/syntax_tree/mod.rs | 1 + src/syntax_tree/nushell.rs | 47 +++++++++++++++++++++++++++++++++++--- 5 files changed, 56 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b88dbd6..d05925d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,6 +81,7 @@ name = "bash-aliases-converter" version = "0.1.0" dependencies = [ "nu-parser", + "nu-protocol", "tree-sitter 0.24.2", "tree-sitter-bash", "tree-sitter-nu", @@ -1566,9 +1567,9 @@ checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" [[package]] name = "unicode-width" -version = "0.1.14" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" [[package]] name = "utf8-width" diff --git a/Cargo.toml b/Cargo.toml index 42e2922..32b8e8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] nu-parser = "0.98.0" +nu-protocol = "0.98.0" tree-sitter = "0.24.2" tree-sitter-bash = "0.23.1" tree-sitter-nu = { git = "https://github.com/nushell/tree-sitter-nu" } diff --git a/src/syntax_tree/alias.rs b/src/syntax_tree/alias.rs index c8c5780..1326aee 100644 --- a/src/syntax_tree/alias.rs +++ b/src/syntax_tree/alias.rs @@ -1,4 +1,6 @@ -use super::validate_nu_tree_sitter_code; +use super::{ + nushell::validate_alias_with_nu_parser, validate_nu_tree_sitter_code, +}; /// Unquote a string (remove the quotes if it has) // https://www.gnu.org/software/bash/manual/html_node/Quoting.html @@ -140,6 +142,10 @@ pub fn find_aliases( if let Ok(alias) = extract_alias(node, source) { let (name, content) = alias; let is_valid_nushell = validate_nu_tree_sitter_code(&content); + let is_valid_nushell_code = + validate_alias_with_nu_parser(&name, &content); + + println!("Parsed: Is valid -> {}", is_valid_nushell_code); let alias = Alias { name, diff --git a/src/syntax_tree/mod.rs b/src/syntax_tree/mod.rs index e0e100c..ccc5dd1 100644 --- a/src/syntax_tree/mod.rs +++ b/src/syntax_tree/mod.rs @@ -10,3 +10,4 @@ pub use traverser::traverse_tree; pub use alias::find_aliases; pub use nushell::validate_nu_tree_sitter_code; +pub use nushell::validate_alias_with_nu_parser; diff --git a/src/syntax_tree/nushell.rs b/src/syntax_tree/nushell.rs index abcfe9b..61da14c 100644 --- a/src/syntax_tree/nushell.rs +++ b/src/syntax_tree/nushell.rs @@ -1,6 +1,10 @@ +use nu_parser::parse; +use nu_protocol::engine::{EngineState, StateWorkingSet}; use tree_sitter::Parser; use tree_sitter_nu::LANGUAGE; +// use nu_source::{Span, Tag}; + pub fn validate_nu_tree_sitter_code(content: &String) -> bool { let mut parser = Parser::new(); let nu_lang = LANGUAGE.into(); @@ -12,9 +16,19 @@ pub fn validate_nu_tree_sitter_code(content: &String) -> bool { parser.parse(content, None).is_some() } -// pub fn validate_nu_parsing(content: &String) -> bool { -// // TODO: -// } +pub fn validate_alias_with_nu_parser(name: &str, content: &str) -> bool { + let engine_state = EngineState::new(); + let mut working_set = StateWorkingSet::new(&engine_state); + + let alias_declaration = format!("alias {} = {}", name, content); + + let _ = + parse(&mut working_set, None, alias_declaration.as_bytes(), true); + + println!("{:?}", working_set.parse_errors ); + + working_set.parse_errors.is_empty() +} #[cfg(test)] mod tests { @@ -29,4 +43,31 @@ mod tests { "Expected valid Nu code to return true, but got false" ); } + + #[test] + fn test_validate_alias_with_nu_parser_valid_input() { + let valid_alias_name = "ll"; + let valid_alias_content = "ls"; + + let result = validate_alias_with_nu_parser( + valid_alias_name, + valid_alias_content, + ); + + assert!(result, "Expected valid alias to return true, but got false"); + } + + #[test] + fn test_validate_alias_with_nu_parser_invalid_input() { + let invalid_alias_name = "homer"; + let invalid_alias_content = "echo $HOME"; + let result = validate_alias_with_nu_parser( + invalid_alias_name, + invalid_alias_content, + ); + assert!( + !result, + "Expected invalid alias to return false, but got true" + ); + } }