summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Korber <p.korber@1aim.com>2018-11-16 18:04:29 +0100
committerPhilipp Korber <p.korber@1aim.com>2018-11-16 18:07:36 +0100
commit606cbacdc9c10ffb2375974b889cc9bebe3fabc7 (patch)
tree29156af16ed0f3c9faa8d05553336f0cf3cfccc4
parent391cb298b730dd99f0826a3261f5ce572ca293a6 (diff)
feat(core/test-utils) added test utils module
- for enabling this feature wil add a `Context` impl. instance in a lazy_static which can be used for testing (but only for that!)
-rw-r--r--core/Cargo.toml2
-rw-r--r--core/src/default_impl/fs.rs2
-rw-r--r--core/src/lib.rs4
-rw-r--r--core/src/test_utils.rs42
4 files changed, 49 insertions, 1 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 52756c8..3af06d1 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -14,6 +14,7 @@ repository = "https://github.com/1aim/mail-core"
serde-impl = ["serde", "mail-headers/serde-impl"]
default = ["default_impl_cpupool"]
default_impl_cpupool = ["futures-cpupool"]
+test-utils = ["default", "lazy_static"]
[dependencies]
mail-internals = { path="../internals" }
@@ -27,6 +28,7 @@ chrono = "0.4"
soft-ascii-string = "1.0"
serde = { version="1.0", optional=true, features=["derive"] }
checked_command = "0.2.2"
+lazy_static = { version="1.2.0", optional=true }
[dependencies.mime]
git="https://github.com/1aim/mime"
diff --git a/core/src/default_impl/fs.rs b/core/src/default_impl/fs.rs
index d6c3ba6..e60b0ad 100644
--- a/core/src/default_impl/fs.rs
+++ b/core/src/default_impl/fs.rs
@@ -251,7 +251,7 @@ mod tests {
#[test]
fn works_reasonable_for_cargo_files() {
- let res = sniff_media_type("./Cargo.lock")
+ let res = sniff_media_type("./Cargo.toml")
.unwrap();
// it currently doesn't take advantage of file endings so
diff --git a/core/src/lib.rs b/core/src/lib.rs
index f3595f5..f1b1ba7 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -28,6 +28,8 @@ extern crate serde_test;
#[cfg(feature="default_impl_cpupool")]
extern crate futures_cpupool;
+#[cfg(feature="test-utils")]
+extern crate lazy_static;
@@ -43,6 +45,8 @@ mod resource;
mod encode;
mod mail;
pub mod compose;
+#[cfg(feature="test-utils")]
+pub mod test_utils;
pub mod default_impl;
diff --git a/core/src/test_utils.rs b/core/src/test_utils.rs
new file mode 100644
index 0000000..29a1c8f
--- /dev/null
+++ b/core/src/test_utils.rs
@@ -0,0 +1,42 @@
+
+use lazy_static::lazy_static;
+
+use headers::header_components::Domain;
+
+use crate::default_impl::simple_context::{
+ self, Context, ContextSetupError
+};
+
+pub struct CtxHolder {
+ inner: Result<Context, ContextSetupError>
+}
+
+impl CtxHolder {
+
+ pub fn get(&self) -> Result<&Context, &ContextSetupError> {
+ self.inner.as_ref()
+ }
+
+ pub fn unwrap(&self) -> &Context {
+ self.get().unwrap()
+ }
+
+ pub fn expect(&self, msg: &'static str) -> &Context {
+ self.get().expect(msg)
+ }
+}
+
+lazy_static! {
+ /// Provides a instance of a impl. of Context _for unit testing_.
+ ///
+ /// This should never be used in any way in production as it is:
+ /// 1. using `example.com` for the domain of content ids
+ /// 2. has a hard coded "unique" part so content ids are not
+ /// at all guaranteed to be world unique.
+ pub static ref CTX: CtxHolder = {
+ let domain = Domain::from_unchecked("example.com".to_owned());
+ let ascii_unique_part = "xm3r2u".parse().unwrap();
+ let ctx = simple_context::new(domain, ascii_unique_part);
+ CtxHolder { inner: ctx }
+ };
+} \ No newline at end of file