Skip to content

Commit 020ddd0

Browse files
brynaryclaude
andcommitted
refactor(coverage): extract is_within_workspace to shared utility
- Add is_path_within_workspace function to utils.rs - Remove wrapper methods and call utility directly from validate.rs and processor.rs - Clean up unused Path imports 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a383159 commit 020ddd0

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

qlty-coverage/src/publish/processor.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::publish::{metrics::CoverageMetrics, Plan, Report, Results};
2+
use crate::utils::is_path_within_workspace;
23
use anyhow::Result;
34
use qlty_types::tests::v1::FileCoverage;
45
use std::collections::HashSet;
5-
use std::path::{Path, PathBuf};
6+
use std::path::PathBuf;
67

78
pub struct Processor {
89
plan: Plan,
@@ -43,7 +44,7 @@ impl Processor {
4344
let path = PathBuf::from(&file_coverage.path);
4445
match path.try_exists() {
4546
Ok(true) => {
46-
if !self.is_within_workspace(&path) {
47+
if !is_path_within_workspace(&path, self.plan.workspace_root.as_ref()) {
4748
outside_workspace_files.insert(file_coverage.path.clone());
4849
false
4950
} else {
@@ -62,7 +63,7 @@ impl Processor {
6263
let path = PathBuf::from(&file_coverage.path);
6364
match path.try_exists() {
6465
Ok(true) => {
65-
if !self.is_within_workspace(&path) {
66+
if !is_path_within_workspace(&path, self.plan.workspace_root.as_ref()) {
6667
outside_workspace_files.insert(file_coverage.path.clone());
6768
} else {
6869
found_files.insert(file_coverage.path.clone());
@@ -92,17 +93,6 @@ impl Processor {
9293
})
9394
}
9495

95-
fn is_within_workspace(&self, file_path: &Path) -> bool {
96-
let Some(ref workspace_root) = self.plan.workspace_root else {
97-
return true;
98-
};
99-
100-
match (file_path.canonicalize(), workspace_root.canonicalize()) {
101-
(Ok(canonical_file), Ok(canonical_root)) => canonical_file.starts_with(&canonical_root),
102-
_ => false,
103-
}
104-
}
105-
10696
fn transform(&self, file_coverage: FileCoverage) -> Option<FileCoverage> {
10797
let mut file_coverage: Option<FileCoverage> = Some(file_coverage.clone());
10898

qlty-coverage/src/utils.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
use crate::formats::Formats;
22
use anyhow::Result;
3-
use std::path::PathBuf;
3+
use std::path::{Path, PathBuf};
44
use std::str::FromStr;
55

6+
pub fn is_path_within_workspace(file_path: &Path, workspace_root: Option<&PathBuf>) -> bool {
7+
let Some(workspace_root) = workspace_root else {
8+
return true;
9+
};
10+
11+
match (file_path.canonicalize(), workspace_root.canonicalize()) {
12+
(Ok(canonical_file), Ok(canonical_root)) => canonical_file.starts_with(&canonical_root),
13+
_ => false,
14+
}
15+
}
16+
617
// Format specification priority:
718
// 1. The format specified in the path, e.g.: simplecov:./coverage/coverage.json
819
// 2. The format specified in the command line arguments, through --report-format simplecov

qlty-coverage/src/validate.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use anyhow::Result;
22
use serde::Serialize;
3-
use std::path::{Path, PathBuf};
3+
use std::path::PathBuf;
44

55
use crate::publish::Report;
6+
use crate::utils::is_path_within_workspace;
67

78
const DEFAULT_THRESHOLD: f64 = 90.0;
89

@@ -46,7 +47,7 @@ impl Validator {
4647
return;
4748
}
4849

49-
if !self.is_within_workspace(&path) {
50+
if !is_path_within_workspace(&path, self.workspace_root.as_ref()) {
5051
validation_result.files_outside_workspace += 1;
5152
return;
5253
}
@@ -74,17 +75,6 @@ impl Validator {
7475

7576
Ok(validation_result)
7677
}
77-
78-
fn is_within_workspace(&self, file_path: &Path) -> bool {
79-
let Some(ref workspace_root) = self.workspace_root else {
80-
return true;
81-
};
82-
83-
match (file_path.canonicalize(), workspace_root.canonicalize()) {
84-
(Ok(canonical_file), Ok(canonical_root)) => canonical_file.starts_with(&canonical_root),
85-
_ => false,
86-
}
87-
}
8878
}
8979

9080
#[derive(Debug, Clone, Serialize, Default, PartialEq)]

0 commit comments

Comments
 (0)