-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added option to specify diff file location (#410)
* Added option to specify diff-file * Added section for diff-file in README * Add unit tests for GitDiffFileTool class * Reformatted files * Ordered imports --------- Co-authored-by: jostmann <[email protected]>
- Loading branch information
Showing
4 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# pylint: disable=missing-function-docstring | ||
|
||
"""Test for diff_cover.git_diff.GitDiffFileTool""" | ||
|
||
import pytest | ||
|
||
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) | ||
|
||
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) | ||
|
||
_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" | ||
|
||
|
||
def test_large_diff_file(mock_file, diff_tool): | ||
large_diff = "diff --git a/file1 b/file2\n" * 1000000 | ||
|
||
mock_file(large_diff) | ||
|
||
_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" | ||
|
||
|
||
def test_diff_committed(mock_file, diff_tool): | ||
diff = "diff --git a/file1 b/file2\n" | ||
|
||
mock_file(diff) | ||
|
||
_diff_tool = diff_tool("diff_file.txt") | ||
|
||
assert _diff_tool.diff_committed() == diff | ||
assert _diff_tool.diff_file_path == "diff_file.txt" | ||
|
||
|
||
def test_empty_diff_file(mock_file, diff_tool): | ||
empty_diff = "" | ||
|
||
mock_file(empty_diff) | ||
|
||
_diff_tool = diff_tool("empty_diff.txt") | ||
|
||
assert _diff_tool.diff_committed() == empty_diff | ||
assert _diff_tool.diff_file_path == "empty_diff.txt" |