Skip to content

Commit de4c618

Browse files
committed
feat(assert): Allow disabling path normalization
This handles the snapbox side of assert-rs#91. We'll need to update the plumbing in trycmd to get this to all the right places. For now this also lets us experiment with it where we can more easily change the API. We still normalize paths when inserting substitions. The two APIs aren't connected at the moment to allow fixing this. This will need more consideration.
1 parent 7cc4c7d commit de4c618

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

crates/snapbox/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
<!-- next-header -->
88
## [Unreleased] - ReleaseDate
99

10+
### Features
11+
12+
- Added `Assert::normalize_paths` to allow disabling path normalization
13+
1014
## [0.3.0] - 2022-08-01
1115

1216
### Breaking Changes

crates/snapbox/src/assert.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::Action;
1717
pub struct Assert {
1818
action: Action,
1919
action_var: Option<String>,
20+
normalize_paths: bool,
2021
substitutions: crate::Substitutions,
2122
pub(crate) palette: crate::report::Palette,
2223
pub(crate) data_format: Option<DataFormat>,
@@ -186,7 +187,7 @@ impl Assert {
186187

187188
actual = actual
188189
.try_coerce(format)
189-
.map_text(crate::utils::normalize_text);
190+
.map_text(|s| self.normalize_text(s));
190191

191192
(expected, actual)
192193
}
@@ -205,7 +206,7 @@ impl Assert {
205206
{
206207
actual = actual
207208
.try_coerce(format)
208-
.map_text(crate::utils::normalize_text)
209+
.map_text(|s| self.normalize_text(s))
209210
.map_text(|t| self.substitutions.normalize(t, expected));
210211
}
211212

@@ -289,6 +290,14 @@ impl Assert {
289290
Ok(())
290291
}
291292
}
293+
294+
fn normalize_text(&self, data: &str) -> String {
295+
let mut data = crate::utils::normalize_lines(data);
296+
if self.normalize_paths {
297+
data = crate::utils::normalize_paths(&data);
298+
}
299+
data
300+
}
292301
}
293302

294303
/// # Directory Assertions
@@ -485,6 +494,14 @@ impl Assert {
485494
self
486495
}
487496

497+
/// Specify whether text should have path separators normalized
498+
///
499+
/// The default is normalized
500+
pub fn normalize_paths(mut self, yes: bool) -> Self {
501+
self.normalize_paths = yes;
502+
self
503+
}
504+
488505
/// Specify whether the content should be treated as binary or not
489506
///
490507
/// The default is to auto-detect
@@ -507,6 +524,7 @@ impl Default for Assert {
507524
Self {
508525
action: Default::default(),
509526
action_var: Default::default(),
527+
normalize_paths: true,
510528
substitutions: Default::default(),
511529
palette: crate::report::Palette::auto(),
512530
data_format: Default::default(),

crates/snapbox/src/utils/mod.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ pub use lines::LinesWithTerminator;
44

55
/// Normalize line endings
66
pub fn normalize_lines(data: &str) -> String {
7-
normalize_line_endings::normalized(data.chars()).collect()
7+
normalize_lines_chars(data.chars()).collect()
8+
}
9+
10+
fn normalize_lines_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item = char> {
11+
normalize_line_endings::normalized(data)
12+
}
13+
14+
/// Normalize path separators
15+
pub fn normalize_paths(data: &str) -> String {
16+
normalize_paths_chars(data.chars()).collect()
17+
}
18+
19+
fn normalize_paths_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item = char> {
20+
data.map(|c| if c == '\\' { '/' } else { c })
821
}
922

1023
/// "Smart" text normalization
@@ -13,8 +26,5 @@ pub fn normalize_lines(data: &str) -> String {
1326
/// - Line endings
1427
/// - Path separators
1528
pub fn normalize_text(data: &str) -> String {
16-
normalize_line_endings::normalized(data.chars())
17-
// Also help out with Windows paths
18-
.map(|c| if c == '\\' { '/' } else { c })
19-
.collect()
29+
normalize_paths_chars(normalize_lines_chars(data.chars())).collect()
2030
}

0 commit comments

Comments
 (0)