summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIan Cordasco <graffatcolmingov@gmail.com>2016-06-07 10:14:45 -0500
committerIan Cordasco <graffatcolmingov@gmail.com>2016-06-07 10:14:45 -0500
commit9ebaa5c69c7dc7077d1f59141a863d22e23284c0 (patch)
tree3fe590afc819143827298f7db579af426e686772 /tests
parent02bcbee245b9b5a0c75345ada0f1d7e70d19b3c0 (diff)
downloadflake8-9ebaa5c69c7dc7077d1f59141a863d22e23284c0.tar.gz
Add tests for parse_unified_diff
We could probably use non-git diff fixtures, but those are what we have for now.
Diffstat (limited to 'tests')
-rw-r--r--tests/fixtures/diffs/multi_file_diff130
-rw-r--r--tests/fixtures/diffs/single_file_diff27
-rw-r--r--tests/fixtures/diffs/two_file_diff45
-rw-r--r--tests/unit/test_utils.py35
4 files changed, 237 insertions, 0 deletions
diff --git a/tests/fixtures/diffs/multi_file_diff b/tests/fixtures/diffs/multi_file_diff
new file mode 100644
index 0000000..de86209
--- /dev/null
+++ b/tests/fixtures/diffs/multi_file_diff
@@ -0,0 +1,130 @@
+diff --git a/flake8/utils.py b/flake8/utils.py
+index f6ce384..7cd12b0 100644
+--- a/flake8/utils.py
++++ b/flake8/utils.py
+@@ -75,8 +75,8 @@ def stdin_get_value():
+ return cached_value.getvalue()
+
+
+-def parse_unified_diff():
+- # type: () -> List[str]
++def parse_unified_diff(diff=None):
++ # type: (str) -> List[str]
+ """Parse the unified diff passed on stdin.
+
+ :returns:
+@@ -84,7 +84,10 @@ def parse_unified_diff():
+ :rtype:
+ dict
+ """
+- diff = stdin_get_value()
++ # Allow us to not have to patch out stdin_get_value
++ if diff is None:
++ diff = stdin_get_value()
++
+ number_of_rows = None
+ current_path = None
+ parsed_paths = collections.defaultdict(set)
+diff --git a/tests/fixtures/diffs/single_file_diff b/tests/fixtures/diffs/single_file_diff
+new file mode 100644
+index 0000000..77ca534
+--- /dev/null
++++ b/tests/fixtures/diffs/single_file_diff
+@@ -0,0 +1,27 @@
++diff --git a/flake8/utils.py b/flake8/utils.py
++index f6ce384..7cd12b0 100644
++--- a/flake8/utils.py
+++++ b/flake8/utils.py
++@@ -75,8 +75,8 @@ def stdin_get_value():
++ return cached_value.getvalue()
++
++
++-def parse_unified_diff():
++- # type: () -> List[str]
+++def parse_unified_diff(diff=None):
+++ # type: (str) -> List[str]
++ """Parse the unified diff passed on stdin.
++
++ :returns:
++@@ -84,7 +84,10 @@ def parse_unified_diff():
++ :rtype:
++ dict
++ """
++- diff = stdin_get_value()
+++ # Allow us to not have to patch out stdin_get_value
+++ if diff is None:
+++ diff = stdin_get_value()
+++
++ number_of_rows = None
++ current_path = None
++ parsed_paths = collections.defaultdict(set)
+diff --git a/tests/fixtures/diffs/two_file_diff b/tests/fixtures/diffs/two_file_diff
+new file mode 100644
+index 0000000..5bd35cd
+--- /dev/null
++++ b/tests/fixtures/diffs/two_file_diff
+@@ -0,0 +1,45 @@
++diff --git a/flake8/utils.py b/flake8/utils.py
++index f6ce384..7cd12b0 100644
++--- a/flake8/utils.py
+++++ b/flake8/utils.py
++@@ -75,8 +75,8 @@ def stdin_get_value():
++ return cached_value.getvalue()
++
++
++-def parse_unified_diff():
++- # type: () -> List[str]
+++def parse_unified_diff(diff=None):
+++ # type: (str) -> List[str]
++ """Parse the unified diff passed on stdin.
++
++ :returns:
++@@ -84,7 +84,10 @@ def parse_unified_diff():
++ :rtype:
++ dict
++ """
++- diff = stdin_get_value()
+++ # Allow us to not have to patch out stdin_get_value
+++ if diff is None:
+++ diff = stdin_get_value()
+++
++ number_of_rows = None
++ current_path = None
++ parsed_paths = collections.defaultdict(set)
++diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
++index d69d939..21482ce 100644
++--- a/tests/unit/test_utils.py
+++++ b/tests/unit/test_utils.py
++@@ -115,3 +115,13 @@ def test_parameters_for_function_plugin():
++ plugin = plugin_manager.Plugin('plugin-name', object())
++ plugin._plugin = fake_plugin
++ assert utils.parameters_for(plugin) == ['physical_line', 'self', 'tree']
+++
+++
+++def read_diff_file(filename):
+++ """Read the diff file in its entirety."""
+++ with open(filename, 'r') as fd:
+++ content = fd.read()
+++ return content
+++
+++
+++SINGLE_FILE_DIFF = read_diff_file('tests/fixtures/diffs/single_file_diff')
+diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
+index d69d939..1461369 100644
+--- a/tests/unit/test_utils.py
++++ b/tests/unit/test_utils.py
+@@ -115,3 +115,14 @@ def test_parameters_for_function_plugin():
+ plugin = plugin_manager.Plugin('plugin-name', object())
+ plugin._plugin = fake_plugin
+ assert utils.parameters_for(plugin) == ['physical_line', 'self', 'tree']
++
++
++def read_diff_file(filename):
++ """Read the diff file in its entirety."""
++ with open(filename, 'r') as fd:
++ content = fd.read()
++ return content
++
++
++SINGLE_FILE_DIFF = read_diff_file('tests/fixtures/diffs/single_file_diff')
++TWO_FILE_DIFF = read_diff_file('tests/fixtures/diffs/two_file_diff')
diff --git a/tests/fixtures/diffs/single_file_diff b/tests/fixtures/diffs/single_file_diff
new file mode 100644
index 0000000..77ca534
--- /dev/null
+++ b/tests/fixtures/diffs/single_file_diff
@@ -0,0 +1,27 @@
+diff --git a/flake8/utils.py b/flake8/utils.py
+index f6ce384..7cd12b0 100644
+--- a/flake8/utils.py
++++ b/flake8/utils.py
+@@ -75,8 +75,8 @@ def stdin_get_value():
+ return cached_value.getvalue()
+
+
+-def parse_unified_diff():
+- # type: () -> List[str]
++def parse_unified_diff(diff=None):
++ # type: (str) -> List[str]
+ """Parse the unified diff passed on stdin.
+
+ :returns:
+@@ -84,7 +84,10 @@ def parse_unified_diff():
+ :rtype:
+ dict
+ """
+- diff = stdin_get_value()
++ # Allow us to not have to patch out stdin_get_value
++ if diff is None:
++ diff = stdin_get_value()
++
+ number_of_rows = None
+ current_path = None
+ parsed_paths = collections.defaultdict(set)
diff --git a/tests/fixtures/diffs/two_file_diff b/tests/fixtures/diffs/two_file_diff
new file mode 100644
index 0000000..5bd35cd
--- /dev/null
+++ b/tests/fixtures/diffs/two_file_diff
@@ -0,0 +1,45 @@
+diff --git a/flake8/utils.py b/flake8/utils.py
+index f6ce384..7cd12b0 100644
+--- a/flake8/utils.py
++++ b/flake8/utils.py
+@@ -75,8 +75,8 @@ def stdin_get_value():
+ return cached_value.getvalue()
+
+
+-def parse_unified_diff():
+- # type: () -> List[str]
++def parse_unified_diff(diff=None):
++ # type: (str) -> List[str]
+ """Parse the unified diff passed on stdin.
+
+ :returns:
+@@ -84,7 +84,10 @@ def parse_unified_diff():
+ :rtype:
+ dict
+ """
+- diff = stdin_get_value()
++ # Allow us to not have to patch out stdin_get_value
++ if diff is None:
++ diff = stdin_get_value()
++
+ number_of_rows = None
+ current_path = None
+ parsed_paths = collections.defaultdict(set)
+diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
+index d69d939..21482ce 100644
+--- a/tests/unit/test_utils.py
++++ b/tests/unit/test_utils.py
+@@ -115,3 +115,13 @@ def test_parameters_for_function_plugin():
+ plugin = plugin_manager.Plugin('plugin-name', object())
+ plugin._plugin = fake_plugin
+ assert utils.parameters_for(plugin) == ['physical_line', 'self', 'tree']
++
++
++def read_diff_file(filename):
++ """Read the diff file in its entirety."""
++ with open(filename, 'r') as fd:
++ content = fd.read()
++ return content
++
++
++SINGLE_FILE_DIFF = read_diff_file('tests/fixtures/diffs/single_file_diff')
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index d69d939..54aa3a7 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -115,3 +115,38 @@ def test_parameters_for_function_plugin():
plugin = plugin_manager.Plugin('plugin-name', object())
plugin._plugin = fake_plugin
assert utils.parameters_for(plugin) == ['physical_line', 'self', 'tree']
+
+
+def read_diff_file(filename):
+ """Read the diff file in its entirety."""
+ with open(filename, 'r') as fd:
+ content = fd.read()
+ return content
+
+
+SINGLE_FILE_DIFF = read_diff_file('tests/fixtures/diffs/single_file_diff')
+SINGLE_FILE_INFO = {
+ 'flake8/utils.py': set(range(75, 83)).union(set(range(84, 94))),
+}
+TWO_FILE_DIFF = read_diff_file('tests/fixtures/diffs/two_file_diff')
+TWO_FILE_INFO = {
+ 'flake8/utils.py': set(range(75, 83)).union(set(range(84, 94))),
+ 'tests/unit/test_utils.py': set(range(115, 128)),
+}
+MULTI_FILE_DIFF = read_diff_file('tests/fixtures/diffs/multi_file_diff')
+MULTI_FILE_INFO = {
+ 'flake8/utils.py': set(range(75, 83)).union(set(range(84, 94))),
+ 'tests/unit/test_utils.py': set(range(115, 129)),
+ 'tests/fixtures/diffs/single_file_diff': set(range(1, 28)),
+ 'tests/fixtures/diffs/two_file_diff': set(range(1, 46)),
+}
+
+
+@pytest.mark.parametrize("diff, parsed_diff", [
+ (SINGLE_FILE_DIFF, SINGLE_FILE_INFO),
+ (TWO_FILE_DIFF, TWO_FILE_INFO),
+ (MULTI_FILE_DIFF, MULTI_FILE_INFO),
+])
+def test_parse_unified_diff(diff, parsed_diff):
+ """Verify that what we parse from a diff matches expectations."""
+ assert utils.parse_unified_diff(diff) == parsed_diff