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

feat(filtering): support comments in the value files #41

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ h2 = "0.3.26"
async_zip = { version = "0.0.17", features = ["tokio", "tokio-fs", "deflate"] }
walkdir = "2.5.0"
globset = "0.4"
regex = "1.10.5"

[dev-dependencies]
rstest = "0.18.2"
5 changes: 5 additions & 0 deletions fixture/filtering/filetype_with_comments.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filteringConfiguration: #This is our filtering configuration block
allowlist:
- type: "fully-qualified-test-name"
# The following file contains actual values
file: "tests_with_comments.txt"
12 changes: 12 additions & 0 deletions fixture/filtering/tests_with_comments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#line comment
com.example.test1
com.example.test2 #issue link = http://test.test/issue

#line comment2

com.example #flaky package

# comment3
ClassName#test4 # comment
SimpleTestName
SimpleTestName2 #comment1
26 changes: 25 additions & 1 deletion src/filtering/convert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use regex::Regex;
use shellexpand;
use std::path::{Path, PathBuf};
use tokio::{
Expand Down Expand Up @@ -265,8 +266,15 @@ async fn validate_filter(
values_file.read_to_string(&mut buffer).await?;

let mut values = Vec::new();

let comment_regex = Regex::new(r"\s+#.*$")?;
for value in buffer.lines() {
values.push(value.to_owned());
let value = value.trim();
if value.is_empty() || value.starts_with('#') {
continue;
}
let value = comment_regex.replace_all(value, "");
values.push(value.as_ref().to_owned());
}
filter.values = Some(values);
filter.file = None;
Expand Down Expand Up @@ -423,6 +431,22 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn test_filetype_with_comments() -> Result<()> {
matzuk marked this conversation as resolved.
Show resolved Hide resolved
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let fixture = Path::new(&manifest_dir)
.join("fixture")
.join("filtering")
.join("filetype_with_comments.yaml");
let result = convert(fixture).await?;
let result = serde_json::to_string(&result)?;
assert_eq!(
result,
r#"{"filteringConfiguration":{"allowlist":[{"type":"fully-qualified-test-name","values":["com.example.test1","com.example.test2","com.example","ClassName#test4","SimpleTestName","SimpleTestName2"]}]}}"#
);
Ok(())
}

#[tokio::test]
async fn test_correct_no_fields() -> Result<()> {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
Expand Down