<commit_before>import os
import shutil
import subprocess
import tempfile

from . import *


# noinspection PyShadowingNames
@pytest.fixture(scope='function')
def temp_dir(commitmsg_script):
    temporary_directory = tempfile.mkdtemp()
    with open(file=os.path.join(temporary_directory, "sample_file.txt"), mode="w") as sample_file:
        sample_file.write("sample content")
    old_cwd = os.getcwd()
    os.chdir(temporary_directory)
    completed_process = subprocess.run(['git', 'init'],
                                       stderr=subprocess.PIPE,
                                       encoding="utf-8")
    assert completed_process.returncode == 0
    assert len(completed_process.stderr) == 0
    git_hook_commit_msg_path = os.path.join(temporary_directory, ".git", "hooks", "commit-msg")
    shutil.copy(commitmsg_script, git_hook_commit_msg_path)
    completed_process = subprocess.run(['git', 'add', '.'],
                                       stderr=subprocess.PIPE,
                                       encoding="utf-8")
    assert completed_process.returncode == 0
    assert len(completed_process.stderr) == 0
    yield None
    os.chdir(old_cwd)


@pytest.mark.usefixtures("temp_dir")
def test_git_commit_with_right_msg():
    completed_process = subprocess.run(['git', 'commit', '-m', 'feat(ui): sample'],
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       encoding="utf-8")
    assert completed_process.returncode == 0
    assert len(completed_process.stderr) == 0


@pytest.mark.usefixtures("temp_dir")
def test_git_commit_with_wrong_msg():
    completed_process = subprocess.run(['git', 'commit', '-m', 'wrong commit message'],
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       encoding="utf-8")
    assert completed_process.returncode != 0
    assert len(completed_process.stderr) > 0<commit_msg>test(refactor): git command<commit_after>import os
import shutil
import subprocess
import tempfile
from typing import List

from . import *


def git_command(*git_args: List) -> subprocess.CompletedProcess:
    args = ['git'] + list(git_args)
    completed_process = subprocess.run(args,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       encoding="utf-8")
    return completed_process


# noinspection PyShadowingNames
@pytest.fixture(scope='function')
def temp_dir(commitmsg_script):
    temporary_directory = tempfile.mkdtemp()
    with open(file=os.path.join(temporary_directory, "sample_file.txt"), mode="w") as sample_file:
        sample_file.write("sample content")
    old_cwd = os.getcwd()
    os.chdir(temporary_directory)

    completed_process = git_command('init')
    assert completed_process.returncode == 0
    assert len(completed_process.stderr) == 0

    completed_process = git_command('config', 'user.name', '<NAME>')
    assert completed_process.returncode == 0
    assert len(completed_process.stderr) == 0

    git_hook_commit_msg_path = os.path.join(temporary_directory, ".git", "hooks", "commit-msg")
    shutil.copy(commitmsg_script, git_hook_commit_msg_path)
    completed_process = git_command('add', '.')
    assert completed_process.returncode == 0
    assert len(completed_process.stderr) == 0

    yield None

    os.chdir(old_cwd)


@pytest.mark.usefixtures("temp_dir")
def test_git_commit_with_right_msg():
    completed_process = git_command('commit', '-m', 'feat(ui): sample')
    assert completed_process.returncode == 0
    assert len(completed_process.stderr) == 0


@pytest.mark.usefixtures("temp_dir")
def test_git_commit_with_wrong_msg():
    completed_process = git_command('commit', '-m', 'wrong commit message')
    assert completed_process.returncode != 0
    assert len(completed_process.stderr) > 0
{'fraction_non_alphanumeric': 0.11058530510585306, 'fraction_numerical': 0.00572851805728518, 'mean_word_length': 2.770892018779343, 'pattern_counts': {'":': 0, '<': 4, '<?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': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7371060', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1154, 'n_words': 209}