|
| 1 | +import locale |
| 2 | +import codecs |
| 3 | + |
| 4 | +import six |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +import click |
| 9 | + |
| 10 | +from django.core.management import get_commands, call_command |
| 11 | +from django.core.management import execute_from_command_line |
| 12 | + |
| 13 | +import djclick |
| 14 | + |
| 15 | + |
| 16 | +todo = pytest.mark.xfail(reason='TODO') |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.skipif(not six.PY3, reason='Only necessary on Python3') |
| 20 | +def test_not_ascii(): |
| 21 | + """Make sure that the systems preferred encoding is not `ascii`. |
| 22 | +
|
| 23 | + Otherwise `click` is raising a RuntimeError for Python3. For a detailed |
| 24 | + description of this very problem please consult the following gist: |
| 25 | + https://gist.github.com/hackebrot/937245251887197ef542 |
| 26 | +
|
| 27 | + This test also checks that `tox.ini` explicitly copies the according |
| 28 | + system environment variables to the test environments. |
| 29 | + """ |
| 30 | + try: |
| 31 | + preferred_encoding = locale.getpreferredencoding() |
| 32 | + fs_enc = codecs.lookup(preferred_encoding).name |
| 33 | + except Exception: |
| 34 | + fs_enc = 'ascii' |
| 35 | + assert fs_enc != 'ascii' |
| 36 | + |
| 37 | + |
| 38 | +def test_attributes(): |
| 39 | + for attr in click.__all__: |
| 40 | + assert hasattr(djclick, attr) |
| 41 | + |
| 42 | + |
| 43 | +def test_command_recognized(): |
| 44 | + assert 'testcmd' in get_commands() |
| 45 | + |
| 46 | + |
| 47 | +def test_call_cli(): |
| 48 | + execute_from_command_line(['./manage.py', 'testcmd']) |
| 49 | + with pytest.raises(RuntimeError): |
| 50 | + execute_from_command_line(['./manage.py', 'testcmd', '--raise']) |
| 51 | + |
| 52 | + |
| 53 | +def test_call_command_args(): |
| 54 | + call_command('testcmd') |
| 55 | + with pytest.raises(RuntimeError): |
| 56 | + call_command('testcmd', '--raise') |
| 57 | + |
| 58 | + |
| 59 | +def test_call_command_kwargs(): |
| 60 | + call_command('testcmd', raise_when_called=False) |
| 61 | + with pytest.raises(RuntimeError): |
| 62 | + call_command('testcmd', raise_when_called=True) |
| 63 | + |
| 64 | + |
| 65 | +def test_call_command_kwargs_rename(): |
| 66 | + call_command('testcmd', **{'raise': False}) |
| 67 | + with pytest.raises(RuntimeError): |
| 68 | + call_command('testcmd', **{'raise': True}) |
| 69 | + |
| 70 | + |
| 71 | +def test_call_directly(): |
| 72 | + from testapp.management.commands.testcmd import command |
| 73 | + |
| 74 | + command(raise_when_called=False) |
| 75 | + |
| 76 | + with pytest.raises(RuntimeError): |
| 77 | + command(raise_when_called=True) |
| 78 | + |
| 79 | + with pytest.raises(RuntimeError): |
| 80 | + command(**{'raise': True}) |
| 81 | + |
| 82 | + |
| 83 | +@todo |
| 84 | +def test_django_verbosity(): |
| 85 | + assert False |
| 86 | + |
| 87 | + |
| 88 | +@todo |
| 89 | +def test_django_pythonpath(): |
| 90 | + assert False |
| 91 | + |
| 92 | + |
| 93 | +@todo |
| 94 | +def test_django_traceback(): |
| 95 | + assert False |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.xfail(six.PY3, reason='Encoding issues') |
| 99 | +def test_django_settings(manage): |
| 100 | + # The --settings switch only works from the command line (or if the django |
| 101 | + # settings where not setup before... this means that we have to call it |
| 102 | + # in a subprocess. |
| 103 | + cmd = 'settingscmd' |
| 104 | + assert manage(cmd) == 'default' |
| 105 | + assert manage(cmd, '--settings', 'testprj.settings') == 'default' |
| 106 | + assert manage(cmd, '--settings', 'testprj.settings_alt') == 'alternative' |
| 107 | + |
| 108 | + |
| 109 | +def test_django_color(capsys): |
| 110 | + call_command('colorcmd') |
| 111 | + out, err = capsys.readouterr() |
| 112 | + # Not passing a --color/--no-color flag defaults to autodetection. As the |
| 113 | + # command is run through the test suite, the autodetection defaults to |
| 114 | + # --no-color |
| 115 | + assert out == 'stdout' |
| 116 | + assert err == 'stderr' |
| 117 | + |
| 118 | + call_command('colorcmd', '--color') |
| 119 | + out, err = capsys.readouterr() |
| 120 | + assert out == click.style('stdout', fg='blue') |
| 121 | + assert err == click.style('stderr', bg='red') |
| 122 | + |
| 123 | + call_command('colorcmd', '--no-color') |
| 124 | + out, err = capsys.readouterr() |
| 125 | + assert out == 'stdout' |
| 126 | + assert err == 'stderr' |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.xfail(six.PY3, reason='Encoding issues') |
| 130 | +def test_django_help(manage): |
| 131 | + # The -h/--help switches cause the program to exit. Invoking the command |
| 132 | + # through execute_from_command_line would cause the test suit to exit as |
| 133 | + # well... this means that we have to call it in a subprocess. |
| 134 | + helps = [ |
| 135 | + manage('helpcmd', '-h'), |
| 136 | + manage('helpcmd', '--help'), |
| 137 | + manage('help', 'helpcmd'), |
| 138 | + ] |
| 139 | + assert len(set(helps)) == 1 |
| 140 | + |
| 141 | + help_text = helps[0] |
| 142 | + assert 'HELP_CALLED' not in help_text |
| 143 | + assert help_text.startswith('Usage: manage.py helpcmd ') |
| 144 | + |
| 145 | + |
| 146 | +@todo |
| 147 | +def test_django_version(): |
| 148 | + assert False |
0 commit comments