import subprocess

from git_fortune._compat import fix_line_endings
from git_fortune.version import __version__


def test_help(capfd):
    subprocess.check_call(["git-fortune", "-h"])
    captured = capfd.readouterr()
    assert (
        fix_line_endings(
            """
A fortune-like command for showing git tips

Invoke it as 'git-fortune' or 'git fortune'
"""
        )
        in captured.out
    )


def test_version(capfd):
    subprocess.check_call(["git-fortune", "--version"])
    captured = capfd.readouterr()
    assert "git-fortune {}".format(__version__) in captured.out


def test_tip_boxformat(capfd):
    subprocess.check_call(["git-fortune", "--id", "3"])
    tip3boxbody = fix_line_endings(
        """\
+-------------------------------------------------------------------------------+
| GIT TIP #3                                                                    |
|                                                                               |
| `git log --graph` can show you a tree-like representation of the git history. |
|                                                                               |
| Try adding in `--oneline --decorate --all`.                                   |
|                                                                               |
+-------------------------------------------------------------------------------+
"""
    )
    captured = capfd.readouterr()
    assert captured.out == tip3boxbody


def test_tip_plainformat(capfd):
    subprocess.check_call(["git-fortune", "--format", "plain", "--id", "1"])
    tip1plainbody = fix_line_endings(
        "Modify your last commit before pushing with `git commit --amend`.\n"
    )
    captured = capfd.readouterr()
    assert captured.out == tip1plainbody


def test_noargs(capfd):
    """just make sure it doesn't crashfail"""
    subprocess.check_call(["git-fortune"])
    captured = capfd.readouterr()
    assert "GIT TIP #" in captured.out  # from the box format


def test_category(capfd):
    """just make sure it doesn't crashfail"""
    subprocess.check_call(["git-fortune", "--category", "diff"])
    captured = capfd.readouterr()
    assert "GIT TIP #" in captured.out  # from the box format


def test_category_and_id_mutex(capfd):
    ret = subprocess.call(["git-fortune", "--category", "diff", "--id", "3"])
    assert ret == 2
    captured = capfd.readouterr()
    assert "" == captured.out
    assert "argument --id: not allowed with argument --category" in captured.err

[((8, 4, 8, 48), 'subprocess.check_call', 'subprocess.check_call', ({(8, 26, 8, 47): "['git-fortune', '-h']"}, {}), "(['git-fortune', '-h'])", False, 'import subprocess\n'), ((23, 4, 23, 55), 'subprocess.check_call', 'subprocess.check_call', ({(23, 26, 23, 54): "['git-fortune', '--version']"}, {}), "(['git-fortune', '--version'])", False, 'import subprocess\n'), ((29, 4, 29, 55), 'subprocess.check_call', 'subprocess.check_call', ({(29, 26, 29, 54): "['git-fortune', '--id', '3']"}, {}), "(['git-fortune', '--id', '3'])", False, 'import subprocess\n'), ((30, 18, 41, 5), 'git_fortune._compat.fix_line_endings', 'fix_line_endings', ({(31, 8, 40, 3): '"""+-------------------------------------------------------------------------------+\n| GIT TIP #3                                                                    |\n|                                                                               |\n| `git log --graph` can show you a tree-like representation of the git history. |\n|                                                                               |\n| Try adding in `--oneline --decorate --all`.                                   |\n|                                                                               |\n+-------------------------------------------------------------------------------+\n"""'}, {}), '(\n    """+-------------------------------------------------------------------------------+\n| GIT TIP #3                                                                    |\n|                                                                               |\n| `git log --graph` can show you a tree-like representation of the git history. |\n|                                                                               |\n| Try adding in `--oneline --decorate --all`.                                   |\n|                                                                               |\n+-------------------------------------------------------------------------------+\n"""\n    )', False, 'from git_fortune._compat import fix_line_endings\n'), ((47, 4, 47, 76), 'subprocess.check_call', 'subprocess.check_call', ({(47, 26, 47, 75): "['git-fortune', '--format', 'plain', '--id', '1']"}, {}), "(['git-fortune', '--format', 'plain', '--id', '1'])", False, 'import subprocess\n'), ((48, 20, 50, 5), 'git_fortune._compat.fix_line_endings', 'fix_line_endings', ({(49, 8, 49, 77): '"""Modify your last commit before pushing with `git commit --amend`.\n"""'}, {}), "(\n    'Modify your last commit before pushing with `git commit --amend`.\\n')", False, 'from git_fortune._compat import fix_line_endings\n'), ((57, 4, 57, 42), 'subprocess.check_call', 'subprocess.check_call', ({(57, 26, 57, 41): "['git-fortune']"}, {}), "(['git-fortune'])", False, 'import subprocess\n'), ((64, 4, 64, 64), 'subprocess.check_call', 'subprocess.check_call', ({(64, 26, 64, 63): "['git-fortune', '--category', 'diff']"}, {}), "(['git-fortune', '--category', 'diff'])", False, 'import subprocess\n'), ((70, 10, 70, 77), 'subprocess.call', 'subprocess.call', ({(70, 26, 70, 76): "['git-fortune', '--category', 'diff', '--id', '3']"}, {}), "(['git-fortune', '--category', 'diff', '--id', '3'])", False, 'import subprocess\n'), ((11, 8, 17, 9), 'git_fortune._compat.fix_line_endings', 'fix_line_endings', ({(12, 12, 16, 3): '"""\nA fortune-like command for showing git tips\n\nInvoke it as \'git-fortune\' or \'git fortune\'\n"""'}, {}), '(\n    """\nA fortune-like command for showing git tips\n\nInvoke it as \'git-fortune\' or \'git fortune\'\n"""\n    )', False, 'from git_fortune._compat import fix_line_endings\n')]