Skip to content

Commit 392619c

Browse files
committed
fix tests
1 parent cb792fd commit 392619c

File tree

5 files changed

+29
-58
lines changed

5 files changed

+29
-58
lines changed

text/diff_util/file_diff.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ impl<'a> FileDiff<'a> {
3131
self.are_different
3232
}
3333

34+
pub fn set_different(&mut self, are_different: bool) {
35+
self.are_different = are_different;
36+
}
37+
3438
fn new(
3539
file1: &'a mut FileData<'a>,
3640
file2: &'a mut FileData<'a>,
@@ -59,15 +63,23 @@ impl<'a> FileDiff<'a> {
5963
let ends_with_newline1 = linereader1.ends_with_newline();
6064
let mut lines1 = Vec::new();
6165
for line in linereader1 {
62-
lines1.push(line);
66+
if !format_options.ignore_trailing_white_spaces {
67+
lines1.push(line);
68+
} else {
69+
lines1.push(line.trim_end());
70+
}
6371
}
6472

6573
let content2 = read_to_string(&path2)?.into_bytes();
6674
let linereader2 = LineReader::new(&content2);
6775
let ends_with_newline2 = linereader2.ends_with_newline();
6876
let mut lines2 = Vec::new();
6977
for line in linereader2 {
70-
lines2.push(line);
78+
if !format_options.ignore_trailing_white_spaces {
79+
lines2.push(line);
80+
} else {
81+
lines2.push(line.trim_end());
82+
}
7183
}
7284
let mut file1 = FileData::get_file(path1, lines1, ends_with_newline1)?;
7385
let mut file2 = FileData::get_file(path2, lines2, ends_with_newline2)?;
@@ -91,6 +103,10 @@ impl<'a> FileDiff<'a> {
91103
diff.hunks
92104
.create_hunks_from_lcs(&lcs_indices, num_lines1, num_lines2);
93105

106+
if diff.hunks.hunk_count() > 0 {
107+
diff.set_different(true);
108+
}
109+
94110
if diff.are_different() {
95111
if let Some(show_if_different) = show_if_different {
96112
println!("{}", show_if_different);
@@ -186,7 +202,7 @@ impl<'a> FileDiff<'a> {
186202
eprintln!("OutputFormat::Context should be handled in other place");
187203
return Ok(DiffExitStatus::Trouble);
188204
}
189-
OutputFormat::ForwardEditScript => hunk.print_forward_edit_script(
205+
OutputFormat::ForwardEditScript => hunk.print_edit_script(
190206
self.file1,
191207
self.file2,
192208
hunk_index == hunks_count - 1,
@@ -269,7 +285,7 @@ impl<'a> FileDiff<'a> {
269285
.map(|(k, _v)| *k);
270286

271287
match key {
272-
None => {},
288+
None => {}
273289
Some(k) => {
274290
let rec = hist.get(k).unwrap();
275291
let x1_new = rec[1] as usize;

text/diff_util/hunks.rs

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl Hunk {
3939
} else if is_ed && (self.ln1_start == self.ln1_end - 1) {
4040
format!("{}", self.ln1_end)
4141
} else {
42-
format!("{},{}", self.ln1_start, self.ln1_end)
42+
format!("{},{}", self.ln1_start+1, self.ln1_end)
4343
}
4444
}
4545

@@ -49,7 +49,7 @@ impl Hunk {
4949
} else if is_ed && (self.ln2_start == self.ln2_end - 1) {
5050
format!("{}", self.ln2_end)
5151
} else {
52-
format!("{},{}", self.ln2_start, self.ln2_end)
52+
format!("{},{}", self.ln2_start+1, self.ln2_end)
5353
}
5454
}
5555

@@ -74,14 +74,14 @@ impl Hunk {
7474
Change::None => {}
7575
Change::Unchanged(_) => {}
7676
Change::Insert(_) => {
77-
println!("{}a{}", self.ln1_start, self.f2_range(false));
77+
println!("{}a{}", self.ln1_start, self.f2_range(true));
7878

7979
for i in self.ln2_start..self.ln2_end {
8080
println!("> {}", file2.line(i));
8181
}
8282
}
8383
Change::Delete(_) => {
84-
println!("{}d{}", self.f1_range(false), self.ln2_end);
84+
println!("{}d{}", self.f1_range(true), self.ln2_end);
8585

8686
for i in self.ln1_start..self.ln1_end {
8787
println!("< {}", file1.line(i));
@@ -92,7 +92,7 @@ impl Hunk {
9292
}
9393
}
9494
Change::Substitute(_) => {
95-
println!("{}c{}", self.f1_range(false), self.f2_range(false));
95+
println!("{}c{}", self.f1_range(true), self.f2_range(true));
9696

9797
for i in self.ln1_start..self.ln1_end {
9898
println!("< {}", file1.line(i));
@@ -204,52 +204,6 @@ impl Hunk {
204204
);
205205
}
206206
}
207-
208-
pub fn print_forward_edit_script(&mut self, file1: &FileData, file2: &FileData, is_last: bool) {
209-
match &self.kind {
210-
Change::None => {}
211-
Change::Unchanged(_) => {}
212-
Change::Insert(_) => {
213-
self.changes.sort_by_key(|change| change.get_ln2());
214-
215-
println!("a{}", self.ln1_end);
216-
for change in &self.changes {
217-
println!("{}", file2.line(change.get_ln2() - 1));
218-
}
219-
220-
println!(".")
221-
}
222-
Change::Delete(_) => {
223-
println!("d{}", self.f1_range(true).replace(",", " "));
224-
}
225-
Change::Substitute(_) => {
226-
self.changes.sort_by_key(|change| change.get_ln2());
227-
println!("c{}", self.f1_range(true).replace(",", " "));
228-
229-
for change in &self.changes {
230-
println!("{}", file2.line(change.get_ln2() - 1));
231-
}
232-
233-
println!(".")
234-
}
235-
}
236-
237-
if is_last && !file1.ends_with_newline() {
238-
println!(
239-
"diff: {}:{}\n",
240-
file1.name(),
241-
&NO_NEW_LINE_AT_END_OF_FILE[1..]
242-
);
243-
}
244-
245-
if is_last && !file2.ends_with_newline() {
246-
println!(
247-
"diff: {}:{}\n",
248-
file2.name(),
249-
&NO_NEW_LINE_AT_END_OF_FILE[1..]
250-
);
251-
}
252-
}
253207
}
254208

255209
pub struct Hunks {
@@ -275,6 +229,10 @@ impl Hunks {
275229
&mut self.hunks[index]
276230
}
277231

232+
pub fn hunk_count(&self) -> usize {
233+
self.hunks.len()
234+
}
235+
278236
pub fn create_hunks_from_lcs(
279237
&mut self,
280238
lcs_indices: &[i32],

text/tests/diff/f1.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ line 4
55
line 5
66
line 6 will change
77
line 7
8-
98
This line will be removed
109

1110
line 8

text/tests/diff/f1/f1.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ line 4
55
line 5
66
line 6 will change
77
line 7
8-
98
This line will be removed
109

1110
line 8

text/tests/diff/f1_with_eol_spaces.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ line 4
55
line 5
66
line 6 will change
77
line 7
8-
98
This line will be removed
109

1110
line 8

0 commit comments

Comments
 (0)