refactor: edit tests to prepend a string

In similar pattern to the first section (append a string), the
tests of the second section (to prepend a string to the orginal
file name) are revised for better portability on different
operating systems.

Signed-off-by: Norwid Behrnd <nbehrnd@yahoo.com>
This commit is contained in:
Norwid Behrnd 2024-11-03 19:50:28 +01:00
parent efdb8b33e7
commit 3e9786f62c
2 changed files with 127 additions and 1 deletions

View file

@ -107,3 +107,65 @@ def test_append(arg1, arg2, arg3):
# check if the OS can process the new file / space cleaning
os.remove(new_filename)
assert os.path.isfile(new_filename) is False
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.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

View file

@ -296,7 +296,71 @@ def test_append(arg1, arg2, arg3):
addition of string containing spaces, as well as the implicit
spacing.
#+begin_src python :tangle no
#+begin_src python :tangle test_appendfilename.py
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.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
#+end_src
@pytest.mark.prepend
@pytest.mark.parametrize("arg1", ["test.txt", "2021-12-31_test.txt",
"2021-12-31T18.48.22_test.txt",