forked from Github/date2name
Maintainers/developers of pytest have little influence only if pytest (for Python 3) in Linux is invoked by pytest, or pytest-3. Commit accounts for their clarification provided.
732 lines
27 KiB
Org Mode
Executable file
732 lines
27 KiB
Org Mode
Executable file
#+NAME: test_generator.org
|
||
#+AUTHOR: nbehrnd@yahoo.com
|
||
#+DATE: 2022-01-09 (YYYY-MM-DD)
|
||
# License: GPL3, 2021.
|
||
|
||
#+PROPERTY: header-args :tangle yes
|
||
# Export the tangled files with C-c C-v t
|
||
|
||
* Intent
|
||
|
||
The application =date2name= by Karl Voit /et al./ ([[https://github.com/novoid/date2name][source)]] prepends date
|
||
stamps to files and folders (e.g., rename file =example.txt= to
|
||
=2021-12-30_example.txt=). By the command =C-c C-v t=, Emacs may use the
|
||
present =.org= file to (re)generate a tangled test script, file
|
||
=test_date2name.py= for a programmatic testing by [[https://docs.pytest.org/en/latest/][pytest]]. (Though =pytest= is
|
||
not part of the Python standard library, it may be obtained easily e.g., from
|
||
[[https://pypi.org/project/pytest/][PyPi]].) Optionally, the testing may be run by the equally tangled =Makefile=.
|
||
|
||
* Deployment
|
||
|
||
The programmatic tests are set up for pytest for Python 3. It
|
||
however depends on your installation (and in case of Linux, the
|
||
authors of your Linux distribution ([[https://github.com/pytest-dev/pytest/discussions/9481][reference]])) if this utility may
|
||
be started by =pytest= (e.g., the pattern in pytest's manual), or by
|
||
=pytest-3= by either one of the pattern below:
|
||
|
||
#+begin_src bash :tangle no
|
||
pytest -v test_date2name.py
|
||
pytest-3 -v test_date2name.py
|
||
#+end_src
|
||
|
||
As of writing, the later pattern is the to be used e.g., in Linux
|
||
Debian 12/bookworm (branch testing) to discern pytest (for
|
||
contemporary Python 3) from pytest (for legacy Python 2).
|
||
|
||
The =Makefile= this =org= file provides for convenience running
|
||
these tests assumes the later syntax pattern. (It might be
|
||
necessary to provide the executable bit to activate the Makefile.)
|
||
|
||
* Setup of Emacs
|
||
|
||
The edit of this =.org= file in Emacs and the subsequent export (tangle) of
|
||
the files are affected by Emacs' own parameters (e.g., the indentation in
|
||
Python). It is recommended to access this file with Emacs in a session
|
||
started by =emacs -q test_generator.org &= and to evaluate the following block
|
||
by =C-c C-c=; this explicitly adjusts a few basic settings, but does not
|
||
permanently overwrite an already existing personalized Emacs configuration.
|
||
|
||
Most of these instructions are elements of Hendrik Suenkler's annotated Emacs
|
||
configuration ([[https://www.suenkler.info/notes/emacs-config/][blog post]]) which are reused with his permission.
|
||
|
||
#+begin_src emacs-lisp :tangle no
|
||
;; support these languages at all:
|
||
(org-babel-do-load-languages
|
||
'org-babel-load-languages
|
||
'((emacs-lisp . t)
|
||
(org . t)
|
||
(shell . t)
|
||
(python . t)))
|
||
|
||
;; enable syntax highlighting:
|
||
(setq org-src-fontify-natively t)
|
||
|
||
;; adjust indentations, set tabs as explicit 4 spaces:
|
||
(setq-default indent-tabs-mode nil)
|
||
(setq default-tab-width 4)
|
||
|
||
(setq custom-tab-width 4)
|
||
(setq-default python-indent-offset custom-tab-width)
|
||
|
||
(setq org-edit-src-content-indentation 0)
|
||
(setq org-src-tab-acts-natively t)
|
||
(setq org-src-preserve-indentation t)
|
||
|
||
;; some comfort functions Suenkler mentions:
|
||
(delete-selection-mode 1)
|
||
(defalias 'yes-or-no-p 'y-or-n-p)
|
||
|
||
(show-paren-mode 1)
|
||
(setq show-paren-style 'parenthesis)
|
||
|
||
(column-number-mode nil)
|
||
|
||
(setq org-src-fontify-natively t)
|
||
#+end_src
|
||
|
||
#+RESULTS:
|
||
: 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.
|
||
|
||
* Building the tests
|
||
|
||
** Building of the Makefile
|
||
|
||
The setup is for GNU Make 4.3 as provided e.g., by Linux Debian 12
|
||
(bookworm), branch testing. Note, the Makefile tangled is a mere convenient
|
||
moderator for =test_date2name.py=; the eventual testing of date2name's action
|
||
does not depend on this Makefile.
|
||
|
||
#+BEGIN_SRC makefile :tangle Makefile
|
||
# GNU Make file for the automation of pytest for date2name.
|
||
#
|
||
# 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_date2name.py in the root folder of date2name
|
||
# 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_date2name.py # the pattern by pytest's manual
|
||
pytest-3 -v test_date2name.py # the alternative pattern (e.g., Debian 12)
|
||
#+end_src
|
||
|
||
** Building a pytest.ini
|
||
|
||
This file defines markers to assign tests into groups. This allows to run
|
||
=pytest= on a subset rather than all tests (which is set up as the default).
|
||
E.g., in presence of =pytest.ini=, a call like
|
||
|
||
#+begin_src bash :tangle no
|
||
pytest-3 test_date2name.py -v -m "elementary"
|
||
#+end_src
|
||
|
||
constrains 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
|
||
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.
|
||
|
||
|
||
#+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
|
||
|
||
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
|
||
|
||
** Building the test script
|
||
|
||
*** header section
|
||
#+BEGIN_SRC python :tangle test_date2name.py
|
||
#!/bin/usr/env python3
|
||
|
||
# name: test_date2name.py
|
||
# author: nbehrnd@yahoo.com
|
||
# license: GPL v3, 2021.
|
||
# date: 2021-08-30 (YYYY-MM-DD)
|
||
# edit: 2022-01-03 (YYYY-MM-DD)
|
||
#
|
||
"""Test pad for functions by date2name with pytest.
|
||
|
||
Written for Python 3.9.2 and pytest 6.2.4 for Python 3 as provided by
|
||
Linux Debian 12/bookworm, branch testing, this is a programmatic check
|
||
of functions offered by date2name. Deposit this script in the root of
|
||
the folder fetched and unzipped from PyPi or GitHub. If your system
|
||
includes both legacy Python 2 and Python 3, pytest for Python 3 likely
|
||
is named pytest-3; otherwise only pytest. Thus, adjust your input on
|
||
the CLI accordingly when running either one of
|
||
|
||
pytest -v test_date2name.py
|
||
pytest-3 -v test_date2name.py
|
||
|
||
These instruction initiate a verbose testing (flag -v) reported back to the CLI.re will be a verbose report to the CLI The script either stops when one of the tests fail (flag -x), or after
|
||
completion of the test sequence. In both cases, the progress of the ongoing
|
||
tests is reported to the CLI (flag -v).
|
||
|
||
"""
|
||
import os
|
||
import time
|
||
|
||
from datetime import datetime
|
||
from subprocess import getstatusoutput, getoutput
|
||
|
||
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
|
||
#+end_src
|
||
|
||
|
||
*** prepare recurrently used functions
|
||
|
||
Define actions which are going to be used multiple times.
|
||
|
||
#+begin_src python :tangle test_date2name.py
|
||
def prepare_testfile(name=TFILE):
|
||
"""The creation of the test file."""
|
||
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(name)
|
||
os.utime(name, (result.st_atime, result.st_mtime + 10.0))
|
||
|
||
|
||
def prepare_testfolder(name=TFOLDER):
|
||
"""Create a test folder."""
|
||
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
|
||
created = str(datetime.fromtimestamp(created))
|
||
return created
|
||
|
||
|
||
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
|
||
|
||
|
||
*** set up very elementary tests
|
||
|
||
These tests do not yet modify a file, nor folder which already exist.
|
||
|
||
#+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)
|
||
assert os.path.isfile(name)
|
||
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)
|
||
assert os.path.isdir(name)
|
||
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)
|
||
#+end_src
|
||
|
||
|
||
*** perform the tests on files [6/6]
|
||
|
||
These tests check the addition of a time stamp ahead of the file name.
|
||
|
||
+ [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"])
|
||
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)
|
||
os.remove(new)
|
||
#+end_src
|
||
|
||
+ [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
|
||
@pytest.mark.parametrize("arg1", ["-C", "--compact",
|
||
"-C -f", "--compact -f",
|
||
"-C --files", "--compact --files",
|
||
"-C -m", "--compact -m",
|
||
"-C --mtime", "--compact --mtime",
|
||
"-C -c", "--compact -c",
|
||
"-C --ctime", "--compact --ctime"])
|
||
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 date stamp:
|
||
day = day.replace("-", "")
|
||
|
||
new = "_".join([day, TFILE])
|
||
test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}")
|
||
assert os.path.isfile(new)
|
||
os.remove(new)
|
||
#+end_src
|
||
|
||
+ [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
|
||
@pytest.mark.files
|
||
@pytest.mark.month
|
||
@pytest.mark.parametrize("arg1", ["-M", "--month",
|
||
"-M -f", "--month -f",
|
||
"-M --files", "--month --files",
|
||
"-M -m", "--month -m",
|
||
"-M --mtime", "--month --mtime",
|
||
"-M -c", "--month -c",
|
||
"-M --ctime", "--month --ctime"])
|
||
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 date stamp:
|
||
day = day[:-3]
|
||
|
||
new = "_".join([day, TFILE])
|
||
test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}")
|
||
assert os.path.isfile(new)
|
||
os.remove(new)
|
||
#+end_src
|
||
|
||
+ [X] short pattern, i.e. prepend YYMMDD_ to file test.txt. A feature
|
||
introduced to date2name 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
|
||
@pytest.mark.parametrize("arg1", ["-S", "--short",
|
||
"-S -f", "--short -f",
|
||
"-S --files", "--short --files",
|
||
"-S -m", "--short -m",
|
||
"-S --mtime", "--short --mtime",
|
||
"-S -c", "--short -c",
|
||
"-S --ctime", "--short --ctime"])
|
||
def test_file_pattern_short(arg1):
|
||
"""Prepend 'YYMMDD_' to the file name."""
|
||
prepare_testfile()
|
||
day = str("")
|
||
new = str("")
|
||
|
||
if arg1 in ["-S", "--short",
|
||
"-S -f", "--short -f",
|
||
"-S --files", "--short --files",
|
||
"-S -m", "--short -m",
|
||
"-S --mtime", "--short --mtime"]:
|
||
day = query_modification_time().split()[0]
|
||
|
||
elif arg1 in ["-S -c", "--short -c",
|
||
"-S --ctime", "--short --ctime"]:
|
||
day = query_creation_time().split()[0]
|
||
|
||
# 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:]
|
||
|
||
new = "_".join([day, TFILE])
|
||
test = getoutput(f"python3 {PROGRAM} {TFILE} {arg1}")
|
||
assert os.path.isfile(new)
|
||
os.remove(new)
|
||
#+end_src
|
||
|
||
+ [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
|
||
|
||
+ [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=.
|
||
|
||
#+begin_src python :tangle test_date2name.py
|
||
@pytest.mark.files
|
||
@pytest.mark.remove
|
||
@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",
|
||
"compact" : "20210921",
|
||
"month" : "2021-09",
|
||
"short" : "210921",
|
||
"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.")
|
||
|
||
test = getoutput(f"python3 {PROGRAM} {TFILE} {arg2}")
|
||
|
||
assert os.path.isfile(TFILE) is False # absence of stamped file
|
||
assert os.path.isfile(BASIS) # presence unstamped file
|
||
|
||
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 [6/6]
|
||
|
||
At present, most of the instructions already defined and used in section
|
||
"test on files" are repeated with small adjustments for checking date2name's
|
||
action on folders. This approach isn't dry; though, 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
|
||
|
||
+ [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 date stamp:
|
||
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
|
||
|
||
+ [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 date stamp:
|
||
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
|
||
|
||
+ [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 date stamp:
|
||
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
|
||
|
||
+ [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
|
||
|
||
+ [X] retraction of the date/time stamp
|
||
|
||
#+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)
|
||
|
||
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
|