Skip to content

Commit

Permalink
add tests & format check
Browse files Browse the repository at this point in the history
  • Loading branch information
vladNed committed Aug 6, 2023
1 parent 355b45b commit 0a286b8
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pipeline-jones.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ jobs:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Format check
run: cargo fmt --check
- name: Run tests
run: cargo test --verbose
17 changes: 9 additions & 8 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ use structopt::StructOpt;

#[derive(StructOpt)]
pub struct CLI {

// Flag to search all classes with that value
#[structopt(short="g", long="grep",
help="Used to retrieve all classes with that pattern")]
#[structopt(
short = "g",
long = "grep",
help = "Used to retrieve all classes with that pattern"
)]
pub grep: bool,

// Class name to be fetched
#[structopt(help="Name of the Python class")]
#[structopt(help = "Name of the Python class")]
pub class_name: String,

// Search directory
#[structopt(parse(from_os_str), default_value=".",
help="Search directory")]
pub dir_path: PathBuf
}
#[structopt(parse(from_os_str), default_value = ".", help = "Search directory")]
pub dir_path: PathBuf,
}
2 changes: 1 addition & 1 deletion src/joneslib/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ pub fn class_matches(found_match_classes: Vec<ClassMatch>) -> () {
Colour::Purple.paint(&line.1)
)
}
}
}
5 changes: 4 additions & 1 deletion src/joneslib/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ pub fn load_python_object(file_path: &PathBuf, class_name: &String) -> Option<Py
let methods_pattern = Regex::new(METHODS_PATTERN).unwrap();
let attributes_pattern = Regex::new(ATTRIBUTES_PATTERN).unwrap();
let class_pattern = Regex::new(CLASS_NAME_PATTERN).unwrap();
let docstring_pattern = RegexBuilder::new(DOCSTRING_PATTERN).dot_matches_new_line(true).build().unwrap();
let docstring_pattern = RegexBuilder::new(DOCSTRING_PATTERN)
.dot_matches_new_line(true)
.build()
.unwrap();
let inheritance_pattern = Regex::new(INHERITANCE_PATTERN).unwrap();
let output_pattern = Regex::new(OUTPUT_PATTERN).unwrap();

Expand Down
51 changes: 22 additions & 29 deletions src/joneslib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ License: MIT
Copyright 2021 Vlad Nedelcu
*/

pub mod objects;
pub mod display;
pub mod loader;
pub mod objects;
pub mod parser;

use std::fs;
Expand All @@ -21,7 +21,6 @@ const CLASS_TEMPLATE: &str = "class {template}:";
const TEMPLATE_KEYWORD: &str = "{template}";
const PYTHON_EXTENSION: &str = "py";


type ClassMatch = (String, String);

/// Check if a file contains the searched class by reading the file.
Expand All @@ -33,31 +32,29 @@ type ClassMatch = (String, String);
/// # Errors
/// It panics if the file is cannot be read properly
fn check_file_contains_class(class_name: &str, file_path: &str) -> bool {
let class_name_inheritance = CLASS_TEMPLATE_INHERITANCE.clone()
.replace(TEMPLATE_KEYWORD, class_name);
let class_name = CLASS_TEMPLATE.clone()
let class_name_inheritance = CLASS_TEMPLATE_INHERITANCE
.clone()
.replace(TEMPLATE_KEYWORD, class_name);
let class_name = CLASS_TEMPLATE.clone().replace(TEMPLATE_KEYWORD, class_name);

match fs::read_to_string(file_path) {
Ok(file_content) => {
let first_check = file_content.contains(&class_name_inheritance);
let second_check = file_content.contains(&class_name);
return first_check || second_check
},
Err(_) => {
return false
return first_check || second_check;
}
Err(_) => return false,
};
}

/// Searches recurssively through a project for a Python class and extracts that
/// Searches recursively through a project for a Python class and extracts that
/// class into an PythonClass struct.
pub fn project_traversal(dir_path: &PathBuf, class_name: &String) -> Option<objects::PythonClass> {
let current_dir = match fs::read_dir(dir_path) {
Ok(dir) => dir,
Err(err) => {
println!("Error occured while reading dir: {}", err);
return None
return None;
}
};

Expand All @@ -66,48 +63,45 @@ pub fn project_traversal(dir_path: &PathBuf, class_name: &String) -> Option<obje
let file_path_name = file_path.to_str().unwrap();
if file_path.is_dir() {
match project_traversal(&file_path, class_name) {
Some(value) => {
return Some(value)
},
None => continue
Some(value) => return Some(value),
None => continue,
};
} else {
match file_path.extension() {
Some(extension) => {
if extension != PYTHON_EXTENSION {
continue
continue;
}
},
None => continue
}
None => continue,
}
if check_file_contains_class(class_name, &file_path_name){
return loader::load_python_object(&file_path, &class_name)
if check_file_contains_class(class_name, &file_path_name) {
return loader::load_python_object(&file_path, &class_name);
}
}
}
return None
return None;
}

/// Loads the project classes and filters them by the class name. Returns a vector
/// of tuples containing the class name and the file path.
pub fn search(dir_path: &PathBuf, class_name: &String) -> Option<Vec<ClassMatch>>{

pub fn search(dir_path: &PathBuf, class_name: &String) -> Option<Vec<ClassMatch>> {
let project_classes = match loader::load_python_project(dir_path) {
Some(classes) => classes,
None => {
println!("Error occurred while loading project classes");
return None
return None;
}
};

let filtered_classes = project_classes.into_iter()
let filtered_classes = project_classes
.into_iter()
.filter(|class| class.0.contains(class_name))
.collect::<Vec<ClassMatch>>();

Some(filtered_classes)
}


#[cfg(test)]
mod tests {
use super::project_traversal;
Expand Down Expand Up @@ -136,7 +130,6 @@ mod tests {

#[test]
fn test_process_only_py_files() {

// Paths
let test_dir = String::from("./testing_none");
let python_file = String::from("./testing_none/test.py");
Expand All @@ -150,9 +143,9 @@ mod tests {
pathbuf.push("./testing_none");

// Assert
assert_eq!( project_traversal(&pathbuf, &"TestCode".to_string()), None);
assert_eq!(project_traversal(&pathbuf, &"TestCode".to_string()), None);

// Destroy the test dir
fs::remove_dir_all("./testing_none").expect("Could not delete dir");
}
}
}
5 changes: 4 additions & 1 deletion src/joneslib/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ impl Parameter {
static_type.clone()
};

Parameter { name, static_type: annotation }
Parameter {
name,
static_type: annotation,
}
}
}
impl fmt::Display for Parameter {
Expand Down
Loading

0 comments on commit 0a286b8

Please sign in to comment.