Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helio-frota committed Jun 11, 2024
1 parent f8ebb6f commit c6daaa1
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 67 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: ci

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:

ci:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- name: Format
run: cargo fmt --check
- name: Check
run: cargo check
- name: Clippy
run: cargo clippy --all-targets --all-features -- -D warnings -D clippy::unwrap_used -D clippy::expect_used
- name: Test
run: cargo test
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
strsim = "0.11"
strsim = "0.11"

73 changes: 7 additions & 66 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,11 @@
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::{fs, io};
use std::io;

use strsim::sorensen_dice;

// #[derive(Debug)]
// struct SimilarBlock {
// block1: String,
// block2: String,
// similarity: f64,
// }

// impl SimilarBlock {
// pub fn new(b1: String, b2: String, s: f64) -> SimilarBlock {
// SimilarBlock {
// block1: b1,
// block2: b2,
// similarity: s,
// }
// }
// }

/// This function gets all the .rs files from a directory, and returns a
/// vector with all the paths of the .rs files.
pub fn rust_files(dir: &str) -> Result<Vec<PathBuf>, io::Error> {
let mut files = Vec::new();
// Gets all the entries from 'current|argument' dir.
let entries = fs::read_dir(dir)?;

for e in entries {
let entry = e?;
let path = entry.path();

if path.is_dir() {
if let Some(file_name) = path.file_name() {
if let Some(file_name_str) = file_name.to_str() {
if file_name_str.contains("target") || file_name_str.contains("test") {
continue;
}
}
}

// Take a look at sub dir
let temp = rust_files(&path.to_string_lossy())?;
// .rs found ? Then add to the vector.
files.extend(temp);
} else if let Some(file_name) = path.file_name() {
// Not a dir and .rs
if file_name.to_string_lossy().ends_with(".rs") {
// Then adds to the vector.
files.push(path);
}
}
}

Ok(files)
}
mod util;

// Reads file content.
fn content(path: &str) -> Result<String, io::Error> {
let mut f = File::open(path)?;
let mut c = String::new();
f.read_to_string(&mut c)?;
Ok(c)
}
#[cfg(test)]
mod test;

fn code_blocks(content: &str) -> Vec<String> {
// Changes the content to a vector lines.
Expand Down Expand Up @@ -164,7 +105,7 @@ fn find_similar_blocks(blocks: Vec<String>) -> Vec<(String, String, f64)> {
}

fn main() -> Result<(), io::Error> {
let rust_files = rust_files("/home/heliofrota/Desktop/tc/trustify/")?;
let rust_files = util::rust_files("/home/heliofrota/Desktop/tc/trustify/")?;
let mut filtered_code_blocks: Vec<String> = Vec::new();
for r in &rust_files {
let r_as_string = r.to_string_lossy();
Expand All @@ -173,7 +114,7 @@ fn main() -> Result<(), io::Error> {
.unwrap_or_else(|| r_as_string.len());
let file_name = &r_as_string[the_index..];

let content = content(r.to_string_lossy().into_owned().as_str())?;
let content = util::content(r.to_string_lossy().into_owned().as_str())?;
let cbs = code_blocks(content.as_str());

for cb in &cbs {
Expand All @@ -196,7 +137,7 @@ fn main() -> Result<(), io::Error> {
let mut b2: Vec<&str> = s.1.split('\n').collect();
let f2 = b2.pop();

println!("### crab {}\n", idx);
println!("### crab {}\n", idx + 1);

if s.2 == 1.0 {
println!("> [!TIP]\n> Exactly the same\n");
Expand Down
10 changes: 10 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[cfg(test)]
mod tests {
use crate::util;

#[test]
fn test_get_rust_files() {
let files = util::rust_files(".").expect("rust files not found.");
assert_eq!(3, files.len());
}
}
48 changes: 48 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::{fs, io};

/// This function gets all the .rs files from a directory, and returns a
/// vector with all the paths of the .rs files.
pub fn rust_files(dir: &str) -> Result<Vec<PathBuf>, io::Error> {
let mut files = Vec::new();
// Gets all the entries from 'current|argument' dir.
let entries = fs::read_dir(dir)?;

for e in entries {
let entry = e?;
let path = entry.path();

if path.is_dir() {
if let Some(file_name) = path.file_name() {
if let Some(file_name_str) = file_name.to_str() {
if file_name_str.contains("target") || file_name_str.contains("test") {
continue;
}
}
}

// Take a look at sub dir
let temp = rust_files(&path.to_string_lossy())?;
// .rs found ? Then add to the vector.
files.extend(temp);
} else if let Some(file_name) = path.file_name() {
// Not a dir and .rs
if file_name.to_string_lossy().ends_with(".rs") {
// Then adds to the vector.
files.push(path);
}
}
}

Ok(files)
}

// Reads file content.
pub fn content(path: &str) -> Result<String, io::Error> {
let mut f = File::open(path)?;
let mut c = String::new();
f.read_to_string(&mut c)?;
Ok(c)
}

0 comments on commit c6daaa1

Please sign in to comment.