summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-03-24 07:41:47 +0200
committerLars Wirzenius <liw@liw.fi>2022-03-25 07:22:50 +0200
commit2937d00329ec9fbd632ffdd9265183919172905d (patch)
treed539bc85ed4b794b96ada0c869a06590b206545b
parent0b7adc238746b3d4c974775a8b13fbb81cb2eda9 (diff)
downloadsubplot-2937d00329ec9fbd632ffdd9265183919172905d.tar.gz
feat: add "file doesn't contain" step to lib/files, Python and Rust
Sponsored-by: author
-rw-r--r--share/common/lib/files.yaml8
-rw-r--r--share/python/lib/files.py8
-rw-r--r--subplotlib/src/steplibrary/files.rs15
-rw-r--r--tests/subplots/common/files.md1
4 files changed, 32 insertions, 0 deletions
diff --git a/share/common/lib/files.yaml b/share/common/lib/files.yaml
index cf85cfa..e4d9b6b 100644
--- a/share/common/lib/files.yaml
+++ b/share/common/lib/files.yaml
@@ -174,6 +174,14 @@
python:
function: files_file_contains
+- then: file (?P<filename>\S+) doesn't contain "(?P<data>.*)"
+ regex: true
+ impl:
+ rust:
+ function: subplotlib::steplibrary::files::file_doesnt_contain
+ python:
+ function: files_file_doesnt_contain
+
- then: file (?P<filename>\S+) matches regex /(?P<regex>.*)/
regex: true
impl:
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, .*/
~~~