diff options
author | Giel van Schijndel <Giel.vanSchijndel@tomtom.com> | 2020-03-02 10:23:38 +0100 |
---|---|---|
committer | Giel van Schijndel <Giel.vanSchijndel@tomtom.com> | 2020-03-02 10:23:38 +0100 |
commit | 58cd5e0e26d29e1e9835342bd158adec766c28bf (patch) | |
tree | 8c4f5dd43a973b1673cbc37a19875020f8037a24 /tests/test_arguments.py | |
parent | ab63d852aba40d3e8feae7527308e4c4d105ec8b (diff) | |
download | click-58cd5e0e26d29e1e9835342bd158adec766c28bf.tar.gz |
test: verify that UTF-8 is used as a fallback for decoding arguments
Diffstat (limited to 'tests/test_arguments.py')
-rw-r--r-- | tests/test_arguments.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/tests/test_arguments.py b/tests/test_arguments.py index ca48fed..fb2f75b 100644 --- a/tests/test_arguments.py +++ b/tests/test_arguments.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- import pytest import click -from click._compat import PY2 +import sys +from click._compat import PY2, text_type def test_nargs_star(runner): @@ -84,6 +85,26 @@ def test_nargs_err(runner): assert 'Got unexpected extra argument (bar)' in result.output +def test_bytes_args(runner, monkeypatch): + @click.command() + @click.argument('arg') + def from_bytes(arg): + assert isinstance(arg, text_type), "UTF-8 encoded argument should be implicitly converted to Unicode" + + # Simulate empty locale environment variables + if PY2: + monkeypatch.setattr(sys.stdin, 'encoding', 'ANSI_X3.4-1968') + monkeypatch.setattr(sys, 'getfilesystemencoding', lambda: 'ANSI_X3.4-1968') + monkeypatch.setattr(sys, 'getdefaultencoding', lambda: 'ascii') + else: + monkeypatch.setattr(sys.stdin, 'encoding', 'utf-8') + monkeypatch.setattr(sys, 'getfilesystemencoding', lambda: 'utf-8') + monkeypatch.setattr(sys, 'getdefaultencoding', lambda: 'utf-8') + + runner.invoke(from_bytes, [u'Something outside of ASCII range: 林'.encode('UTF-8')], + catch_exceptions=False) + + def test_file_args(runner): @click.command() @click.argument('input', type=click.File('rb')) |