style: lint tests against flake8

Tests were linted with flake8 (7.1.1 (mccabe: 0.7.0, pycodestyle:
2.12.1, pyflakes: 3.2.0) CPython 3.12.7).

Signed-off-by: Norwid Behrnd <nbehrnd@yahoo.com>
This commit is contained in:
Norwid Behrnd 2024-11-21 21:11:26 +01:00
parent 3b82ea43e8
commit 052ca37939
2 changed files with 74 additions and 68 deletions

View file

@ -10,8 +10,8 @@
Initially written for Python 3.9.9 and pytest 6.2.4 and recently update Initially written for Python 3.9.9 and pytest 6.2.4 and recently update
for Python 3.12.6/pytest 8.3.3, this script provides a programmatic check for Python 3.12.6/pytest 8.3.3, this script provides a programmatic check
of functions offered by appendfilename. Deposit this script in the root of of functions offered by appendfilename. Deposit this script in the root
the folder fetched and unzipped from PyPi or GitHub. Create a virtual of the folder fetched and unzipped from PyPi or GitHub. Create a virtual
environment for Python, e.g. by environment for Python, e.g. by
```shell ```shell
@ -29,7 +29,7 @@ python -m pytest
As a reminder, the following optional pytest flags may be useful to obtain As a reminder, the following optional pytest flags may be useful to obtain
a report tailored to your needs: a report tailored to your needs:
- `-x` exits right after the first failing test (reported by `E` instead of `.`) - `-x` exit after the first failing test (reported by `E` instead of `.`)
- `-v` provide a more verbose output - `-v` provide a more verbose output
- `-s` equally report the test criterion, e.g. the queried file name - `-s` equally report the test criterion, e.g. the queried file name
""" """
@ -37,7 +37,6 @@ a report tailored to your needs:
import re import re
import os import os
import shlex import shlex
import sys
import subprocess import subprocess
from itertools import product from itertools import product
@ -46,9 +45,9 @@ import pytest
PROGRAM = os.path.join("appendfilename", "__init__.py") # Cross-platform path PROGRAM = os.path.join("appendfilename", "__init__.py") # Cross-platform path
# The following section tests the applications default pattern where a string # The following section tests the applications default pattern where a
# is added to the file name, just prior to the file's file extension. The # string is added to the file name, just prior to the file's file
# permutation of the three arguments and their levels defines 120 tests. # extension. The permutations of the arguments define 120 tests.
arg1_values = [ arg1_values = [
"test.txt", "2021-12-31_test.txt", "2021-12-31T18.48.22_test.txt" "test.txt", "2021-12-31_test.txt", "2021-12-31T18.48.22_test.txt"
@ -73,6 +72,7 @@ arg3_values = [
# create the permutations: # create the permutations:
test_cases = list(product(arg1_values, arg2_values, arg3_values)) test_cases = list(product(arg1_values, arg2_values, arg3_values))
@pytest.mark.default @pytest.mark.default
@pytest.mark.parametrize("arg1, arg2, arg3", test_cases) @pytest.mark.parametrize("arg1, arg2, arg3", test_cases)
def test_append(arg1, arg2, arg3): def test_append(arg1, arg2, arg3):
@ -89,7 +89,7 @@ def test_append(arg1, arg2, arg3):
# run the test to be tested: # run the test to be tested:
full_command = ["python", PROGRAM, arg1 full_command = ["python", PROGRAM, arg1
] + shlex.split(arg2) + shlex.split(arg3) ] + shlex.split(arg2) + shlex.split(arg3)
subprocess.run(full_command, text = True, check = True) subprocess.run(full_command, text=True, check=True)
# construct the new file name to be tested: # construct the new file name to be tested:
if len(shlex.split(arg3)) == 0: if len(shlex.split(arg3)) == 0:
@ -98,9 +98,8 @@ def test_append(arg1, arg2, arg3):
separator = shlex.split(arg3)[1] separator = shlex.split(arg3)[1]
new_filename = "".join( new_filename = "".join(
[ arg1[:-4], separator, [arg1[:-4], separator, shlex.split(arg2)[1], ".txt"])
shlex.split(arg2)[1], ".txt" ]) print(f"test criterion: {new_filename}") # for an optional `pytest -s`
print(f"test criterion: {new_filename}") # visible by optional `pytest -s`
# is the new file present? # is the new file present?
assert os.path.isfile(new_filename) assert os.path.isfile(new_filename)
@ -109,9 +108,10 @@ def test_append(arg1, arg2, arg3):
os.remove(new_filename) os.remove(new_filename)
assert os.path.isfile(new_filename) is False assert os.path.isfile(new_filename) is False
# The following section is about tests to prepend a user defined string and # The following section is about tests to prepend a user defined string
# an adjustable separator to the original file name of the file submitted. By # and an adjustable separator to the original file name of the submitted
# permutation of the parameter's levels, this defines 240 tests. # file. The permutation of the parameters defines 240 tests.
arg1_values = [ arg1_values = [
"test.txt", "2021-12-31_test.txt", "2021-12-31T18.48.22_test.txt" "test.txt", "2021-12-31_test.txt", "2021-12-31T18.48.22_test.txt"
@ -140,6 +140,7 @@ arg4_values = [
# create the permutations: # create the permutations:
test_cases = list(product(arg1_values, arg2_values, arg3_values, arg4_values)) test_cases = list(product(arg1_values, arg2_values, arg3_values, arg4_values))
@pytest.mark.prepend @pytest.mark.prepend
@pytest.mark.parametrize("arg1, arg2, arg3, arg4", test_cases) @pytest.mark.parametrize("arg1, arg2, arg3, arg4", test_cases)
def test_prepend(arg1, arg2, arg3, arg4): def test_prepend(arg1, arg2, arg3, arg4):
@ -158,7 +159,7 @@ def test_prepend(arg1, arg2, arg3, arg4):
full_command = [ full_command = [
"python", PROGRAM, arg1 "python", PROGRAM, arg1
] + shlex.split(arg2) + shlex.split(arg3) + shlex.split(arg4) ] + shlex.split(arg2) + shlex.split(arg3) + shlex.split(arg4)
subprocess.run(full_command, text = True, check = True) subprocess.run(full_command, text=True, check=True)
# construct the new file name to be tested: # construct the new file name to be tested:
if len(shlex.split(arg3)) == 0: if len(shlex.split(arg3)) == 0:
@ -166,7 +167,7 @@ def test_prepend(arg1, arg2, arg3, arg4):
else: else:
separator = shlex.split(arg3)[1] separator = shlex.split(arg3)[1]
new_filename = "".join( [ shlex.split(arg2)[1], separator, arg1 ] ) new_filename = "".join([shlex.split(arg2)[1], separator, arg1])
print(f"test criterion: {new_filename}") # visible by optional `pytest -s` print(f"test criterion: {new_filename}") # visible by optional `pytest -s`
# is the new file present? # is the new file present?
@ -179,12 +180,13 @@ def test_prepend(arg1, arg2, arg3, arg4):
# This section tests the insertion of a string into the file's file name # This section tests the insertion of a string into the file's file name
# just after the file's time or date stamp as provided `date2name`. # just after the file's time or date stamp as provided `date2name`.
arg1_values = [ arg1_values = [
"2021-12-31T18.48.22_test.txt", "2021-12-31T18.48.22_test.txt",
"2021-12-31_test.txt", "2021-12-31_test.txt",
# "20211231_test.txt", # by now `20211231_test.txt` -> 20211231_test ping.txt # "20211231_test.txt", # by now `20211231_test.txt` -> 20211231_test ping.txt
# "2021-12_test.txt", # by now `2021-12_test.txt` -> `2021-12_test ping.txt` # "2021-12_test.txt", # by now `2021-12_test.txt` -> `2021-12_test ping.txt`
# "211231_test.txt" # by now `211231_test.txt` -> `211231_test ping.txt` # "211231_test.txt" # by now `211231_test.txt` -> `211231_test ping.txt`
] ]
arg2_values = [ arg2_values = [
"-t book", "-t book",
@ -194,15 +196,15 @@ arg2_values = [
] ]
arg3_values = [ arg3_values = [
"", # i.e. fall back to default single space "", # i.e. fall back to default single space
# "--separator '!'", # "--separator '!'",
# "--separator '@'", # "--separator '@'",
# "--separator '#'", # "--separator '#'",
# "--separator '$'", # "--separator '$'",
# "--separator '%'", # "--separator '%'",
# "--separator '_'", # "--separator '_'",
# "--separator '+'", # "--separator '+'",
# "--separator '='", # "--separator '='",
# "--separator '-'" # "--separator '-'"
] ]
# Note: The check with pytest and `*` as separator in Windows 10 fails. # Note: The check with pytest and `*` as separator in Windows 10 fails.
# Contrasting to Linux Debian 13, a `pytest` in Windows 10 revealed every # Contrasting to Linux Debian 13, a `pytest` in Windows 10 revealed every
@ -211,6 +213,7 @@ arg3_values = [
# create the permutations: # create the permutations:
test_cases = list(product(arg1_values, arg2_values, arg3_values)) test_cases = list(product(arg1_values, arg2_values, arg3_values))
@pytest.mark.smart @pytest.mark.smart
@pytest.mark.parametrize("arg1, arg2, arg3", test_cases) @pytest.mark.parametrize("arg1, arg2, arg3", test_cases)
def test_smart_prepend(arg1, arg2, arg3): def test_smart_prepend(arg1, arg2, arg3):
@ -221,17 +224,17 @@ def test_smart_prepend(arg1, arg2, arg3):
arg3 the separator (at least in Windows 10, do not use `*` arg3 the separator (at least in Windows 10, do not use `*`
""" """
time_stamp = "" time_stamp = ""
time_stamp_separator = "" # time_stamp_separator = ""
old_filename_no_timestamp = "" old_filename_no_timestamp = ""
# create a test file: # create a test file:
with open(arg1, mode="w", encoding="utf-8") as newfile: with open(arg1, mode="w", encoding="utf-8") as newfile:
newfile.write("this is a placeholder\n") newfile.write("this is a placeholder\n")
#run `appendfilename` on this test file # run `appendfilename` on this test file
run_appendfilename = " ".join( run_appendfilename = " ".join(
["python", PROGRAM, arg1, arg2, arg3, " --smart-prepend"]) ["python", PROGRAM, arg1, arg2, arg3, " --smart-prepend"])
subprocess.run(run_appendfilename, shell=True, check = True) subprocess.run(run_appendfilename, shell=True, check=True)
# construct the new file name to be testedt: # construct the new file name to be testedt:
old_filename = arg1 old_filename = arg1
@ -288,9 +291,9 @@ def test_smart_prepend(arg1, arg2, arg3):
time_stamp_separator = old_filename[6] time_stamp_separator = old_filename[6]
old_filename_no_timestamp = old_filename[7:] old_filename_no_timestamp = old_filename[7:]
new_filename = "".join([time_stamp, #time_stamp_separator, new_filename = "".join([time_stamp, # time_stamp_separator,
separator, shlex.split(arg2)[1], separator, separator, shlex.split(arg2)[1], separator,
old_filename_no_timestamp ]) old_filename_no_timestamp])
# is the new file present? # is the new file present?
print("\nnew_filename") # optional check for `pytest -s` print("\nnew_filename") # optional check for `pytest -s`

View file

@ -1,8 +1,8 @@
# name: test_generator.org # name: test_generator.org
# author: nbehrnd@yahoo.com # author: nbehrnd@yahoo.com
# date: 2022-01-05 (YYYY-MM-DD) # date: 2022-01-05 (YYYY-MM-DD)
# edit: [2024-11-05 Tue] # edit: [2024-11-21 Thu]
# license: GPL3, 2022. # license: GPL3, 2022-2024
# Export the tangled files with C-c C-v t # Export the tangled files with C-c C-v t
#+PROPERTY: header-args :tangle yes #+PROPERTY: header-args :tangle yes
@ -163,8 +163,8 @@ markers =
Initially written for Python 3.9.9 and pytest 6.2.4 and recently update Initially written for Python 3.9.9 and pytest 6.2.4 and recently update
for Python 3.12.6/pytest 8.3.3, this script provides a programmatic check for Python 3.12.6/pytest 8.3.3, this script provides a programmatic check
of functions offered by appendfilename. Deposit this script in the root of of functions offered by appendfilename. Deposit this script in the root
the folder fetched and unzipped from PyPi or GitHub. Create a virtual of the folder fetched and unzipped from PyPi or GitHub. Create a virtual
environment for Python, e.g. by environment for Python, e.g. by
```shell ```shell
@ -182,7 +182,7 @@ python -m pytest
As a reminder, the following optional pytest flags may be useful to obtain As a reminder, the following optional pytest flags may be useful to obtain
a report tailored to your needs: a report tailored to your needs:
- `-x` exits right after the first failing test (reported by `E` instead of `.`) - `-x` exit after the first failing test (reported by `E` instead of `.`)
- `-v` provide a more verbose output - `-v` provide a more verbose output
- `-s` equally report the test criterion, e.g. the queried file name - `-s` equally report the test criterion, e.g. the queried file name
""" """
@ -190,7 +190,6 @@ a report tailored to your needs:
import re import re
import os import os
import shlex import shlex
import sys
import subprocess import subprocess
from itertools import product from itertools import product
@ -206,9 +205,9 @@ PROGRAM = os.path.join("appendfilename", "__init__.py") # Cross-platform path
yield =test example.txt=. yield =test example.txt=.
#+begin_src python :tangle test_appendfilename.py #+begin_src python :tangle test_appendfilename.py
# The following section tests the applications default pattern where a string # The following section tests the applications default pattern where a
# is added to the file name, just prior to the file's file extension. The # string is added to the file name, just prior to the file's file
# permutation of the three arguments and their levels defines 120 tests. # extension. The permutations of the arguments define 120 tests.
arg1_values = [ arg1_values = [
"test.txt", "2021-12-31_test.txt", "2021-12-31T18.48.22_test.txt" "test.txt", "2021-12-31_test.txt", "2021-12-31T18.48.22_test.txt"
@ -233,6 +232,7 @@ arg3_values = [
# create the permutations: # create the permutations:
test_cases = list(product(arg1_values, arg2_values, arg3_values)) test_cases = list(product(arg1_values, arg2_values, arg3_values))
@pytest.mark.default @pytest.mark.default
@pytest.mark.parametrize("arg1, arg2, arg3", test_cases) @pytest.mark.parametrize("arg1, arg2, arg3", test_cases)
def test_append(arg1, arg2, arg3): def test_append(arg1, arg2, arg3):
@ -249,7 +249,7 @@ def test_append(arg1, arg2, arg3):
# run the test to be tested: # run the test to be tested:
full_command = ["python", PROGRAM, arg1 full_command = ["python", PROGRAM, arg1
] + shlex.split(arg2) + shlex.split(arg3) ] + shlex.split(arg2) + shlex.split(arg3)
subprocess.run(full_command, text = True, check = True) subprocess.run(full_command, text=True, check=True)
# construct the new file name to be tested: # construct the new file name to be tested:
if len(shlex.split(arg3)) == 0: if len(shlex.split(arg3)) == 0:
@ -258,9 +258,8 @@ def test_append(arg1, arg2, arg3):
separator = shlex.split(arg3)[1] separator = shlex.split(arg3)[1]
new_filename = "".join( new_filename = "".join(
[ arg1[:-4], separator, [arg1[:-4], separator, shlex.split(arg2)[1], ".txt"])
shlex.split(arg2)[1], ".txt" ]) print(f"test criterion: {new_filename}") # for an optional `pytest -s`
print(f"test criterion: {new_filename}") # visible by optional `pytest -s`
# is the new file present? # is the new file present?
assert os.path.isfile(new_filename) assert os.path.isfile(new_filename)
@ -278,9 +277,10 @@ def test_append(arg1, arg2, arg3):
#+begin_src python :tangle test_appendfilename.py #+begin_src python :tangle test_appendfilename.py
# The following section is about tests to prepend a user defined string and # The following section is about tests to prepend a user defined string
# an adjustable separator to the original file name of the file submitted. By # and an adjustable separator to the original file name of the submitted
# permutation of the parameter's levels, this defines 240 tests. # file. The permutation of the parameters defines 240 tests.
arg1_values = [ arg1_values = [
"test.txt", "2021-12-31_test.txt", "2021-12-31T18.48.22_test.txt" "test.txt", "2021-12-31_test.txt", "2021-12-31T18.48.22_test.txt"
@ -309,6 +309,7 @@ arg4_values = [
# create the permutations: # create the permutations:
test_cases = list(product(arg1_values, arg2_values, arg3_values, arg4_values)) test_cases = list(product(arg1_values, arg2_values, arg3_values, arg4_values))
@pytest.mark.prepend @pytest.mark.prepend
@pytest.mark.parametrize("arg1, arg2, arg3, arg4", test_cases) @pytest.mark.parametrize("arg1, arg2, arg3, arg4", test_cases)
def test_prepend(arg1, arg2, arg3, arg4): def test_prepend(arg1, arg2, arg3, arg4):
@ -327,7 +328,7 @@ def test_prepend(arg1, arg2, arg3, arg4):
full_command = [ full_command = [
"python", PROGRAM, arg1 "python", PROGRAM, arg1
] + shlex.split(arg2) + shlex.split(arg3) + shlex.split(arg4) ] + shlex.split(arg2) + shlex.split(arg3) + shlex.split(arg4)
subprocess.run(full_command, text = True, check = True) subprocess.run(full_command, text=True, check=True)
# construct the new file name to be tested: # construct the new file name to be tested:
if len(shlex.split(arg3)) == 0: if len(shlex.split(arg3)) == 0:
@ -335,7 +336,7 @@ def test_prepend(arg1, arg2, arg3, arg4):
else: else:
separator = shlex.split(arg3)[1] separator = shlex.split(arg3)[1]
new_filename = "".join( [ shlex.split(arg2)[1], separator, arg1 ] ) new_filename = "".join([shlex.split(arg2)[1], separator, arg1])
print(f"test criterion: {new_filename}") # visible by optional `pytest -s` print(f"test criterion: {new_filename}") # visible by optional `pytest -s`
# is the new file present? # is the new file present?
@ -363,12 +364,13 @@ def test_prepend(arg1, arg2, arg3, arg4):
# This section tests the insertion of a string into the file's file name # This section tests the insertion of a string into the file's file name
# just after the file's time or date stamp as provided `date2name`. # just after the file's time or date stamp as provided `date2name`.
arg1_values = [ arg1_values = [
"2021-12-31T18.48.22_test.txt", "2021-12-31T18.48.22_test.txt",
"2021-12-31_test.txt", "2021-12-31_test.txt",
# "20211231_test.txt", # by now `20211231_test.txt` -> 20211231_test ping.txt # "20211231_test.txt", # by now `20211231_test.txt` -> 20211231_test ping.txt
# "2021-12_test.txt", # by now `2021-12_test.txt` -> `2021-12_test ping.txt` # "2021-12_test.txt", # by now `2021-12_test.txt` -> `2021-12_test ping.txt`
# "211231_test.txt" # by now `211231_test.txt` -> `211231_test ping.txt` # "211231_test.txt" # by now `211231_test.txt` -> `211231_test ping.txt`
] ]
arg2_values = [ arg2_values = [
"-t book", "-t book",
@ -378,15 +380,15 @@ arg2_values = [
] ]
arg3_values = [ arg3_values = [
"", # i.e. fall back to default single space "", # i.e. fall back to default single space
# "--separator '!'", # "--separator '!'",
# "--separator '@'", # "--separator '@'",
# "--separator '#'", # "--separator '#'",
# "--separator '$'", # "--separator '$'",
# "--separator '%'", # "--separator '%'",
# "--separator '_'", # "--separator '_'",
# "--separator '+'", # "--separator '+'",
# "--separator '='", # "--separator '='",
# "--separator '-'" # "--separator '-'"
] ]
# Note: The check with pytest and `*` as separator in Windows 10 fails. # Note: The check with pytest and `*` as separator in Windows 10 fails.
# Contrasting to Linux Debian 13, a `pytest` in Windows 10 revealed every # Contrasting to Linux Debian 13, a `pytest` in Windows 10 revealed every
@ -395,6 +397,7 @@ arg3_values = [
# create the permutations: # create the permutations:
test_cases = list(product(arg1_values, arg2_values, arg3_values)) test_cases = list(product(arg1_values, arg2_values, arg3_values))
@pytest.mark.smart @pytest.mark.smart
@pytest.mark.parametrize("arg1, arg2, arg3", test_cases) @pytest.mark.parametrize("arg1, arg2, arg3", test_cases)
def test_smart_prepend(arg1, arg2, arg3): def test_smart_prepend(arg1, arg2, arg3):
@ -405,17 +408,17 @@ def test_smart_prepend(arg1, arg2, arg3):
arg3 the separator (at least in Windows 10, do not use `*` arg3 the separator (at least in Windows 10, do not use `*`
""" """
time_stamp = "" time_stamp = ""
time_stamp_separator = "" # time_stamp_separator = ""
old_filename_no_timestamp = "" old_filename_no_timestamp = ""
# create a test file: # create a test file:
with open(arg1, mode="w", encoding="utf-8") as newfile: with open(arg1, mode="w", encoding="utf-8") as newfile:
newfile.write("this is a placeholder\n") newfile.write("this is a placeholder\n")
#run `appendfilename` on this test file # run `appendfilename` on this test file
run_appendfilename = " ".join( run_appendfilename = " ".join(
["python", PROGRAM, arg1, arg2, arg3, " --smart-prepend"]) ["python", PROGRAM, arg1, arg2, arg3, " --smart-prepend"])
subprocess.run(run_appendfilename, shell=True, check = True) subprocess.run(run_appendfilename, shell=True, check=True)
# construct the new file name to be testedt: # construct the new file name to be testedt:
old_filename = arg1 old_filename = arg1
@ -472,9 +475,9 @@ def test_smart_prepend(arg1, arg2, arg3):
time_stamp_separator = old_filename[6] time_stamp_separator = old_filename[6]
old_filename_no_timestamp = old_filename[7:] old_filename_no_timestamp = old_filename[7:]
new_filename = "".join([time_stamp, #time_stamp_separator, new_filename = "".join([time_stamp, # time_stamp_separator,
separator, shlex.split(arg2)[1], separator, separator, shlex.split(arg2)[1], separator,
old_filename_no_timestamp ]) old_filename_no_timestamp])
# is the new file present? # is the new file present?
print("\nnew_filename") # optional check for `pytest -s` print("\nnew_filename") # optional check for `pytest -s`