From 2e3654fd781f8bff42a74770c6f6e95f101190f1 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Wed, 22 Sep 2021 21:00:53 +0000 Subject: [PATCH 01/14] error retracting tags, ticketed version The development version on GitHub offers the option to retract time stamps date2name prepended to the files' file names. As this version of the test script reveals, stamps obtained with the option --withtime however are not fully reverted. The finding was shared with Karl Voit as an issue for date2name earlier today (2021-09-22) for a cross check. --- Makefile | 0 test_date2name.py | 30 ++++++++++++++++++++++++++++++ test_generator.org | 45 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 73 insertions(+), 2 deletions(-) mode change 100644 => 100755 Makefile diff --git a/Makefile b/Makefile old mode 100644 new mode 100755 diff --git a/test_date2name.py b/test_date2name.py index 267c472..859d762 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -224,3 +224,33 @@ def test_short_pattern_YYMMDD(arg1): test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") assert os.path.isfile(new) os.remove(new) + +import time, os, sys +@pytest.mark.parametrize("arg1", ["default", "short", "compact", "month", "withtime"]) +@pytest.mark.parametrize("arg2", ["-r", "--remove"]) +def test_remove_stamp(arg1, arg2): + """Check the retraction of the leading time stamp.""" + substitution = {"default" : "2021-09-21", + "short" : "210921", + "compact" : "20210921", + "month" : "2021-09", + "withtime": "2021-09-21T13.59.59"} + prepend = substitution.get(arg1) + + BASIS = "test.txt" + TFILE = "" + TFILE = "_".join([prepend, BASIS]) + with open(TFILE, mode = "w") as newfile: + newfile.write("This is a test file.") + time.sleep(2) + + test = getoutput(f"python3 {PROGRAM} {TFILE} {arg2}") + time.sleep(2) + assert os.path.isfile(TFILE) is False # absence of stamped file + assert os.path.isfile(BASIS) # presence unstamped file + + try: + os.remove("test.txt") + except OSError: + print("Running remove test, file 'test.txt' was not erased.") + sys.exit() diff --git a/test_generator.org b/test_generator.org index c125923..d9044c9 100755 --- a/test_generator.org +++ b/test_generator.org @@ -1,6 +1,6 @@ #+NAME: test_generator.org #+AUTHOR: nbehrnd@yahoo.com -#+DATE: 2021-09-17 (YYYY-MM-DD) +#+DATE: 2021-09-22 (YYYY-MM-DD) # License: GPL3, 2021. #+PROPERTY: header-args :tangle yes @@ -176,7 +176,7 @@ #+end_src -*** perform the tests on files [5/5] +*** perform the tests on files [5/6] These tests check the addition of a time stamp ahead of the file name. @@ -352,3 +352,44 @@ assert os.path.isfile(new) os.remove(new) #+end_src + + + [ ] Check the retraction of the date/time stamp + + The test is constrained to the five fix formats prepending the file name. + + /Not ready for inclusion into the main branch/ To trace the advancement of + pytest's processing, long delays are set. A manual check of date2time on + the CLI confirms that date2name writes the --withtime format, but is not + successful to retract this stamp. + + #+begin_src python :tangle test_date2name.py + import time, os, sys + @pytest.mark.parametrize("arg1", ["default", "short", "compact", "month", "withtime"]) + @pytest.mark.parametrize("arg2", ["-r", "--remove"]) + def test_remove_stamp(arg1, arg2): + """Check the retraction of the leading time stamp.""" + substitution = {"default" : "2021-09-21", + "short" : "210921", + "compact" : "20210921", + "month" : "2021-09", + "withtime": "2021-09-21T13.59.59"} + prepend = substitution.get(arg1) + + BASIS = "test.txt" + TFILE = "" + TFILE = "_".join([prepend, BASIS]) + with open(TFILE, mode = "w") as newfile: + newfile.write("This is a test file.") + time.sleep(2) + + test = getoutput(f"python3 {PROGRAM} {TFILE} {arg2}") + time.sleep(2) + assert os.path.isfile(TFILE) is False # absence of stamped file + assert os.path.isfile(BASIS) # presence unstamped file + + try: + os.remove("test.txt") + except OSError: + print("Running remove test, file 'test.txt' was not erased.") + sys.exit() + #+end_src From 7504aead864370e35ffd85213281e8379bc429b1 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Tue, 28 Sep 2021 14:26:46 +0000 Subject: [PATCH 02/14] Continue on all tests, even if one fails. The retraction of the -x parameter for pytest ensures that all test functions defined are actually used. Previously, testing stopped immediately, conveying an incomplete picture of date2name's work implemented. Testing the retraction option -r, there is no need for a delay between the indivdual options, either. --- Makefile | 10 ++++++---- test_date2name.py | 16 ++++++---------- test_generator.org | 28 +++++++++++++--------------- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index c197b8f..e6588ff 100755 --- a/Makefile +++ b/Makefile @@ -13,8 +13,10 @@ # chmod +x * # make ./Makefile # -# to run the tests. The test sequence either stops at the first test -# failing, or after completion. +# to run the tests. The test sequence will explicitly report if a test +# was passed successfully, or failed. If you want to script to stop on +# the first encounter of a test failed, add option -x on the commands +# set below -# pytest -xv test_date2name.py # only pytest for Python 3 is present -pytest-3 -xv test_date2name.py # pytest if Python 2 and Python 3 coexist +# pytest -v test_date2name.py # only pytest for Python 3 is present +pytest-3 -v test_date2name.py # pytest if Python 2 and Python 3 coexist diff --git a/test_date2name.py b/test_date2name.py index 859d762..b42bdda 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -4,7 +4,7 @@ # author: nbehrnd@yahoo.com # license: GPL v3, 2021. # date: 2021-08-30 (YYYY-MM-DD) -# edit: 2021-09-17 (YYYY-MM-DD) +# edit: 2021-09-28 (YYYY-MM-DD) # """Test pad for functions by date2name with pytest. @@ -225,8 +225,8 @@ def test_short_pattern_YYMMDD(arg1): assert os.path.isfile(new) os.remove(new) -import time, os, sys -@pytest.mark.parametrize("arg1", ["default", "short", "compact", "month", "withtime"]) +@pytest.mark.parametrize("arg1", ["default", "short", "compact", + "month", "withtime"]) @pytest.mark.parametrize("arg2", ["-r", "--remove"]) def test_remove_stamp(arg1, arg2): """Check the retraction of the leading time stamp.""" @@ -242,15 +242,11 @@ def test_remove_stamp(arg1, arg2): TFILE = "_".join([prepend, BASIS]) with open(TFILE, mode = "w") as newfile: newfile.write("This is a test file.") - time.sleep(2) test = getoutput(f"python3 {PROGRAM} {TFILE} {arg2}") - time.sleep(2) + assert os.path.isfile(TFILE) is False # absence of stamped file assert os.path.isfile(BASIS) # presence unstamped file - try: - os.remove("test.txt") - except OSError: - print("Running remove test, file 'test.txt' was not erased.") - sys.exit() + os.remove("test.txt") # succesful space cleaning for next test + assert os.path.isfile("test.txt") is False diff --git a/test_generator.org b/test_generator.org index d9044c9..aab5920 100755 --- a/test_generator.org +++ b/test_generator.org @@ -72,11 +72,13 @@ # chmod +x * # make ./Makefile # - # to run the tests. The test sequence either stops at the first test - # failing, or after completion. - - # pytest -xv test_date2name.py # only pytest for Python 3 is present - pytest-3 -xv test_date2name.py # pytest if Python 2 and Python 3 coexist + # to run the tests. The test sequence will explicitly report if a test + # was passed successfully, or failed. If you want to script to stop on + # the first encounter of a test failed, add option -x on the commands + # set below + + # pytest -v test_date2name.py # only pytest for Python 3 is present + pytest-3 -v test_date2name.py # pytest if Python 2 and Python 3 coexist #+end_src @@ -90,7 +92,7 @@ # author: nbehrnd@yahoo.com # license: GPL v3, 2021. # date: 2021-08-30 (YYYY-MM-DD) - # edit: 2021-09-17 (YYYY-MM-DD) + # edit: 2021-09-28 (YYYY-MM-DD) # """Test pad for functions by date2name with pytest. @@ -363,8 +365,8 @@ successful to retract this stamp. #+begin_src python :tangle test_date2name.py - import time, os, sys - @pytest.mark.parametrize("arg1", ["default", "short", "compact", "month", "withtime"]) + @pytest.mark.parametrize("arg1", ["default", "short", "compact", + "month", "withtime"]) @pytest.mark.parametrize("arg2", ["-r", "--remove"]) def test_remove_stamp(arg1, arg2): """Check the retraction of the leading time stamp.""" @@ -380,16 +382,12 @@ TFILE = "_".join([prepend, BASIS]) with open(TFILE, mode = "w") as newfile: newfile.write("This is a test file.") - time.sleep(2) test = getoutput(f"python3 {PROGRAM} {TFILE} {arg2}") - time.sleep(2) + assert os.path.isfile(TFILE) is False # absence of stamped file assert os.path.isfile(BASIS) # presence unstamped file - try: - os.remove("test.txt") - except OSError: - print("Running remove test, file 'test.txt' was not erased.") - sys.exit() + os.remove("test.txt") # succesful space cleaning for next test + assert os.path.isfile("test.txt") is False #+end_src From 20fbfe25043ff08e922c29c5a5af8c91b6e77f7b Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Tue, 28 Sep 2021 14:41:00 +0000 Subject: [PATCH 03/14] Change names to query creation/modification time. Down the road, the application's action on folders will be checked as well. Thus, it is appropriate to alter the functions' names to querry the time of creation/modifications into a more flexible, yet correct form. --- test_date2name.py | 36 ++++++++++++++++++------------------ test_generator.org | 44 ++++++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/test_date2name.py b/test_date2name.py index b42bdda..fc4568e 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -43,16 +43,16 @@ def prepare_testfile(): os.utime(TFILE, (result.st_atime, result.st_mtime + 10.0)) -def query_file_creation(): - """Determine the time of creation of the file.""" - created = os.stat(TFILE).st_ctime +def query_creation_time(name=TFILE): + """Determine the time of creation of the file/folder.""" + created = os.stat(name).st_ctime created = str(datetime.fromtimestamp(created)) return created -def query_file_modification(): - """Determine the time when the file was modified.""" - modified = os.stat(TFILE).st_mtime +def query_modification_time(name=TFILE): + """Determine the time when the file/folder was modified.""" + modified = os.stat(name).st_mtime modified = str(datetime.fromtimestamp(modified)) return modified @@ -84,10 +84,10 @@ def test_default_pattern_YYYY_MM_DD(arg1): new = str("") if arg1 in [" ", "-f", "--files", "-m", "--mtime"]: - day = query_file_modification().split()[0] + day = query_modification_time().split()[0] elif arg1 in ["-c", "--ctime"]: - day = query_file_creation().split()[0] + day = query_creation_time().split()[0] new = "_".join([day, TFILE]) test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") @@ -112,11 +112,11 @@ def test_compact_pattern_YYYYMMDD(arg1): "-C --files", "--compact --files", "-C -m", "--compact -m", "-C --mtime", "--compact --mtime"]: - day = query_file_modification().split()[0] + day = query_modification_time().split()[0] elif arg1 in ["-C -c", "--compact -c", "-C --ctime", "--compact --ctime"]: - day = query_file_creation().split()[0] + day = query_creation_time().split()[0] # drop the hyphens in the datestamp: day = day.replace("-", "") @@ -144,11 +144,11 @@ def test_compact_month_YYYY_MM(arg1): "-M --files", "--month --files", "-M -m", "--month -m", "-M --mtime", "--month --mtime"]: - day = query_file_modification().split()[0] + day = query_modification_time().split()[0] elif arg1 in ["-M -c", "--month -c", "-M --ctime", "--month --ctime"]: - day = query_file_creation().split()[0] + day = query_creation_time().split()[0] # trim off the last three characters in the datestamp: day = day[:-3] @@ -174,13 +174,13 @@ def test_default_pattern_YYYY_MM_DDThh_mm_ss(arg1): "--withtime -f", "--withtime --files", "-w -m", "-w --mtime", "--withtime -m", "--withtime --mtime"]: - day = query_file_modification().split()[0] - second = query_file_modification().split()[1] + day = query_modification_time().split()[0] + second = query_modification_time().split()[1] elif arg1 in ["-w -c", "-w --ctime", "--withtime -c", "--withtime --ctime"]: - day = query_file_creation().split()[0] - second = query_file_creation().split()[1] + day = query_creation_time().split()[0] + second = query_creation_time().split()[1] second = second.split(".")[0] # use integer seconds only second = second.replace(":", ".") # adjust representation @@ -209,11 +209,11 @@ def test_short_pattern_YYMMDD(arg1): "-S --files", "--short --files", "-S -m", "--short -m", "-S --mtime", "--short --mtime"]: - day = query_file_modification().split()[0] + day = query_modification_time().split()[0] elif arg1 in ["-S -c", "--short -c", "-S --ctime", "--short --ctime"]: - day = query_file_creation().split()[0] + day = query_creation_time().split()[0] # drop the hyphens in the datestamp: day = day.replace("-", "") diff --git a/test_generator.org b/test_generator.org index aab5920..7587c1f 100755 --- a/test_generator.org +++ b/test_generator.org @@ -136,18 +136,18 @@ # https://stackoverflow.com/questions/53111614/how-to-modify-the-file-modification-date-with-python-on-mac result = os.stat(TFILE) os.utime(TFILE, (result.st_atime, result.st_mtime + 10.0)) - - - def query_file_creation(): - """Determine the time of creation of the file.""" - created = os.stat(TFILE).st_ctime + + + def query_creation_time(name=TFILE): + """Determine the time of creation of the file/folder.""" + created = os.stat(name).st_ctime created = str(datetime.fromtimestamp(created)) return created - - - def query_file_modification(): - """Determine the time when the file was modified.""" - modified = os.stat(TFILE).st_mtime + + + def query_modification_time(name=TFILE): + """Determine the time when the file/folder was modified.""" + modified = os.stat(name).st_mtime modified = str(datetime.fromtimestamp(modified)) return modified #+end_src @@ -194,10 +194,10 @@ new = str("") if arg1 in [" ", "-f", "--files", "-m", "--mtime"]: - day = query_file_modification().split()[0] + day = query_modification_time().split()[0] elif arg1 in ["-c", "--ctime"]: - day = query_file_creation().split()[0] + day = query_creation_time().split()[0] new = "_".join([day, TFILE]) test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") @@ -227,11 +227,11 @@ "-C --files", "--compact --files", "-C -m", "--compact -m", "-C --mtime", "--compact --mtime"]: - day = query_file_modification().split()[0] + day = query_modification_time().split()[0] elif arg1 in ["-C -c", "--compact -c", "-C --ctime", "--compact --ctime"]: - day = query_file_creation().split()[0] + day = query_creation_time().split()[0] # drop the hyphens in the datestamp: day = day.replace("-", "") @@ -264,11 +264,11 @@ "-M --files", "--month --files", "-M -m", "--month -m", "-M --mtime", "--month --mtime"]: - day = query_file_modification().split()[0] + day = query_modification_time().split()[0] elif arg1 in ["-M -c", "--month -c", "-M --ctime", "--month --ctime"]: - day = query_file_creation().split()[0] + day = query_creation_time().split()[0] # trim off the last three characters in the datestamp: day = day[:-3] @@ -298,13 +298,13 @@ "--withtime -f", "--withtime --files", "-w -m", "-w --mtime", "--withtime -m", "--withtime --mtime"]: - day = query_file_modification().split()[0] - second = query_file_modification().split()[1] + day = query_modification_time().split()[0] + second = query_modification_time().split()[1] elif arg1 in ["-w -c", "-w --ctime", "--withtime -c", "--withtime --ctime"]: - day = query_file_creation().split()[0] - second = query_file_creation().split()[1] + day = query_creation_time().split()[0] + second = query_creation_time().split()[1] second = second.split(".")[0] # use integer seconds only second = second.replace(":", ".") # adjust representation @@ -338,11 +338,11 @@ "-S --files", "--short --files", "-S -m", "--short -m", "-S --mtime", "--short --mtime"]: - day = query_file_modification().split()[0] + day = query_modification_time().split()[0] elif arg1 in ["-S -c", "--short -c", "-S --ctime", "--short --ctime"]: - day = query_file_creation().split()[0] + day = query_creation_time().split()[0] # drop the hyphens in the datestamp: day = day.replace("-", "") From d1f5672db0cacd99a8e08a5a65f389c8daced650 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Tue, 28 Sep 2021 14:51:14 +0000 Subject: [PATCH 04/14] Adjust present test functions to file. The present test functions act on files only. Early tests to stack folders on top of the present functions do not work reliably yet. Thus, better to name the functions (uniformly) about what they test which are actions on files only. This change equally prevents some line breaks on the CLI, too. --- test_date2name.py | 12 ++++++------ test_generator.org | 44 ++++++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/test_date2name.py b/test_date2name.py index fc4568e..b9125ae 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -77,7 +77,7 @@ def test_script_version(): @pytest.mark.parametrize("arg1", [" ", "-f", "--files", "-m", "--mtime", "-c", "--ctime"]) -def test_default_pattern_YYYY_MM_DD(arg1): +def test_file_pattern_default(arg1): """Prepend 'YYYY-MM-DD_' to the file name.""" prepare_testfile() day = str("") @@ -101,7 +101,7 @@ def test_default_pattern_YYYY_MM_DD(arg1): "-C --mtime", "--compact --mtime", "-C -c", "--compact -c", "-C --ctime", "--compact --ctime"]) -def test_compact_pattern_YYYYMMDD(arg1): +def test_file_pattern_compact(arg1): """Prepend 'YYYYMMDD_' to the file name.""" prepare_testfile() day = str("") @@ -133,7 +133,7 @@ def test_compact_pattern_YYYYMMDD(arg1): "-M --mtime", "--month --mtime", "-M -c", "--month -c", "-M --ctime", "--month --ctime"]) -def test_compact_month_YYYY_MM(arg1): +def test_file_pattern_month(arg1): """Prepend 'YYYY-MM_' to the file name.""" prepare_testfile() day = str("") @@ -164,7 +164,7 @@ def test_compact_month_YYYY_MM(arg1): "--withtime -m", "--withtime --mtime", "-w -c", "-w --ctime", "--withtime -c", "--withtime --ctime"]) -def test_default_pattern_YYYY_MM_DDThh_mm_ss(arg1): +def test_file_pattern_withtime(arg1): """Prepend 'YYYY-MM-DDThh.mm.ss_' to the file name.""" prepare_testfile() day = str("") @@ -198,7 +198,7 @@ def test_default_pattern_YYYY_MM_DDThh_mm_ss(arg1): "-S --mtime", "--short --mtime", "-S -c", "--short -c", "-S --ctime", "--short --ctime"]) -def test_short_pattern_YYMMDD(arg1): +def test_file_pattern_short(arg1): """Prepend 'YYMMDD_' to the file name.""" prepare_testfile() day = str("") @@ -228,7 +228,7 @@ def test_short_pattern_YYMMDD(arg1): @pytest.mark.parametrize("arg1", ["default", "short", "compact", "month", "withtime"]) @pytest.mark.parametrize("arg2", ["-r", "--remove"]) -def test_remove_stamp(arg1, arg2): +def test_file_remove_stamp(arg1, arg2): """Check the retraction of the leading time stamp.""" substitution = {"default" : "2021-09-21", "short" : "210921", diff --git a/test_generator.org b/test_generator.org index 7587c1f..c94b181 100755 --- a/test_generator.org +++ b/test_generator.org @@ -187,18 +187,18 @@ @pytest.mark.parametrize("arg1", [" ", "-f", "--files", "-m", "--mtime", "-c", "--ctime"]) - def test_default_pattern_YYYY_MM_DD(arg1): + def test_file_pattern_default(arg1): """Prepend 'YYYY-MM-DD_' to the file name.""" prepare_testfile() day = str("") new = str("") - + if arg1 in [" ", "-f", "--files", "-m", "--mtime"]: day = query_modification_time().split()[0] - + elif arg1 in ["-c", "--ctime"]: day = query_creation_time().split()[0] - + new = "_".join([day, TFILE]) test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") assert os.path.isfile(new) @@ -216,26 +216,26 @@ "-C --mtime", "--compact --mtime", "-C -c", "--compact -c", "-C --ctime", "--compact --ctime"]) - def test_compact_pattern_YYYYMMDD(arg1): + def test_file_pattern_compact(arg1): """Prepend 'YYYYMMDD_' to the file name.""" prepare_testfile() day = str("") new = str("") - + if arg1 in ["-C", "--compact", "-C -f", "--compact -f", "-C --files", "--compact --files", "-C -m", "--compact -m", "-C --mtime", "--compact --mtime"]: day = query_modification_time().split()[0] - + elif arg1 in ["-C -c", "--compact -c", "-C --ctime", "--compact --ctime"]: day = query_creation_time().split()[0] - + # drop the hyphens in the datestamp: day = day.replace("-", "") - + new = "_".join([day, TFILE]) test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") assert os.path.isfile(new) @@ -253,26 +253,26 @@ "-M --mtime", "--month --mtime", "-M -c", "--month -c", "-M --ctime", "--month --ctime"]) - def test_compact_month_YYYY_MM(arg1): + def test_file_pattern_month(arg1): """Prepend 'YYYY-MM_' to the file name.""" prepare_testfile() day = str("") new = str("") - + if arg1 in ["-M", "--month", "-M -f", "--month -f", "-M --files", "--month --files", "-M -m", "--month -m", "-M --mtime", "--month --mtime"]: day = query_modification_time().split()[0] - + elif arg1 in ["-M -c", "--month -c", "-M --ctime", "--month --ctime"]: day = query_creation_time().split()[0] - + # trim off the last three characters in the datestamp: day = day[:-3] - + new = "_".join([day, TFILE]) test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") assert os.path.isfile(new) @@ -288,29 +288,29 @@ "--withtime -m", "--withtime --mtime", "-w -c", "-w --ctime", "--withtime -c", "--withtime --ctime"]) - def test_default_pattern_YYYY_MM_DDThh_mm_ss(arg1): + def test_file_pattern_withtime(arg1): """Prepend 'YYYY-MM-DDThh.mm.ss_' to the file name.""" prepare_testfile() day = str("") new = str("") - + if arg1 in ["-w -f", "-w --files", "--withtime -f", "--withtime --files", "-w -m", "-w --mtime", "--withtime -m", "--withtime --mtime"]: day = query_modification_time().split()[0] second = query_modification_time().split()[1] - + elif arg1 in ["-w -c", "-w --ctime", "--withtime -c", "--withtime --ctime"]: day = query_creation_time().split()[0] second = query_creation_time().split()[1] - + second = second.split(".")[0] # use integer seconds only second = second.replace(":", ".") # adjust representation - + new = "".join([day, "T", second, "_", TFILE]) - + test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") assert os.path.isfile(new) os.remove(new) @@ -327,7 +327,7 @@ "-S --mtime", "--short --mtime", "-S -c", "--short -c", "-S --ctime", "--short --ctime"]) - def test_short_pattern_YYMMDD(arg1): + def test_file_pattern_short(arg1): """Prepend 'YYMMDD_' to the file name.""" prepare_testfile() day = str("") @@ -368,7 +368,7 @@ @pytest.mark.parametrize("arg1", ["default", "short", "compact", "month", "withtime"]) @pytest.mark.parametrize("arg2", ["-r", "--remove"]) - def test_remove_stamp(arg1, arg2): + def test_file_remove_stamp(arg1, arg2): """Check the retraction of the leading time stamp.""" substitution = {"default" : "2021-09-21", "short" : "210921", From ba14f65f34de34177b1c6160f8d8adacf1733a0c Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Tue, 28 Sep 2021 17:13:41 +0000 Subject: [PATCH 05/14] Initialize folder testing (elementary tests). First tests on generation/remove of folders before date2name's action on folders. --- test_date2name.py | 32 +++++++++++++++++++++++++------- test_generator.org | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/test_date2name.py b/test_date2name.py index b9125ae..ffd6732 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -32,15 +32,23 @@ import pytest PROGRAM = str("./date2name/__init__.py") TFILE = str("test_file.txt") # the intermediate test file written +TFOLDER = str("test_folder") # for complementary check on folders -def prepare_testfile(): +def prepare_testfile(name=TFILE): """The creation of the test file.""" - with open (TFILE, mode="w") as newfile: + with open (name, mode="w") as newfile: newfile.write("This is the test file for test_date2name.py.") # adjust modification time stamp, based on # https://stackoverflow.com/questions/53111614/how-to-modify-the-file-modification-date-with-python-on-mac - result = os.stat(TFILE) - os.utime(TFILE, (result.st_atime, result.st_mtime + 10.0)) + result = os.stat(name) + os.utime(name, (result.st_atime, result.st_mtime + 10.0)) + + +def prepare_testfolder(name=TFOLDER): + """Create a testfolder.""" + os.mkdir(name) + result = os.stat(name) + os.utime(name, (result.st_atime, result.st_mtime + 10.0)) def query_creation_time(name=TFILE): @@ -56,10 +64,20 @@ def query_modification_time(name=TFILE): modified = str(datetime.fromtimestamp(modified)) return modified -def test_create_remove_testfile(): +def test_create_remove_testfile(name=TFILE): """Merely check if the test file may be written and removed.""" - prepare_testfile() - os.remove(TFILE) + prepare_testfile(name=TFILE) + assert os.path.isfile(name) + os.remove(name) + assert os.path.isfile(name) is False + + +def test_create_remove_testfolder(name=TFOLDER): + """Probe the generation/removal of a test folder.""" + prepare_testfolder(name=TFOLDER) + assert os.path.isdir(name) + os.rmdir(name) + assert os.path.isdir(name) is False def test_script_existence(): diff --git a/test_generator.org b/test_generator.org index c94b181..07b8113 100755 --- a/test_generator.org +++ b/test_generator.org @@ -120,6 +120,7 @@ PROGRAM = str("./date2name/__init__.py") TFILE = str("test_file.txt") # the intermediate test file written + TFOLDER = str("test_folder") # for complementary check on folders #+end_src @@ -128,16 +129,23 @@ Define actions which are going to be used multiple times. #+begin_src python :tangle test_date2name.py - def prepare_testfile(): + def prepare_testfile(name=TFILE): """The creation of the test file.""" - with open (TFILE, mode="w") as newfile: + with open (name, mode="w") as newfile: newfile.write("This is the test file for test_date2name.py.") # adjust modification time stamp, based on # https://stackoverflow.com/questions/53111614/how-to-modify-the-file-modification-date-with-python-on-mac - result = os.stat(TFILE) - os.utime(TFILE, (result.st_atime, result.st_mtime + 10.0)) + result = os.stat(name) + os.utime(name, (result.st_atime, result.st_mtime + 10.0)) + def prepare_testfolder(name=TFOLDER): + """Create a testfolder.""" + os.mkdir(name) + result = os.stat(name) + os.utime(name, (result.st_atime, result.st_mtime + 10.0)) + + def query_creation_time(name=TFILE): """Determine the time of creation of the file/folder.""" created = os.stat(name).st_ctime @@ -158,20 +166,30 @@ These tests do not modify a file, nor folder by =date2time=. #+begin_src python :tangle test_date2name.py - def test_create_remove_testfile(): + def test_create_remove_testfile(name=TFILE): """Merely check if the test file may be written and removed.""" - prepare_testfile() - os.remove(TFILE) - - + prepare_testfile(name=TFILE) + assert os.path.isfile(name) + os.remove(name) + assert os.path.isfile(name) is False + + + def test_create_remove_testfolder(name=TFOLDER): + """Probe the generation/removal of a test folder.""" + prepare_testfolder(name=TFOLDER) + assert os.path.isdir(name) + os.rmdir(name) + assert os.path.isdir(name) is False + + def test_script_existence(): """Merely check for the script's presence.""" assert os.path.isfile(PROGRAM) - - + + def test_script_version(): """Check for the correct output of the version. - + CLI equivalence: date2name --version """ out = getoutput(f"python3 {PROGRAM} --version") assert out.strip() == "__init__.py 2018-05-09" From 55cd6445ef0b201ba3f2d6631c1f9dbc870a0152 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Tue, 28 Sep 2021 17:50:39 +0000 Subject: [PATCH 06/14] Group pytest's tests (elementary, files). The introduction of a pytest.ini appears as a convienient approach to group tests of pytests sharing an element in common. This seems beneficial as long as there is no reliable approch identified to stack "file" and "folder" as levels of an additional parameter in theses tests for date2name. By now, the two layers "elementary" and "files" are installed. --- pytest.ini | 5 +++++ test_date2name.py | 10 ++++++++++ test_generator.org | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 pytest.ini diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..4a300ce --- /dev/null +++ b/pytest.ini @@ -0,0 +1,5 @@ +[pytest] +markers = + elementary: elementary tests ahead of action on files/folders by date2name + files: tests about affect by date2name on files + folders: tests about affect by date2name on folders diff --git a/test_date2name.py b/test_date2name.py index ffd6732..c6bd8a5 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -64,6 +64,7 @@ def query_modification_time(name=TFILE): modified = str(datetime.fromtimestamp(modified)) return modified +@pytest.mark.elementary def test_create_remove_testfile(name=TFILE): """Merely check if the test file may be written and removed.""" prepare_testfile(name=TFILE) @@ -72,6 +73,7 @@ def test_create_remove_testfile(name=TFILE): assert os.path.isfile(name) is False +@pytest.mark.elementary def test_create_remove_testfolder(name=TFOLDER): """Probe the generation/removal of a test folder.""" prepare_testfolder(name=TFOLDER) @@ -80,11 +82,13 @@ def test_create_remove_testfolder(name=TFOLDER): assert os.path.isdir(name) is False +@pytest.mark.elementary def test_script_existence(): """Merely check for the script's presence.""" assert os.path.isfile(PROGRAM) +@pytest.mark.elementary def test_script_version(): """Check for the correct output of the version. @@ -92,6 +96,7 @@ def test_script_version(): out = getoutput(f"python3 {PROGRAM} --version") assert out.strip() == "__init__.py 2018-05-09" +@pytest.mark.files @pytest.mark.parametrize("arg1", [" ", "-f", "--files", "-m", "--mtime", "-c", "--ctime"]) @@ -112,6 +117,7 @@ def test_file_pattern_default(arg1): assert os.path.isfile(new) os.remove(new) +@pytest.mark.files @pytest.mark.parametrize("arg1", ["-C", "--compact", "-C -f", "--compact -f", "-C --files", "--compact --files", @@ -144,6 +150,7 @@ def test_file_pattern_compact(arg1): assert os.path.isfile(new) os.remove(new) +@pytest.mark.files @pytest.mark.parametrize("arg1", ["-M", "--month", "-M -f", "--month -f", "-M --files", "--month --files", @@ -176,6 +183,7 @@ def test_file_pattern_month(arg1): assert os.path.isfile(new) os.remove(new) +@pytest.mark.files @pytest.mark.parametrize("arg1", ["-w -f", "-w --files", "--withtime -f", "--withtime --files", "-w -m", "-w --mtime", @@ -209,6 +217,7 @@ def test_file_pattern_withtime(arg1): assert os.path.isfile(new) os.remove(new) +@pytest.mark.files @pytest.mark.parametrize("arg1", ["-S", "--short", "-S -f", "--short -f", "-S --files", "--short --files", @@ -243,6 +252,7 @@ def test_file_pattern_short(arg1): assert os.path.isfile(new) os.remove(new) +@pytest.mark.files @pytest.mark.parametrize("arg1", ["default", "short", "compact", "month", "withtime"]) @pytest.mark.parametrize("arg2", ["-r", "--remove"]) diff --git a/test_generator.org b/test_generator.org index 07b8113..3f709f8 100755 --- a/test_generator.org +++ b/test_generator.org @@ -81,6 +81,31 @@ pytest-3 -v test_date2name.py # pytest if Python 2 and Python 3 coexist #+end_src +** Building a pytest.ini + + This file defines markers which groups the tests into groups. Subsequently, + tests with pytest may focus on them rather than performing all tests (which + is set up as the default). In presence of =pytest.ini=, the typical call + then is + #+begin_src bash :tangle no + pytest-3 test_date2name.py -v -m "elementary" + #+end_src + to constrain the tester's action to all tests labeled as "elementary". At + present, tests are grouped as + + elementary; ahead of checking date2name's action on files or folders + + files; checking date2name's action on files, and + + folders; checking date2name's action on folders. + This became necessary since a reliable approach to stack the levels "files" + and "folders" in this testing suite was not yet identified. + + + #+begin_src python :tangle pytest.ini + [pytest] + markers = + elementary: elementary tests ahead of action on files/folders by date2name + files: tests about affect by date2name on files + folders: tests about affect by date2name on folders + #+end_src ** Building the test script @@ -166,6 +191,7 @@ These tests do not modify a file, nor folder by =date2time=. #+begin_src python :tangle test_date2name.py + @pytest.mark.elementary def test_create_remove_testfile(name=TFILE): """Merely check if the test file may be written and removed.""" prepare_testfile(name=TFILE) @@ -173,7 +199,8 @@ os.remove(name) assert os.path.isfile(name) is False - + + @pytest.mark.elementary def test_create_remove_testfolder(name=TFOLDER): """Probe the generation/removal of a test folder.""" prepare_testfolder(name=TFOLDER) @@ -181,12 +208,14 @@ os.rmdir(name) assert os.path.isdir(name) is False - + + @pytest.mark.elementary def test_script_existence(): """Merely check for the script's presence.""" assert os.path.isfile(PROGRAM) + @pytest.mark.elementary def test_script_version(): """Check for the correct output of the version. @@ -202,6 +231,7 @@ + [X] default pattern, i.e. prepend YYYY-MM-DD_ to file test.txt #+begin_src python :tangle test_date2name.py + @pytest.mark.files @pytest.mark.parametrize("arg1", [" ", "-f", "--files", "-m", "--mtime", "-c", "--ctime"]) @@ -227,6 +257,7 @@ This may re-use much of the instructions used for the default pattern and only needs to drop the hyphens. #+begin_src python :tangle test_date2name.py + @pytest.mark.files @pytest.mark.parametrize("arg1", ["-C", "--compact", "-C -f", "--compact -f", "-C --files", "--compact --files", @@ -264,6 +295,7 @@ Departing from the standard format YYYY-MM-DD, it suffices to trim off the last three characters. #+begin_src python :tangle test_date2name.py + @pytest.mark.files @pytest.mark.parametrize("arg1", ["-M", "--month", "-M -f", "--month -f", "-M --files", "--month --files", @@ -300,6 +332,7 @@ + [X] To prepend date and time to file test.txt in a pattern of YYYY-MM-DDThh.mm.ss, the default pattern YYYY-MM-DD is extended. #+begin_src python :tangle test_date2name.py + @pytest.mark.files @pytest.mark.parametrize("arg1", ["-w -f", "-w --files", "--withtime -f", "--withtime --files", "-w -m", "-w --mtime", @@ -338,6 +371,7 @@ Related to the basic pattern, except truncating of the first two characters. #+begin_src python :tangle test_date2name.py + @pytest.mark.files @pytest.mark.parametrize("arg1", ["-S", "--short", "-S -f", "--short -f", "-S --files", "--short --files", @@ -383,6 +417,7 @@ successful to retract this stamp. #+begin_src python :tangle test_date2name.py + @pytest.mark.files @pytest.mark.parametrize("arg1", ["default", "short", "compact", "month", "withtime"]) @pytest.mark.parametrize("arg2", ["-r", "--remove"]) From 21935c7aac00bf485da6c8e83b41ed12e66e65c8 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Wed, 29 Sep 2021 07:33:47 +0000 Subject: [PATCH 07/14] Introduce second layer of pytest marks. Many of pytest's test functions iterate over levels. For local testing of date2name's action, e.g. about --remove, it may be useful to narrow testing further than only "files" (and later, to "folders"). Thus, a second layer of keyword markers was introduced which may be used in two forms: + in combination with either one of (files, folders) for a check which would check e.g., the remove of stamps on files; or + independent og (files, folders) to check e.g., the remove of stamps on files and on folders --- pytest.ini | 7 +++++++ test_date2name.py | 8 +++++++- test_generator.org | 26 ++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/pytest.ini b/pytest.ini index 4a300ce..0fd288c 100644 --- a/pytest.ini +++ b/pytest.ini @@ -3,3 +3,10 @@ markers = elementary: elementary tests ahead of action on files/folders by date2name files: tests about affect by date2name on files folders: tests about affect by date2name on folders + + default: stamp pattern default + compact: stamp pattern compact + month: stamp pattern month + withtime: stamp pattern withtime + short: stamp pattern short + remove: stamp retraction diff --git a/test_date2name.py b/test_date2name.py index c6bd8a5..39db9b8 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -4,7 +4,7 @@ # author: nbehrnd@yahoo.com # license: GPL v3, 2021. # date: 2021-08-30 (YYYY-MM-DD) -# edit: 2021-09-28 (YYYY-MM-DD) +# edit: 2021-09-29 (YYYY-MM-DD) # """Test pad for functions by date2name with pytest. @@ -97,6 +97,7 @@ def test_script_version(): assert out.strip() == "__init__.py 2018-05-09" @pytest.mark.files +@pytest.mark.default @pytest.mark.parametrize("arg1", [" ", "-f", "--files", "-m", "--mtime", "-c", "--ctime"]) @@ -118,6 +119,7 @@ def test_file_pattern_default(arg1): os.remove(new) @pytest.mark.files +@pytest.mark.compact @pytest.mark.parametrize("arg1", ["-C", "--compact", "-C -f", "--compact -f", "-C --files", "--compact --files", @@ -151,6 +153,7 @@ def test_file_pattern_compact(arg1): os.remove(new) @pytest.mark.files +@pytest.mark.month @pytest.mark.parametrize("arg1", ["-M", "--month", "-M -f", "--month -f", "-M --files", "--month --files", @@ -184,6 +187,7 @@ def test_file_pattern_month(arg1): os.remove(new) @pytest.mark.files +@pytest.mark.withtime @pytest.mark.parametrize("arg1", ["-w -f", "-w --files", "--withtime -f", "--withtime --files", "-w -m", "-w --mtime", @@ -218,6 +222,7 @@ def test_file_pattern_withtime(arg1): os.remove(new) @pytest.mark.files +@pytest.mark.short @pytest.mark.parametrize("arg1", ["-S", "--short", "-S -f", "--short -f", "-S --files", "--short --files", @@ -253,6 +258,7 @@ def test_file_pattern_short(arg1): os.remove(new) @pytest.mark.files +@pytest.mark.remove @pytest.mark.parametrize("arg1", ["default", "short", "compact", "month", "withtime"]) @pytest.mark.parametrize("arg2", ["-r", "--remove"]) diff --git a/test_generator.org b/test_generator.org index 3f709f8..9823b04 100755 --- a/test_generator.org +++ b/test_generator.org @@ -1,6 +1,6 @@ #+NAME: test_generator.org #+AUTHOR: nbehrnd@yahoo.com -#+DATE: 2021-09-22 (YYYY-MM-DD) +#+DATE: 2021-09-29 (YYYY-MM-DD) # License: GPL3, 2021. #+PROPERTY: header-args :tangle yes @@ -95,6 +95,15 @@ + elementary; ahead of checking date2name's action on files or folders + files; checking date2name's action on files, and + folders; checking date2name's action on folders. + in a first layer. Orthogonal to this, the five fixed pattern (keyword + =default=, =compact=, =month=, =withtime=, or =short=) and the stamps' + retraction (keyword =remove=) may be used as mutually exclusive levels, + either alone, or in combination with the keyword =files= or =folders=, e.g. + + #+begin_src bash :tangle no + pytest-3 test_date2name.py -m "files and default" -v + #+end_src + This became necessary since a reliable approach to stack the levels "files" and "folders" in this testing suite was not yet identified. @@ -105,6 +114,13 @@ elementary: elementary tests ahead of action on files/folders by date2name files: tests about affect by date2name on files folders: tests about affect by date2name on folders + + default: stamp pattern default + compact: stamp pattern compact + month: stamp pattern month + withtime: stamp pattern withtime + short: stamp pattern short + remove: stamp retraction #+end_src ** Building the test script @@ -117,7 +133,7 @@ # author: nbehrnd@yahoo.com # license: GPL v3, 2021. # date: 2021-08-30 (YYYY-MM-DD) - # edit: 2021-09-28 (YYYY-MM-DD) + # edit: 2021-09-29 (YYYY-MM-DD) # """Test pad for functions by date2name with pytest. @@ -232,6 +248,7 @@ + [X] default pattern, i.e. prepend YYYY-MM-DD_ to file test.txt #+begin_src python :tangle test_date2name.py @pytest.mark.files + @pytest.mark.default @pytest.mark.parametrize("arg1", [" ", "-f", "--files", "-m", "--mtime", "-c", "--ctime"]) @@ -258,6 +275,7 @@ and only needs to drop the hyphens. #+begin_src python :tangle test_date2name.py @pytest.mark.files + @pytest.mark.compact @pytest.mark.parametrize("arg1", ["-C", "--compact", "-C -f", "--compact -f", "-C --files", "--compact --files", @@ -296,6 +314,7 @@ off the last three characters. #+begin_src python :tangle test_date2name.py @pytest.mark.files + @pytest.mark.month @pytest.mark.parametrize("arg1", ["-M", "--month", "-M -f", "--month -f", "-M --files", "--month --files", @@ -333,6 +352,7 @@ YYYY-MM-DDThh.mm.ss, the default pattern YYYY-MM-DD is extended. #+begin_src python :tangle test_date2name.py @pytest.mark.files + @pytest.mark.withtime @pytest.mark.parametrize("arg1", ["-w -f", "-w --files", "--withtime -f", "--withtime --files", "-w -m", "-w --mtime", @@ -372,6 +392,7 @@ characters. #+begin_src python :tangle test_date2name.py @pytest.mark.files + @pytest.mark.short @pytest.mark.parametrize("arg1", ["-S", "--short", "-S -f", "--short -f", "-S --files", "--short --files", @@ -418,6 +439,7 @@ #+begin_src python :tangle test_date2name.py @pytest.mark.files + @pytest.mark.remove @pytest.mark.parametrize("arg1", ["default", "short", "compact", "month", "withtime"]) @pytest.mark.parametrize("arg2", ["-r", "--remove"]) From bda8757f015171e22a6cbbb500a52fa01c614524 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Wed, 29 Sep 2021 08:40:52 +0000 Subject: [PATCH 08/14] Sort sequence of entries present. Because the pattern for testing on files will be used for the on folders, a reorganization of the instructions. Stamps checked so far are generated either in default pattern, or (compact, month, short) pattern, or with time. Thus, it appears reasonable to harmonize this sequence across the relevant files. --- date2name/__init__.py | 6 +- pytest.ini | 10 ++-- test_date2name.py | 70 ++++++++++++------------ test_generator.org | 124 +++++++++++++++++++++--------------------- 4 files changed, 105 insertions(+), 105 deletions(-) diff --git a/date2name/__init__.py b/date2name/__init__.py index 978cbfe..d404a11 100755 --- a/date2name/__init__.py +++ b/date2name/__init__.py @@ -71,15 +71,15 @@ parser.add_option("-d", "--directories", dest="onlydirectories", parser.add_option("-f", "--files", dest="onlyfiles", action="store_true", help="modify only file names") -parser.add_option("-S", "--short", dest="short", - action="store_true", - help="use short datestamp (YYMMDD)") parser.add_option("-C", "--compact", dest="compact", action="store_true", help="use compact datestamp (YYYYMMDD)") parser.add_option("-M", "--month", dest="month", action="store_true", help="use datestamp with year and month (YYYY-MM)") +parser.add_option("-S", "--short", dest="short", + action="store_true", + help="use short datestamp (YYMMDD)") parser.add_option("-w", "--withtime", dest="withtime", action="store_true", help="use datestamp including seconds (YYYY-MM-DDThh.mm.ss)") diff --git a/pytest.ini b/pytest.ini index 0fd288c..0f7d5b7 100644 --- a/pytest.ini +++ b/pytest.ini @@ -4,9 +4,9 @@ markers = files: tests about affect by date2name on files folders: tests about affect by date2name on folders - default: stamp pattern default - compact: stamp pattern compact - month: stamp pattern month - withtime: stamp pattern withtime - short: stamp pattern short + default: stamp pattern default, YYYY-MM-DD + compact: stamp pattern compact, YYYYMMDD + month: stamp pattern month, YYYY-MM + short: stamp pattern short, YYMMDD + withtime: stamp pattern withtime, YYYY-MM-DDThh.mm.ss remove: stamp retraction diff --git a/test_date2name.py b/test_date2name.py index 39db9b8..725fa54 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -186,41 +186,6 @@ def test_file_pattern_month(arg1): assert os.path.isfile(new) os.remove(new) -@pytest.mark.files -@pytest.mark.withtime -@pytest.mark.parametrize("arg1", ["-w -f", "-w --files", - "--withtime -f", "--withtime --files", - "-w -m", "-w --mtime", - "--withtime -m", "--withtime --mtime", - "-w -c", "-w --ctime", - "--withtime -c", "--withtime --ctime"]) -def test_file_pattern_withtime(arg1): - """Prepend 'YYYY-MM-DDThh.mm.ss_' to the file name.""" - prepare_testfile() - day = str("") - new = str("") - - if arg1 in ["-w -f", "-w --files", - "--withtime -f", "--withtime --files", - "-w -m", "-w --mtime", - "--withtime -m", "--withtime --mtime"]: - day = query_modification_time().split()[0] - second = query_modification_time().split()[1] - - elif arg1 in ["-w -c", "-w --ctime", - "--withtime -c", "--withtime --ctime"]: - day = query_creation_time().split()[0] - second = query_creation_time().split()[1] - - second = second.split(".")[0] # use integer seconds only - second = second.replace(":", ".") # adjust representation - - new = "".join([day, "T", second, "_", TFILE]) - - test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") - assert os.path.isfile(new) - os.remove(new) - @pytest.mark.files @pytest.mark.short @pytest.mark.parametrize("arg1", ["-S", "--short", @@ -257,6 +222,41 @@ def test_file_pattern_short(arg1): assert os.path.isfile(new) os.remove(new) +@pytest.mark.files +@pytest.mark.withtime +@pytest.mark.parametrize("arg1", ["-w -f", "-w --files", + "--withtime -f", "--withtime --files", + "-w -m", "-w --mtime", + "--withtime -m", "--withtime --mtime", + "-w -c", "-w --ctime", + "--withtime -c", "--withtime --ctime"]) +def test_file_pattern_withtime(arg1): + """Prepend 'YYYY-MM-DDThh.mm.ss_' to the file name.""" + prepare_testfile() + day = str("") + new = str("") + + if arg1 in ["-w -f", "-w --files", + "--withtime -f", "--withtime --files", + "-w -m", "-w --mtime", + "--withtime -m", "--withtime --mtime"]: + day = query_modification_time().split()[0] + second = query_modification_time().split()[1] + + elif arg1 in ["-w -c", "-w --ctime", + "--withtime -c", "--withtime --ctime"]: + day = query_creation_time().split()[0] + second = query_creation_time().split()[1] + + second = second.split(".")[0] # use integer seconds only + second = second.replace(":", ".") # adjust representation + + new = "".join([day, "T", second, "_", TFILE]) + + test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") + assert os.path.isfile(new) + os.remove(new) + @pytest.mark.files @pytest.mark.remove @pytest.mark.parametrize("arg1", ["default", "short", "compact", diff --git a/test_generator.org b/test_generator.org index 9823b04..9055d8c 100755 --- a/test_generator.org +++ b/test_generator.org @@ -115,11 +115,11 @@ files: tests about affect by date2name on files folders: tests about affect by date2name on folders - default: stamp pattern default - compact: stamp pattern compact - month: stamp pattern month - withtime: stamp pattern withtime - short: stamp pattern short + default: stamp pattern default, YYYY-MM-DD + compact: stamp pattern compact, YYYYMMDD + month: stamp pattern month, YYYY-MM + short: stamp pattern short, YYMMDD + withtime: stamp pattern withtime, YYYY-MM-DDThh.mm.ss remove: stamp retraction #+end_src @@ -241,7 +241,7 @@ #+end_src -*** perform the tests on files [5/6] +*** perform the tests on files [6/6] These tests check the addition of a time stamp ahead of the file name. @@ -270,9 +270,9 @@ os.remove(new) #+end_src - + [X] prepend the day in the compact format YYYYMMDD_ to file test.txt - This may re-use much of the instructions used for the default pattern - and only needs to drop the hyphens. + + [X] compact pattern, i.e. prepend YYYYMMDD_ to file test.txt. This may + re-use much of the instructions used for the default pattern and only + needs to drop the hyphens. #+begin_src python :tangle test_date2name.py @pytest.mark.files @pytest.mark.compact @@ -309,7 +309,7 @@ os.remove(new) #+end_src - + [X] Prepend year and month in the format YYYY-MM_ to file test.txt. + + [X] month pattern, i.e. prepend YYYY-MM_ to file test.txt. Departing from the standard format YYYY-MM-DD, it suffices to trim off the last three characters. #+begin_src python :tangle test_date2name.py @@ -348,48 +348,9 @@ os.remove(new) #+end_src - + [X] To prepend date and time to file test.txt in a pattern of - YYYY-MM-DDThh.mm.ss, the default pattern YYYY-MM-DD is extended. - #+begin_src python :tangle test_date2name.py - @pytest.mark.files - @pytest.mark.withtime - @pytest.mark.parametrize("arg1", ["-w -f", "-w --files", - "--withtime -f", "--withtime --files", - "-w -m", "-w --mtime", - "--withtime -m", "--withtime --mtime", - "-w -c", "-w --ctime", - "--withtime -c", "--withtime --ctime"]) - def test_file_pattern_withtime(arg1): - """Prepend 'YYYY-MM-DDThh.mm.ss_' to the file name.""" - prepare_testfile() - day = str("") - new = str("") - - if arg1 in ["-w -f", "-w --files", - "--withtime -f", "--withtime --files", - "-w -m", "-w --mtime", - "--withtime -m", "--withtime --mtime"]: - day = query_modification_time().split()[0] - second = query_modification_time().split()[1] - - elif arg1 in ["-w -c", "-w --ctime", - "--withtime -c", "--withtime --ctime"]: - day = query_creation_time().split()[0] - second = query_creation_time().split()[1] - - second = second.split(".")[0] # use integer seconds only - second = second.replace(":", ".") # adjust representation - - new = "".join([day, "T", second, "_", TFILE]) - - test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") - assert os.path.isfile(new) - os.remove(new) - #+end_src - - + [X] Preprend the short datestamp (YYMMDD, feature by Reiner Rottmann) - Related to the basic pattern, except truncating of the first two - characters. + + [X] short pattern, i.e. preprend YYMMDD_ to file test.txt. A feature by + Reiner Rottmann. Related to the basic pattern, except the two first + characters are truncated. #+begin_src python :tangle test_date2name.py @pytest.mark.files @pytest.mark.short @@ -428,27 +389,66 @@ os.remove(new) #+end_src - + [ ] Check the retraction of the date/time stamp + + [X] withtime pattern, i.e. prepend YYYY-MM-DDThh.mm.ss_ to file test.txt. + This extends the default pattern YYYY-MM-DD. + #+begin_src python :tangle test_date2name.py + @pytest.mark.files + @pytest.mark.withtime + @pytest.mark.parametrize("arg1", ["-w -f", "-w --files", + "--withtime -f", "--withtime --files", + "-w -m", "-w --mtime", + "--withtime -m", "--withtime --mtime", + "-w -c", "-w --ctime", + "--withtime -c", "--withtime --ctime"]) + def test_file_pattern_withtime(arg1): + """Prepend 'YYYY-MM-DDThh.mm.ss_' to the file name.""" + prepare_testfile() + day = str("") + new = str("") + + if arg1 in ["-w -f", "-w --files", + "--withtime -f", "--withtime --files", + "-w -m", "-w --mtime", + "--withtime -m", "--withtime --mtime"]: + day = query_modification_time().split()[0] + second = query_modification_time().split()[1] + + elif arg1 in ["-w -c", "-w --ctime", + "--withtime -c", "--withtime --ctime"]: + day = query_creation_time().split()[0] + second = query_creation_time().split()[1] + + second = second.split(".")[0] # use integer seconds only + second = second.replace(":", ".") # adjust representation + + new = "".join([day, "T", second, "_", TFILE]) + + test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}") + assert os.path.isfile(new) + os.remove(new) + #+end_src - The test is constrained to the five fix formats prepending the file name. + + [X] Check the retraction of the date/time stamp on files. + + Based on a pattern comparison, a file like =20210921_test.txt= is renamed + =test.txt=. At present (Linux Debian 12/bookworm, branch testing), + date2name is known to struggle for files with the tag date2time prepended + by parameter =--withtime= (or =-w=). This is why the two corrsponding + tests fail. - /Not ready for inclusion into the main branch/ To trace the advancement of - pytest's processing, long delays are set. A manual check of date2time on - the CLI confirms that date2name writes the --withtime format, but is not - successful to retract this stamp. - #+begin_src python :tangle test_date2name.py @pytest.mark.files @pytest.mark.remove - @pytest.mark.parametrize("arg1", ["default", "short", "compact", - "month", "withtime"]) + @pytest.mark.parametrize("arg1", ["default", + "compact", "month", "short", + "withtime"]) @pytest.mark.parametrize("arg2", ["-r", "--remove"]) def test_file_remove_stamp(arg1, arg2): """Check the retraction of the leading time stamp.""" substitution = {"default" : "2021-09-21", - "short" : "210921", "compact" : "20210921", "month" : "2021-09", + "short" : "210921", "withtime": "2021-09-21T13.59.59"} prepend = substitution.get(arg1) From 6dfe900beb0d24147fe08752b527f31faa238730 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Wed, 29 Sep 2021 09:36:04 +0000 Subject: [PATCH 09/14] transplant file test (default) on folders --- test_date2name.py | 31 ++++++++++++++++++++++++++--- test_generator.org | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/test_date2name.py b/test_date2name.py index 725fa54..ca1ce74 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -259,15 +259,16 @@ def test_file_pattern_withtime(arg1): @pytest.mark.files @pytest.mark.remove -@pytest.mark.parametrize("arg1", ["default", "short", "compact", - "month", "withtime"]) +@pytest.mark.parametrize("arg1", ["default", + "compact", "month", "short", + "withtime"]) @pytest.mark.parametrize("arg2", ["-r", "--remove"]) def test_file_remove_stamp(arg1, arg2): """Check the retraction of the leading time stamp.""" substitution = {"default" : "2021-09-21", - "short" : "210921", "compact" : "20210921", "month" : "2021-09", + "short" : "210921", "withtime": "2021-09-21T13.59.59"} prepend = substitution.get(arg1) @@ -284,3 +285,27 @@ def test_file_remove_stamp(arg1, arg2): os.remove("test.txt") # succesful space cleaning for next test assert os.path.isfile("test.txt") is False + +@pytest.mark.folders +@pytest.mark.default +@pytest.mark.parametrize("arg1", [" ", "-d", "--directories", + "-m", "--mtime", + "-c", "--ctime"]) +def test_folder_pattern_default(arg1, name=TFOLDER): + """Prepend 'YYYY-MM-DD_' to the folder name.""" + prepare_testfolder(name) + day = str("") + new = str("") + + if arg1 in [" ", "-d", "--directories", "-m", "--mtime"]: + day = query_modification_time(name).split()[0] + + elif arg1 in ["-c", "--ctime"]: + day = query_creation_time(name).split()[0] + + new = "_".join([day, name]) + test = getoutput(f"python3 {PROGRAM} {name} {arg1}") + assert os.path.isdir(name) is False # absence unstamped folder + assert os.path.isdir(new) # presence stamped folder + os.rmdir(new) + assert os.path.isdir(new) is False # space cleaning diff --git a/test_generator.org b/test_generator.org index 9055d8c..e5f4996 100755 --- a/test_generator.org +++ b/test_generator.org @@ -466,3 +466,52 @@ os.remove("test.txt") # succesful space cleaning for next test assert os.path.isfile("test.txt") is False #+end_src + +*** perform the tests on folders [1/6] + + At present, most of the instructions already defined and used in section + "test on files" is repeated with small adjustments for checking date2name's + action on folders. While this approach isn't dry, given current experience, + it however is more reliable in eventual code execution running pytest, than + stacking the files/folders levels as an additional parameter. + + + [X] default pattern, YYYY-MM-DD_ prepended + #+begin_src python :tangle test_date2name.py + @pytest.mark.folders + @pytest.mark.default + @pytest.mark.parametrize("arg1", [" ", "-d", "--directories", + "-m", "--mtime", + "-c", "--ctime"]) + def test_folder_pattern_default(arg1, name=TFOLDER): + """Prepend 'YYYY-MM-DD_' to the folder name.""" + prepare_testfolder(name) + day = str("") + new = str("") + + if arg1 in [" ", "-d", "--directories", "-m", "--mtime"]: + day = query_modification_time(name).split()[0] + + elif arg1 in ["-c", "--ctime"]: + day = query_creation_time(name).split()[0] + + new = "_".join([day, name]) + test = getoutput(f"python3 {PROGRAM} {name} {arg1}") + assert os.path.isdir(name) is False # absence unstamped folder + assert os.path.isdir(new) # presence stamped folder + os.rmdir(new) + assert os.path.isdir(new) is False # space cleaning + #+end_src + + + [ ] compact pattern, YYYYMMDD_ prepended + + + [ ] month pattern, YYYY-MM_ prepended + + + [ ] short pattern, YYMMDD_ prepended + + + [ ] withtime pattern, YYYY-MM-DDThh.mm.ss_ prepended + + + [ ] retraction of the date/time stamp + + + + From 72c827438568067c89dc8bfdf9b3a0eea8767a05 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Wed, 29 Sep 2021 09:43:26 +0000 Subject: [PATCH 10/14] transplant file test (compact) on folders --- test_date2name.py | 37 +++++++++++++++++++++++++++++++++++++ test_generator.org | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/test_date2name.py b/test_date2name.py index ca1ce74..5d605e7 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -309,3 +309,40 @@ def test_folder_pattern_default(arg1, name=TFOLDER): assert os.path.isdir(new) # presence stamped folder os.rmdir(new) assert os.path.isdir(new) is False # space cleaning + +@pytest.mark.folders +@pytest.mark.compact +@pytest.mark.parametrize("arg1", ["-C", "--compact", + "-C -d", "--compact -d", + "-C --directories", "--compact --directories", + "-C -m", "--compact -m", + "-C --mtime", "--compact --mtime", + "-C -c", "--compact -c", + "-C --ctime", "--compact --ctime"]) +def test_folder_pattern_compact(arg1, name=TFOLDER): + """Prepend 'YYYYMMDD_' to the folder name.""" + prepare_testfolder(name) + day = str("") + new = str("") + + if arg1 in ["-C", "--compact", + "-C -d", "--compact -d", + "-C --directories", "--compact --directories", + "-C -m", "--compact -m", + "-C --mtime", "--compact --mtime"]: + day = query_modification_time(name).split()[0] + + elif arg1 in ["-C -c", "--compact -c", + "-C --ctime", "--compact --ctime"]: + day = query_creation_time(name).split()[0] + + # drop the hyphens in the datestamp: + day = day.replace("-", "") + + new = "_".join([day, name]) + test = getoutput(f"python3 {PROGRAM} {name} {arg1}") + + assert os.path.isdir(name) is False # absence unstamped folder + assert os.path.isdir(new) # presence stamped folder + os.rmdir(new) + assert os.path.isdir(new) is False # space cleaning diff --git a/test_generator.org b/test_generator.org index e5f4996..0f76fba 100755 --- a/test_generator.org +++ b/test_generator.org @@ -467,7 +467,7 @@ assert os.path.isfile("test.txt") is False #+end_src -*** perform the tests on folders [1/6] +*** perform the tests on folders [2/6] At present, most of the instructions already defined and used in section "test on files" is repeated with small adjustments for checking date2name's @@ -502,7 +502,46 @@ assert os.path.isdir(new) is False # space cleaning #+end_src - + [ ] compact pattern, YYYYMMDD_ prepended + + [X] compact pattern, YYYYMMDD_ prepended + #+begin_src python :tangle test_date2name.py + @pytest.mark.folders + @pytest.mark.compact + @pytest.mark.parametrize("arg1", ["-C", "--compact", + "-C -d", "--compact -d", + "-C --directories", "--compact --directories", + "-C -m", "--compact -m", + "-C --mtime", "--compact --mtime", + "-C -c", "--compact -c", + "-C --ctime", "--compact --ctime"]) + def test_folder_pattern_compact(arg1, name=TFOLDER): + """Prepend 'YYYYMMDD_' to the folder name.""" + prepare_testfolder(name) + day = str("") + new = str("") + + if arg1 in ["-C", "--compact", + "-C -d", "--compact -d", + "-C --directories", "--compact --directories", + "-C -m", "--compact -m", + "-C --mtime", "--compact --mtime"]: + day = query_modification_time(name).split()[0] + + elif arg1 in ["-C -c", "--compact -c", + "-C --ctime", "--compact --ctime"]: + day = query_creation_time(name).split()[0] + + # drop the hyphens in the datestamp: + day = day.replace("-", "") + + new = "_".join([day, name]) + test = getoutput(f"python3 {PROGRAM} {name} {arg1}") + + assert os.path.isdir(name) is False # absence unstamped folder + assert os.path.isdir(new) # presence stamped folder + os.rmdir(new) + assert os.path.isdir(new) is False # space cleaning + + #+end_src + [ ] month pattern, YYYY-MM_ prepended From ec1a3ccf2b848baf8a474ca37170d11f23388f84 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Wed, 29 Sep 2021 09:50:58 +0000 Subject: [PATCH 11/14] transplant file test (month) on folders --- test_date2name.py | 37 +++++++++++++++++++++++++++++++++++++ test_generator.org | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/test_date2name.py b/test_date2name.py index 5d605e7..abcc357 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -346,3 +346,40 @@ def test_folder_pattern_compact(arg1, name=TFOLDER): assert os.path.isdir(new) # presence stamped folder os.rmdir(new) assert os.path.isdir(new) is False # space cleaning + +@pytest.mark.folders +@pytest.mark.month +@pytest.mark.parametrize("arg1", ["-M", "--month", + "-M -d", "--month -d", + "-M --directories", "--month --directories", + "-M -m", "--month -m", + "-M --mtime", "--month --mtime", + "-M -c", "--month -c", + "-M --ctime", "--month --ctime"]) +def test_file_pattern_month(arg1, name=TFOLDER): + """Prepend 'YYYY-MM_' to the file name.""" + prepare_testfolder(name) + day = str("") + new = str("") + + if arg1 in ["-M", "--month", + "-M -d", "--month -d", + "-M --directories", "--month --directories", + "-M -m", "--month -m", + "-M --mtime", "--month --mtime"]: + day = query_modification_time(name).split()[0] + + elif arg1 in ["-M -c", "--month -c", + "-M --ctime", "--month --ctime"]: + day = query_creation_time(name).split()[0] + + # trim off the last three characters in the datestamp: + day = day[:-3] + + new = "_".join([day, name]) + test = getoutput(f"python3 {PROGRAM} {name} {arg1}") + + assert os.path.isdir(name) is False # absence unstamped folder + assert os.path.isdir(new) # presence stamped folder + os.rmdir(new) + assert os.path.isdir(new) is False # space cleaning diff --git a/test_generator.org b/test_generator.org index 0f76fba..ade8710 100755 --- a/test_generator.org +++ b/test_generator.org @@ -467,7 +467,7 @@ assert os.path.isfile("test.txt") is False #+end_src -*** perform the tests on folders [2/6] +*** perform the tests on folders [3/6] At present, most of the instructions already defined and used in section "test on files" is repeated with small adjustments for checking date2name's @@ -540,10 +540,47 @@ assert os.path.isdir(new) # presence stamped folder os.rmdir(new) assert os.path.isdir(new) is False # space cleaning - #+end_src - + [ ] month pattern, YYYY-MM_ prepended + + [X] month pattern, YYYY-MM_ prepended + #+begin_src python :tangle test_date2name.py + @pytest.mark.folders + @pytest.mark.month + @pytest.mark.parametrize("arg1", ["-M", "--month", + "-M -d", "--month -d", + "-M --directories", "--month --directories", + "-M -m", "--month -m", + "-M --mtime", "--month --mtime", + "-M -c", "--month -c", + "-M --ctime", "--month --ctime"]) + def test_file_pattern_month(arg1, name=TFOLDER): + """Prepend 'YYYY-MM_' to the file name.""" + prepare_testfolder(name) + day = str("") + new = str("") + + if arg1 in ["-M", "--month", + "-M -d", "--month -d", + "-M --directories", "--month --directories", + "-M -m", "--month -m", + "-M --mtime", "--month --mtime"]: + day = query_modification_time(name).split()[0] + + elif arg1 in ["-M -c", "--month -c", + "-M --ctime", "--month --ctime"]: + day = query_creation_time(name).split()[0] + + # trim off the last three characters in the datestamp: + day = day[:-3] + + new = "_".join([day, name]) + test = getoutput(f"python3 {PROGRAM} {name} {arg1}") + + assert os.path.isdir(name) is False # absence unstamped folder + assert os.path.isdir(new) # presence stamped folder + os.rmdir(new) + assert os.path.isdir(new) is False # space cleaning + #+end_src + [ ] short pattern, YYMMDD_ prepended From 765179c2a11c5a746181af9fad3537d507bdb9c9 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Wed, 29 Sep 2021 09:57:19 +0000 Subject: [PATCH 12/14] transplant file test (short) on folders --- test_date2name.py | 39 +++++++++++++++++++++++++++++++++++++++ test_generator.org | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/test_date2name.py b/test_date2name.py index abcc357..7426d42 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -383,3 +383,42 @@ def test_file_pattern_month(arg1, name=TFOLDER): assert os.path.isdir(new) # presence stamped folder os.rmdir(new) assert os.path.isdir(new) is False # space cleaning + +@pytest.mark.folders +@pytest.mark.short +@pytest.mark.parametrize("arg1", ["-S", "--short", + "-S -d", "--short -d", + "-S --directories", "--short --directories", + "-S -m", "--short -m", + "-S --mtime", "--short --mtime", + "-S -c", "--short -c", + "-S --ctime", "--short --ctime"]) +def test_folder_pattern_short(arg1, name=TFOLDER): + """Prepend 'YYMMDD_' to the file name.""" + prepare_testfolder(name) + day = str("") + new = str("") + + if arg1 in ["-S", "--short", + "-S -d", "--short -d", + "-S --directories", "--short --directories", + "-S -m", "--short -m", + "-S --mtime", "--short --mtime"]: + day = query_modification_time(name).split()[0] + + elif arg1 in ["-S -c", "--short -c", + "-S --ctime", "--short --ctime"]: + day = query_creation_time(name).split()[0] + + # drop the hyphens in the datestamp: + day = day.replace("-", "") + # drop the first two characters about the year (e.g., 1789 -> 89) + day = day[2:] + + new = "_".join([day, name]) + test = getoutput(f"python3 {PROGRAM} {name} {arg1}") + + assert os.path.isdir(name) is False # absence unstamped folder + assert os.path.isdir(new) # presence stamped folder + os.rmdir(new) + assert os.path.isdir(new) is False # space cleaning diff --git a/test_generator.org b/test_generator.org index ade8710..e05ed71 100755 --- a/test_generator.org +++ b/test_generator.org @@ -467,7 +467,7 @@ assert os.path.isfile("test.txt") is False #+end_src -*** perform the tests on folders [3/6] +*** perform the tests on folders [4/6] At present, most of the instructions already defined and used in section "test on files" is repeated with small adjustments for checking date2name's @@ -582,7 +582,47 @@ assert os.path.isdir(new) is False # space cleaning #+end_src - + [ ] short pattern, YYMMDD_ prepended + + [X] short pattern, YYMMDD_ prepended + #+begin_src python :tangle test_date2name.py + @pytest.mark.folders + @pytest.mark.short + @pytest.mark.parametrize("arg1", ["-S", "--short", + "-S -d", "--short -d", + "-S --directories", "--short --directories", + "-S -m", "--short -m", + "-S --mtime", "--short --mtime", + "-S -c", "--short -c", + "-S --ctime", "--short --ctime"]) + def test_folder_pattern_short(arg1, name=TFOLDER): + """Prepend 'YYMMDD_' to the file name.""" + prepare_testfolder(name) + day = str("") + new = str("") + + if arg1 in ["-S", "--short", + "-S -d", "--short -d", + "-S --directories", "--short --directories", + "-S -m", "--short -m", + "-S --mtime", "--short --mtime"]: + day = query_modification_time(name).split()[0] + + elif arg1 in ["-S -c", "--short -c", + "-S --ctime", "--short --ctime"]: + day = query_creation_time(name).split()[0] + + # drop the hyphens in the datestamp: + day = day.replace("-", "") + # drop the first two characters about the year (e.g., 1789 -> 89) + day = day[2:] + + new = "_".join([day, name]) + test = getoutput(f"python3 {PROGRAM} {name} {arg1}") + + assert os.path.isdir(name) is False # absence unstamped folder + assert os.path.isdir(new) # presence stamped folder + os.rmdir(new) + assert os.path.isdir(new) is False # space cleaning + #+end_src + [ ] withtime pattern, YYYY-MM-DDThh.mm.ss_ prepended From d630277ed6a1a55896003e94748ac97fc62dc5be Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Wed, 29 Sep 2021 10:17:56 +0000 Subject: [PATCH 13/14] transplant file test (withtime) on folders --- test_date2name.py | 38 ++++++++++++++++++++++++++++++++++++++ test_generator.org | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/test_date2name.py b/test_date2name.py index 7426d42..868af2a 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -422,3 +422,41 @@ def test_folder_pattern_short(arg1, name=TFOLDER): assert os.path.isdir(new) # presence stamped folder os.rmdir(new) assert os.path.isdir(new) is False # space cleaning + +@pytest.mark.folders +@pytest.mark.withtime +@pytest.mark.parametrize("arg1", ["-w -d", "-w --directories", + "--withtime -d", "--withtime --directories", + "-w -m", "-w --mtime", + "--withtime -m", "--withtime --mtime", + "-w -c", "-w --ctime", + "--withtime -c", "--withtime --ctime"]) +def test_file_pattern_withtime(arg1, name=TFOLDER): + """Prepend 'YYYY-MM-DDThh.mm.ss_' to the folder name.""" + prepare_testfolder(name) + day = str("") + new = str("") + + if arg1 in ["-w -d", "-w --directories", + "--withtime -d", "--withtime --directories", + "-w -m", "-w --mtime", + "--withtime -m", "--withtime --mtime"]: + day = query_modification_time(name).split()[0] + second = query_modification_time(name).split()[1] + + elif arg1 in ["-w -c", "-w --ctime", + "--withtime -c", "--withtime --ctime"]: + day = query_creation_time(name).split()[0] + second = query_creation_time(name).split()[1] + + second = second.split(".")[0] # use integer seconds only + second = second.replace(":", ".") # adjust representation + + new = "".join([day, "T", second, "_", name]) + + test = getoutput(f"python3 {PROGRAM} {name} {arg1}") + + assert os.path.isdir(name) is False # absence unstamped folder + assert os.path.isdir(new) # presence stamped folder + os.rmdir(new) + assert os.path.isdir(new) is False # space cleaning diff --git a/test_generator.org b/test_generator.org index e05ed71..c22fd80 100755 --- a/test_generator.org +++ b/test_generator.org @@ -467,7 +467,7 @@ assert os.path.isfile("test.txt") is False #+end_src -*** perform the tests on folders [4/6] +*** perform the tests on folders [5/6] At present, most of the instructions already defined and used in section "test on files" is repeated with small adjustments for checking date2name's @@ -624,7 +624,46 @@ assert os.path.isdir(new) is False # space cleaning #+end_src - + [ ] withtime pattern, YYYY-MM-DDThh.mm.ss_ prepended + + [X] withtime pattern, YYYY-MM-DDThh.mm.ss_ prepended + #+begin_src python :tangle test_date2name.py + @pytest.mark.folders + @pytest.mark.withtime + @pytest.mark.parametrize("arg1", ["-w -d", "-w --directories", + "--withtime -d", "--withtime --directories", + "-w -m", "-w --mtime", + "--withtime -m", "--withtime --mtime", + "-w -c", "-w --ctime", + "--withtime -c", "--withtime --ctime"]) + def test_file_pattern_withtime(arg1, name=TFOLDER): + """Prepend 'YYYY-MM-DDThh.mm.ss_' to the folder name.""" + prepare_testfolder(name) + day = str("") + new = str("") + + if arg1 in ["-w -d", "-w --directories", + "--withtime -d", "--withtime --directories", + "-w -m", "-w --mtime", + "--withtime -m", "--withtime --mtime"]: + day = query_modification_time(name).split()[0] + second = query_modification_time(name).split()[1] + + elif arg1 in ["-w -c", "-w --ctime", + "--withtime -c", "--withtime --ctime"]: + day = query_creation_time(name).split()[0] + second = query_creation_time(name).split()[1] + + second = second.split(".")[0] # use integer seconds only + second = second.replace(":", ".") # adjust representation + + new = "".join([day, "T", second, "_", name]) + + test = getoutput(f"python3 {PROGRAM} {name} {arg1}") + + assert os.path.isdir(name) is False # absence unstamped folder + assert os.path.isdir(new) # presence stamped folder + os.rmdir(new) + assert os.path.isdir(new) is False # space cleaning + #+end_src + [ ] retraction of the date/time stamp From 287939add157faf33eadc34593f553617db46a10 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Wed, 29 Sep 2021 11:47:17 +0000 Subject: [PATCH 14/14] transplant file test (remove) on folders Again, testing fails for the two variants -r/--remove when aiming to retract a -w/--withtime stamp. This commit eventually offers the same coverage of tests now on folders (all five fixed stamp pattern to add and their retraction). --- test_date2name.py | 45 ++++++++++++++++++++++++------ test_generator.org | 69 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 90 insertions(+), 24 deletions(-) diff --git a/test_date2name.py b/test_date2name.py index 868af2a..4dd8c8a 100644 --- a/test_date2name.py +++ b/test_date2name.py @@ -45,7 +45,7 @@ def prepare_testfile(name=TFILE): def prepare_testfolder(name=TFOLDER): - """Create a testfolder.""" + """Create a test folder.""" os.mkdir(name) result = os.stat(name) os.utime(name, (result.st_atime, result.st_mtime + 10.0)) @@ -144,7 +144,7 @@ def test_file_pattern_compact(arg1): "-C --ctime", "--compact --ctime"]: day = query_creation_time().split()[0] - # drop the hyphens in the datestamp: + # drop the hyphens in the date stamp: day = day.replace("-", "") new = "_".join([day, TFILE]) @@ -178,7 +178,7 @@ def test_file_pattern_month(arg1): "-M --ctime", "--month --ctime"]: day = query_creation_time().split()[0] - # trim off the last three characters in the datestamp: + # trim off the last three characters in the date stamp: day = day[:-3] new = "_".join([day, TFILE]) @@ -212,7 +212,7 @@ def test_file_pattern_short(arg1): "-S --ctime", "--short --ctime"]: day = query_creation_time().split()[0] - # drop the hyphens in the datestamp: + # drop the hyphens in the date stamp: day = day.replace("-", "") # drop the first two characters about the year (e.g., 1789 -> 89) day = day[2:] @@ -283,7 +283,7 @@ def test_file_remove_stamp(arg1, arg2): assert os.path.isfile(TFILE) is False # absence of stamped file assert os.path.isfile(BASIS) # presence unstamped file - os.remove("test.txt") # succesful space cleaning for next test + os.remove("test.txt") # successful space cleaning for next test assert os.path.isfile("test.txt") is False @pytest.mark.folders @@ -336,7 +336,7 @@ def test_folder_pattern_compact(arg1, name=TFOLDER): "-C --ctime", "--compact --ctime"]: day = query_creation_time(name).split()[0] - # drop the hyphens in the datestamp: + # drop the hyphens in the date stamp: day = day.replace("-", "") new = "_".join([day, name]) @@ -373,7 +373,7 @@ def test_file_pattern_month(arg1, name=TFOLDER): "-M --ctime", "--month --ctime"]: day = query_creation_time(name).split()[0] - # trim off the last three characters in the datestamp: + # trim off the last three characters in the date stamp: day = day[:-3] new = "_".join([day, name]) @@ -410,7 +410,7 @@ def test_folder_pattern_short(arg1, name=TFOLDER): "-S --ctime", "--short --ctime"]: day = query_creation_time(name).split()[0] - # drop the hyphens in the datestamp: + # drop the hyphens in the date stamp: day = day.replace("-", "") # drop the first two characters about the year (e.g., 1789 -> 89) day = day[2:] @@ -460,3 +460,32 @@ def test_file_pattern_withtime(arg1, name=TFOLDER): assert os.path.isdir(new) # presence stamped folder os.rmdir(new) assert os.path.isdir(new) is False # space cleaning + +@pytest.mark.folders +@pytest.mark.remove +@pytest.mark.parametrize("arg1", ["default", + "compact", "month", "short", + "withtime"]) +@pytest.mark.parametrize("arg2", ["-r", "--remove"]) +def test_folder_remove_stamp(arg1, arg2, name=TFOLDER): + """Check the retraction of the leading time stamp.""" + substitution = {"default" : "2021-09-21", + "compact" : "20210921", + "month" : "2021-09", + "short" : "210921", + "withtime": "2021-09-21T13.59.59"} + prepend = substitution.get(arg1) + + # os.mkdir(name) + BASIS = str(name) + stamped_folder = "" + stamped_folder = "_".join([prepend, BASIS]) + os.mkdir(stamped_folder) + assert os.path.isdir(stamped_folder) # presence stamped folder + + test = getoutput(f"python3 {PROGRAM} {stamped_folder} {arg2}") + + assert os.path.isdir(stamped_folder) is False + assert os.path.isdir(name) # presence unstamped folder + os.rmdir(name) + assert os.path.isdir(name) is False # space cleaning diff --git a/test_generator.org b/test_generator.org index c22fd80..4e2f519 100755 --- a/test_generator.org +++ b/test_generator.org @@ -8,7 +8,7 @@ * Intent - The application =date2name= by Karl Voit /et al./ ([[https://github.com/novoid/date2name][source)]] prepends datestamps + The application =date2name= by Karl Voit /et al./ ([[https://github.com/novoid/date2name][source)]] prepends date stamps to files and folders (YYYY-MM-DD, YYYYMMDD, YYYY-MM, and YYYY-MM-DDThh.mm.ss). This Emacs .org file is used to prepare the automatic testing of the file processing by [[https://docs.pytest.org/en/latest/][pytest]]. @@ -24,7 +24,7 @@ * Dependencies - The testing script is set up with Python 3.9.2 in mind. Thus, to run the + The testing script is set up with Python 3.9.7 in mind. Thus, to run the tests successfully, you need a working installation of Python 3 with pytest for Python 3. @@ -34,7 +34,7 @@ tests is either one of the following instructions (you might need to add the executable bit): - python pytest -xv test_date2name.py + python pytest -v test_date2name.py ./Makefile In case the computer you use equally includes an installation of legacy @@ -42,12 +42,14 @@ branch of the two. Depending on your OS, this requires an adjustment of the command issue. In Linux Debian 12/bookworm, branch testing, for example, - python3 pytest-3 -xv test_date2name.py + python3 pytest-3 -v test_date2name.py or, after adjustment of the =Makefile= and provision of the executable bit ./Makefile + See section about building a =pytest.ini= for additional options prepared. + * Building the tests @@ -181,7 +183,7 @@ def prepare_testfolder(name=TFOLDER): - """Create a testfolder.""" + """Create a test folder.""" os.mkdir(name) result = os.stat(name) os.utime(name, (result.st_atime, result.st_mtime + 10.0)) @@ -300,7 +302,7 @@ "-C --ctime", "--compact --ctime"]: day = query_creation_time().split()[0] - # drop the hyphens in the datestamp: + # drop the hyphens in the date stamp: day = day.replace("-", "") new = "_".join([day, TFILE]) @@ -339,7 +341,7 @@ "-M --ctime", "--month --ctime"]: day = query_creation_time().split()[0] - # trim off the last three characters in the datestamp: + # trim off the last three characters in the date stamp: day = day[:-3] new = "_".join([day, TFILE]) @@ -348,7 +350,7 @@ os.remove(new) #+end_src - + [X] short pattern, i.e. preprend YYMMDD_ to file test.txt. A feature by + + [X] short pattern, i.e. prepend YYMMDD_ to file test.txt. A feature by Reiner Rottmann. Related to the basic pattern, except the two first characters are truncated. #+begin_src python :tangle test_date2name.py @@ -378,7 +380,7 @@ "-S --ctime", "--short --ctime"]: day = query_creation_time().split()[0] - # drop the hyphens in the datestamp: + # drop the hyphens in the date stamp: day = day.replace("-", "") # drop the first two characters about the year (e.g., 1789 -> 89) day = day[2:] @@ -433,7 +435,7 @@ Based on a pattern comparison, a file like =20210921_test.txt= is renamed =test.txt=. At present (Linux Debian 12/bookworm, branch testing), date2name is known to struggle for files with the tag date2time prepended - by parameter =--withtime= (or =-w=). This is why the two corrsponding + by parameter =--withtime= (or =-w=). This is why the two corresponding tests fail. #+begin_src python :tangle test_date2name.py @@ -463,11 +465,11 @@ assert os.path.isfile(TFILE) is False # absence of stamped file assert os.path.isfile(BASIS) # presence unstamped file - os.remove("test.txt") # succesful space cleaning for next test + os.remove("test.txt") # successful space cleaning for next test assert os.path.isfile("test.txt") is False #+end_src -*** perform the tests on folders [5/6] +*** perform the tests on folders [6/6] At present, most of the instructions already defined and used in section "test on files" is repeated with small adjustments for checking date2name's @@ -530,7 +532,7 @@ "-C --ctime", "--compact --ctime"]: day = query_creation_time(name).split()[0] - # drop the hyphens in the datestamp: + # drop the hyphens in the date stamp: day = day.replace("-", "") new = "_".join([day, name]) @@ -570,7 +572,7 @@ "-M --ctime", "--month --ctime"]: day = query_creation_time(name).split()[0] - # trim off the last three characters in the datestamp: + # trim off the last three characters in the date stamp: day = day[:-3] new = "_".join([day, name]) @@ -610,7 +612,7 @@ "-S --ctime", "--short --ctime"]: day = query_creation_time(name).split()[0] - # drop the hyphens in the datestamp: + # drop the hyphens in the date stamp: day = day.replace("-", "") # drop the first two characters about the year (e.g., 1789 -> 89) day = day[2:] @@ -665,7 +667,42 @@ assert os.path.isdir(new) is False # space cleaning #+end_src - + [ ] retraction of the date/time stamp + + [X] retraction of the date/time stamp + + Similar to the retraction of a prepended time stamp on files, the two + checks to remove a time stamp added by =--withtime= or =-w= currently fail + when running =pytest-3= on Linux Debian 12/bookworm, branch testing. + + #+begin_src python :tangle test_date2name.py + @pytest.mark.folders + @pytest.mark.remove + @pytest.mark.parametrize("arg1", ["default", + "compact", "month", "short", + "withtime"]) + @pytest.mark.parametrize("arg2", ["-r", "--remove"]) + def test_folder_remove_stamp(arg1, arg2, name=TFOLDER): + """Check the retraction of the leading time stamp.""" + substitution = {"default" : "2021-09-21", + "compact" : "20210921", + "month" : "2021-09", + "short" : "210921", + "withtime": "2021-09-21T13.59.59"} + prepend = substitution.get(arg1) + + # os.mkdir(name) + BASIS = str(name) + stamped_folder = "" + stamped_folder = "_".join([prepend, BASIS]) + os.mkdir(stamped_folder) + assert os.path.isdir(stamped_folder) # presence stamped folder + + test = getoutput(f"python3 {PROGRAM} {stamped_folder} {arg2}") + + assert os.path.isdir(stamped_folder) is False + assert os.path.isdir(name) # presence unstamped folder + os.rmdir(name) + assert os.path.isdir(name) is False # space cleaning + #+end_src