Skip to content

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

Closed
@MihailPereverza

Description

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.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions