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

Comment on lines +30 to +34
impl Debug for CheckDiffError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CheckDiffError").finish()
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets #[derive(Debug)] instead of manually defining it. You'll probably also need to add a #[derive(Debug)] annotation to the GitError enum.

Comment on lines +140 to +149
let output = Command::new(&self.binary_path)
.env("LD_LIBRARY_PATH", &self.ld_library_path)
.args([
"--unstable-features",
"--skip-children",
"--emit=stdout",
config.as_str(),
code,
])
.output()?;
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 we'll need to tweak this slightly. We want to pass the code to rustfmt via stdin, and in order to do that we probably need to do something similar to rust-analyzer.

Also, I think we could simplify creating the config arg above. We should probably abstract that into a standalone function.

Comment on lines +89 to +90
let diff = diffy::create_patch(src_format.as_str(), feature_format.as_str());
Ok(format!("{diff}"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this was in my suggestion when messaging on Zulip. I think this should be fine, but we could also consider returning a diffy::Patch or maybe even our own wrapper type.

It would be really nice if the diff::Patch gave us some way to tell if the patch was empty, but I don't think it does so we probably need to roll our own way to figure that out.

Comment on lines +346 to +348
for entry in WalkDir::new(".").into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
if path.is_file() && path.extension().map_or(false, |ext| ext == "rs") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can call WalkDir::new("."). We need to pass the path for the repository that we want to run the check_diff function on. Remember that we're going to clone various 3rd party repos to run the check-diff on them. We should update the function parameters of check_diff to take the path we want to search in.

Also, I still think it would be great if we abstracted the .rs file search into a function that returned an Iterator of Path or PathBuf objects so that check_diff isn't responsible for that logic. Plus it'll be a lot easier to unit test that we're searching for all .rs files within a directory if we break it out.

if path.is_file() && path.extension().map_or(false, |ext| ext == "rs") {
match runners.create_diff(path, &config) {
Ok(diff) => {
if !diff.is_empty() {
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 what I was getting at earlier. We need a convenient way to check if the diff is empty. I haven't worked with the diffy crate before so I don't know what it outputs when there is no diff. Does it still include the diff header like

--- a
+++ b

or is it just an empty string?

It would be great if you could investigate that / create a unit test for that behavior.

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