From 2a9891c0df25ae45d7020dd98684979b71402d78 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Mon, 30 Aug 2021 10:52:59 +0100 Subject: library: Make files.yaml properly polyglot Signed-off-by: Daniel Silverstone --- share/common/lib/files.yaml | 188 ++++++++++++++++++++++++++++++++++++++++ share/python/lib/files.md | 2 +- share/python/lib/files.py | 35 ++------ share/python/lib/files.yaml | 133 ---------------------------- share/python/lib/runcmd.md | 2 +- share/python/lib/runcmd_test.py | 2 +- share/rust/lib/files.yaml | 139 ----------------------------- 7 files changed, 200 insertions(+), 301 deletions(-) create mode 100644 share/common/lib/files.yaml delete mode 100644 share/python/lib/files.yaml delete mode 100644 share/rust/lib/files.yaml (limited to 'share') diff --git a/share/common/lib/files.yaml b/share/common/lib/files.yaml new file mode 100644 index 0000000..40c092a --- /dev/null +++ b/share/common/lib/files.yaml @@ -0,0 +1,188 @@ +# Bindings for the files steps + +- given: file {embedded_file} + impl: + rust: + function: subplotlib::steplibrary::files::create_from_embedded + python: + function: files_create_from_embedded + types: + embedded_file: file + +- given: file {filename_on_disk} from {embedded_file} + impl: + rust: + function: subplotlib::steplibrary::files::create_from_embedded_with_other_name + python: + function: files_create_from_embedded_with_other_name + types: + embedded_file: file + +- given: file (?P\S+) has modification time (?P\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) + regex: true + impl: + rust: + function: subplotlib::steplibrary::files::touch_with_timestamp + python: + function: files_touch_with_timestamp + types: + mtime: text + +- when: I write "(?P.*)" to file (?P\S+) + regex: true + impl: + rust: + function: subplotlib::steplibrary::files::create_from_text + python: + function: files_create_from_text + +- when: I remember metadata for file {filename} + impl: + rust: + function: subplotlib::steplibrary::files::remember_metadata + python: + function: files_remember_metadata + +- when: I touch file {filename} + impl: + rust: + function: subplotlib::steplibrary::files::touch + python: + function: files_touch + +- then: file {filename} exists + impl: + rust: + function: subplotlib::steplibrary::files::file_exists + python: + function: files_file_exists + +- then: file {filename} does not exist + impl: + rust: + function: subplotlib::steplibrary::files::file_does_not_exist + python: + function: files_file_does_not_exist + +- then: only files (?P.+) exist + impl: + rust: + function: subplotlib::steplibrary::files::only_these_exist + python: + function: files_only_these_exist + regex: true + +- then: file (?P\S+) contains "(?P.*)" + regex: true + impl: + rust: + function: subplotlib::steplibrary::files::file_contains + python: + function: files_file_contains + +- then: file (?P\S+) matches regex /(?P.*)/ + regex: true + impl: + rust: + function: subplotlib::steplibrary::files::file_matches_regex + python: + function: files_file_matches_regex + +- then: file (?P\S+) matches regex "(?P.*)" + regex: true + impl: + rust: + function: subplotlib::steplibrary::files::file_matches_regex + python: + function: files_file_matches_regex + +- then: files {filename1} and {filename2} match + impl: + rust: + function: subplotlib::steplibrary::files::file_match + python: + function: files_match + +- then: file {filename} has same metadata as before + impl: + rust: + function: subplotlib::steplibrary::files::has_remembered_metadata + python: + function: files_has_remembered_metadata + +- then: file {filename} has different metadata from before + impl: + rust: + function: subplotlib::steplibrary::files::has_different_metadata + python: + function: files_has_different_metadata + +- then: file {filename} has changed from before + impl: + rust: + function: subplotlib::steplibrary::files::has_different_metadata + python: + function: files_has_different_metadata + +- then: file {filename} has a very recent modification time + impl: + rust: + function: subplotlib::steplibrary::files::mtime_is_recent + python: + function: files_mtime_is_recent + +- then: file {filename} has a very old modification time + impl: + rust: + function: subplotlib::steplibrary::files::mtime_is_ancient + python: + function: files_mtime_is_ancient + +- given: a directory {path} + impl: + rust: + function: subplotlib::steplibrary::files::make_directory + python: + function: files_make_directory + +- when: I create directory {path} + impl: + rust: + function: subplotlib::steplibrary::files::make_directory + python: + function: files_make_directory + +- when: I remove directory {path} + impl: + rust: + function: subplotlib::steplibrary::files::remove_directory + python: + function: files_remove_directory + +- then: directory {path} exists + impl: + rust: + function: subplotlib::steplibrary::files::path_exists + python: + function: files_directory_exists + +- then: directory {path} does not exist + impl: + rust: + function: subplotlib::steplibrary::files::path_does_not_exist + python: + function: files_directory_does_not_exist + +- then: directory {path} is empty + impl: + rust: + function: subplotlib::steplibrary::files::path_is_empty + python: + function: files_directory_is_empty + +- then: directory {path} is not empty + impl: + rust: + function: subplotlib::steplibrary::files::path_is_not_empty + python: + function: files_directory_is_not_empty diff --git a/share/python/lib/files.md b/share/python/lib/files.md index 823c760..5638791 100644 --- a/share/python/lib/files.md +++ b/share/python/lib/files.md @@ -99,7 +99,7 @@ title: Acceptance criteria for the files Subplot library author: The Subplot project template: python bindings: -- files.yaml +- lib/files.yaml functions: - files.py ... diff --git a/share/python/lib/files.py b/share/python/lib/files.py index fa219b4..8c2c3a7 100644 --- a/share/python/lib/files.py +++ b/share/python/lib/files.py @@ -1,3 +1,5 @@ +from time import strptime + import logging import os import re @@ -5,21 +7,21 @@ import shutil import time -def files_create_from_embedded(ctx, filename=None): - files_make_directory(ctx, path=os.path.dirname(filename) or ".") +def files_create_from_embedded(ctx, embedded_file=None): + files_make_directory(ctx, path=os.path.dirname(embedded_file) or ".") files_create_from_embedded_with_other_name( - ctx, filename_on_disk=filename, embedded_filename=filename + ctx, filename_on_disk=embedded_file, embedded_file=embedded_file ) def files_create_from_embedded_with_other_name( - ctx, filename_on_disk=None, embedded_filename=None + ctx, filename_on_disk=None, embedded_file=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)) + f.write(get_file(embedded_file)) def files_create_from_text(ctx, filename=None, text=None): @@ -106,27 +108,8 @@ def files_match(ctx, filename1=None, filename2=None): assert_eq(data1, data2) -def files_touch_with_timestamp( - ctx, - filename=None, - year=None, - month=None, - day=None, - hour=None, - minute=None, - second=None, -): - t = ( - int(year), - int(month), - int(day), - int(hour), - int(minute), - int(second), - -1, - -1, - -1, - ) +def files_touch_with_timestamp(ctx, filename=None, mtime = None): + t = strptime(mtime, "%Y-%m-%d %H:%M:%S") ts = time.mktime(t) _files_touch(filename, ts) diff --git a/share/python/lib/files.yaml b/share/python/lib/files.yaml deleted file mode 100644 index 64a2d14..0000000 --- a/share/python/lib/files.yaml +++ /dev/null @@ -1,133 +0,0 @@ -- given: file {filename} - impl: - python: - function: files_create_from_embedded - types: - filename: file - -- given: file {filename_on_disk} from {embedded_filename} - impl: - python: - function: files_create_from_embedded_with_other_name - types: - embedded_filename: file - -- given: file {filename} has modification time {year}-{month}-{day} {hour}:{minute}:{second} - impl: - python: - function: files_touch_with_timestamp - -- when: I write "(?P.*)" to file (?P\S+) - regex: true - impl: - python: - function: files_create_from_text - -- when: I remember metadata for file {filename} - impl: - python: - function: files_remember_metadata - -- when: I touch file {filename} - impl: - python: - function: files_touch - -- then: file {filename} exists - impl: - python: - function: files_file_exists - -- then: file {filename} does not exist - impl: - python: - function: files_file_does_not_exist - -- then: only files (?P.+) exist - impl: - python: - function: files_only_these_exist - regex: true - -- then: file (?P\S+) contains "(?P.*)" - regex: true - impl: - python: - function: files_file_contains - -- then: file (?P\S+) matches regex /(?P.*)/ - regex: true - impl: - python: - function: files_file_matches_regex - -- then: file (?P\S+) matches regex "(?P.*)" - regex: true - impl: - python: - function: files_file_matches_regex - -- then: files {filename1} and {filename2} match - impl: - python: - function: files_match - -- then: file {filename} has same metadata as before - impl: - python: - function: files_has_remembered_metadata - -- then: file {filename} has different metadata from before - impl: - python: - function: files_has_different_metadata - -- then: file {filename} has changed from before - impl: - python: - function: files_has_different_metadata - -- then: file {filename} has a very recent modification time - impl: - python: - function: files_mtime_is_recent - -- then: file {filename} has a very old modification time - impl: - python: - function: files_mtime_is_ancient - -- given: a directory {path} - impl: - python: - function: files_make_directory - -- when: I create directory {path} - impl: - python: - function: files_make_directory - -- when: I remove directory {path} - impl: - python: - function: files_remove_directory - -- then: directory {path} exists - impl: - python: - function: files_directory_exists - -- then: directory {path} does not exist - impl: - python: - function: files_directory_does_not_exist - -- then: directory {path} is empty - impl: - python: - function: files_directory_is_empty - -- then: directory {path} is not empty - impl: - python: - function: files_directory_is_not_empty diff --git a/share/python/lib/runcmd.md b/share/python/lib/runcmd.md index f28ec2a..f165ad3 100644 --- a/share/python/lib/runcmd.md +++ b/share/python/lib/runcmd.md @@ -206,7 +206,7 @@ template: python bindings: - lib/runcmd.yaml - runcmd_test.yaml -- files.yaml +- lib/files.yaml functions: - runcmd.py - runcmd_test.py diff --git a/share/python/lib/runcmd_test.py b/share/python/lib/runcmd_test.py index 6aa435a..4aa5f49 100644 --- a/share/python/lib/runcmd_test.py +++ b/share/python/lib/runcmd_test.py @@ -8,7 +8,7 @@ def create_script_from_embedded(ctx, filename=None, embedded=None): # Create the file. files_create_from_embedded_with_other_name( - ctx, filename_on_disk=filename, embedded_filename=embedded + ctx, filename_on_disk=filename, embedded_file=embedded ) # Make the new file executable. diff --git a/share/rust/lib/files.yaml b/share/rust/lib/files.yaml deleted file mode 100644 index cabda12..0000000 --- a/share/rust/lib/files.yaml +++ /dev/null @@ -1,139 +0,0 @@ -# Bindings for the files steps -# These bind the files step library for subplotlib - -- given: file {embedded_file} - impl: - rust: - function: subplotlib::steplibrary::files::create_from_embedded - types: - embedded_file: file - -- given: file {filename_on_disk} from {embedded_file} - impl: - rust: - function: subplotlib::steplibrary::files::create_from_embedded_with_other_name - types: - embedded_file: file - -- given: file (?P\S+) has modification time (?P\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) - regex: true - impl: - rust: - function: subplotlib::steplibrary::files::touch_with_timestamp - types: - mtime: text - -- when: I write "(?P.*)" to file (?P\S+) - regex: true - impl: - rust: - function: subplotlib::steplibrary::files::create_from_text - -- when: I remember metadata for file {filename} - impl: - rust: - function: subplotlib::steplibrary::files::remember_metadata - -- when: I touch file {filename} - impl: - rust: - function: subplotlib::steplibrary::files::touch - -- then: file {filename} exists - impl: - rust: - function: subplotlib::steplibrary::files::file_exists - -- then: file {filename} does not exist - impl: - rust: - function: subplotlib::steplibrary::files::file_does_not_exist - -- then: only files (?P.+) exist - impl: - rust: - function: subplotlib::steplibrary::files::only_these_exist - regex: true - -- then: file (?P\S+) contains "(?P.*)" - regex: true - impl: - rust: - function: subplotlib::steplibrary::files::file_contains - -- then: file (?P\S+) matches regex /(?P.*)/ - regex: true - impl: - rust: - function: subplotlib::steplibrary::files::file_matches_regex - -- then: file (?P\S+) matches regex "(?P.*)" - regex: true - impl: - rust: - function: subplotlib::steplibrary::files::file_matches_regex - -- then: files {filename1} and {filename2} match - impl: - rust: - function: subplotlib::steplibrary::files::file_match - -- then: file {filename} has same metadata as before - impl: - rust: - function: subplotlib::steplibrary::files::has_remembered_metadata - -- then: file {filename} has different metadata from before - impl: - rust: - function: subplotlib::steplibrary::files::has_different_metadata - -- then: file {filename} has changed from before - impl: - rust: - function: subplotlib::steplibrary::files::has_different_metadata - -- then: file {filename} has a very recent modification time - impl: - rust: - function: subplotlib::steplibrary::files::mtime_is_recent - -- then: file {filename} has a very old modification time - impl: - rust: - function: subplotlib::steplibrary::files::mtime_is_ancient - -- given: a directory {path} - impl: - rust: - function: subplotlib::steplibrary::files::make_directory - -- when: I create directory {path} - impl: - rust: - function: subplotlib::steplibrary::files::make_directory - -- when: I remove directory {path} - impl: - rust: - function: subplotlib::steplibrary::files::remove_directory - -- then: directory {path} exists - impl: - rust: - function: subplotlib::steplibrary::files::path_exists - -- then: directory {path} does not exist - impl: - rust: - function: subplotlib::steplibrary::files::path_does_not_exist - -- then: directory {path} is empty - impl: - rust: - function: subplotlib::steplibrary::files::path_is_empty - -- then: directory {path} is not empty - impl: - rust: - function: subplotlib::steplibrary::files::path_is_not_empty -- cgit v1.2.1