diff options
author | Daniel Silverstone <dsilvers+gitlab@digital-scurf.org> | 2022-03-26 09:19:36 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers+gitlab@digital-scurf.org> | 2022-03-26 09:19:36 +0000 |
commit | fab67b9481a8df42656b1bb0777d818bb350bd17 (patch) | |
tree | d539bc85ed4b794b96ada0c869a06590b206545b | |
parent | 641a1cddce0fd7bdbb9416bb2f0bbfa55048620f (diff) | |
parent | 2937d00329ec9fbd632ffdd9265183919172905d (diff) | |
download | subplot-fab67b9481a8df42656b1bb0777d818bb350bd17.tar.gz |
Merge branch 'liw/files' into 'main'
add "file doesn't contain" step to lib/files
Closes #266
See merge request subplot/subplot!261
-rw-r--r-- | share/common/lib/files.yaml | 140 | ||||
-rw-r--r-- | share/python/lib/files.py | 8 | ||||
-rw-r--r-- | subplotlib/src/steplibrary/files.rs | 15 | ||||
-rw-r--r-- | tests/subplots/common/files.md | 1 |
4 files changed, 103 insertions, 61 deletions
diff --git a/share/common/lib/files.yaml b/share/common/lib/files.yaml index 40c092a..e4d9b6b 100644 --- a/share/common/lib/files.yaml +++ b/share/common/lib/files.yaml @@ -1,4 +1,6 @@ -# Bindings for the files steps +# Bindings for the lib/files step library + +# Create or modify files. - given: file {embedded_file} impl: @@ -18,90 +20,90 @@ types: embedded_file: file -- given: file (?P<filename>\S+) has modification time (?P<mtime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) +- when: I write "(?P<text>.*)" to file (?P<filename>\S+) regex: true impl: rust: - function: subplotlib::steplibrary::files::touch_with_timestamp + function: subplotlib::steplibrary::files::create_from_text python: - function: files_touch_with_timestamp - types: - mtime: text + function: files_create_from_text -- when: I write "(?P<text>.*)" to file (?P<filename>\S+) - regex: true +# Manage directories (distinct from files). + +- given: a directory {path} impl: rust: - function: subplotlib::steplibrary::files::create_from_text + function: subplotlib::steplibrary::files::make_directory python: - function: files_create_from_text + function: files_make_directory -- when: I remember metadata for file {filename} +- when: I create directory {path} impl: rust: - function: subplotlib::steplibrary::files::remember_metadata + function: subplotlib::steplibrary::files::make_directory python: - function: files_remember_metadata + function: files_make_directory -- when: I touch file {filename} +- when: I remove directory {path} impl: rust: - function: subplotlib::steplibrary::files::touch + function: subplotlib::steplibrary::files::remove_directory python: - function: files_touch + function: files_remove_directory -- then: file {filename} exists +- then: directory {path} exists impl: rust: - function: subplotlib::steplibrary::files::file_exists + function: subplotlib::steplibrary::files::path_exists python: - function: files_file_exists + function: files_directory_exists -- then: file {filename} does not exist +- then: directory {path} does not exist impl: rust: - function: subplotlib::steplibrary::files::file_does_not_exist + function: subplotlib::steplibrary::files::path_does_not_exist python: - function: files_file_does_not_exist + function: files_directory_does_not_exist -- then: only files (?P<filenames>.+) exist +- then: directory {path} is empty impl: rust: - function: subplotlib::steplibrary::files::only_these_exist + function: subplotlib::steplibrary::files::path_is_empty python: - function: files_only_these_exist - regex: true + function: files_directory_is_empty -- then: file (?P<filename>\S+) contains "(?P<data>.*)" - regex: true +- then: directory {path} is not empty impl: rust: - function: subplotlib::steplibrary::files::file_contains + function: subplotlib::steplibrary::files::path_is_not_empty python: - function: files_file_contains + function: files_directory_is_not_empty -- then: file (?P<filename>\S+) matches regex /(?P<regex>.*)/ +# File metadata management and testing. + +- given: file (?P<filename>\S+) has modification time (?P<mtime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) regex: true impl: rust: - function: subplotlib::steplibrary::files::file_matches_regex + function: subplotlib::steplibrary::files::touch_with_timestamp python: - function: files_file_matches_regex + function: files_touch_with_timestamp + types: + mtime: text -- then: file (?P<filename>\S+) matches regex "(?P<regex>.*)" - regex: true +- when: I remember metadata for file {filename} impl: rust: - function: subplotlib::steplibrary::files::file_matches_regex + function: subplotlib::steplibrary::files::remember_metadata python: - function: files_file_matches_regex + function: files_remember_metadata -- then: files {filename1} and {filename2} match +- when: I touch file {filename} impl: rust: - function: subplotlib::steplibrary::files::file_match + function: subplotlib::steplibrary::files::touch python: - function: files_match + function: files_touch - then: file {filename} has same metadata as before impl: @@ -138,51 +140,67 @@ python: function: files_mtime_is_ancient -- given: a directory {path} +# Testing file existence. + +- then: file {filename} exists impl: rust: - function: subplotlib::steplibrary::files::make_directory + function: subplotlib::steplibrary::files::file_exists python: - function: files_make_directory + function: files_file_exists -- when: I create directory {path} +- then: file {filename} does not exist impl: rust: - function: subplotlib::steplibrary::files::make_directory + function: subplotlib::steplibrary::files::file_does_not_exist python: - function: files_make_directory + function: files_file_does_not_exist -- when: I remove directory {path} +- then: only files (?P<filenames>.+) exist impl: rust: - function: subplotlib::steplibrary::files::remove_directory + function: subplotlib::steplibrary::files::only_these_exist python: - function: files_remove_directory + function: files_only_these_exist + regex: true -- then: directory {path} exists +# Tests on file content. + +- then: file (?P<filename>\S+) contains "(?P<data>.*)" + regex: true impl: rust: - function: subplotlib::steplibrary::files::path_exists + function: subplotlib::steplibrary::files::file_contains python: - function: files_directory_exists + function: files_file_contains -- then: directory {path} does not exist +- then: file (?P<filename>\S+) doesn't contain "(?P<data>.*)" + regex: true impl: rust: - function: subplotlib::steplibrary::files::path_does_not_exist + function: subplotlib::steplibrary::files::file_doesnt_contain python: - function: files_directory_does_not_exist + function: files_file_doesnt_contain -- then: directory {path} is empty +- then: file (?P<filename>\S+) matches regex /(?P<regex>.*)/ + regex: true impl: rust: - function: subplotlib::steplibrary::files::path_is_empty + function: subplotlib::steplibrary::files::file_matches_regex python: - function: files_directory_is_empty + function: files_file_matches_regex -- then: directory {path} is not empty +- then: file (?P<filename>\S+) matches regex "(?P<regex>.*)" + regex: true impl: rust: - function: subplotlib::steplibrary::files::path_is_not_empty + function: subplotlib::steplibrary::files::file_matches_regex python: - function: files_directory_is_not_empty + function: files_file_matches_regex + +- then: files {filename1} and {filename2} match + impl: + rust: + function: subplotlib::steplibrary::files::file_match + python: + function: files_match diff --git a/share/python/lib/files.py b/share/python/lib/files.py index fe94ed2..3ea5877 100644 --- a/share/python/lib/files.py +++ b/share/python/lib/files.py @@ -85,6 +85,14 @@ def files_file_contains(ctx, filename=None, data=None): assert_eq(data in actual, True) +def files_file_doesnt_contain(ctx, filename=None, data=None): + assert_eq = globals()["assert_eq"] + with open(filename, "rb") as f: + actual = f.read() + actual = actual.decode("UTF-8") + assert_eq(data in actual, False) + + def files_file_matches_regex(ctx, filename=None, regex=None): assert_eq = globals()["assert_eq"] with open(filename) as f: diff --git a/subplotlib/src/steplibrary/files.rs b/subplotlib/src/steplibrary/files.rs index fd84e1c..b94155f 100644 --- a/subplotlib/src/steplibrary/files.rs +++ b/subplotlib/src/steplibrary/files.rs @@ -232,6 +232,21 @@ pub fn file_contains(context: &Datadir, filename: &str, data: &str) { } } +/// Check if a file lacks a given sequence of characters +/// +/// # `then file (?P<filename>\S+) does not contain "(?P<data>.*)"` +/// +/// This will load the content of the named file and ensure it lacks the given string. +/// Note: this assumes everything is utf-8 encoded. If not, things will fail. +#[step] +pub fn file_doesnt_contain(context: &Datadir, filename: &str, data: &str) { + let full_path = context.canonicalise_filename(filename)?; + let body = fs::read_to_string(full_path)?; + if body.contains(data) { + throw!("unexpected file content found"); + } +} + /// Check if a file's content matches the given regular expression /// /// # `then file (?P<filename>\S+) matches regex /(?P<regex>.*)/` diff --git a/tests/subplots/common/files.md b/tests/subplots/common/files.md index 13d9874..d8d598b 100644 --- a/tests/subplots/common/files.md +++ b/tests/subplots/common/files.md @@ -67,6 +67,7 @@ These steps verify contents of files. ~~~scenario given file hello.txt then file hello.txt contains "hello, world" +then file hello.txt doesn't contain "hello, sailor" and file hello.txt matches regex "hello, .*" and file hello.txt matches regex /hello, .*/ ~~~ |