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

check_diff function rewrite #6390

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

benluiwj
Copy link
Contributor

@benluiwj benluiwj commented Nov 9, 2024

No description provided.

@benluiwj benluiwj marked this pull request as ready for review November 10, 2024 07:07
Copy link
Contributor

@ytmimi ytmimi left a 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.

  1. The Display output of diffy::Patch under a few different scenarios:
  • when the two &str inputs to diffy::create_patch are the same
  • when the two &str inputs to diffy::create_patch are different
  • The best way I can think to test this would be to create a new trait CodeFormatter with a single function format_code that matches the definition we've already defined for RustfmtRunner::format_code. Then you can implement the trait for RustfmtRunner using the current definition, and make CheckDiffRunners generic over T: CodeFormatter. Should be simple enough at that point to create a unit test where you initialize a CheckDiffRunners with mock implementation of CodeFormatter to test the output of calling CheckDiffRunners::create_diff
  1. 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.

  2. 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.

check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@ytmimi ytmimi left a 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.

Comment on lines +146 to +151
result.push(',');
for arg in configs.iter() {
result.push_str(arg.as_str());
result.push(',');
}
result.pop();
Copy link
Contributor

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:

Suggested change
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{}",
Copy link
Contributor

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=

Suggested change
"error_on_line_overflow=false,error_on_unformatted=false{}",
"--config=error_on_line_overflow=false,error_on_unformatted=false{}",

Comment on lines +351 to +360
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());
}
Copy link
Contributor

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?

Comment on lines +364 to +379
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;
}
});
Copy link
Contributor

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.

Comment on lines +1 to +22
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);
}
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants