-
Notifications
You must be signed in to change notification settings - Fork 892
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
check_diff
function rewrite
#6390
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're on the right track here, but I think there are a few things we need to double check.
- The
Display
output ofdiffy::Patch
under a few different scenarios:
- when the two
&str
inputs todiffy::create_patch
are the same - when the two
&str
inputs todiffy::create_patch
are different - The best way I can think to test this would be to create a new trait
CodeFormatter
with a single functionformat_code
that matches the definition we've already defined forRustfmtRunner::format_code
. Then you can implement the trait forRustfmtRunner
using the current definition, and makeCheckDiffRunners
generic overT: CodeFormatter
. Should be simple enough at that point to create a unit test where you initialize aCheckDiffRunners
with mock implementation ofCodeFormatter
to test the output of callingCheckDiffRunners::create_diff
-
Invoking rustfmt correctly. We probably should write a unit test where we at least compile rustfmt from source and run it on a simple misformatted input and make sure we get back the expected output. We should also review how rust-analyzer calls rustfmt and follow a similar approach, but adapt it for the arguments we want to pass when calling rustfmt.
-
Abstracting the
.rs
files search and unit testing it to make sure that we're picking up all.rs
files in the current directory and any nested directories.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making some updates! Take a look at the inline comments below.
result.push(','); | ||
for arg in configs.iter() { | ||
result.push_str(arg.as_str()); | ||
result.push(','); | ||
} | ||
result.pop(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be simplified. What we need to do is make sure we add a leading comma before each user supplied option:
result.push(','); | |
for arg in configs.iter() { | |
result.push_str(arg.as_str()); | |
result.push(','); | |
} | |
result.pop(); | |
for arg in configs.iter() { | |
result.push(','); | |
result.push_str(arg.as_str()); | |
} |
Sine we're concatenating the user supplied options with error_on_line_overflow=false,error_on_unformatted=false
the result would be something like:
error_on_line_overflow=false,error_on_unformatted=false,another_config=another_value
None => String::new(), | ||
}; | ||
let config = format!( | ||
"error_on_line_overflow=false,error_on_unformatted=false{}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we need to start this with --config=
"error_on_line_overflow=false,error_on_unformatted=false{}", | |
"--config=error_on_line_overflow=false,error_on_unformatted=false{}", |
fn search_for_rs_files(repo: &Path) -> impl Iterator<Item = PathBuf> { | ||
return WalkDir::new(repo) | ||
.into_iter() | ||
.filter_map(|e| e.ok()) | ||
.filter(|entry| { | ||
let path = entry.path(); | ||
return path.is_file() && path.extension().map_or(false, |ext| ext == "rs"); | ||
}) | ||
.map(|entry| entry.into_path()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really happy to see that this has been abstracted out. Is there a way to combine these chained calls to filter_map
, filter
, and map
into a single filter_map
call?
search_for_rs_files(repo).for_each(|file| match runners.create_diff(file.as_path(), &config) { | ||
Ok(diff) => { | ||
if !diff.is_empty() { | ||
eprint!("{diff}"); | ||
errors += 1; | ||
} | ||
} | ||
Err(e) => { | ||
eprintln!( | ||
"Error creating diff for {:?}: {:?}", | ||
file.as_path().display(), | ||
e | ||
); | ||
errors += 1; | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personal preference, but I really think the code is clearer when written using a for loop
. Please update this when you get a chance.
use diffy::{self, create_patch}; | ||
|
||
#[test] | ||
fn diffy_test_diff() { | ||
let original = "The quick brown fox jumps over the lazy dog"; | ||
let modified = "The quick brown fox jumps over the LAZY dog"; | ||
|
||
let patch = create_patch(original, modified); | ||
// diffy uses hunks which indicates the lines that are different | ||
assert_eq!(patch.hunks().is_empty(), false); | ||
// hence regardless, patch.to_string() will never be empty | ||
assert_eq!(patch.to_string().is_empty(), false); | ||
} | ||
|
||
#[test] | ||
fn diffy_test_no_diff() { | ||
let original = "The quick brown fox jumps over the lazy dog"; | ||
|
||
let patch = create_patch(original, original); | ||
assert_eq!(patch.hunks().is_empty(), true); | ||
assert_eq!(patch.to_string().is_empty(), false); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good start, and definitely helps demonstrate what we can expect to get back from create_path
when the input is the same and when it's different, but I don't think we should be directly testing a 3rd party API in this way. We should be calling our own functions e.g CheckDiffRunners::create_diff
, and making assertions based on what they return.
No description provided.