-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_appendfilename.py
executable file
·310 lines (251 loc) · 10 KB
/
test_appendfilename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/usr/env python3
# name: test_appendfilename.py
# author: [email protected]
# license: GPL v3, 2022.
# date: 2022-01-05 (YYYY-MM-DD)
# edit: [2024-11-22 Fri]
#
"""Test pad for functions by appendfilename with pytest.
Initially written for Python 3.9.9 and pytest 6.2.4 and recently update
for Python 3.12.6/pytest 8.3.3, this script provides a programmatic check
of functions offered by appendfilename. Deposit this script in the root
of the folder fetched and unzipped from PyPi or GitHub. Create a virtual
environment for Python, e.g. by
```shell
python -m venv sup
```
In the activated virtual environment, resolve the dependencies - either by
`pip install pyreadline3 pytest`, or `pip install -r requirements.txt` -
and launch the tests by
```shell
python -m pytest
```
As a reminder, the following optional pytest flags may be useful to obtain
a report tailored to your needs:
- `-x` exit after the first failing test (reported by `E` instead of `.`)
- `-v` provide a more verbose output
- `-s` equally report the test criterion, e.g. the queried file name
Equally keep in mind you can constrain pytest tests. Labels assigned are
- default: test appendfilename's default string insertion
- prepend: test appendfilename's optional -p/--prepend flag
- smart_prepend: test appendfilename's optional --smart-prepend flag
"""
import re
import os
import shlex
import subprocess
from itertools import product
import pytest
PROGRAM = os.path.join("appendfilename", "__init__.py")
# The following section tests the applications default pattern where a
# string is added to the file name, just prior to the file's file
# extension. The permutations of the arguments define 120 tests.
arg1_values = [
"test.txt", "2021-12-31_test.txt", "2021-12-31T18.48.22_test.txt"
]
arg2_values = [
"-t book", "-t book_shelf", "--text book", "--text book_shelf"
]
arg3_values = [
"", # i.e. fall back to default single space
"--separator '!'",
"--separator '@'",
"--separator '#'",
"--separator '$'",
"--separator '%'",
"--separator '_'",
"--separator '+'",
"--separator '='",
"--separator '-'"
]
# Note: In Windows 10, the check with pytest and `*` as separator fails
# because it is not a permitted character in a file name there. See
# <https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file>
# create the permutations:
test_cases = list(product(arg1_values, arg2_values, arg3_values))
@pytest.mark.default
@pytest.mark.parametrize("arg1, arg2, arg3", test_cases)
def test_append(arg1, arg2, arg3):
"""Test default which appends a string just prior file extension
arg1 the test file to process, partly inspired by `date2name`
arg2 the text string to be added
arg3 the separator (at least in Windows 10, do not use `*`)"""
# create a test file:
with open(arg1, mode="w", encoding="utf-8") as newfile:
newfile.write("This is a place holder.\n")
# run the test to be tested:
full_command = ["python", PROGRAM, arg1
] + shlex.split(arg2) + shlex.split(arg3)
subprocess.run(full_command, text=True, check=True)
# construct the new file name to be tested:
if len(shlex.split(arg3)) == 0:
separator = " "
else:
separator = shlex.split(arg3)[1]
new_filename = "".join(
[arg1[:-4], separator, shlex.split(arg2)[1], ".txt"])
print(f"test criterion: {new_filename}") # for an optional `pytest -s`
# is the new file present?
assert os.path.isfile(new_filename)
# check if the OS can process the new file / space cleaning
os.remove(new_filename)
assert os.path.isfile(new_filename) is False
# The following section is about tests to prepend a user defined string
# and an adjustable separator to the original file name of the submitted
# file. The permutation of the parameters defines 240 tests.
arg1_values = [
"test.txt", "2021-12-31_test.txt", "2021-12-31T18.48.22_test.txt"
]
arg2_values = [
"-t book", "-t book_shelf", "--text book", "--text book_shelf"
]
arg3_values = [
"", # i.e. fall back to default single space
"--separator '!'",
"--separator '@'",
"--separator '#'",
"--separator '$'",
"--separator '%'",
"--separator '_'",
"--separator '+'",
"--separator '='",
"--separator '-'"
]
# Note: The check with pytest and `*` as separator in Windows 10 fails.
arg4_values = [
"-p", "--prepend"
]
# create the permutations:
test_cases = list(product(arg1_values, arg2_values, arg3_values, arg4_values))
@pytest.mark.prepend
@pytest.mark.parametrize("arg1, arg2, arg3, arg4", test_cases)
def test_prepend(arg1, arg2, arg3, arg4):
"""test to prepend a string to the original file name
arg1 the test file to process, partly inspired by `date2name`
arg2 the text string to be added
arg3 the separator (at least in Windows 10, do not use `*`)
arg4 either short of long form to introduce the string as leading """
# create a test file:
with open(arg1, mode="w", encoding="utf-8") as newfile:
newfile.write("This is a place holder.\n")
# run the test to be tested:
full_command = [
"python", PROGRAM, arg1
] + shlex.split(arg2) + shlex.split(arg3) + shlex.split(arg4)
subprocess.run(full_command, text=True, check=True)
# construct the new file name to be tested:
if len(shlex.split(arg3)) == 0:
separator = " "
else:
separator = shlex.split(arg3)[1]
new_filename = "".join([shlex.split(arg2)[1], separator, arg1])
print(f"test criterion: {new_filename}") # visible by optional `pytest -s`
# is the new file present?
assert os.path.isfile(new_filename)
# check if the OS can process the new file / space cleaning
os.remove(new_filename)
assert os.path.isfile(new_filename) is False
# This section tests the insertion of a string into the file's file name
# just after the file's time or date stamp as provided `date2name`.
arg1_values = [
"2021-12-31T18.48.22_test.txt",
"2021-12-31_test.txt",
# "20211231_test.txt", # by now `20211231_test.txt` -> 20211231_test ping.txt
# "2021-12_test.txt", # by now `2021-12_test.txt` -> `2021-12_test ping.txt`
# "211231_test.txt" # by now `211231_test.txt` -> `211231_test ping.txt`
]
arg2_values = [
"-t book",
"-t book_shelf",
"--text book",
"--text book_shelf"
]
arg3_values = [
"", # i.e. fall back to default single space
# "--separator '!'",
# "--separator '@'",
# "--separator '#'",
# "--separator '$'",
# "--separator '%'",
# "--separator '_'",
# "--separator '+'",
# "--separator '='",
# "--separator '-'"
]
# Note: The check with pytest and `*` as separator in Windows 10 fails.
# Contrasting to Linux Debian 13, a `pytest` in Windows 10 revealed every
# of these special characters can not safely used as an additional separator.
# create the permutations:
test_cases = list(product(arg1_values, arg2_values, arg3_values))
@pytest.mark.smart_prepend
@pytest.mark.parametrize("arg1, arg2, arg3", test_cases)
def test_smart_prepend(arg1, arg2, arg3):
"""test the insertion of a new string just past the time stamp
arg1 the test file to process, partly inspired by `date2name`
arg2 the text string to be added
arg3 the separator (at least in Windows 10, do not use `*`
"""
timestamp = ""
# timestamp_separator = ""
old_filename_no_timestamp = ""
# create a test file:
with open(arg1, mode="w", encoding="utf-8") as newfile:
newfile.write("this is a placeholder\n")
# run `appendfilename` on this test file
run_appendfilename = " ".join(
["python", PROGRAM, arg1, arg2, arg3, " --smart-prepend"])
subprocess.run(run_appendfilename, shell=True, check=True)
# construct the new file name to be testedt:
old_filename = arg1
# account for the implicit separator, i.e. the single space:
if len(shlex.split(arg3)) == 0:
separator = " "
else:
separator = shlex.split(arg3)[1]
# Timestamps `date2name` provides can be either one of five formats
#
# YYYY-MM-DDTHH.MM.SS `--withtime`
# YYYY-MM-DD default
# YYYYMMDD `--compact`
# YYYY-MM `--month`
# YYMMDD `--short`
# Currently, one observes two patterns by `appendfilename`: one which
# substitutes the separator by `date2name`, the other which retains it.
# Note patterns `compact`, `month`, and `short`, currently append the
# additional string rather than smartly prepend after the date stamp --
# for now, these three are not tested. Equally see discussions 15 and 16,
# https://github.com/novoid/appendfilename/issues/15
# https://github.com/novoid/appendfilename/issues/16
patterns = [
r"^\d{4}-[012]\d-[0-3]\dT[012]\d\.[0-5]\d\.[0-5]\d",
r"^\d{4}-[012]\d-[0-3]\d",
r"^\d{4}[012]\d[0-3]\d",
r"^\d{4}-[012]\d",
r"^\d{2}[012]\d[0-3]\d"
]
for pattern in patterns:
match = re.search(pattern, old_filename)
if match:
timestamp = re.findall(pattern, old_filename)[0]
timestamp_separator = str(old_filename)[len(timestamp)]
old_filename_no_timestamp = old_filename[len(timestamp) + 1:]
print("\n\ntest of option smart-prepend:") # `pytest -s` diagnosis
print("old_filename:")
print(old_filename)
print("timestamp, timestamp_separator, old_filename_no_timestamp")
print(timestamp)
print(timestamp_separator)
print(old_filename_no_timestamp)
break
new_filename = "".join([
timestamp, # timestamp_separator,
separator, shlex.split(arg2)[1], separator,
old_filename_no_timestamp
])
# is the new file present?
print("new_filename") # optional check for `pytest -s`
print(new_filename)
assert os.path.isfile(new_filename)
# check if the IS can process the new file / space cleaning
os.remove(new_filename)
assert os.path.isfile(new_filename) is False