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(filter): Rename Filters to have Filter prefix #328

Merged
merged 1 commit into from
May 23, 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
8 changes: 4 additions & 4 deletions crates/snapbox/src/assert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use anstream::stderr;
#[cfg(not(feature = "color"))]
use std::io::stderr;

use crate::filter::{Filter as _, FilterRedactions, NormalizeNewlines, NormalizePaths};
use crate::filter::{Filter as _, FilterNewlines, FilterPaths, FilterRedactions};
use crate::IntoData;

pub use action::Action;
Expand Down Expand Up @@ -175,18 +175,18 @@ impl Assert {
mut expected: crate::Data,
) -> (crate::Data, crate::Data) {
if expected.filters.is_newlines_set() {
expected = NormalizeNewlines.filter(expected);
expected = FilterNewlines.filter(expected);
}

// On `expected` being an error, make a best guess
let format = expected.intended_format();
actual = actual.coerce_to(format);

if self.normalize_paths && expected.filters.is_paths_set() {
actual = NormalizePaths.filter(actual);
actual = FilterPaths.filter(actual);
}
if expected.filters.is_newlines_set() {
actual = NormalizeNewlines.filter(actual);
actual = FilterNewlines.filter(actual);
}
if expected.filters.is_redaction_set() {
actual = FilterRedactions::new(&self.substitutions, &expected).filter(actual);
Expand Down
4 changes: 2 additions & 2 deletions crates/snapbox/src/data/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use super::Data;

pub use crate::filter::Filter as Normalize;

#[deprecated(since = "0.5.11", note = "Replaced with `filter::NormalizeNewlines")]
#[deprecated(since = "0.5.11", note = "Replaced with `filter::FilterNewlines")]
pub struct NormalizeNewlines;
impl Normalize for NormalizeNewlines {
fn normalize(&self, data: Data) -> Data {
crate::filter::NormalizeNewlines.normalize(data)
}
}

#[deprecated(since = "0.5.11", note = "Replaced with `filter::NormalizePaths")]
#[deprecated(since = "0.5.11", note = "Replaced with `filter::FilterPaths")]
pub struct NormalizePaths;
impl Normalize for NormalizePaths {
fn normalize(&self, data: Data) -> Data {
Expand Down
12 changes: 6 additions & 6 deletions crates/snapbox/src/dir/diff.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "dir")]
use crate::filter::{Filter as _, FilterRedactions, NormalizeNewlines, NormalizePaths};
use crate::filter::{Filter as _, FilterNewlines, FilterPaths, FilterRedactions};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PathDiff {
Expand Down Expand Up @@ -78,9 +78,9 @@ impl PathDiff {
crate::Data::try_read_from(&actual_path, None).map_err(Self::Failure)?;

let expected =
NormalizeNewlines.filter(crate::Data::read_from(&expected_path, None));
FilterNewlines.filter(crate::Data::read_from(&expected_path, None));

actual = NormalizeNewlines.filter(actual.coerce_to(expected.intended_format()));
actual = FilterNewlines.filter(actual.coerce_to(expected.intended_format()));

if expected != actual {
return Err(Self::ContentMismatch {
Expand Down Expand Up @@ -154,14 +154,14 @@ impl PathDiff {
crate::Data::try_read_from(&actual_path, None).map_err(Self::Failure)?;

let expected =
NormalizeNewlines.filter(crate::Data::read_from(&expected_path, None));
FilterNewlines.filter(crate::Data::read_from(&expected_path, None));

actual = actual.coerce_to(expected.intended_format());
if normalize_paths {
actual = NormalizePaths.filter(actual);
actual = FilterPaths.filter(actual);
}
actual = FilterRedactions::new(substitutions, &expected)
.filter(NormalizeNewlines.filter(actual));
.filter(FilterNewlines.filter(actual));

if expected != actual {
return Err(Self::ContentMismatch {
Expand Down
22 changes: 20 additions & 2 deletions crates/snapbox/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,26 @@ pub trait Filter {
}
}

#[deprecated(since = "0.5.11", note = "Replaced with `filter::FilterNewlines")]
pub struct NormalizeNewlines;
#[allow(deprecated)]
impl Filter for NormalizeNewlines {
fn normalize(&self, data: Data) -> Data {
FilterNewlines.normalize(data)
}
}

#[deprecated(since = "0.5.11", note = "Replaced with `filter::FilterPaths")]
pub struct NormalizePaths;
#[allow(deprecated)]
impl Filter for NormalizePaths {
fn normalize(&self, data: Data) -> Data {
FilterPaths.normalize(data)
}
}

pub struct FilterNewlines;
impl Filter for FilterNewlines {
fn normalize(&self, data: Data) -> Data {
let source = data.source;
let filters = data.filters;
Expand Down Expand Up @@ -64,8 +82,8 @@ fn normalize_lines_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item
normalize_line_endings::normalized(data)
}

pub struct NormalizePaths;
impl Filter for NormalizePaths {
pub struct FilterPaths;
impl Filter for FilterPaths {
fn normalize(&self, data: Data) -> Data {
let source = data.source;
let filters = data.filters;
Expand Down
16 changes: 8 additions & 8 deletions crates/snapbox/src/filter/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use super::*;
fn json_normalize_paths_and_lines() {
let json = json!({"name": "John\\Doe\r\n"});
let data = Data::json(json);
let data = NormalizePaths.filter(data);
let data = FilterPaths.filter(data);
assert_eq!(Data::json(json!({"name": "John/Doe\r\n"})), data);
let data = NormalizeNewlines.filter(data);
let data = FilterNewlines.filter(data);
assert_eq!(Data::json(json!({"name": "John/Doe\n"})), data);
}

Expand All @@ -26,15 +26,15 @@ fn json_normalize_obj_paths_and_lines() {
}
});
let data = Data::json(json);
let data = NormalizePaths.filter(data);
let data = FilterPaths.filter(data);
let assert = json!({
"person": {
"name": "John/Doe\r\n",
"nickname": "Jo/hn\r\n",
}
});
assert_eq!(Data::json(assert), data);
let data = NormalizeNewlines.filter(data);
let data = FilterNewlines.filter(data);
let assert = json!({
"person": {
"name": "John/Doe\n",
Expand All @@ -49,10 +49,10 @@ fn json_normalize_obj_paths_and_lines() {
fn json_normalize_array_paths_and_lines() {
let json = json!({"people": ["John\\Doe\r\n", "Jo\\hn\r\n"]});
let data = Data::json(json);
let data = NormalizePaths.filter(data);
let data = FilterPaths.filter(data);
let paths = json!({"people": ["John/Doe\r\n", "Jo/hn\r\n"]});
assert_eq!(Data::json(paths), data);
let data = NormalizeNewlines.filter(data);
let data = FilterNewlines.filter(data);
let new_lines = json!({"people": ["John/Doe\n", "Jo/hn\n"]});
assert_eq!(Data::json(new_lines), data);
}
Expand All @@ -69,7 +69,7 @@ fn json_normalize_array_obj_paths_and_lines() {
]
});
let data = Data::json(json);
let data = NormalizePaths.filter(data);
let data = FilterPaths.filter(data);
let paths = json!({
"people": [
{
Expand All @@ -79,7 +79,7 @@ fn json_normalize_array_obj_paths_and_lines() {
]
});
assert_eq!(Data::json(paths), data);
let data = NormalizeNewlines.filter(data);
let data = FilterNewlines.filter(data);
let new_lines = json!({
"people": [
{
Expand Down
4 changes: 2 additions & 2 deletions crates/trycmd/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::io::stderr;
use rayon::prelude::*;
use snapbox::data::DataFormat;
use snapbox::dir::FileType;
use snapbox::filter::{Filter as _, FilterRedactions, NormalizeNewlines, NormalizePaths};
use snapbox::filter::{Filter as _, FilterNewlines, FilterPaths, FilterRedactions};
use snapbox::IntoData;

#[derive(Debug)]
Expand Down Expand Up @@ -738,7 +738,7 @@ impl Stream {
if content.format() != DataFormat::Text {
self.status = StreamStatus::Failure("Unable to convert underlying Data to Text".into());
}
self.content = NormalizeNewlines.filter(NormalizePaths.filter(content));
self.content = FilterNewlines.filter(FilterPaths.filter(content));
self
}

Expand Down
10 changes: 5 additions & 5 deletions crates/trycmd/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! [`OneShot`] is the top-level item in the `cmd.toml` files.

use snapbox::filter::{Filter as _, NormalizeNewlines, NormalizePaths};
use snapbox::filter::{Filter as _, FilterNewlines, FilterPaths};
use std::collections::BTreeMap;
use std::collections::VecDeque;

Expand Down Expand Up @@ -40,8 +40,8 @@ impl TryCmd {
let stdout_path = path.with_extension("stdout");
let stdout = if stdout_path.exists() {
Some(
NormalizeNewlines.filter(
NormalizePaths
FilterNewlines.filter(
FilterPaths
.filter(crate::Data::read_from(&stdout_path, Some(is_binary))),
),
)
Expand All @@ -55,8 +55,8 @@ impl TryCmd {
let stderr_path = path.with_extension("stderr");
let stderr = if stderr_path.exists() {
Some(
NormalizeNewlines.filter(
NormalizePaths
FilterNewlines.filter(
FilterPaths
.filter(crate::Data::read_from(&stderr_path, Some(is_binary))),
),
)
Expand Down
Loading