| 1 |
import argparse |
| 2 |
import os |
| 3 |
import subprocess |
| 4 |
import sys |
| 5 |
from collections import namedtuple |
| 6 |
import contextlib |
| 7 |
|
| 8 |
import wordcloud as wc |
| 9 |
from wordcloud import wordcloud_cli as cli |
| 10 |
|
| 11 |
from unittest.mock import patch |
| 12 |
import pytest |
| 13 |
|
| 14 |
import matplotlib |
| 15 |
matplotlib.use('Agg') |
| 16 |
|
| 17 |
|
| 18 |
class PassFile(object): |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
ArgOption = namedtuple('ArgOption', ['cli_name', 'init_name', 'pass_value', 'fail_value']) |
| 23 |
ARGUMENT_SPEC_TYPED = [ |
| 24 |
ArgOption(cli_name='width', init_name='width', pass_value=13, fail_value=1.), |
| 25 |
ArgOption(cli_name='height', init_name='height', pass_value=15, fail_value=1.), |
| 26 |
ArgOption(cli_name='margin', init_name='margin', pass_value=17, fail_value=1.), |
| 27 |
ArgOption(cli_name='relative_scaling', init_name='relative_scaling', pass_value=1, fail_value='c'), |
| 28 |
] |
| 29 |
ARGUMENT_SPEC_UNARY = [ |
| 30 |
ArgOption(cli_name='no_collocations', init_name='collocations', pass_value=True, fail_value=1), |
| 31 |
ArgOption(cli_name='include_numbers', init_name='include_numbers', pass_value=True, fail_value=2), |
| 32 |
ArgOption(cli_name='no_normalize_plurals', init_name='normalize_plurals', pass_value=True, fail_value=3), |
| 33 |
ArgOption(cli_name='repeat', init_name='repeat', pass_value=True, fail_value=4), |
| 34 |
] |
| 35 |
ARGUMENT_SPEC_REMAINING = [ |
| 36 |
ArgOption(cli_name='stopwords', init_name='stopwords', pass_value=PassFile(), fail_value=None), |
| 37 |
ArgOption(cli_name='regexp', init_name='regexp', pass_value=r'\w{2,}', fail_value=r'12('), |
| 38 |
ArgOption(cli_name='mask', init_name='mask', pass_value=PassFile(), fail_value=None), |
| 39 |
ArgOption(cli_name='fontfile', init_name='font_path', pass_value=PassFile(), fail_value=None), |
| 40 |
ArgOption(cli_name='color', init_name='color_func', pass_value='red', fail_value=None), |
| 41 |
ArgOption(cli_name='background', init_name='background_color', pass_value='grey', fail_value=None), |
| 42 |
ArgOption(cli_name='contour_color', init_name='contour_color', pass_value='grey', fail_value=None), |
| 43 |
ArgOption(cli_name='contour_width', init_name='contour_width', pass_value=0.5, fail_value='blue'), |
| 44 |
ArgOption(cli_name='min_word_length', init_name='min_word_length', pass_value=5, fail_value='blue'), |
| 45 |
ArgOption(cli_name='prefer_horizontal', init_name='prefer_horizontal', pass_value=.1, fail_value='blue'), |
| 46 |
ArgOption(cli_name='scale', init_name='scale', pass_value=1., fail_value='blue'), |
| 47 |
ArgOption(cli_name='colormap', init_name='colormap', pass_value='Greens', fail_value=1), |
| 48 |
ArgOption(cli_name='mode', init_name='mode', pass_value='RGBA', fail_value=2), |
| 49 |
ArgOption(cli_name='max_words', init_name='max_words', pass_value=10, fail_value='blue'), |
| 50 |
ArgOption(cli_name='min_font_size', init_name='min_font_size', pass_value=10, fail_value='blue'), |
| 51 |
ArgOption(cli_name='max_font_size', init_name='max_font_size', pass_value=10, fail_value='blue'), |
| 52 |
ArgOption(cli_name='font_step', init_name='font_step', pass_value=10, fail_value='blue'), |
| 53 |
ArgOption(cli_name='random_state', init_name='random_state', pass_value=100, fail_value='blue'), |
| 54 |
] |
| 55 |
ARGUMENT_CLI_NAMES_UNARY = [arg_opt.cli_name for arg_opt in ARGUMENT_SPEC_UNARY] |
| 56 |
|
| 57 |
|
| 58 |
def all_arguments(): |
| 59 |
arguments = [] |
| 60 |
arguments.extend(ARGUMENT_SPEC_TYPED) |
| 61 |
arguments.extend(ARGUMENT_SPEC_UNARY) |
| 62 |
arguments.extend(ARGUMENT_SPEC_REMAINING) |
| 63 |
return arguments |
| 64 |
|
| 65 |
|
| 66 |
def test_main_passes_arguments_through(tmpdir): |
| 67 |
|
| 68 |
image_filepath = str(tmpdir.join('word_cloud.png')) |
| 69 |
|
| 70 |
args = argparse.Namespace() |
| 71 |
for option in all_arguments(): |
| 72 |
setattr(args, option.init_name, option.pass_value) |
| 73 |
|
| 74 |
text = 'some long text' |
| 75 |
image_file = open(image_filepath, 'w') |
| 76 |
with patch('wordcloud.wordcloud_cli.wc.WordCloud', autospec=True) as mock_word_cloud: |
| 77 |
cli.main(vars(args), text, image_file) |
| 78 |
|
| 79 |
posargs, kwargs = mock_word_cloud.call_args |
| 80 |
for option in all_arguments(): |
| 81 |
assert option.init_name in kwargs |
| 82 |
|
| 83 |
|
| 84 |
def check_argument(text_filepath, name, result_name, value): |
| 85 |
args, text, image_file = cli.parse_args(['--text', text_filepath, '--' + name, str(value)]) |
| 86 |
assert result_name in args |
| 87 |
|
| 88 |
|
| 89 |
def check_argument_unary(text_filepath, name, result_name): |
| 90 |
args, text, image_file = cli.parse_args(['--text', text_filepath, '--' + name]) |
| 91 |
assert result_name in args |
| 92 |
|
| 93 |
|
| 94 |
def check_argument_type(text_filepath, name, value): |
| 95 |
with pytest.raises((SystemExit, ValueError),): |
| 96 |
args, text, image_file = cli.parse_args(['--text', text_filepath, '--' + name, str(value)]) |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.parametrize("option", all_arguments()) |
| 100 |
def test_parse_args_are_passed_along(option, tmpdir, tmp_text_file): |
| 101 |
if option.cli_name in ARGUMENT_CLI_NAMES_UNARY: |
| 102 |
check_argument_unary(str(tmp_text_file), option.cli_name, option.init_name) |
| 103 |
elif option.cli_name != 'mask': |
| 104 |
pass_value = option.pass_value |
| 105 |
if isinstance(option.pass_value, PassFile): |
| 106 |
input_file = tmpdir.join("%s_file" % option.cli_name) |
| 107 |
input_file.write(b"") |
| 108 |
pass_value = str(input_file) |
| 109 |
check_argument(str(tmp_text_file), option.cli_name, option.init_name, pass_value) |
| 110 |
|
| 111 |
|
| 112 |
@pytest.mark.parametrize("option", ARGUMENT_SPEC_TYPED) |
| 113 |
def test_parse_arg_types(option, tmp_text_file): |
| 114 |
check_argument_type(str(tmp_text_file), option.cli_name, option.fail_value) |
| 115 |
|
| 116 |
|
| 117 |
def test_check_duplicate_color_error(tmpdir, tmp_text_file): |
| 118 |
color_mask_file = tmpdir.join("input_color_mask.png") |
| 119 |
color_mask_file.write(b"") |
| 120 |
|
| 121 |
with pytest.raises(ValueError, match=r'.*specify either.*'): |
| 122 |
cli.parse_args(['--color', 'red', '--colormask', str(color_mask_file), '--text', str(tmp_text_file)]) |
| 123 |
|
| 124 |
|
| 125 |
def test_parse_args_defaults_to_random_color(tmp_text_file): |
| 126 |
args, text, image_file = cli.parse_args(['--text', str(tmp_text_file)]) |
| 127 |
assert args['color_func'] == wc.random_color_func |
| 128 |
|
| 129 |
|
| 130 |
def test_unicode_text_file(): |
| 131 |
unicode_file = os.path.join(os.path.dirname(__file__), "unicode_text.txt") |
| 132 |
args, text, image_file = cli.parse_args(['--text', unicode_file]) |
| 133 |
assert len(text) == 16 |
| 134 |
|
| 135 |
|
| 136 |
def test_unicode_with_stopwords(): |
| 137 |
unicode_file = os.path.join(os.path.dirname(__file__), "unicode_text.txt") |
| 138 |
stopwords_file = os.path.join(os.path.dirname(__file__), "unicode_stopwords.txt") |
| 139 |
args, text, image_file = cli.parse_args(['--text', unicode_file, '--stopwords', stopwords_file]) |
| 140 |
|
| 141 |
# expect the unicode character from stopwords file was correctly read in |
| 142 |
assert u'\u304D' in args['stopwords'] |
| 143 |
|
| 144 |
|
| 145 |
def test_cli_writes_to_imagefile(tmpdir, tmp_text_file): |
| 146 |
# ensure writing works with all python versions |
| 147 |
tmp_image_file = tmpdir.join("word_cloud.png") |
| 148 |
|
| 149 |
tmp_text_file.write(b'some text') |
| 150 |
|
| 151 |
args, text, image_file = cli.parse_args(['--text', str(tmp_text_file), '--imagefile', str(tmp_image_file)]) |
| 152 |
cli.main(args, text, image_file) |
| 153 |
|
| 154 |
# expecting image to be written to imagefile |
| 155 |
assert tmp_image_file.size() > 0 |
| 156 |
|
| 157 |
|
| 158 |
# capsysbinary should be used here, but it's not supported in python 2. |
| 159 |
def test_cli_writes_to_stdout(tmpdir, tmp_text_file): |
| 160 |
# ensure writing works with all python versions |
| 161 |
tmp_image_file = tmpdir.join("word_cloud.png") |
| 162 |
|
| 163 |
tmp_text_file.write(b'some text') |
| 164 |
|
| 165 |
with contextlib.redirect_stdout(tmp_image_file.open('w+')): |
| 166 |
args, text, image_file = cli.parse_args(['--text', str(tmp_text_file)]) |
| 167 |
cli.main(args, text, image_file) |
| 168 |
|
| 169 |
# expecting image to be written to stdout |
| 170 |
assert tmp_image_file.size() > 0 |
| 171 |
|
| 172 |
|
| 173 |
def test_cli_regexp(tmp_text_file): |
| 174 |
cli.parse_args(['--regexp', r"\w[\w']+", '--text', str(tmp_text_file)]) |
| 175 |
|
| 176 |
|
| 177 |
def test_cli_regexp_invalid(tmp_text_file, capsys): |
| 178 |
with pytest.raises(SystemExit): |
| 179 |
cli.parse_args(['--regexp', r"invalid[", '--text', str(tmp_text_file)]) |
| 180 |
|
| 181 |
_, err = capsys.readouterr() |
| 182 |
assert "Invalid regular expression" in err |
| 183 |
|
| •184 |
""" |
| 185 |
@pytest.mark.parametrize("command,expected_output, expected_exit_code", [ |
| 186 |
("wordcloud_cli --help", "usage: wordcloud_cli", 0), |
| 187 |
("%s -m wordcloud --help" % sys.executable, "usage: __main__", 0), |
| 188 |
("%s %s/../wordcloud/wordcloud_cli.py --help" % (sys.executable, os.path.dirname(__file__)), "To execute the CLI", 1), |
| 189 |
]) |
| 190 |
def test_cli_as_executable(command, expected_output, expected_exit_code, tmpdir, capfd, no_cover_compat): |
| 191 |
|
| 192 |
ret_code = 0 |
| 193 |
try: |
| 194 |
subprocess.check_call( |
| 195 |
command, |
| 196 |
shell=True, |
| 197 |
cwd=str(tmpdir) |
| 198 |
) |
| 199 |
except subprocess.CalledProcessError as excinfo: |
| 200 |
ret_code = excinfo.returncode |
| 201 |
|
| 202 |
out, err = capfd.readouterr() |
| 203 |
assert expected_output in out if ret_code == 0 else err |
| 204 |
|
| 205 |
assert ret_code == expected_exit_code |
| •206 |
""" |