<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# module: tests/test_add.py
# author: <NAME> <pmav99,gmail>

"""

"""

import re
import json
import shlex
import subprocess

import importlib
import fxr

from munch import Munch
import pytest



with open("./tests/fixtures.json") as fd:
    FIXTURES = json.load(fd)


@pytest.fixture
def temp_file(tmpdir):
    sample_file = tmpdir.join("file.txt")
    return sample_file


class TestFXR(object):

    def run_cli(self, filepath, pattern, added_text, prepend=False, literal=False, **kwargs):
        literal = '--literal' if literal else ''
        prepend = "--prepend" if prepend else ''
        cmd = "fxr add {literal} {prepend} --single {filepath} '{pattern}' '{added_text}'"
        cmd = shlex.split(cmd.format(**locals()))
        subprocess.check_call(cmd)

    def run_prog(self, testdata, filepath):
        return fxr.add_text(args=testdata, filepath=filepath, raise_on_error=True)

    @pytest.mark.parametrize("testdata", FIXTURES["tests"]["add"]["valid"])
    def test_add_valid(self, temp_file, testdata):
        temp_file.write(testdata["original"])
        self.run_cli(temp_file, **testdata)
        assert temp_file.read() == testdata["expected"]

    @pytest.mark.parametrize("testdata", FIXTURES["tests"]["add"]["exceptions"])
    def test_add_exceptions(self, temp_file, testdata):
        testdata = Munch(**testdata)
        temp_file.write(testdata["original"])
        with pytest.raises(SystemExit) as exc:
            self.run_prog(testdata, temp_file)
            # fxr.add_text(args=testdata, filepath=temp_file, raise_on_error=True)
        assert exc.typename == testdata["exception_type"]
        assert str(exc.value) == testdata["exception_text"]<commit_msg>[minor]<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# module: tests/test_add.py
# author: <NAME> <pmav99,gmail>

"""

"""

import re
import json
import shlex
import subprocess

import importlib
import fxr

from munch import Munch
import pytest



with open("./tests/fixtures.json") as fd:
    FIXTURES = json.load(fd)


@pytest.fixture
def temp_file(tmpdir):
    sample_file = tmpdir.join("file.txt")
    return sample_file


class TestFXR(object):

    def run_cli(self, filepath, pattern, added_text, prepend=False, literal=False, **kwargs):
        literal = '--literal' if literal else ''
        prepend = "--prepend" if prepend else ''
        cmd = "fxr add {literal} {prepend} --single {filepath} '{pattern}' '{added_text}'"
        cmd = shlex.split(cmd.format(**locals()))
        subprocess.check_call(cmd)

    def run_prog(self, filepath, testdata):
        return fxr.add_text(args=testdata, filepath=filepath, raise_on_error=True)

    @pytest.mark.parametrize("testdata", FIXTURES["tests"]["add"]["valid"])
    def test_add_valid(self, temp_file, testdata):
        temp_file.write(testdata["original"])
        self.run_cli(temp_file, **testdata)
        assert temp_file.read() == testdata["expected"]

    @pytest.mark.parametrize("testdata", FIXTURES["tests"]["add"]["exceptions"])
    def test_add_exceptions(self, temp_file, testdata):
        testdata = Munch(**testdata)
        temp_file.write(testdata["original"])
        with pytest.raises(SystemExit) as exc:
            self.run_prog(temp_file, testdata)
            # fxr.add_text(args=testdata, filepath=temp_file, raise_on_error=True)
        assert exc.typename == testdata["exception_type"]
        assert str(exc.value) == testdata["exception_text"]
{'fraction_non_alphanumeric': 0.14424173318129987, 'fraction_numerical': 0.002280501710376283, 'mean_word_length': 3.813443072702332, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1828721', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1180, 'n_words': 231}