Repository: PyCQA/bandit
Pull request #673 (2020-12-17 07:39:35)

Show column offset on all formatters
Recently, #618 introduced column offsets to the custom formatter.
But other formatters should also show the column offset.

Signed-off-by: Eric Brown <browne@vmware.com>

tonybaloney: Thanks @ericwb this will be really helpful integrating into different IDE's/code analysis tools

Patch:
diff --git a/bandit/formatters/csv.py b/bandit/formatters/csv.py
index 1fc5878..14e18c5 100644
--- a/bandit/formatters/csv.py
+++ b/bandit/formatters/csv.py
@@ -59,6 +59,7 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
                       'issue_confidence',
                       'issue_text',
                       'line_number',
+                      'col_offset',
                       'line_range',
                       'more_info']
 
diff --git a/bandit/formatters/screen.py b/bandit/formatters/screen.py
index 2f7f84f..d501dff 100644
--- a/bandit/formatters/screen.py
+++ b/bandit/formatters/screen.py
@@ -102,9 +102,10 @@ def _output_issue_str(issue, indent, show_lineno=True, show_code=True,
     bits.append("%s   Severity: %s   Confidence: %s" % (
         indent, issue.severity.capitalize(), issue.confidence.capitalize()))
 
-    bits.append("%s   Location: %s:%s" % (
+    bits.append("%s   Location: %s:%s:%s" % (
         indent, issue.fname,
-        issue.lineno if show_lineno else ""))
+        issue.lineno if show_lineno else "",
+        issue.col_offset if show_lineno else ""))
 
     bits.append("%s   More Info: %s%s" % (
         indent, docs_utils.get_url(issue.test_id), COLOR['DEFAULT']))
diff --git a/bandit/formatters/text.py b/bandit/formatters/text.py
index 10f044f..dbb2cf5 100644
--- a/bandit/formatters/text.py
+++ b/bandit/formatters/text.py
@@ -76,8 +76,9 @@ def _output_issue_str(issue, indent, show_lineno=True, show_code=True,
     bits.append("%s   Severity: %s   Confidence: %s" % (
         indent, issue.severity.capitalize(), issue.confidence.capitalize()))
 
-    bits.append("%s   Location: %s:%s" % (
-        indent, issue.fname, issue.lineno if show_lineno else ""))
+    bits.append("%s   Location: %s:%s:%s" % (
+        indent, issue.fname, issue.lineno if show_lineno else "",
+        issue.col_offset if show_lineno else ""))
 
     bits.append("%s   More Info: %s" % (
         indent, docs_utils.get_url(issue.test_id)))


Test patch:
diff --git a/tests/unit/formatters/test_screen.py b/tests/unit/formatters/test_screen.py
index 12dca68..533abbd 100644
--- a/tests/unit/formatters/test_screen.py
+++ b/tests/unit/formatters/test_screen.py
@@ -35,8 +35,9 @@ class ScreenFormatterTests(testtools.TestCase):
                           "{}   Severity: {}   Confidence: {}".
                           format(_indent_val, _issue.severity.capitalize(),
                                  _issue.confidence.capitalize()),
-                          "{}   Location: {}:{}".
-                          format(_indent_val, _issue.fname, _issue.lineno),
+                          "{}   Location: {}:{}:{}".
+                          format(_indent_val, _issue.fname, _issue.lineno,
+                                 _issue.col_offset),
                           "{}   More Info: {}{}".format(
                               _indent_val, docs_utils.get_url(_issue.test_id),
                               screen.COLOR['DEFAULT'])]
@@ -56,6 +57,7 @@ class ScreenFormatterTests(testtools.TestCase):
         self.assertEqual(expected_return, issue_text)
 
         issue.lineno = ''
+        issue.col_offset = ''
         issue_text = screen._output_issue_str(issue, indent_val,
                                               show_lineno=False)
         expected_return = _template(issue, indent_val, 'DDDDDDD',
diff --git a/tests/unit/formatters/test_text.py b/tests/unit/formatters/test_text.py
index 355ace9..f31051b 100644
--- a/tests/unit/formatters/test_text.py
+++ b/tests/unit/formatters/test_text.py
@@ -35,8 +35,9 @@ class TextFormatterTests(testtools.TestCase):
                           "{}   Severity: {}   Confidence: {}".
                           format(_indent_val, _issue.severity.capitalize(),
                                  _issue.confidence.capitalize()),
-                          "{}   Location: {}:{}".
-                          format(_indent_val, _issue.fname, _issue.lineno),
+                          "{}   Location: {}:{}:{}".
+                          format(_indent_val, _issue.fname, _issue.lineno,
+                                 _issue.col_offset),
                           "{}   More Info: {}".format(
                               _indent_val, docs_utils.get_url(_issue.test_id))]
             if _code:
@@ -53,6 +54,7 @@ class TextFormatterTests(testtools.TestCase):
         self.assertEqual(expected_return, issue_text)
 
         issue.lineno = ''
+        issue.col_offset = ''
         issue_text = b_text._output_issue_str(issue, indent_val,
                                               show_lineno=False)
         expected_return = _template(issue, indent_val, 'DDDDDDD')