| 1 | """CLI test module.""" |
| 2 | import argparse |
| 3 | import io |
| 4 | import json |
| 5 | |
| 6 | import pytest |
| 7 | from gjson._cli import cli |
| 8 | from gjson.exceptions import GJSONParseError |
| 9 | |
| 10 | from .test_init import INPUT_JSON, INPUT_LINES, INPUT_LINES_WITH_ERRORS |
| 11 | |
| 12 | |
| 13 | def test_cli_help(capsys): |
| 14 | """It should read the data from stdin and query it.""" |
| 15 | with pytest.raises(SystemExit) as exc_info: |
| 16 | cli(['--help']) |
| 17 | |
| 18 | assert exc_info.value.args[0] == 0 |
| 19 | captured = capsys.readouterr() |
| 20 | assert 'usage: gjson' in captured.out |
| 21 | assert 'See also the full documentation available at' in captured.out |
| 22 | assert not captured.err |
| 23 | |
| 24 | |
| 25 | def test_cli_stdin(monkeypatch, capsys): |
| 26 | """It should read the data from stdin and query it.""" |
| 27 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_JSON)) |
| 28 | ret = cli(['-', 'name.first']) |
| 29 | assert ret == 0 |
| 30 | captured = capsys.readouterr() |
| 31 | assert captured.out == '"Tom"\n' |
| 32 | assert not captured.err |
| 33 | |
| 34 | |
| 35 | def test_cli_file(tmp_path, capsys): |
| 36 | """It should read the data from the provided file and query it.""" |
| 37 | data_file = tmp_path / 'input.json' |
| 38 | data_file.write_text(INPUT_JSON) |
| 39 | ret = cli([str(data_file), 'name.first']) |
| 40 | assert ret == 0 |
| 41 | captured = capsys.readouterr() |
| 42 | assert captured.out == '"Tom"\n' |
| 43 | assert not captured.err |
| 44 | |
| 45 | |
| 46 | @pytest.mark.parametrize('query', ( |
| 47 | 'a\tkey', |
| 48 | '..0.a\tkey', |
| 49 | )) |
| 50 | def test_cli_file_with_control_chars(query, tmp_path, capsys): |
| 51 | """It should read the data from the provided file and query it.""" |
| 52 | data_file = tmp_path / 'input.json' |
| 53 | data_file.write_text('{"a\tkey": "a\tvalue"}') |
| 54 | ret = cli(['-vvv', str(data_file), query]) |
| 55 | assert ret == 0 |
| 56 | captured = capsys.readouterr() |
| 57 | assert captured.out == '"a\\tvalue"\n' |
| 58 | assert not captured.err |
| 59 | |
| 60 | |
| 61 | @pytest.mark.parametrize('query', ( |
| 62 | 'a\tkey', |
| 63 | '..0.a\tkey', |
| 64 | )) |
| 65 | @pytest.mark.parametrize('lines', (True, False)) |
| 66 | def test_cli_stdin_with_control_chars(query, lines, monkeypatch, capsys): |
| 67 | """It should read the data from stdin and query it.""" |
| 68 | monkeypatch.setattr('sys.stdin', io.StringIO('{"a\tkey": "a\tvalue"}')) |
| 69 | params = ['-vvv', '-', query] |
| 70 | if lines: |
| 71 | params.insert(0, '--lines') |
| 72 | |
| 73 | ret = cli(params) |
| 74 | assert ret == 0 |
| 75 | captured = capsys.readouterr() |
| 76 | assert captured.out == '"a\\tvalue"\n' |
| 77 | assert not captured.err |
| 78 | |
| 79 | |
| 80 | def test_cli_nonexistent_file(tmp_path, capsys): |
| 81 | """It should exit with a failure exit code and no output.""" |
| 82 | ret = cli([str(tmp_path / 'nonexistent.json'), 'name.first']) |
| 83 | assert ret == 1 |
| 84 | captured = capsys.readouterr() |
| 85 | assert not captured.out |
| 86 | assert not captured.err |
| 87 | |
| 88 | |
| 89 | def test_cli_nonexistent_file_verbosity_1(tmp_path, capsys): |
| 90 | """It should exit with a failure exit code and print the error message.""" |
| 91 | ret = cli(['-v', str(tmp_path / 'nonexistent.json'), 'name.first']) |
| 92 | assert ret == 1 |
| 93 | captured = capsys.readouterr() |
| 94 | assert not captured.out |
| 95 | assert captured.err.startswith("ArgumentTypeError: can't open") |
| 96 | assert 'nonexistent.json' in captured.err |
| 97 | |
| 98 | |
| 99 | def test_cli_nonexistent_file_verbosity_2(tmp_path): |
| 100 | """It should raise the exception and print the full traceback.""" |
| 101 | with pytest.raises( |
| 102 | argparse.ArgumentTypeError, match=r"can't open .*/nonexistent.json.* No such file or directory"): |
| 103 | cli(['-vv', str(tmp_path / 'nonexistent.json'), 'name.first']) |
| 104 | |
| 105 | |
| 106 | def test_cli_stdin_query_verbosity_1(monkeypatch, capsys): |
| 107 | """It should exit with a failure exit code and print the error message.""" |
| 108 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_JSON)) |
| 109 | ret = cli(['-v', '-', 'nonexistent']) |
| 110 | assert ret == 1 |
| 111 | captured = capsys.readouterr() |
| 112 | assert not captured.out |
| 113 | assert captured.err == ('GJSONParseError: Mapping object does not have key `nonexistent`.\n' |
| 114 | 'Query: nonexistent\n-------^\n') |
| 115 | |
| 116 | |
| 117 | def test_cli_stdin_query_verbosity_2(monkeypatch): |
| 118 | """It should exit with a failure exit code and print the full traceback.""" |
| 119 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_JSON)) |
| 120 | with pytest.raises(GJSONParseError, match=r'Mapping object does not have key `nonexistent`.'): |
| 121 | cli(['-vv', '-', 'nonexistent']) |
| 122 | |
| 123 | |
| 124 | def test_cli_lines_ok(monkeypatch, capsys): |
| 125 | """It should apply the same query to each line.""" |
| 126 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_LINES)) |
| 127 | ret = cli(['--lines', '-', 'name']) |
| 128 | assert ret == 0 |
| 129 | captured = capsys.readouterr() |
| 130 | assert captured.out == '"Gilbert"\n"Alexa"\n"May"\n"Deloise"\n' |
| 131 | assert not captured.err |
| 132 | |
| 133 | |
| 134 | def test_cli_lines_failed_lines_verbosity_0(monkeypatch, capsys): |
| 135 | """It should keep going with the other lines and just skip the failed line.""" |
| 136 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_LINES_WITH_ERRORS)) |
| 137 | ret = cli(['--lines', '-', 'name']) |
| 138 | assert ret == 1 |
| 139 | captured = capsys.readouterr() |
| 140 | assert captured.out == '"Gilbert"\n"Deloise"\n' |
| 141 | assert not captured.err |
| 142 | |
| 143 | |
| 144 | def test_cli_lines_failed_lines_verbosity_1(monkeypatch, capsys): |
| 145 | """It should keep going with the other lines printing an error for the failed lines.""" |
| 146 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_LINES_WITH_ERRORS)) |
| 147 | ret = cli(['-v', '--lines', '-', 'name']) |
| 148 | assert ret == 1 |
| 149 | captured = capsys.readouterr() |
| 150 | assert captured.out == '"Gilbert"\n"Deloise"\n' |
| 151 | assert captured.err.count('JSONDecodeError') == 2 |
| 152 | |
| 153 | |
| 154 | def test_cli_lines_failed_lines_verbosity_2(monkeypatch): |
| 155 | """It should interrupt the processing and print the full traceback.""" |
| 156 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_LINES_WITH_ERRORS)) |
| 157 | with pytest.raises( |
| 158 | json.decoder.JSONDecodeError, match=r'Expecting property name enclosed in double quotes'): |
| 159 | cli(['-vv', '--lines', '-', 'name']) |
| 160 | |
| 161 | |
| 162 | def test_cli_lines_double_dot_query(monkeypatch, capsys): |
| 163 | """It should encapsulate each line in an array to allow queries.""" |
| 164 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_LINES)) |
| 165 | ret = cli(['--lines', '..#(age>45).name']) |
| 166 | assert ret == 1 |
| 167 | captured = capsys.readouterr() |
| 168 | assert captured.out == '"Gilbert"\n"May"\n' |
| 169 | assert not captured.err |
| 170 | |
| 171 | |
| 172 | def test_cli_double_dot_query_ok(monkeypatch, capsys): |
| 173 | """It should encapsulate the input in an array and apply the query to the array.""" |
| 174 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_LINES)) |
| 175 | ret = cli(['-', '..#.name']) |
| 176 | assert ret == 0 |
| 177 | captured = capsys.readouterr() |
| 178 | assert captured.out == '["Gilbert", "Alexa", "May", "Deloise"]\n' |
| 179 | assert not captured.err |
| 180 | |
| 181 | |
| 182 | def test_cli_double_dot_query_failed_lines_verbosity_0(monkeypatch, capsys): |
| 183 | """It should encapsulate the input in an array skipping failing lines.""" |
| 184 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_LINES_WITH_ERRORS)) |
| 185 | ret = cli(['-', '..#.name']) |
| 186 | assert ret == 1 |
| 187 | captured = capsys.readouterr() |
| 188 | assert not captured.out |
| 189 | assert not captured.err |
| 190 | |
| 191 | |
| 192 | def test_cli_double_dot_query_failed_lines_verbosity_1(monkeypatch, capsys): |
| 193 | """It should encapsulate the input in an array skipping failing lines and printing an error for each failure.""" |
| 194 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_LINES_WITH_ERRORS)) |
| 195 | ret = cli(['-v', '-', '..#.name']) |
| 196 | assert ret == 1 |
| 197 | captured = capsys.readouterr() |
| 198 | assert not captured.out |
| 199 | assert captured.err.startswith('JSONDecodeError: Expecting property name enclosed in double quotes') |
| 200 | |
| 201 | |
| 202 | def test_cli_double_dot_query_failed_lines_verbosity_2(monkeypatch): |
| 203 | """It should interrupt the execution at the first invalid line and exit printing the traceback.""" |
| 204 | monkeypatch.setattr('sys.stdin', io.StringIO(INPUT_LINES_WITH_ERRORS)) |
| 205 | with pytest.raises(json.decoder.JSONDecodeError, match=r'Expecting property name enclosed in double quotes'): |
| 206 | cli(['-vv', '-', '..#.name']) |