Skip to content

Commit

Permalink
test: Add validation for alias content using nu-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelarie committed Oct 14, 2024
1 parent 1e04bb4 commit f48c606
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
8 changes: 7 additions & 1 deletion src/syntax_tree/alias.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/syntax_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
47 changes: 44 additions & 3 deletions src/syntax_tree/nushell.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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 {
Expand All @@ -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"
);
}
}

0 comments on commit f48c606

Please sign in to comment.