import sys
import contextlib
import os
import shutil
import tempfile

import pytest

from commitizen import cli
from commitizen import commands


@contextlib.contextmanager
def get_temp_dir():
    temp_dir = tempfile.mkdtemp()
    try:
        yield temp_dir
    finally:
        shutil.rmtree(temp_dir)


def test_check_jira_fails(mocker, capsys):
    testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
    mocker.patch.object(sys, "argv", testargs)
    mocker.patch(
        "commitizen.commands.check.open",
        mocker.mock_open(read_data="random message for J-2 #fake_command blah"),
    )
    with pytest.raises(SystemExit):
        cli.main()
    _, err = capsys.readouterr()
    assert "commit validation: failed!" in err


def test_check_jira_command_after_issue_one_space(mocker, capsys):
    testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
    mocker.patch.object(sys, "argv", testargs)
    mocker.patch(
        "commitizen.commands.check.open",
        mocker.mock_open(read_data="JR-23 #command some arguments etc"),
    )
    cli.main()
    out, _ = capsys.readouterr()
    assert "Commit validation: successful!" in out


def test_check_jira_command_after_issue_two_spaces(mocker, capsys):
    testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
    mocker.patch.object(sys, "argv", testargs)
    mocker.patch(
        "commitizen.commands.check.open",
        mocker.mock_open(read_data="JR-2  #command some arguments etc"),
    )
    cli.main()
    out, _ = capsys.readouterr()
    assert "Commit validation: successful!" in out


def test_check_jira_text_between_issue_and_command(mocker, capsys):
    testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
    mocker.patch.object(sys, "argv", testargs)
    mocker.patch(
        "commitizen.commands.check.open",
        mocker.mock_open(read_data="JR-234 some text #command some arguments etc"),
    )
    cli.main()
    out, _ = capsys.readouterr()
    assert "Commit validation: successful!" in out


def test_check_jira_multiple_commands(mocker, capsys):
    testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
    mocker.patch.object(sys, "argv", testargs)
    mocker.patch(
        "commitizen.commands.check.open",
        mocker.mock_open(read_data="JRA-23 some text #command1 args #command2 args"),
    )
    cli.main()
    out, _ = capsys.readouterr()
    assert "Commit validation: successful!" in out


def test_check_conventional_commit_succeeds(mocker, capsys):
    testargs = ["cz", "check", "--commit-msg-file", "some_file"]
    mocker.patch.object(sys, "argv", testargs)
    mocker.patch(
        "commitizen.commands.check.open",
        mocker.mock_open(read_data="fix(scope): some commit message"),
    )
    cli.main()
    out, _ = capsys.readouterr()
    assert "Commit validation: successful!" in out


def test_check_no_conventional_commit(config, mocker, tmpdir):
    with pytest.raises(SystemExit):
        error_mock = mocker.patch("commitizen.out.error")

        with get_temp_dir() as dir:

            tempfile = os.path.join(dir, "temp_commit_file")
            with open(tempfile, "w") as f:
                f.write("no conventional commit")

            check_cmd = commands.Check(
                config=config, arguments={"commit_msg_file": tempfile}
            )
            check_cmd()
            error_mock.assert_called_once()


def test_check_conventional_commit(config, mocker):
    success_mock = mocker.patch("commitizen.out.success")
    with get_temp_dir() as dir:

        tempfile = os.path.join(dir, "temp_commit_file")
        with open(tempfile, "w") as f:
            f.write("feat(lang): added polish language")

        check_cmd = commands.Check(
            config=config, arguments={"commit_msg_file": tempfile}
        )

        check_cmd()
        success_mock.assert_called_once()


def test_check_command_when_commit_file_not_found(config):
    with pytest.raises(FileNotFoundError):
        commands.Check(config=config, arguments={"commit_msg_file": ""})()

@@ -7,66 +7,8 @@
  sys
-%0Aimport contextlib%0Aimport os%0Aimport shutil%0Aimport tempfile
 %0A%0Aim
@@ -85,168 +85,8 @@
 s%0A%0A%0A
-@contextlib.contextmanager%0Adef get_temp_dir():%0A    temp_dir = tempfile.mkdtemp()%0A    try:%0A        yield temp_dir%0A    finally:%0A        shutil.rmtree(temp_dir)%0A%0A%0A
 def 
@@ -2868,49 +2868,8 @@
 %22)%0A%0A
-        with get_temp_dir() as dir:%0A%0A    
     
@@ -2883,34 +2883,28 @@
 pfile = 
-os.path
+tmpdir
 .join(
-dir, 
 %22temp_co
@@ -2927,60 +2927,16 @@
     
-    with open(tempfile, %22w%22) as f:%0A                f
+tempfile
 .wri
@@ -2965,36 +2965,32 @@
 mmit%22)%0A%0A        
-   
- 
 check_cmd = comm
@@ -2993,36 +2993,32 @@
 commands.Check(%0A
-    
             conf
@@ -3076,26 +3076,18 @@
         
-   
- 
 )%0A
-    
         
@@ -3094,28 +3094,24 @@
 check_cmd()%0A
-    
         erro
@@ -3189,16 +3189,24 @@
 , mocker
+, tmpdir
 ):%0A    s
@@ -3262,45 +3262,9 @@
 s%22)%0A
-    with get_temp_dir() as dir:%0A%0A    
+%0A
     
@@ -3278,26 +3278,20 @@
 e = 
-os.path
+tmpdir
 .join(
-dir, 
 %22tem
@@ -3314,56 +3314,16 @@
     
-    with open(tempfile, %22w%22) as f:%0A            f
+tempfile
 .wri
@@ -3359,36 +3359,32 @@
 language%22)%0A%0A    
-   
- 
 check_cmd = comm
@@ -3390,37 +3390,24 @@
 mands.Check(
-%0A            
 config=confi
@@ -3452,28 +3452,15 @@
 ile%7D
-%0A        
 )%0A%0A    
- 
-   
 chec
@@ -3463,28 +3463,24 @@
 check_cmd()%0A
-    
     success_

use pytest fixture tmpdir replace self implemented contextmanager