forked from novoid/appendfilename
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_appendfilename.py
289 lines (237 loc) · 8.84 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
#!/bin/usr/env python3
# name: test_appendfilename.py
# author: [email protected]
# license: GPL v3, 2022.
# date: 2022-01-05 (YYYY-MM-DD)
# edit: 2022-01-09 (YYYY-MM-DD)
#
"""Tests for appendfilename using pytest.
Python 3.12.7
"""
import re
import subprocess
import sys
from pathlib import Path
import pytest
TEST_DIR = Path("./.tmptestfiles")
TEST_DIR.mkdir(exist_ok=True)
TARGET_PROGRAM = Path("./appendfilename/__init__.py")
@pytest.fixture
def create_test_file(filename):
"""Fixture to create a test file.
Yields Path objects"""
test_file_path = TEST_DIR / filename
# Create the file
test_file_path.touch()
# assert test_file_path.is_file()
# Yield the Path object for use in tests
yield test_file_path
# cleanup step (important if test fails, want to remove all remnants)
# Comment out for now, want to know if any files get left behind
# test_file_path.unlink(missing_ok=True)
@pytest.mark.default
@pytest.mark.parametrize(
"filename",
[
"test",
"test.",
"test.txt",
"2021-12-31.txt",
"2021-12-31T18.48.22.txt",
"20211231.t.x.t",
"211231 test.longextensiontext",
],
)
@pytest.mark.parametrize(
"append_text", [
"book",
"book_shelf",
"book shelf"
]
)
@pytest.mark.parametrize(
"sep", [
" ",
"_",
"-"
]
)
def test_append_sep_notags(create_test_file: Path, append_text: str, sep: str):
"""Check addition just ahead the file extension.
create_test_file A fixture to create the files and remove them
append_text text to append
sep separator between existing filename and append_text
"""
original_path = create_test_file
command = f"{sys.executable} {TARGET_PROGRAM} -t \"{append_text}\" --sep \"{sep}\" \"{original_path}\""
subprocess.run(command)
new_filename = f"{original_path.stem}{sep}{append_text}{original_path.suffix}"
new_path = original_path.parent / new_filename
assert new_path.is_file()
new_path.unlink()
@pytest.mark.prepend
@pytest.mark.parametrize("filename", [
"test.txt",
"2021-12-31_test -- tag.txt",
"20211231 test -- tag tag2 tag3.txt",
"2012-12_test.txt",
"211231_test.txt"
])
@pytest.mark.parametrize("append_text", ["book", "book_shelf",
])
@pytest.mark.parametrize("sep", [
" ",
"!",
"@",
"-"])
def test_prepend_sep(create_test_file, append_text: str, sep: str):
"""Check addition just ahead the file extension.
create_test_file A fixture to create the files and remove them
append_text text to append (in this case, prepend)
sep separator between existing filename and append_text
"""
original_path = create_test_file
command = f"{sys.executable} {TARGET_PROGRAM} -t \"{append_text}\" --sep \"{sep}\" -p \"{original_path}\""
subprocess.run(command)
new_filename = f"{append_text}{sep}{original_path.name}"
new_path = original_path.parent / new_filename
assert new_path.is_file()
new_path.unlink()
@pytest.mark.smartprepend
@pytest.mark.parametrize("filename", [
"test.txt",
"2021-12-31 test.txt",
"2021-12-31T18.48.22 test -- tag tag2.txt",
"2021-12-31T18.48.22 test.txt",
"20211231_test -- tag.txt",
"2021-12_test.txt",
"211231_test.txt"
])
@pytest.mark.parametrize("append_text", [
"book",
"book_shelf",
"book she-lf",
])
@pytest.mark.parametrize("sep", [
" " ,
"#",
# "*", windows doesn't allow star in filenames
"_",
"-"
])
def test_smartprepend(create_test_file, append_text, sep):
"""Check that any time stamp stays at the front of the filename.
create_test_file A fixture to create the files and remove them
append_text text to append (in this case, prepend)
sep separator between existing filename and append_text
"""
original_path = create_test_file
command = f"{sys.executable} {TARGET_PROGRAM} -t \"{append_text}\" --sep \"{sep}\" --smart-prepend \"{original_path}\""
subprocess.run(command)
# analysis section:
old_filename = str(original_path.name)
# test patterns based on date2name vs. other pattern
RE_DATE2NAME_DEFAULT = r"^\d{4}-[01]\d-[0-3]\d" # date2name default (YYYY-MM-DD)
RE_DATE2NAME_WITHTIME = RE_DATE2NAME_DEFAULT + r"T[012]\d\.[0-5]\d\.[0-5]\d" # date2name --withtime (YYYY-MM-DDTHH.MM.SS)
RE_DATE2NAME_COMPACT = r"^\d{4}[01]\d[0-3]\d" # date2name --compact (YYYYMMDD)
RE_DATE2NAME_MONTH = r"^\d{4}-[01]\d" # date2name --month (YYYY-MM)
RE_DATE2NAME_SHORT = r"^\d{2}[01]\d[0-3]\d" # date2name --short (YYMMDD)
if (re.search(RE_DATE2NAME_DEFAULT, old_filename) or
re.search(RE_DATE2NAME_WITHTIME, old_filename) or
re.search(RE_DATE2NAME_COMPACT, old_filename) or
re.search(RE_DATE2NAME_MONTH, old_filename) or
re.search(RE_DATE2NAME_SHORT, old_filename)
):
if re.search(RE_DATE2NAME_WITHTIME, old_filename):
# if (running date2name --withtime) then .true.
time_stamp = old_filename[:19]
time_stamp_separator = old_filename[19]
file_extension = old_filename.split(".")[-1]
old_filename_no_timestamp = old_filename[20:]
elif re.search(RE_DATE2NAME_DEFAULT, old_filename):
# if (running date2name in default mode) then .true.
time_stamp = old_filename[:10]
time_stamp_separator = old_filename[10]
file_extension = old_filename.split(".")[-1]
old_filename_no_timestamp = old_filename[11:]
elif re.search(RE_DATE2NAME_COMPACT, old_filename):
# if (running date2name --compact) then .true.
time_stamp = old_filename[:8]
time_stamp_separator = old_filename[8]
file_extension = old_filename.split(".")[-1]
old_filename_no_timestamp = old_filename[9:]
elif re.search(RE_DATE2NAME_MONTH, old_filename):
# if (running date2name --month) then .true.
time_stamp = old_filename[:7]
time_stamp_separator = old_filename[7]
file_extension = old_filename.split(".")[-1]
old_filename_no_timestamp = old_filename[8:]
elif re.search(RE_DATE2NAME_SHORT, old_filename):
# if (running date2name --short) then .true.
time_stamp = old_filename[:6]
time_stamp_separator = old_filename[6]
file_extension = old_filename.split(".")[-1]
old_filename_no_timestamp = old_filename[7:]
stem_elements = old_filename_no_timestamp.split(".")[:-1]
stem = ".".join(stem_elements)
new_filename = f"{time_stamp}{sep}{append_text}{sep}{stem}.{file_extension}"
print(f"{time_stamp=}")
print(f"{sep=}")
print(f"{append_text=}")
print("TEST new_filename", new_filename)
# new_filename = f"{append_text}{sep}{original_path.name}"
new_path = original_path.parent / new_filename
print("TEST_NEWPATH!", new_path)
# assert new_path.is_file()
# new_path.unlink()
# assert os.path.isfile(new_path) is False
else:
# # within the scope set, a file which did not pass date2name earlier
new_filename = f"{append_text}{sep}{original_path.name}"
new_path = original_path.parent / new_filename
assert new_path.is_file()
new_path.unlink()
@pytest.mark.defaultwithtags
@pytest.mark.parametrize(
"filename",
[
"test -- tag",
"test -- tag.",
"test -- tag.txt",
"2021-12-31 -- tag.txt",
"2021-12-31T18.48.22 -- tag.txt",
"20211231.t.x -- tag.t",
"211231 test -- tag.longextensiontext",
],
)
@pytest.mark.parametrize(
"append_text", [
"book",
"book_shelf",
"book shelf"
]
)
@pytest.mark.parametrize(
"sep", [
" ",
"_",
"-"
]
)
def test_append_sep_tags(create_test_file: Path, append_text: str, sep: str):
"""Check addition just ahead the file extension.
create_test_file A fixture to create the files and remove them
append_text text to append
sep separator between existing filename and append_text
"""
original_path = create_test_file
command = f"{sys.executable} {TARGET_PROGRAM} -t \"{append_text}\" --sep \"{sep}\" \"{original_path}\""
subprocess.run(command)
new_filename = str(original_path).replace(" -- ", f"{sep}{append_text} -- ")
new_path = Path(new_filename)
print("-TEST OLD PATH=".ljust(25), original_path)
print("+TEST NEW PATH=".ljust(25), new_path)
assert new_path.is_file()
new_path.unlink()
# XXX test the regexs hard