From 3e9786f62cd79664cade233436bcba6c29a2e1c6 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Sun, 3 Nov 2024 19:50:28 +0100 Subject: [PATCH] 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 --- test_appendfilename.py | 62 +++++++++++++++++++++++++++++++++++++++ test_generator.org | 66 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 127 insertions(+), 1 deletion(-) diff --git a/test_appendfilename.py b/test_appendfilename.py index 59baa6b..6c998b1 100644 --- a/test_appendfilename.py +++ b/test_appendfilename.py @@ -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 diff --git a/test_generator.org b/test_generator.org index 2d38a5d..43d8b13 100755 --- a/test_generator.org +++ b/test_generator.org @@ -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",