From 736daa8bbcd56eb80c0f18cf8e2a2f78d6432e99 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 25 Feb 2021 08:11:15 +0200 Subject: feat: directory handling steps for Python lib/files --- share/python/lib/files.md | 23 +++++++++++++++++++++++ share/python/lib/files.py | 36 ++++++++++++++++++++++++++++++++++++ share/python/lib/files.yaml | 21 +++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/share/python/lib/files.md b/share/python/lib/files.md index 68ef1ac..823c760 100644 --- a/share/python/lib/files.md +++ b/share/python/lib/files.md @@ -70,6 +70,29 @@ and file hello.txt matches regex "hello, .*" and file hello.txt matches regex /hello, .*/ ~~~ +# Directories + +There are also a large number of directory based steps and some directory +based behaviour available in creating files which are available in the files +library. + +```scenario +given a directory first +then directory first exists +and directory first is empty +and directory second does not exist +when I remove directory first +then directory first does not exist +when I create directory second +then directory second exists +and directory second is empty +given file second/third/hello.txt from hello.txt +then directory second is not empty +and directory second/third exists +and directory second/third is not empty +when I remove directory second +then directory second does not exist +``` --- title: Acceptance criteria for the files Subplot library diff --git a/share/python/lib/files.py b/share/python/lib/files.py index ec37b9d..dd5b9f8 100644 --- a/share/python/lib/files.py +++ b/share/python/lib/files.py @@ -1,10 +1,12 @@ import logging import os import re +import shutil import time def files_create_from_embedded(ctx, filename=None): + files_make_directory(ctx, path=os.path.dirname(filename) or ".") files_create_from_embedded_with_other_name( ctx, filename_on_disk=filename, embedded_filename=filename ) @@ -14,15 +16,29 @@ def files_create_from_embedded_with_other_name( ctx, filename_on_disk=None, embedded_filename=None ): get_file = globals()["get_file"] + + files_make_directory(ctx, path=os.path.dirname(filename_on_disk) or ".") with open(filename_on_disk, "wb") as f: f.write(get_file(embedded_filename)) def files_create_from_text(ctx, filename=None, text=None): + files_make_directory(ctx, path=os.path.dirname(filename) or ".") with open(filename, "w") as f: f.write(text) +def files_make_directory(ctx, path=None): + path = "./" + path + if not os.path.exists(path): + os.makedirs(path) + + +def files_remove_directory(ctx, path=None): + path = "./" + path + shutil.rmtree(path) + + def files_file_exists(ctx, filename=None): assert_eq = globals()["assert_eq"] assert_eq(os.path.exists(filename), True) @@ -33,6 +49,26 @@ def files_file_does_not_exist(ctx, filename=None): assert_eq(os.path.exists(filename), False) +def files_directory_exists(ctx, path=None): + assert_eq = globals()["assert_eq"] + assert_eq(os.path.isdir(path), True) + + +def files_directory_does_not_exist(ctx, path=None): + assert_eq = globals()["assert_eq"] + assert_eq(os.path.isdir(path), False) + + +def files_directory_is_empty(ctx, path=None): + assert_eq = globals()["assert_eq"] + assert_eq(os.listdir(path), []) + + +def files_directory_is_not_empty(ctx, path=None): + assert_ne = globals()["assert_ne"] + assert_ne(os.listdir(path), False) + + def files_only_these_exist(ctx, filenames=None): assert_eq = globals()["assert_eq"] filenames = filenames.replace(",", "").split() diff --git a/share/python/lib/files.yaml b/share/python/lib/files.yaml index be69920..f18b8cd 100644 --- a/share/python/lib/files.yaml +++ b/share/python/lib/files.yaml @@ -60,3 +60,24 @@ - then: file {filename} has a very old modification time function: files_mtime_is_ancient + +- given: a directory {path} + function: files_make_directory + +- when: I create directory {path} + function: files_make_directory + +- when: I remove directory {path} + function: files_remove_directory + +- then: directory {path} exists + function: files_directory_exists + +- then: directory {path} does not exist + function: files_directory_does_not_exist + +- then: directory {path} is empty + function: files_directory_is_empty + +- then: directory {path} is not empty + function: files_directory_is_not_empty -- cgit v1.2.1