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

[((140, 184), 'subprocess.check_call', 'subprocess.check_call', (["['git-fortune', '-h']"], {}), "(['git-fortune', '-h'])\n", (161, 184), False, 'import subprocess\n'), ((438, 489), 'subprocess.check_call', 'subprocess.check_call', (["['git-fortune', '--version']"], {}), "(['git-fortune', '--version'])\n", (459, 489), False, 'import subprocess\n'), ((625, 676), 'subprocess.check_call', 'subprocess.check_call', (["['git-fortune', '--id', '3']"], {}), "(['git-fortune', '--id', '3'])\n", (646, 676), False, 'import subprocess\n'), ((695, 1385), 'git_fortune._compat.fix_line_endings', 'fix_line_endings', (['"""+-------------------------------------------------------------------------------+\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    )\n', (711, 1385), False, 'from git_fortune._compat import fix_line_endings\n'), ((1504, 1576), 'subprocess.check_call', 'subprocess.check_call', (["['git-fortune', '--format', 'plain', '--id', '1']"], {}), "(['git-fortune', '--format', 'plain', '--id', '1'])\n", (1525, 1576), False, 'import subprocess\n'), ((1597, 1689), 'git_fortune._compat.fix_line_endings', 'fix_line_endings', (['"""Modify your last commit before pushing with `git commit --amend`.\n"""'], {}), "(\n    'Modify your last commit before pushing with `git commit --amend`.\\n')\n", (1613, 1689), False, 'from git_fortune._compat import fix_line_endings\n'), ((1850, 1888), 'subprocess.check_call', 'subprocess.check_call', (["['git-fortune']"], {}), "(['git-fortune'])\n", (1871, 1888), False, 'import subprocess\n'), ((2063, 2123), 'subprocess.check_call', 'subprocess.check_call', (["['git-fortune', '--category', 'diff']"], {}), "(['git-fortune', '--category', 'diff'])\n", (2084, 2123), False, 'import subprocess\n'), ((2271, 2338), 'subprocess.call', 'subprocess.call', (["['git-fortune', '--category', 'diff', '--id', '3']"], {}), "(['git-fortune', '--category', 'diff', '--id', '3'])\n", (2286, 2338), False, 'import subprocess\n'), ((240, 364), 'git_fortune._compat.fix_line_endings', 'fix_line_endings', (['"""\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    )\n', (256, 364), False, 'from git_fortune._compat import fix_line_endings\n')]