Skip to content

Commit

Permalink
Reformatted files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsostmann committed Jun 24, 2024
1 parent af6c42d commit 9c3890e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
10 changes: 5 additions & 5 deletions diff_cover/diff_cover_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,8 @@ def parse_coverage_args(argv):
"-c", "--config-file", help=CONFIG_FILE_HELP, metavar="CONFIG_FILE"
)

parser.add_argument(
"--diff-file", type=str, default=None, help=DIFF_FILE_HELP
)

parser.add_argument("--diff-file", type=str, default=None, help=DIFF_FILE_HELP)

defaults = {
"show_uncovered": False,
"compare_branch": "origin/main",
Expand Down Expand Up @@ -288,7 +286,9 @@ def main(argv=None, directory=None):
diff_tool = None

if not arg_dict["diff_file"]:
diff_tool = GitDiffTool(arg_dict["diff_range_notation"], arg_dict["ignore_whitespace"])
diff_tool = GitDiffTool(
arg_dict["diff_range_notation"], arg_dict["ignore_whitespace"]
)
else:
diff_tool = GitDiffFileTool(arg_dict["diff_file"])

Expand Down
15 changes: 8 additions & 7 deletions diff_cover/git_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,36 +111,37 @@ def untracked(self):
return []
return [line for line in output.splitlines() if line]


class GitDiffFileTool(GitDiffTool):

def __init__(self, diff_file_path):

self.diff_file_path = diff_file_path
super().__init__("...", False)

def diff_committed(self, compare_branch="origin/main"):
"""
Returns the contents of a diff file.
Raises a `GitDiffError` if the file cannot be read.
"""
try:
with open(self.diff_file_path, 'r') as file:
with open(self.diff_file_path, "r") as file:
return file.read()
except IOError as e:
raise ValueError(
dedent(
f"""
Could not read the diff file. Make sure '{self.diff_file_path}' exists?
"""
)
)

)

def diff_unstaged(self):
return ""

def diff_staged(self):
return ""

def untracked(self):
return ""
return ""
41 changes: 26 additions & 15 deletions tests/test_git_diff_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,67 @@

from diff_cover.git_diff import GitDiffFileTool


@pytest.fixture
def mock_file(mocker):
def _inner(file_content):
mock_open = mocker.mock_open(read_data=file_content)
mocker.patch('builtins.open', mock_open)
mocker.patch("builtins.open", mock_open)

return _inner


@pytest.fixture
def diff_tool():
def _inner(file):
return GitDiffFileTool(file)

return _inner


def test_diff_file_not_found(mocker, diff_tool):
mocker.patch('builtins.open', side_effect=IOError)
mocker.patch("builtins.open", side_effect=IOError)

_diff_tool = diff_tool('non_existent_diff_file.txt')
_diff_tool = diff_tool("non_existent_diff_file.txt")

with pytest.raises(ValueError) as excinfo:
_diff_tool.diff_committed()

assert f"Could not read the diff file. Make sure '{_diff_tool.diff_file_path}' exists?" in str(excinfo.value)
assert _diff_tool.diff_file_path == 'non_existent_diff_file.txt'
assert (
f"Could not read the diff file. Make sure '{_diff_tool.diff_file_path}' exists?"
in str(excinfo.value)
)
assert _diff_tool.diff_file_path == "non_existent_diff_file.txt"


def test_large_diff_file(mock_file, diff_tool):
large_diff = 'diff --git a/file1 b/file2\n' * 1000000
large_diff = "diff --git a/file1 b/file2\n" * 1000000

mock_file(large_diff)

_diff_tool = diff_tool('large_diff_file.txt')
_diff_tool = diff_tool("large_diff_file.txt")

assert _diff_tool.diff_committed() == large_diff
assert _diff_tool.diff_file_path == 'large_diff_file.txt'
assert _diff_tool.diff_file_path == "large_diff_file.txt"


def test_diff_committed(mock_file, diff_tool):
diff = 'diff --git a/file1 b/file2\n'
diff = "diff --git a/file1 b/file2\n"

mock_file(diff)

_diff_tool = diff_tool('diff_file.txt')
_diff_tool = diff_tool("diff_file.txt")

assert _diff_tool.diff_committed() == diff
assert _diff_tool.diff_file_path == 'diff_file.txt'
assert _diff_tool.diff_file_path == "diff_file.txt"


def test_empty_diff_file(mock_file, diff_tool):
empty_diff = ''
empty_diff = ""

mock_file(empty_diff)
_diff_tool = diff_tool('empty_diff.txt')

_diff_tool = diff_tool("empty_diff.txt")

assert _diff_tool.diff_committed() == empty_diff
assert _diff_tool.diff_file_path == 'empty_diff.txt'
assert _diff_tool.diff_file_path == "empty_diff.txt"

0 comments on commit 9c3890e

Please sign in to comment.