Skip to content

Commit

Permalink
adding unit test for CodeclimateJSONFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
thushjandan committed Feb 5, 2021
1 parent 72b1940 commit 0eaa241
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ansiblelint/formatters/__init__.py
Expand Up @@ -149,8 +149,15 @@ def format(self, match: "MatchError") -> str:


class CodeclimateJSONFormatter(BaseFormatter):
"""Formatter for emitting violations in Codeclimate JSON report format.
Reference: https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-type
"""

def format(self, matches: List["MatchError"]) -> str:

if not isinstance(matches, list):
raise RuntimeError("The CodeclimatJSONFormatter was expecting a list of MatchError.")

result = []
for match in matches:
issue = {}
Expand Down
60 changes: 60 additions & 0 deletions test/TestCodeclimateJSONFormatter.py
@@ -0,0 +1,60 @@
# Copyright (c) 2016 Will Thames <will@thames.id.au>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# pylint: disable=preferred-module # FIXME: remove once migrated per GH-725
import pathlib
import json
import unittest

from ansiblelint.errors import MatchError
from ansiblelint.formatters import CodeclimateJSONFormatter
from ansiblelint.rules import AnsibleLintRule


class TestCodeclimateJSONFormatter(unittest.TestCase):

def setUp(self):
self.rule = AnsibleLintRule()
self.rule.id = "TCF0001"
self.matches = []
self.matches.append(MatchError(
message="message",
linenumber=1,
details="hello",
filename="filename.yml",
rule=self.rule
))
self.matches.append(MatchError(
message="message",
linenumber=2,
details="hello",
filename="filename.yml",
rule=self.rule
))
self.formatter = CodeclimateJSONFormatter(pathlib.Path.cwd(), display_relative_path=True)

def test_format_list(self):
self.formatter.format(self.matches)

def test_result_is_json(self):
json.loads(self.formatter.format(self.matches))

def test_single_match(self):
with self.assertRaises(RuntimeError):
self.formatter.format(self.matches[0])

0 comments on commit 0eaa241

Please sign in to comment.