From 052ca3793924a6ca80eba806b933114e32f19571 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Thu, 21 Nov 2024 21:11:26 +0100 Subject: [PATCH 1/5] style: lint tests against flake8 Tests were linted with flake8 (7.1.1 (mccabe: 0.7.0, pycodestyle: 2.12.1, pyflakes: 3.2.0) CPython 3.12.7). Signed-off-by: Norwid Behrnd --- test_appendfilename.py | 69 ++++++++++++++++++++------------------- test_generator.org | 73 ++++++++++++++++++++++-------------------- 2 files changed, 74 insertions(+), 68 deletions(-) diff --git a/test_appendfilename.py b/test_appendfilename.py index a680818..eb1fdf4 100755 --- a/test_appendfilename.py +++ b/test_appendfilename.py @@ -10,8 +10,8 @@ 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 +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 @@ -29,7 +29,7 @@ python -m pytest As a reminder, the following optional pytest flags may be useful to obtain a report tailored to your needs: -- `-x` exits right after the first failing test (reported by `E` instead of `.`) +- `-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 """ @@ -37,7 +37,6 @@ a report tailored to your needs: import re import os import shlex -import sys import subprocess from itertools import product @@ -46,9 +45,9 @@ import pytest PROGRAM = os.path.join("appendfilename", "__init__.py") # Cross-platform path -# 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 -# permutation of the three arguments and their levels defines 120 tests. +# 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" @@ -73,6 +72,7 @@ arg3_values = [ # 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): @@ -89,7 +89,7 @@ def test_append(arg1, arg2, arg3): # 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) + subprocess.run(full_command, text=True, check=True) # construct the new file name to be tested: if len(shlex.split(arg3)) == 0: @@ -98,9 +98,8 @@ def test_append(arg1, arg2, arg3): separator = shlex.split(arg3)[1] new_filename = "".join( - [ arg1[:-4], separator, - shlex.split(arg2)[1], ".txt" ]) - print(f"test criterion: {new_filename}") # visible by optional `pytest -s` + [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) @@ -109,9 +108,10 @@ def test_append(arg1, arg2, arg3): 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 file submitted. By -# permutation of the parameter's levels, this defines 240 tests. +# 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" @@ -140,6 +140,7 @@ arg4_values = [ # 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): @@ -158,7 +159,7 @@ def test_prepend(arg1, arg2, arg3, arg4): full_command = [ "python", PROGRAM, arg1 ] + shlex.split(arg2) + shlex.split(arg3) + shlex.split(arg4) - subprocess.run(full_command, text = True, check = True) + subprocess.run(full_command, text=True, check=True) # construct the new file name to be tested: if len(shlex.split(arg3)) == 0: @@ -166,7 +167,7 @@ def test_prepend(arg1, arg2, arg3, arg4): else: separator = shlex.split(arg3)[1] - new_filename = "".join( [ shlex.split(arg2)[1], separator, arg1 ] ) + 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? @@ -179,12 +180,13 @@ def test_prepend(arg1, arg2, arg3, arg4): # 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` + # "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", @@ -194,15 +196,15 @@ arg2_values = [ ] arg3_values = [ "", # i.e. fall back to default single space -# "--separator '!'", -# "--separator '@'", -# "--separator '#'", -# "--separator '$'", -# "--separator '%'", -# "--separator '_'", -# "--separator '+'", -# "--separator '='", -# "--separator '-'" + # "--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 @@ -211,6 +213,7 @@ arg3_values = [ # create the permutations: test_cases = list(product(arg1_values, arg2_values, arg3_values)) + @pytest.mark.smart @pytest.mark.parametrize("arg1, arg2, arg3", test_cases) def test_smart_prepend(arg1, arg2, arg3): @@ -221,17 +224,17 @@ def test_smart_prepend(arg1, arg2, arg3): arg3 the separator (at least in Windows 10, do not use `*` """ time_stamp = "" - time_stamp_separator = "" + # time_stamp_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` on this test file run_appendfilename = " ".join( ["python", PROGRAM, arg1, arg2, arg3, " --smart-prepend"]) - subprocess.run(run_appendfilename, shell=True, check = True) + subprocess.run(run_appendfilename, shell=True, check=True) # construct the new file name to be testedt: old_filename = arg1 @@ -288,9 +291,9 @@ def test_smart_prepend(arg1, arg2, arg3): time_stamp_separator = old_filename[6] old_filename_no_timestamp = old_filename[7:] - new_filename = "".join([time_stamp, #time_stamp_separator, + new_filename = "".join([time_stamp, # time_stamp_separator, separator, shlex.split(arg2)[1], separator, - old_filename_no_timestamp ]) + old_filename_no_timestamp]) # is the new file present? print("\nnew_filename") # optional check for `pytest -s` diff --git a/test_generator.org b/test_generator.org index fe27ac8..0b1a26e 100755 --- a/test_generator.org +++ b/test_generator.org @@ -1,8 +1,8 @@ # name: test_generator.org # author: nbehrnd@yahoo.com # date: 2022-01-05 (YYYY-MM-DD) -# edit: [2024-11-05 Tue] -# license: GPL3, 2022. +# edit: [2024-11-21 Thu] +# license: GPL3, 2022-2024 # Export the tangled files with C-c C-v t #+PROPERTY: header-args :tangle yes @@ -163,8 +163,8 @@ markers = 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 +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 @@ -182,7 +182,7 @@ python -m pytest As a reminder, the following optional pytest flags may be useful to obtain a report tailored to your needs: -- `-x` exits right after the first failing test (reported by `E` instead of `.`) +- `-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 """ @@ -190,7 +190,6 @@ a report tailored to your needs: import re import os import shlex -import sys import subprocess from itertools import product @@ -206,9 +205,9 @@ PROGRAM = os.path.join("appendfilename", "__init__.py") # Cross-platform path yield =test example.txt=. #+begin_src python :tangle test_appendfilename.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 -# permutation of the three arguments and their levels defines 120 tests. +# 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" @@ -233,6 +232,7 @@ arg3_values = [ # 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): @@ -249,7 +249,7 @@ def test_append(arg1, arg2, arg3): # 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) + subprocess.run(full_command, text=True, check=True) # construct the new file name to be tested: if len(shlex.split(arg3)) == 0: @@ -258,9 +258,8 @@ def test_append(arg1, arg2, arg3): separator = shlex.split(arg3)[1] new_filename = "".join( - [ arg1[:-4], separator, - shlex.split(arg2)[1], ".txt" ]) - print(f"test criterion: {new_filename}") # visible by optional `pytest -s` + [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) @@ -278,9 +277,10 @@ def test_append(arg1, arg2, arg3): #+begin_src python :tangle test_appendfilename.py -# The following section is about tests to prepend a user defined string and -# an adjustable separator to the original file name of the file submitted. By -# permutation of the parameter's levels, this defines 240 tests. +# 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" @@ -309,6 +309,7 @@ arg4_values = [ # 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): @@ -327,7 +328,7 @@ def test_prepend(arg1, arg2, arg3, arg4): full_command = [ "python", PROGRAM, arg1 ] + shlex.split(arg2) + shlex.split(arg3) + shlex.split(arg4) - subprocess.run(full_command, text = True, check = True) + subprocess.run(full_command, text=True, check=True) # construct the new file name to be tested: if len(shlex.split(arg3)) == 0: @@ -335,7 +336,7 @@ def test_prepend(arg1, arg2, arg3, arg4): else: separator = shlex.split(arg3)[1] - new_filename = "".join( [ shlex.split(arg2)[1], separator, arg1 ] ) + 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? @@ -363,12 +364,13 @@ def test_prepend(arg1, arg2, arg3, arg4): # 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` + # "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", @@ -378,15 +380,15 @@ arg2_values = [ ] arg3_values = [ "", # i.e. fall back to default single space -# "--separator '!'", -# "--separator '@'", -# "--separator '#'", -# "--separator '$'", -# "--separator '%'", -# "--separator '_'", -# "--separator '+'", -# "--separator '='", -# "--separator '-'" + # "--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 @@ -395,6 +397,7 @@ arg3_values = [ # create the permutations: test_cases = list(product(arg1_values, arg2_values, arg3_values)) + @pytest.mark.smart @pytest.mark.parametrize("arg1, arg2, arg3", test_cases) def test_smart_prepend(arg1, arg2, arg3): @@ -405,17 +408,17 @@ def test_smart_prepend(arg1, arg2, arg3): arg3 the separator (at least in Windows 10, do not use `*` """ time_stamp = "" - time_stamp_separator = "" + # time_stamp_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` on this test file run_appendfilename = " ".join( ["python", PROGRAM, arg1, arg2, arg3, " --smart-prepend"]) - subprocess.run(run_appendfilename, shell=True, check = True) + subprocess.run(run_appendfilename, shell=True, check=True) # construct the new file name to be testedt: old_filename = arg1 @@ -472,9 +475,9 @@ def test_smart_prepend(arg1, arg2, arg3): time_stamp_separator = old_filename[6] old_filename_no_timestamp = old_filename[7:] - new_filename = "".join([time_stamp, #time_stamp_separator, + new_filename = "".join([time_stamp, # time_stamp_separator, separator, shlex.split(arg2)[1], separator, - old_filename_no_timestamp ]) + old_filename_no_timestamp]) # is the new file present? print("\nnew_filename") # optional check for `pytest -s` From 548f4f848ea8acb30e35188302a52897e13acdba Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Thu, 21 Nov 2024 21:13:32 +0100 Subject: [PATCH 2/5] style: remove Makefile no longer used Signed-off-by: Norwid Behrnd --- Makefile | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 Makefile diff --git a/Makefile b/Makefile deleted file mode 100644 index 299aff7..0000000 --- a/Makefile +++ /dev/null @@ -1,21 +0,0 @@ -# GNU Make file for the automation of pytest for appendfilename -# -# While the test script is written for Python 3.9.2, it depends on -# your installation of pytest (and in case of Linux, the authors of -# your distribution) if pytest for Python 3 is invoked either by -# pytest, or pytest-3. In some distributions, pytest actually may -# invoke pyest for legacy Python 2; the tests in test_date2name.py -# however are incompatible to this. -# -# Put this file like test_appendfilename.py in the root folder of -# appendfilename fetched from PyPi or GitHub. Then run -# -# chmod +x * -# make ./Makefile -# -# to run the tests. If you want pytest to exit the test sequence -# right after the first test failing, use the -x flag to the -# instructions on the CLI in addition to the verbosity flag to (-v). - -# pytest -v test_appendfilename.py # the pattern by pytest's manual -pytest-3 -v test_appendfilename.py # the alternative pattern (e.g., Debian 12) From 045f946439a682d90af23111412e2e9c8e06c4d5 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Thu, 21 Nov 2024 21:17:57 +0100 Subject: [PATCH 3/5] style: more compact generation of pytest.ini Signed-off-by: Norwid Behrnd --- pytest.ini | 6 +++--- test_appendfilename.py | 2 +- test_generator.org | 35 +++++++++++++---------------------- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/pytest.ini b/pytest.ini index 1bd280b..f986737 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,5 +1,5 @@ [pytest] markers = - default: appendfilename's default string insertions - prepend: appendfilename's optional -p/--prepend flag - smart: appendfilename's optional --smart-prepend flag + default: test appendfilename's default string insertion + prepend: test appendfilename's optional -p/--prepend flag + smart_prepend: test appendfilename's optional --smart-prepend flag diff --git a/test_appendfilename.py b/test_appendfilename.py index eb1fdf4..35e2f11 100755 --- a/test_appendfilename.py +++ b/test_appendfilename.py @@ -214,7 +214,7 @@ arg3_values = [ test_cases = list(product(arg1_values, arg2_values, arg3_values)) -@pytest.mark.smart +@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 diff --git a/test_generator.org b/test_generator.org index 0b1a26e..d27de88 100755 --- a/test_generator.org +++ b/test_generator.org @@ -104,17 +104,16 @@ python -m pytest -m "prepend" : t If the previous block was evaluated as .TRUE. (=t=), test script and - Makefile may be tangled right now by =C-c C-v t=. After closing - this =.org= file, deploy them as indicated earlier. + =pytest.ini= may be tangled right now by =C-c C-v t=. After closing + this =.org= file, deploy them as indicated. * Building the tests ** Building file =pytest.ini= - Markers file =pytest.ini= defines and which are used in file - =test_appendfilename.py= allows to constrain the run of - =pytest=. Rather than to launch a check on all tests, a call - like e.g., + File =pytest.ini= lists markers in file =test_appendfilename.py= + to optionally constrain =pytest= tests. Rather than to launch + a check on all tests defined, a call like e.g., #+begin_src bash :tangle no python -m pytest -m "prepend" @@ -123,11 +122,13 @@ python -m pytest -m "prepend" only checks those of set =prepend=. At present, tests are grouped as - + =default= appendfilename's default string insertions - + =prepend= corresponding to either appendfilename's optional =-p= - or =--prepend= flag, and - + =smart= corresponding to appendfilename's optional - =--smart-prepend= flag + #+begin_src shell :tangle pytest.ini +[pytest] +markers = + default: test appendfilename's default string insertion + prepend: test appendfilename's optional -p/--prepend flag + smart_prepend: test appendfilename's optional --smart-prepend flag + #+end_src It is possible to run one, two, or all three groups in one run of pytest, for instance @@ -136,16 +137,6 @@ python -m pytest -m "prepend" pytest-3 test_appendfilename.py -m "default and prepend" -v #+end_src - The content of =pytest.ini=: - - #+begin_src shell :tangle pytest.ini -[pytest] -markers = - default: appendfilename's default string insertions - prepend: appendfilename's optional -p/--prepend flag - smart: appendfilename's optional --smart-prepend flag - #+end_src - ** Building the test script *** header section @@ -398,7 +389,7 @@ arg3_values = [ test_cases = list(product(arg1_values, arg2_values, arg3_values)) -@pytest.mark.smart +@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 From 62bf545c0cdf6a6f10f1bb1d3395c2b9f783d9ff Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Thu, 21 Nov 2024 21:21:59 +0100 Subject: [PATCH 4/5] style: edit test series 'default' for compactness Signed-off-by: Norwid Behrnd --- test_appendfilename.py | 20 ++++++++++++++------ test_generator.org | 24 ++++++++++++++++-------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/test_appendfilename.py b/test_appendfilename.py index 35e2f11..41fd7ba 100755 --- a/test_appendfilename.py +++ b/test_appendfilename.py @@ -4,7 +4,7 @@ # author: nbehrnd@yahoo.com # license: GPL v3, 2022. # date: 2022-01-05 (YYYY-MM-DD) -# edit: [2024-11-05 Tue] +# edit: [2024-11-21 Thu] # """Test pad for functions by appendfilename with pytest. @@ -18,9 +18,9 @@ environment for Python, e.g. by python -m venv sup ``` -In the activated virtual environment, ensure the dependencies are met - -either by `pip install pyreadline3 pytest`, or `pip install -r requirements.txt` -- and launch the tests by +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 @@ -32,6 +32,12 @@ 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 @@ -43,7 +49,7 @@ from itertools import product import pytest -PROGRAM = os.path.join("appendfilename", "__init__.py") # Cross-platform path +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 @@ -67,7 +73,9 @@ arg3_values = [ "--separator '='", "--separator '-'" ] -# Note: The check with pytest and `*` as separator in Windows 10 fails. +# 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 +# # create the permutations: test_cases = list(product(arg1_values, arg2_values, arg3_values)) diff --git a/test_generator.org b/test_generator.org index d27de88..1e2d440 100755 --- a/test_generator.org +++ b/test_generator.org @@ -148,7 +148,7 @@ pytest-3 test_appendfilename.py -m "default and prepend" -v # author: nbehrnd@yahoo.com # license: GPL v3, 2022. # date: 2022-01-05 (YYYY-MM-DD) -# edit: [2024-11-05 Tue] +# edit: [2024-11-21 Thu] # """Test pad for functions by appendfilename with pytest. @@ -162,9 +162,9 @@ environment for Python, e.g. by python -m venv sup ``` -In the activated virtual environment, ensure the dependencies are met - -either by `pip install pyreadline3 pytest`, or `pip install -r requirements.txt` -- and launch the tests by +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 @@ -176,6 +176,12 @@ 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 @@ -187,13 +193,13 @@ from itertools import product import pytest -PROGRAM = os.path.join("appendfilename", "__init__.py") # Cross-platform path +PROGRAM = os.path.join("appendfilename", "__init__.py") #+end_src *** appendfilename, default position - Departing with file =test.txt=, appendfile's addition of =example= should - yield =test example.txt=. + Departing from e.g., file =2021-12-31_test.txt=, appendfile's addition of + =example= should yield =2021-12-31_test example.txt=. #+begin_src python :tangle test_appendfilename.py # The following section tests the applications default pattern where a @@ -218,7 +224,9 @@ arg3_values = [ "--separator '='", "--separator '-'" ] -# Note: The check with pytest and `*` as separator in Windows 10 fails. +# 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 +# # create the permutations: test_cases = list(product(arg1_values, arg2_values, arg3_values)) From 78157342b751f7ee5bf704684aa20464f5b5cf89 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Fri, 22 Nov 2024 10:24:25 +0100 Subject: [PATCH 5/5] refactor: rewrite pattern search, smart-prepend option The pattern search about the smart-prepend option was rewritten to be easier to read and maintain. Signed-off-by: Norwid Behrnd --- .github/workflows/pytest.yml | 7 ++-- test_appendfilename.py | 63 ++++++++++++++++---------------- test_generator.org | 69 +++++++++++++++++------------------- 3 files changed, 68 insertions(+), 71 deletions(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 02d87d9..3b9e41e 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -3,7 +3,7 @@ name: CI_pytest_appendfilename # name : pytest.yml # purpose : regularly run pytest on appendfilename # date : [2024-10-31 Thu] -# edit : [2024-11-12 Tue] +# edit : [2024-11-22 Fri] on: push: @@ -43,7 +43,10 @@ jobs: - name: run the check by pytest run: | + echo "default" python -m pytest -m "default" + echo "prepend" python -m pytest -m "prepend" - python -m pytest -m "smart" + echo "smart_prepend" + python -m pytest -m "smart_prepend" diff --git a/test_appendfilename.py b/test_appendfilename.py index 41fd7ba..97482ab 100755 --- a/test_appendfilename.py +++ b/test_appendfilename.py @@ -4,7 +4,7 @@ # author: nbehrnd@yahoo.com # license: GPL v3, 2022. # date: 2022-01-05 (YYYY-MM-DD) -# edit: [2024-11-21 Thu] +# edit: [2024-11-22 Fri] # """Test pad for functions by appendfilename with pytest. @@ -231,8 +231,8 @@ def test_smart_prepend(arg1, arg2, arg3): arg2 the text string to be added arg3 the separator (at least in Windows 10, do not use `*` """ - time_stamp = "" - # time_stamp_separator = "" + timestamp = "" + # timestamp_separator = "" old_filename_no_timestamp = "" # create a test file: @@ -253,7 +253,7 @@ def test_smart_prepend(arg1, arg2, arg3): else: separator = shlex.split(arg3)[1] - # Time stamps `date2name` provides can be either one of five formats + # Timestamps `date2name` provides can be either one of five formats # # YYYY-MM-DDTHH.MM.SS `--withtime` # YYYY-MM-DD default @@ -269,42 +269,39 @@ def test_smart_prepend(arg1, arg2, arg3): # https://github.com/novoid/appendfilename/issues/15 # https://github.com/novoid/appendfilename/issues/16 - # pattern `--with-time` - if re.search(r"^\d{4}-[012]\d-[0-3]\dT[012]\d\.[0-5]\d\.[0-5]\d", old_filename): - time_stamp = old_filename[:19] - time_stamp_separator = old_filename[19] - old_filename_no_timestamp = old_filename[20:] + 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" + ] - # default pattern - elif re.search(r"^\d{4}-[012]\d-[0-3]\d", old_filename): - time_stamp = old_filename[:10] - time_stamp_separator = old_filename[10] - old_filename_no_timestamp = old_filename[11:] + 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:] - # pattern `--compact` # currently fails - elif re.search(r"^\d{4}[012]\d[0-3]\d", old_filename): - time_stamp = old_filename[:8] - time_stamp_separator = old_filename[8] - old_filename_no_timestamp = old_filename[9:] + 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) - # pattern `--month` # currently fails - elif re.search(r"^\d{4}-[012]\d", old_filename): - time_stamp = old_filename[:7] - time_stamp_separator = old_filename[7] - old_filename_no_timestamp = old_filename[8:] + break - # pattern `--short` # currently fails - elif re.search(r"^\d{4}[012]\d[0-3]\d", old_filename): - time_stamp = old_filename[:6] - time_stamp_separator = old_filename[6] - old_filename_no_timestamp = old_filename[7:] - - new_filename = "".join([time_stamp, # time_stamp_separator, + new_filename = "".join([ + timestamp, # timestamp_separator, separator, shlex.split(arg2)[1], separator, - old_filename_no_timestamp]) + old_filename_no_timestamp + ]) # is the new file present? - print("\nnew_filename") # optional check for `pytest -s` + print("new_filename") # optional check for `pytest -s` print(new_filename) assert os.path.isfile(new_filename) diff --git a/test_generator.org b/test_generator.org index 1e2d440..e9e62b3 100755 --- a/test_generator.org +++ b/test_generator.org @@ -1,7 +1,7 @@ # name: test_generator.org # author: nbehrnd@yahoo.com # date: 2022-01-05 (YYYY-MM-DD) -# edit: [2024-11-21 Thu] +# edit: [2024-11-22 Fri] # license: GPL3, 2022-2024 # Export the tangled files with C-c C-v t @@ -39,10 +39,10 @@ python -m pytest - =-x= stops the sequence after the first failing test - =-s= occasionally provides information e.g., about the tests' criteria - The tests are organized in sets =default=, =prepend=, and =smart=. + The tests are organized in sets =default=, =prepend=, and =smart_prepend=. This allows to selectively run only checks which are about the results by =appendfilename= in the /default/ mode, /prepend/ mode, - or /smart prepend/ mode alone, e.g. + or /smart-prepend/ mode alone, e.g. #+begin_src shell :tangle no python -m pytest -m "prepend" @@ -148,7 +148,7 @@ pytest-3 test_appendfilename.py -m "default and prepend" -v # author: nbehrnd@yahoo.com # license: GPL v3, 2022. # date: 2022-01-05 (YYYY-MM-DD) -# edit: [2024-11-21 Thu] +# edit: [2024-11-22 Fri] # """Test pad for functions by appendfilename with pytest. @@ -406,8 +406,8 @@ def test_smart_prepend(arg1, arg2, arg3): arg2 the text string to be added arg3 the separator (at least in Windows 10, do not use `*` """ - time_stamp = "" - # time_stamp_separator = "" + timestamp = "" + # timestamp_separator = "" old_filename_no_timestamp = "" # create a test file: @@ -428,7 +428,7 @@ def test_smart_prepend(arg1, arg2, arg3): else: separator = shlex.split(arg3)[1] - # Time stamps `date2name` provides can be either one of five formats + # Timestamps `date2name` provides can be either one of five formats # # YYYY-MM-DDTHH.MM.SS `--withtime` # YYYY-MM-DD default @@ -444,42 +444,39 @@ def test_smart_prepend(arg1, arg2, arg3): # https://github.com/novoid/appendfilename/issues/15 # https://github.com/novoid/appendfilename/issues/16 - # pattern `--with-time` - if re.search(r"^\d{4}-[012]\d-[0-3]\dT[012]\d\.[0-5]\d\.[0-5]\d", old_filename): - time_stamp = old_filename[:19] - time_stamp_separator = old_filename[19] - old_filename_no_timestamp = old_filename[20:] + 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" + ] - # default pattern - elif re.search(r"^\d{4}-[012]\d-[0-3]\d", old_filename): - time_stamp = old_filename[:10] - time_stamp_separator = old_filename[10] - old_filename_no_timestamp = old_filename[11:] + 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:] - # pattern `--compact` # currently fails - elif re.search(r"^\d{4}[012]\d[0-3]\d", old_filename): - time_stamp = old_filename[:8] - time_stamp_separator = old_filename[8] - old_filename_no_timestamp = old_filename[9:] + 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) - # pattern `--month` # currently fails - elif re.search(r"^\d{4}-[012]\d", old_filename): - time_stamp = old_filename[:7] - time_stamp_separator = old_filename[7] - old_filename_no_timestamp = old_filename[8:] + break - # pattern `--short` # currently fails - elif re.search(r"^\d{4}[012]\d[0-3]\d", old_filename): - time_stamp = old_filename[:6] - time_stamp_separator = old_filename[6] - old_filename_no_timestamp = old_filename[7:] - - new_filename = "".join([time_stamp, # time_stamp_separator, + new_filename = "".join([ + timestamp, # timestamp_separator, separator, shlex.split(arg2)[1], separator, - old_filename_no_timestamp]) + old_filename_no_timestamp + ]) # is the new file present? - print("\nnew_filename") # optional check for `pytest -s` + print("new_filename") # optional check for `pytest -s` print(new_filename) assert os.path.isfile(new_filename)