<commit_before>import os
import pytest
import shutil
import subprocess
import unittest

from codeclimate_test_reporter.components.formatter import Formatter
from codeclimate_test_reporter.components.formatter import InvalidReportVersion
from codeclimate_test_reporter.components.payload_validator import PayloadValidator


class FormatterTest(unittest.TestCase):
    def test_payload(self):
        orig_dir = os.getcwd()
        os.chdir("./tests/fixtures")

        subprocess.call(["git", "init"])
        subprocess.call(["git", "config", "user.name", "Test User"])
        subprocess.call(["git", "config", "user.email", "<EMAIL>"])
        subprocess.call(["git", "commit", "--allow-empty", "--message", "init"])

        try:
            formatter = Formatter("coverage.xml")
            payload = formatter.payload()
            formatter_latin_1 = Formatter("coverage_for_latin_1_source.xml")
            payload_latin_1 = formatter_latin_1.payload()
        finally:
            os.chdir(orig_dir)
            shutil.rmtree("./tests/fixtures/.git")

        assert type(payload) is dict
        assert len(payload["source_files"]) == 1

        source_file = payload["source_files"][0]
        expected_line_counts = {"covered": 9, "missed": 1, "total": 10}
        expected_coverage = "[null, null, null, 1, 1, 1, 1, null, 1, 1, null, 1, 0, null, 1, 1]"

        assert source_file["line_counts"] == expected_line_counts
        assert source_file["covered_percent"] == 0.9
        assert source_file["covered_strength"] == 1.0
        assert source_file["coverage"] == expected_coverage

        assert PayloadValidator(payload).validate()

        # Assertions for the latin 1 file
        assert type(payload_latin_1) is dict
        assert len(payload_latin_1["source_files"]) == 1

        assert source_file["line_counts"] == expected_line_counts
        assert source_file["covered_percent"] == 0.9
        assert source_file["covered_strength"] == 1.0
        assert source_file["coverage"] == expected_coverage

        assert PayloadValidator(payload_latin_1).validate()

    def test_payload_incompatible_version(self):
        orig_dir = os.getcwd()
        os.chdir("./tests/fixtures")

        try:
            formatter = Formatter("coverage_invalid_version.xml")
            self.assertRaises(InvalidReportVersion, formatter.payload)
        finally:
            os.chdir(orig_dir)<commit_msg>Separate latin 1 spec from main payload spec<commit_after>import os
import pytest
import shutil
import subprocess
import unittest

from codeclimate_test_reporter.components.formatter import Formatter
from codeclimate_test_reporter.components.formatter import InvalidReportVersion
from codeclimate_test_reporter.components.payload_validator import PayloadValidator


class FormatterTest(unittest.TestCase):
    def test_payload_latin_1(self):
        orig_dir = os.getcwd()
        os.chdir("./tests/fixtures")

        self.__setup_git()

        try:
            formatter = Formatter("coverage_for_latin_1_source.xml")
            payload = formatter.payload()
        finally:
            os.chdir(orig_dir)
            shutil.rmtree("./tests/fixtures/.git")

        source_file = payload["source_files"][0]
        expected_line_counts = {"covered": 9, "missed": 1, "total": 10}
        expected_coverage = "[null, null, null, null, 1, 1, 1, 1, null, 1, 1, null, 1, 0, null, 1, 1]"

        assert type(payload) is dict
        assert len(payload["source_files"]) == 1

        assert source_file["line_counts"] == expected_line_counts
        assert source_file["covered_percent"] == 0.9
        assert source_file["covered_strength"] == 1.0
        assert source_file["coverage"] == expected_coverage

        assert PayloadValidator(payload).validate()

    def test_payload(self):
        orig_dir = os.getcwd()
        os.chdir("./tests/fixtures")

        self.__setup_git()

        try:
            formatter = Formatter("coverage.xml")
            payload = formatter.payload()
        finally:
            os.chdir(orig_dir)
            shutil.rmtree("./tests/fixtures/.git")

        assert type(payload) is dict
        assert len(payload["source_files"]) == 1

        source_file = payload["source_files"][0]
        expected_line_counts = {"covered": 9, "missed": 1, "total": 10}
        expected_coverage = "[null, null, null, 1, 1, 1, 1, null, 1, 1, null, 1, 0, null, 1, 1]"

        assert source_file["line_counts"] == expected_line_counts
        assert source_file["covered_percent"] == 0.9
        assert source_file["covered_strength"] == 1.0
        assert source_file["coverage"] == expected_coverage

        assert PayloadValidator(payload).validate()

    def test_payload_incompatible_version(self):
        orig_dir = os.getcwd()
        os.chdir("./tests/fixtures")

        try:
            formatter = Formatter("coverage_invalid_version.xml")
            self.assertRaises(InvalidReportVersion, formatter.payload)
        finally:
            os.chdir(orig_dir)

    def __setup_git(self):
        subprocess.call(["git", "init"])
        subprocess.call(["git", "config", "user.name", "Test User"])
        subprocess.call(["git", "config", "user.email", "<EMAIL>"])
        subprocess.call(["git", "commit", "--allow-empty", "--message", "init"])
{'fraction_non_alphanumeric': 0.12361032598454871, 'fraction_numerical': 0.014320708498209912, 'mean_word_length': 3.009063444108761, 'pattern_counts': {'":': 9, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2452109', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1562, 'n_words': 326}