diff --git a/.github/workflows/clippy.yaml b/.github/workflows/clippy.yaml new file mode 100644 index 0000000..b2b3234 --- /dev/null +++ b/.github/workflows/clippy.yaml @@ -0,0 +1,54 @@ +name: Clippy Test + +on: + pull_request: + + +jobs: + git-diff: + runs-on: ubuntu-latest + name: 'Git Diff - Powered by Rust' + outputs: + DIFF_FILES: ${{ steps.git-diff.outputs.DIFF_FILES }} + DIFF_COUNT: ${{ steps.git-diff.outputs.DIFF_COUNT }} + steps: + - uses: actions/checkout@v4 + - uses: LuisLiraC/git-diff@v1.0.2 + id: git-diff + with: + patterns: '*.rs' + + clippy-test: + runs-on: ubuntu-latest + needs: [git-diff] + if: ${{ needs.git-diff.outputs.DIFF_COUNT != 0 }} + name: Run Tests + steps: + - uses: actions/checkout@v4 + + - name: Cache cargo registry + uses: actions/cache@v4 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo-registry- + + - name: Cache cargo index + uses: actions/cache@v4 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo-index- + + - name: Cache cargo build + uses: actions/cache@v4 + with: + path: target + key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo-build- + + - name: Run Tests + run: cargo clippy --color always -- -Dwarnings -W clippy::pedantic diff --git a/.github/workflows/prerelease.yaml b/.github/workflows/prerelease.yaml index 4dfaaad..51b1bb9 100644 --- a/.github/workflows/prerelease.yaml +++ b/.github/workflows/prerelease.yaml @@ -40,7 +40,7 @@ jobs: ${{ runner.os }}-cargo-build- - name: Build - run: cargo build --release + run: cargo build --release --color always - name: Create Archive Folder run: mkdir ${{ runner.os }} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 9cfe617..f59aa9c 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -40,7 +40,7 @@ jobs: ${{ runner.os }}-cargo-build- - name: Build - run: cargo build --release + run: cargo build --release --color always - name: Create Archive Folder run: mkdir ${{ runner.os }} diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 8740716..e7a2b50 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -5,8 +5,23 @@ on: jobs: + git-diff: + runs-on: ubuntu-latest + name: 'Git Diff - Powered by Rust' + outputs: + DIFF_FILES: ${{ steps.git-diff.outputs.DIFF_FILES }} + DIFF_COUNT: ${{ steps.git-diff.outputs.DIFF_COUNT }} + steps: + - uses: actions/checkout@v4 + - uses: LuisLiraC/git-diff@v1.0.2 + id: git-diff + with: + patterns: '*.rs' + test: runs-on: ubuntu-latest + needs: [git-diff] + if: ${{ needs.git-diff.outputs.DIFF_COUNT != 0 }} name: Run Tests steps: - uses: actions/checkout@v4 @@ -36,4 +51,4 @@ jobs: ${{ runner.os }}-cargo-build- - name: Run Tests - run: cargo test + run: cargo test --color always diff --git a/action.yaml b/action.yaml index 23c919c..6beafdd 100644 --- a/action.yaml +++ b/action.yaml @@ -20,7 +20,7 @@ runs: - name: Get Binary shell: bash run: | - BIN_URL=$(curl https://api.github.com/repos/LuisLiraC/git-diff/releases/tags/v1.0.2 | jq -r '.assets[0].browser_download_url') + BIN_URL=$(curl https://api.github.com/repos/LuisLiraC/git-diff/releases/tags/v1.0.3 | jq -r '.assets[0].browser_download_url') curl -s -L $BIN_URL -o rust-binary.tgz tar -xzvf rust-binary.tgz mv ./Linux/git-diff ./git-diff diff --git a/src/main.rs b/src/main.rs index 2e3b9f5..4f986fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,9 +15,9 @@ fn main() { let args: Vec = env::args().collect(); let mut patterns_filters: Vec = Vec::new(); - for arg in args.iter() { + for arg in &args { if arg.starts_with("--patterns=") { - patterns_filters = create_patterns_filters(&arg); + patterns_filters = create_patterns_filters(arg); } } @@ -26,34 +26,34 @@ fn main() { return; } - let (include_patterns_filters, exclude_patterns_filters) = categorize_filters(patterns_filters); + let (include_patterns_filters, exclude_patterns_filters) = categorize_filters(&patterns_filters); let start = Instant::now(); let changed_files = get_changed_files(); let duration = start.elapsed(); - println!("Getting changed files done in: {:?}", duration); + println!("Getting changed files done in: {duration:?}"); - println!("Changed files: {:?}", changed_files); + println!("Changed files: {changed_files:?}"); let start = Instant::now(); - let filtered_files = filter_files(changed_files, include_patterns_filters, exclude_patterns_filters); + let filtered_files = filter_files(&changed_files, &include_patterns_filters, &exclude_patterns_filters); let duration = start.elapsed(); - println!("Filtering files done in: {:?}", duration); + println!("Filtering files done in: {duration:?}"); - let count = get_count(filtered_files.clone()); + let count = get_count(&filtered_files); - println!("Filtered files: {:?}", filtered_files); - println!("Count: {}", count); + println!("Filtered files: {filtered_files:?}"); + println!("Count: {count}"); Command::new("sh") .arg("-c") - .arg(format!("echo \"DIFF_FILES={:?}\" >> $GITHUB_OUTPUT", filtered_files)) + .arg(format!("echo \"DIFF_FILES={filtered_files:?}\" >> $GITHUB_OUTPUT")) .output() .expect("Failed to execute DIFF_FILES command"); Command::new("sh") .arg("-c") - .arg(format!("echo \"DIFF_COUNT={}\" >> $GITHUB_OUTPUT", count)) + .arg(format!("echo \"DIFF_COUNT={count}\" >> $GITHUB_OUTPUT")) .output() .expect("Failed to execute DIFF_COUNT command"); } @@ -63,9 +63,9 @@ fn create_patterns_filters(arg: &str) -> Vec { .split('=') .last() .expect("Failed to get patterns") - .replace(" ", "") - .replace("\n", ",") - .replace("\r", "") + .replace(' ', "") + .replace('\n', ",") + .replace('\r', "") .replace(",,", ",") .trim_end_matches(',') .to_string(); @@ -76,12 +76,12 @@ fn create_patterns_filters(arg: &str) -> Vec { let mut patterns_filters: Vec = Vec::new(); - for pattern in patterns.iter() { + for pattern in &patterns { let exclude = pattern.starts_with('!'); let pattern = if exclude { pattern[1..].to_string() } else { - pattern.to_string() + (*pattern).to_string() }; patterns_filters.push(PatternFilter { @@ -104,11 +104,11 @@ fn get_changed_files() -> Vec { Command::new("sh") .arg("-c") - .arg(format!("git fetch origin {}", base_ref_env)) + .arg(format!("git fetch origin {base_ref_env}")) .output() .expect("Failed to execute fetch branch command"); - let base_ref_string = format!("refs/remotes/origin/{}", base_ref_env); + let base_ref_string = format!("refs/remotes/origin/{base_ref_env}"); let base_ref = repository.find_reference(&base_ref_string).expect("Failed to find default branch"); let base_commit = base_ref.peel_to_commit().expect("Failed to peel default branch to commit"); @@ -134,41 +134,41 @@ fn get_changed_files() -> Vec { changed_files } -fn filter_files(changed_files: Vec, include_patterns_filters: HashSet, exclude_patterns_filters: HashSet) -> HashSet { +fn filter_files(changed_files: &Vec, include_patterns_filters: &HashSet, exclude_patterns_filters: &HashSet) -> HashSet { let mut hash_set_filtered_files = HashSet::new(); - for changed_file in changed_files.iter() { - include_patterns_filters.iter().for_each(|pattern| { + for changed_file in changed_files { + for pattern in include_patterns_filters { if Pattern::new(pattern).expect("Failed to create pattern").matches(changed_file) { hash_set_filtered_files.insert(changed_file.to_string()); } - exclude_patterns_filters.iter().for_each(|pattern| { + for pattern in exclude_patterns_filters { if Pattern::new(pattern).expect("Failed to create pattern").matches(changed_file) { hash_set_filtered_files.remove(changed_file); } - }); - }); + } + } } hash_set_filtered_files } -fn get_count(filtered_files: HashSet) -> usize { +fn get_count(filtered_files: &HashSet) -> usize { filtered_files.len() } -fn categorize_filters(filters: Vec) -> (HashSet, HashSet) { +fn categorize_filters(filters: &Vec) -> (HashSet, HashSet) { let mut exclude_patterns_filters: HashSet = HashSet::new(); let mut include_patterns_filters: HashSet = HashSet::new(); - filters.iter().for_each(|pattern_filter| { + for pattern_filter in filters { if pattern_filter.exclude { exclude_patterns_filters.insert(pattern_filter.clone().pattern); } else { include_patterns_filters.insert(pattern_filter.clone().pattern); } - }); + } (include_patterns_filters, exclude_patterns_filters) } diff --git a/src/tests/integration.rs b/src/tests/integration.rs index d945133..b779997 100644 --- a/src/tests/integration.rs +++ b/src/tests/integration.rs @@ -1,32 +1,29 @@ use crate::*; #[cfg(test)] -mod integration { - use super::*; - #[test] - fn test_filter() { - let arg = "--patterns=*.rs,!*..txt"; - let files = vec![ - String::from("src/main.rs"), - String::from("lib.rs"), - String::from("test.txt"), - ]; +#[test] +fn test_filter() { + let arg = "--patterns=*.rs,!*..txt"; + let files = vec![ + String::from("src/main.rs"), + String::from("lib.rs"), + String::from("test.txt"), + ]; - let filters = create_patterns_filters(arg); + let filters = create_patterns_filters(arg); - let (include_patterns_filters, exclude_patterns_filters) = categorize_filters(filters); + let (include_patterns_filters, exclude_patterns_filters) = categorize_filters(&filters); - let filtered_files = filter_files(files, include_patterns_filters, exclude_patterns_filters); + let filtered_files = filter_files(&files, &include_patterns_filters, &exclude_patterns_filters); - let count = get_count(filtered_files.clone()); + let count = get_count(&filtered_files); - let expected_filtered_files = HashSet::from([ - String::from("src/main.rs"), - String::from("lib.rs"), - ]); + let expected_filtered_files = HashSet::from([ + String::from("src/main.rs"), + String::from("lib.rs"), + ]); - assert_eq!(filtered_files, expected_filtered_files); - assert_eq!(count, 2); - } + assert_eq!(filtered_files, expected_filtered_files); + assert_eq!(count, 2); } diff --git a/src/tests/unit.rs b/src/tests/unit.rs index 9377ca5..05bb958 100644 --- a/src/tests/unit.rs +++ b/src/tests/unit.rs @@ -1,118 +1,115 @@ use crate::*; #[cfg(test)] -mod unit { - use super::*; - #[test] - fn test_create_patterns_filters_single_line() { - let arg = "--patterns=*.rs,!test/*.rs"; - let filters = create_patterns_filters(arg); - assert_eq!(filters.len(), 2); - assert_eq!(filters[0].pattern, "*.rs"); - assert_eq!( - filters[0].exclude, false, - "Expected 'exclude' to be false for pattern '*.rs'" - ); - assert_eq!(filters[1].pattern, "test/*.rs"); - assert_eq!(filters[1].exclude, true); - } +#[test] +fn test_create_patterns_filters_single_line() { + let arg = "--patterns=*.rs,!test/*.rs"; + let filters = create_patterns_filters(arg); + assert_eq!(filters.len(), 2); + assert_eq!(filters[0].pattern, "*.rs"); + assert_eq!( + filters[0].exclude, false, + "Expected 'exclude' to be false for pattern '*.rs'" + ); + assert_eq!(filters[1].pattern, "test/*.rs"); + assert_eq!(filters[1].exclude, true); +} - #[test] - fn test_create_patterns_filters_multiple_lines() { - let arg = "--patterns=*.rs - !test/*.rs - .gitignore - "; - let filters = create_patterns_filters(arg); +#[test] +fn test_create_patterns_filters_multiple_lines() { + let arg = "--patterns=*.rs + !test/*.rs + .gitignore + "; + let filters = create_patterns_filters(arg); - assert_eq!(filters.len(), 3); - assert_eq!(filters[0].pattern, "*.rs"); - assert_eq!(filters[0].exclude, false); - assert_eq!(filters[1].pattern, "test/*.rs"); - assert_eq!(filters[1].exclude, true); - assert_eq!(filters[2].pattern, ".gitignore"); - assert_eq!(filters[2].exclude, false); - } + assert_eq!(filters.len(), 3); + assert_eq!(filters[0].pattern, "*.rs"); + assert!(!filters[0].exclude); + assert_eq!(filters[1].pattern, "test/*.rs"); + assert!(filters[1].exclude); + assert_eq!(filters[2].pattern, ".gitignore"); + assert!(!filters[2].exclude); +} - #[test] - fn test_categorize_filters() { - let filters = vec![ - PatternFilter { - pattern: String::from("*.rs"), - exclude: false, - }, - PatternFilter { - pattern: String::from("test/*.rs"), - exclude: true, - }, - ]; - let (include_patterns_filters, exclude_patterns_filters) = categorize_filters(filters); - assert_eq!(include_patterns_filters.len(), 1); - assert_eq!(exclude_patterns_filters.len(), 1); - assert_eq!(include_patterns_filters.contains("*.rs"), true); - assert_eq!(exclude_patterns_filters.contains("test/*.rs"), true); - } +#[test] +fn test_categorize_filters() { + let filters = vec![ + PatternFilter { + pattern: String::from("*.rs"), + exclude: false, + }, + PatternFilter { + pattern: String::from("test/*.rs"), + exclude: true, + }, + ]; + let (include_patterns_filters, exclude_patterns_filters) = categorize_filters(&filters); + assert_eq!(include_patterns_filters.len(), 1); + assert_eq!(exclude_patterns_filters.len(), 1); + assert!(include_patterns_filters.contains("*.rs")); + assert!(exclude_patterns_filters.contains("test/*.rs")); +} - #[test] - fn test_filter() { - let files = vec![ - String::from("src/main.rs"), - String::from("lib.rs"), - String::from("test.txt"), - ]; - let include_patterns_filters = HashSet::from([ - String::from("*.rs"), - String::from("src/**"), - String::from("*.txt"), - ]); - let exclude_patterns_filters = HashSet::from([ - String::from("test.txt") - ]); - let filtered_files = filter_files(files, include_patterns_filters, exclude_patterns_filters); - let expected_filtered_files = HashSet::from([ - String::from("src/main.rs"), - String::from("lib.rs"), - ]); +#[test] +fn test_filter() { + let files = vec![ + String::from("src/main.rs"), + String::from("lib.rs"), + String::from("test.txt"), + ]; + let include_patterns_filters = HashSet::from([ + String::from("*.rs"), + String::from("src/**"), + String::from("*.txt"), + ]); + let exclude_patterns_filters = HashSet::from([ + String::from("test.txt") + ]); + let filtered_files = filter_files(&files, &include_patterns_filters, &exclude_patterns_filters); + let expected_filtered_files = HashSet::from([ + String::from("src/main.rs"), + String::from("lib.rs"), + ]); - assert_eq!(filtered_files, expected_filtered_files); - } + assert_eq!(filtered_files, expected_filtered_files); +} - #[test] - fn test_filter_exclude_files_exclusion() { - let exclude_patterns_filters = HashSet::from([ - String::from("test.txt"), - ]); - let include_patterns_filters = HashSet::from([ - String::from("*.rs"), - String::from("*.txt"), - ]); +#[test] +fn test_filter_exclude_files_exclusion() { + let exclude_patterns_filters = HashSet::from([ + String::from("test.txt"), + ]); + let include_patterns_filters = HashSet::from([ + String::from("*.rs"), + String::from("*.txt"), + ]); - let files = vec![ - String::from("main.rs"), - String::from("lib.rs"), - String::from("version.txt"), - String::from("test.txt"), - ]; + let files = vec![ + String::from("main.rs"), + String::from("lib.rs"), + String::from("version.txt"), + String::from("test.txt"), + ]; - let filtered_files = filter_files(files, include_patterns_filters, exclude_patterns_filters); - let expected_filtered_files = HashSet::from([ - String::from("main.rs"), - String::from("lib.rs"), - String::from("version.txt"), - ]); + let filtered_files = filter_files(&files, &include_patterns_filters, &exclude_patterns_filters); + let expected_filtered_files = HashSet::from([ + String::from("main.rs"), + String::from("lib.rs"), + String::from("version.txt"), + ]); - assert_eq!(filtered_files, expected_filtered_files); - } + assert_eq!(filtered_files, expected_filtered_files); +} - #[test] - fn test_get_count() { - let files = HashSet::from([ - String::from("main.rs"), - String::from("lib.rs"), - String::from("version.txt"), - ]); - let count = get_count(files); - assert_eq!(count, 3); - } +#[test] +fn test_get_count() { + let files = HashSet::from([ + String::from("main.rs"), + String::from("lib.rs"), + String::from("version.txt"), + ]); + let count = get_count(&files); + assert_eq!(count, 3); } diff --git a/version b/version index b482243..13637f4 100644 --- a/version +++ b/version @@ -1 +1 @@ -v1.0.2 \ No newline at end of file +v1.0.3 \ No newline at end of file