|
1
|
|
|
import shlex |
|
2
|
|
|
|
|
3
|
|
|
import pytest |
|
4
|
|
|
|
|
5
|
|
|
from jrnl.args import parse_args |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
def cli_as_dict(str): |
|
9
|
|
|
cli = shlex.split(str) |
|
10
|
|
|
args = parse_args(cli) |
|
11
|
|
|
return vars(args) |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def expected_args(**kwargs): |
|
15
|
|
|
default_args = { |
|
16
|
|
|
"contains": None, |
|
17
|
|
|
"debug": False, |
|
18
|
|
|
"delete": False, |
|
19
|
|
|
"edit": False, |
|
20
|
|
|
"end_date": None, |
|
21
|
|
|
"excluded": [], |
|
22
|
|
|
"export": False, |
|
23
|
|
|
"filename": None, |
|
24
|
|
|
"limit": None, |
|
25
|
|
|
"on_date": None, |
|
26
|
|
|
"preconfig_cmd": None, |
|
27
|
|
|
"postconfig_cmd": None, |
|
28
|
|
|
"short": False, |
|
29
|
|
|
"starred": False, |
|
30
|
|
|
"start_date": None, |
|
31
|
|
|
"strict": False, |
|
32
|
|
|
"tags": False, |
|
33
|
|
|
"text": [], |
|
34
|
|
|
} |
|
35
|
|
|
return {**default_args, **kwargs} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def test_empty(): |
|
39
|
|
|
assert cli_as_dict("") == expected_args() |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def test_contains_alone(): |
|
43
|
|
|
assert cli_as_dict("-contains whatever") == expected_args(contains="whatever") |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def test_debug_alone(): |
|
47
|
|
|
assert cli_as_dict("--debug") == expected_args(debug=True) |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
def test_delete_alone(): |
|
51
|
|
|
assert cli_as_dict("--delete") == expected_args(delete=True) |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def test_diagnostic_alone(): |
|
55
|
|
|
from jrnl.commands import preconfig_diagnostic |
|
56
|
|
|
|
|
57
|
|
|
assert cli_as_dict("--diagnostic") == expected_args( |
|
58
|
|
|
preconfig_cmd=preconfig_diagnostic |
|
59
|
|
|
) |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
def test_edit_alone(): |
|
63
|
|
|
assert cli_as_dict("--edit") == expected_args(edit=True) |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
def test_encrypt_alone(): |
|
67
|
|
|
from jrnl.commands import postconfig_encrypt |
|
68
|
|
|
|
|
69
|
|
|
assert cli_as_dict("--encrypt") == expected_args(postconfig_cmd=postconfig_encrypt) |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
def test_decrypt_alone(): |
|
73
|
|
|
from jrnl.commands import postconfig_decrypt |
|
74
|
|
|
|
|
75
|
|
|
assert cli_as_dict("--decrypt") == expected_args(postconfig_cmd=postconfig_decrypt) |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
def test_end_date_alone(): |
|
79
|
|
|
expected = expected_args(end_date="2020-01-01") |
|
80
|
|
|
assert expected == cli_as_dict("-until 2020-01-01") |
|
81
|
|
|
assert expected == cli_as_dict("-to 2020-01-01") |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
def test_not_alone(): |
|
85
|
|
|
assert cli_as_dict("-not test") == expected_args(excluded=["test"]) |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
def test_not_multiple_alone(): |
|
89
|
|
|
assert cli_as_dict("-not one -not two") == expected_args(excluded=["one", "two"]) |
|
90
|
|
|
assert cli_as_dict("-not one -not two -not three") == expected_args( |
|
91
|
|
|
excluded=["one", "two", "three"] |
|
92
|
|
|
) |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
@pytest.mark.parametrize( |
|
96
|
|
|
"cli", |
|
97
|
|
|
[ |
|
98
|
|
|
"two -not one -not three", |
|
99
|
|
|
"-not one two -not three", |
|
100
|
|
|
"-not one -not three two", |
|
101
|
|
|
], |
|
102
|
|
|
) |
|
103
|
|
|
def test_not_mixed(cli): |
|
104
|
|
|
result = expected_args(excluded=["one", "three"], text=["two"]) |
|
105
|
|
|
assert cli_as_dict(cli) == result |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
def test_not_interspersed(): |
|
109
|
|
|
result = expected_args(excluded=["one", "three"], text=["two", "two", "two"]) |
|
110
|
|
|
assert cli_as_dict("two -not one two -not three two") == result |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
def test_export_alone(): |
|
114
|
|
|
assert cli_as_dict("--export json") == expected_args(export="json") |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
def test_import_alone(): |
|
118
|
|
|
from jrnl.commands import postconfig_import |
|
119
|
|
|
|
|
120
|
|
|
assert cli_as_dict("--import") == expected_args(postconfig_cmd=postconfig_import) |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
def test_file_flag_alone(): |
|
124
|
|
|
assert cli_as_dict("--file test.txt") == expected_args(filename="test.txt") |
|
125
|
|
|
assert cli_as_dict("--file 'lorem ipsum.txt'") == expected_args( |
|
126
|
|
|
filename="lorem ipsum.txt" |
|
127
|
|
|
) |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
def test_limit_alone(): |
|
131
|
|
|
assert cli_as_dict("-n 5") == expected_args(limit=5) |
|
132
|
|
|
assert cli_as_dict("-n 999") == expected_args(limit=999) |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
def test_limit_shorthand_alone(): |
|
136
|
|
|
assert cli_as_dict("-5") == expected_args(limit=5) |
|
137
|
|
|
assert cli_as_dict("-999") == expected_args(limit=999) |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
def test_list_alone(): |
|
141
|
|
|
from jrnl.commands import postconfig_list |
|
142
|
|
|
|
|
143
|
|
|
assert cli_as_dict("--ls") == expected_args(postconfig_cmd=postconfig_list) |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
def test_on_date_alone(): |
|
147
|
|
|
assert cli_as_dict("-on 'saturday'") == expected_args(on_date="saturday") |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
def test_short_alone(): |
|
151
|
|
|
assert cli_as_dict("--short") == expected_args(short=True) |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
def test_starred_alone(): |
|
155
|
|
|
assert cli_as_dict("-starred") == expected_args(starred=True) |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
def test_start_date_alone(): |
|
159
|
|
|
assert cli_as_dict("-from 2020-01-01") == expected_args(start_date="2020-01-01") |
|
160
|
|
|
assert cli_as_dict("-from 'January 1st'") == expected_args(start_date="January 1st") |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
def test_and_alone(): |
|
164
|
|
|
assert cli_as_dict("-and") == expected_args(strict=True) |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
def test_tags_alone(): |
|
168
|
|
|
assert cli_as_dict("--tags") == expected_args(tags=True) |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
def test_text_alone(): |
|
172
|
|
|
assert cli_as_dict("lorem ipsum dolor sit amet") == expected_args( |
|
173
|
|
|
text=["lorem", "ipsum", "dolor", "sit", "amet"] |
|
174
|
|
|
) |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
def test_version_alone(): |
|
178
|
|
|
from jrnl.commands import preconfig_version |
|
179
|
|
|
|
|
180
|
|
|
assert cli_as_dict("--version") == expected_args(preconfig_cmd=preconfig_version) |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
# @see https://github.com/jrnl-org/jrnl/issues/520 |
|
184
|
|
|
@pytest.mark.parametrize( |
|
185
|
|
|
"cli", |
|
186
|
|
|
[ |
|
187
|
|
|
"-and second @oldtag @newtag", |
|
188
|
|
|
"second @oldtag @newtag -and", |
|
189
|
|
|
"second -and @oldtag @newtag", |
|
190
|
|
|
"second @oldtag -and @newtag", |
|
191
|
|
|
], |
|
192
|
|
|
) |
|
193
|
|
|
def test_and_ordering(cli): |
|
194
|
|
|
result = expected_args(strict=True, text=["second", "@oldtag", "@newtag"]) |
|
195
|
|
|
assert cli_as_dict(cli) == result |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
# @see https://github.com/jrnl-org/jrnl/issues/520 |
|
199
|
|
|
@pytest.mark.parametrize( |
|
200
|
|
|
"cli", |
|
201
|
|
|
[ |
|
202
|
|
|
"--edit second @oldtag @newtag", |
|
203
|
|
|
"second @oldtag @newtag --edit", |
|
204
|
|
|
"second --edit @oldtag @newtag", |
|
205
|
|
|
"second @oldtag --edit @newtag", |
|
206
|
|
|
], |
|
207
|
|
|
) |
|
208
|
|
|
def test_edit_ordering(cli): |
|
209
|
|
|
result = expected_args(edit=True, text=["second", "@oldtag", "@newtag"]) |
|
210
|
|
|
assert cli_as_dict(cli) == result |
|
211
|
|
|
|