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

UnicodeDecodeError when an untracked binary file is present in the repository #412

Closed
MihailPereverza opened this issue Jul 12, 2024 · 2 comments · Fixed by #413
Closed

Comments

@MihailPereverza
Copy link
Contributor

Problem description
I have discovered a bug in the diff-cover library. When an untracked binary file is present in the repository, the library fails to read it and throws the following error:

  File "/Users/user/PycharmProjects/some-project/.direnv/python-3.12.3/lib/python3.12/site-packages/diff_cover/diff_reporter.py", line 172, in src_paths_changed
    num_lines = len(file_handle.readlines())
                    ^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte

Steps to Reproduce:

  1. Create a new repository or use an existing one.
  2. Add a binary file to the repository that is not tracked by the version control system (untracked file).
  3. Run diff-cover on this repository with --include-untracked flag

Environment:
diff-cover version: 6.5.1, 9.1.0
Python version: 3.12
Operating System: mac os [Sonomna 14.5]

Additional Information:
The problem is likely related to attempting to read all untracked. It might be helpful to use the mechanism from

self._git_diff()

that checks

self._is_path_excluded(src_path) 

and:

if (
   not self._supported_extensions
   or extension in self._supported_extensions
):

This will ensure that only supported file types are read, preventing the UnicodeDecodeError when encountering binary files.

@Bachmann1234
Copy link
Owner

Thanks for the detailed report. I think this sounds reasonable. I was hoping to give this a deeper look this weekend but I have run out of time. Hopefully I can give this a look this week.

@MihailPereverza
Copy link
Contributor Author

Hi @Bachmann1234,

I've put together a simple solution that leverages existing mechanisms within the project. You can find the draft PR with my proposed changes here.

If this approach looks good to you, I'd be more than happy to see it through to completion. I'm excited about the opportunity to contribute to the maintenance of this library and to give back to the community.

Looking forward to your feedback!

MihailPereverza pushed a commit to MihailPereverza/diff_cover that referenced this issue Jul 17, 2024
Bachmann1234 pushed a commit that referenced this issue Jul 18, 2024
Co-authored-by: m.pereverza <m.pereverza@cian.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants