summaryrefslogtreecommitdiff
path: root/subplotlib
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2020-12-28 15:51:10 +0000
committerDaniel Silverstone <dsilvers@digital-scurf.org>2020-12-28 15:51:10 +0000
commit780d4c3e04e3884abb8250f0f86ebd28013ac592 (patch)
tree73ab6d9a669ed4532b018f8d7fd1ce7428fc7804 /subplotlib
parent0dbd69a89552874e24473330f300cc2e10012f23 (diff)
downloadsubplot-780d4c3e04e3884abb8250f0f86ebd28013ac592.tar.gz
subplotlib: Trivial datadir support
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'subplotlib')
-rw-r--r--subplotlib/Cargo.toml2
-rw-r--r--subplotlib/src/lib.rs1
-rw-r--r--subplotlib/src/steplibrary.rs20
-rw-r--r--subplotlib/src/steplibrary/datadir.rs85
-rw-r--r--subplotlib/steplibrary/datadir.yaml13
-rw-r--r--subplotlib/subplotlib.md11
-rw-r--r--subplotlib/tests/subplotlib.rs21
7 files changed, 153 insertions, 0 deletions
diff --git a/subplotlib/Cargo.toml b/subplotlib/Cargo.toml
index 2e6c306..e99c7e0 100644
--- a/subplotlib/Cargo.toml
+++ b/subplotlib/Cargo.toml
@@ -11,3 +11,5 @@ subplotlib-derive = { version="0.1", path = "../subplotlib-derive" }
lazy_static = "1"
base64 = "0.13"
state = "0.4"
+tempfile = "3.1"
+fs2 = "0.4" \ No newline at end of file
diff --git a/subplotlib/src/lib.rs b/subplotlib/src/lib.rs
index 8c31672..ecf512c 100644
--- a/subplotlib/src/lib.rs
+++ b/subplotlib/src/lib.rs
@@ -14,5 +14,6 @@ pub mod file;
pub mod prelude;
pub mod scenario;
pub mod step;
+pub mod steplibrary;
pub mod types;
pub mod utils;
diff --git a/subplotlib/src/steplibrary.rs b/subplotlib/src/steplibrary.rs
new file mode 100644
index 0000000..3466bd8
--- /dev/null
+++ b/subplotlib/src/steplibrary.rs
@@ -0,0 +1,20 @@
+//! Subplot step library
+//!
+//! In the Python and shell runners, there are some libraries to help write
+//! subplots which provide things like assistance with stored files, checking of
+//! file content, running of subprocesses, managing of directories of data, etc.
+//!
+//! These submodules contain steps to that effect. In order to use them you
+//! will want to:
+//!
+//!
+//! ```
+//! use subplotlib::steplibrary::datadir::*;
+//! ```
+//!
+//! or similar.
+//!
+//! Then you can simply use the bindings for the library in question from
+//! requisite yaml files.
+
+pub mod datadir;
diff --git a/subplotlib/src/steplibrary/datadir.rs b/subplotlib/src/steplibrary/datadir.rs
new file mode 100644
index 0000000..d727bcc
--- /dev/null
+++ b/subplotlib/src/steplibrary/datadir.rs
@@ -0,0 +1,85 @@
+//! The datadir step library provides a small number of steps which can be used
+//! in your scenarios, though its primary use is to provide the Datadir context
+//! object which other libraries will use to provide filesystem access.
+//!
+//! If you want to create files, run commands, etc. from your scenarios, you
+//! will likely be using them from within the datadir.
+
+use std::path::Path;
+
+pub use crate::prelude::*;
+
+pub struct Datadir {
+ inner: Option<DatadirInner>,
+}
+
+pub struct DatadirInner {
+ base: tempfile::TempDir,
+}
+
+impl Default for Datadir {
+ fn default() -> Self {
+ Self { inner: None }
+ }
+}
+
+impl ContextElement for Datadir {
+ fn created(&mut self, scenario: &Scenario) {
+ assert!(self.inner.is_none());
+ self.inner = DatadirInner::build(scenario.title());
+ }
+
+ // TODO: Support saving datadir between steps
+}
+
+impl DatadirInner {
+ fn build(title: &str) -> Option<Self> {
+ let safe_title: String = title
+ .chars()
+ .map(|c| match c {
+ 'a'..='z' | 'A'..='Z' | '0'..='9' => c,
+ _ => '-',
+ })
+ .collect();
+ let base = tempfile::Builder::new()
+ .prefix("subplot")
+ .suffix(&safe_title)
+ .rand_bytes(5)
+ .tempdir()
+ .ok()?;
+
+ Some(Self { base })
+ }
+}
+
+impl Datadir {
+ fn inner(&self) -> &DatadirInner {
+ self.inner
+ .as_ref()
+ .expect("Attempted to access Datadir too early (or late)")
+ }
+
+ /// Retrieve the base data directory path which can be used to store
+ /// files etc. for this step.
+ pub fn base_path(&self) -> &Path {
+ self.inner().base.path()
+ }
+}
+
+/// A simple check for enough disk space in the data dir
+#[step]
+pub fn datadir_has_enough_space(datadir: &Datadir, bytes: u64) {
+ let available = fs2::available_space(datadir.base_path())?;
+ if available < bytes {
+ throw!(format!(
+ "Available space check failed, wanted {} bytes, but only {} were available",
+ bytes, available
+ ));
+ }
+}
+
+/// A convenience step for enough disk space in the data dir in megabytes
+#[step]
+pub fn datadir_has_enough_space_megabytes(context: &ScenarioContext, megabytes: u64) {
+ datadir_has_enough_space::call(context, megabytes * 1024 * 1024)
+}
diff --git a/subplotlib/steplibrary/datadir.yaml b/subplotlib/steplibrary/datadir.yaml
new file mode 100644
index 0000000..acd4ad4
--- /dev/null
+++ b/subplotlib/steplibrary/datadir.yaml
@@ -0,0 +1,13 @@
+# Bindings for the datadir steps
+# These steps are pretty simplistic since Datadir is mostly
+# a utility context for use by other step libraries, however some
+# of the capabilities are worth exporting as steps
+
+- given: datadir has at least {bytes}B of space
+ function: subplotlib::steplibrary::datadir::datadir_has_enough_space
+ types:
+ bytes: uint
+- given: datadir has at least {megabytes}M of space
+ function: subplotlib::steplibrary::datadir::datadir_has_enough_space_megabytes
+ types:
+ megabytes: uint
diff --git a/subplotlib/subplotlib.md b/subplotlib/subplotlib.md
index 8cb63b9..4d59398 100644
--- a/subplotlib/subplotlib.md
+++ b/subplotlib/subplotlib.md
@@ -3,6 +3,7 @@ title: Testing the Rust crate "subplotlib"
template: rust
bindings:
- subplotlib.yaml
+ - steplibrary/datadir.yaml
functions:
- helpers/subplotlib_context.rs
- helpers/subplotlib_impl.rs
@@ -43,3 +44,13 @@ given I have read the file example.txt into EXAMPLE
when I look at EXAMPLE
then I see "will be embedded"
```
+
+# Data directory
+
+There is a data directory, and for now this test will fail if there's not
+at least 1 megabyte of space in the data directory
+
+```scenario
+given datadir has at least 1024000B of space
+and datadir has at least 1M of space
+```
diff --git a/subplotlib/tests/subplotlib.rs b/subplotlib/tests/subplotlib.rs
index a8a9695..174f5a1 100644
--- a/subplotlib/tests/subplotlib.rs
+++ b/subplotlib/tests/subplotlib.rs
@@ -170,3 +170,24 @@ fn embedded_files() {
scenario.run().unwrap();
}
+
+// ---------------------------------
+
+// Data directory
+#[test]
+fn data_directory() {
+ let mut scenario = Scenario::new(&base64_decode("RGF0YSBkaXJlY3Rvcnk="));
+
+ let step = subplotlib::steplibrary::datadir::datadir_has_enough_space::Builder::default()
+ .bytes(1024000)
+ .build();
+ scenario.add_step(step, None);
+
+ let step =
+ subplotlib::steplibrary::datadir::datadir_has_enough_space_megabytes::Builder::default()
+ .megabytes(1)
+ .build();
+ scenario.add_step(step, None);
+
+ scenario.run().unwrap();
+}