aboutsummaryrefslogtreecommitdiffstats
path: root/crates/config/src/testing.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/config/src/testing.rs')
-rw-r--r--crates/config/src/testing.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/crates/config/src/testing.rs b/crates/config/src/testing.rs
new file mode 100644
index 0000000..8101ee3
--- /dev/null
+++ b/crates/config/src/testing.rs
@@ -0,0 +1,27 @@
+//! What the crate's tests share.
+
+use std::path::PathBuf;
+
+/// A directory of a test's files, removed with it.
+pub(crate) struct Scratch(pub(crate) PathBuf);
+
+impl Scratch {
+ pub(crate) fn new(name: &str) -> Scratch {
+ let dir = std::env::temp_dir().join(format!("config-{name}-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+ Scratch(dir)
+ }
+
+ /// A file holding `content`: its name.
+ pub(crate) fn file(&self, name: &str, content: &str) -> String {
+ std::fs::write(self.0.join(name), content).unwrap();
+ name.to_owned()
+ }
+}
+
+impl Drop for Scratch {
+ fn drop(&mut self) {
+ let _ = std::fs::remove_dir_all(&self.0);
+ }
+}