Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coverage paths sanitization issue #1411

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions qlty-coverage/src/transformer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use anyhow::Result;
qltysh[bot] marked this conversation as resolved.
Show resolved Hide resolved
use globset::{Glob, GlobSet, GlobSetBuilder};
use qlty_analysis::utils::fs::path_to_string;
use qlty_types::tests::v1::{CoverageMetadata, CoverageSummary, FileCoverage};
use std::{
env::current_dir,
fmt::Debug,
path::{PathBuf, MAIN_SEPARATOR},
};
use std::{env::current_dir, fmt::Debug, path::PathBuf};

pub trait Transformer: Debug + Send + Sync + 'static {
fn transform(&self, file_coverage: FileCoverage) -> Option<FileCoverage>;
Expand Down Expand Up @@ -153,31 +148,34 @@ impl Transformer for AddPrefix {

#[derive(Debug, Clone)]
pub struct StripPrefix {
prefix: String,
prefix: PathBuf,
}

impl Default for StripPrefix {
fn default() -> Self {
Self {
prefix: format!(
"{}{}",
path_to_string(current_dir().unwrap_or_else(|_| PathBuf::from("."))),
MAIN_SEPARATOR
),
prefix: current_dir().unwrap_or_else(|_| PathBuf::from(".")),
}
}
}

impl StripPrefix {
pub fn new(prefix: String) -> Self {
Self { prefix }
Self {
prefix: PathBuf::from(prefix),
}
}
}

impl Transformer for StripPrefix {
fn transform(&self, file_coverage: FileCoverage) -> Option<FileCoverage> {
let mut file_coverage = file_coverage;
file_coverage.path = file_coverage.path.replacen(&self.prefix, "", 1);
let coverage_path = PathBuf::from(&file_coverage.path);

if let Ok(sanitized_path) = coverage_path.strip_prefix(&self.prefix) {
file_coverage.path = sanitized_path.to_string_lossy().to_string();
}

Some(file_coverage)
}

Expand All @@ -202,3 +200,28 @@ impl Transformer for StripDotSlashPrefix {
Box::new(Self)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_strip_prefix_transformer() {
let transformer = StripPrefix::new("/home/circleci/project".to_string());
let file_coverage = FileCoverage {
build_id: "1234".to_string(),
path: "/home/circleci/project/app/deep/nested/file.rb".to_string(),
summary: Some(CoverageSummary {
covered: 5,
missed: 5,
omit: 3,
total: 13,
}),
hits: vec![-1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1],
branch: "test-branch".to_string(),
..Default::default()
};
let file_coverage = transformer.transform(file_coverage).unwrap();
assert_eq!(file_coverage.path, "app/deep/nested/file.rb".to_string());
}
}
Loading