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"