Skip to content

Commit

Permalink
fix(all): Fix doc tests in website
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset committed Dec 9, 2019
1 parent a90b548 commit e51c53b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ webpki = { version = "^0.21", optional = true }
criterion = "^0.3"
env_logger = "^0.7"
glob = "^0.3"
walkdir = "^2"

[[bench]]
harness = false
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ lettre = "0.9"
```rust,no_run
extern crate lettre;
use lettre::{SmtpClient, Transport, Email, mime::TEXT_PLAIN};
use lettre::{SmtpClient, Transport, Email, builder::mime::TEXT_PLAIN};
use std::path::Path;
fn main() {
Expand Down
1 change: 1 addition & 0 deletions src/builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{error::Error as LettreError, EmailAddress, Envelope, SendableEmail};
pub use email::{Address, Header, Mailbox, MimeMessage, MimeMultipartType};
use error::Error;
pub use mime;
use mime::Mime;
use std::ffi::OsStr;
use std::fs;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub mod sendmail;
pub mod smtp;
pub mod stub;

#[cfg(feature = "builder")]
pub use crate::builder::Email;
use crate::error::EmailResult;
use crate::error::Error;
#[cfg(feature = "file-transport")]
Expand Down
12 changes: 12 additions & 0 deletions src/smtp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use crate::{SendableEmail, Transport};
use log::{debug, info};
#[cfg(feature = "native-tls")]
use native_tls::TlsConnector;
#[cfg(feature = "rustls")]
use rustls::ClientConfig;
use std::net::{SocketAddr, ToSocketAddrs};
use std::time::Duration;

Expand Down Expand Up @@ -150,6 +152,16 @@ impl SmtpClient {
)
}

#[cfg(feature = "rustls")]
pub fn new_simple(domain: &str) -> Result<SmtpClient, Error> {
let tls_parameters = ClientTlsParameters::new(domain.to_string(), ClientConfig::new());

SmtpClient::new(
(domain, SUBMISSIONS_PORT),
ClientSecurity::Wrapper(tls_parameters),
)
}

/// Creates a new local SMTP client to port 25
pub fn new_unencrypted_localhost() -> Result<SmtpClient, Error> {
SmtpClient::new(("localhost", SMTP_PORT), ClientSecurity::None)
Expand Down
26 changes: 12 additions & 14 deletions tests/skeptic.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
use glob::glob;
use std::env;
use std::env::consts::EXE_EXTENSION;
use std::env::{self, consts::EXE_EXTENSION};
use std::path::Path;
use std::process::Command;
use walkdir::WalkDir;

#[test]
fn book_test() {
let mut book_path = env::current_dir().unwrap();
book_path.push(
Path::new(file!())
.parent()
.unwrap()
.parent()
.unwrap()
.join("../website/content/sending-messages"),
); // For some reasons, calling .parent() once more gives us None...
skeptic_test(Path::new("README.md"));

for md in glob(&format!("{}/*.md", book_path.to_str().unwrap())).unwrap() {
skeptic_test(&md.unwrap());
for entry in WalkDir::new("website").into_iter().filter(|e| {
e.as_ref()
.unwrap()
.path()
.extension()
.map(|ex| ex == "md")
.unwrap_or(false)
}) {
skeptic_test(entry.unwrap().path());
}
}

Expand Down
6 changes: 3 additions & 3 deletions website/src/creating-messages/email.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ The `email` part builds email messages. For now, it does not support attachments
An email is built using an `EmailBuilder`. The simplest email could be:

```rust
extern crate lettre_email;
extern crate lettre;

use lettre_email::Email;
use lettre::Email;

fn main() {
// Create an email
Expand All @@ -22,7 +22,7 @@ fn main() {
.subject("Hi, Hello world")
.alternative("<h2>Hi, Hello world.</h2>", "Hi, Hello world.")
.build();

assert!(email.is_ok());
}
```
Expand Down
17 changes: 10 additions & 7 deletions website/src/sending-messages/smtp.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ fn main() {
"id".to_string(),
"Hello world".to_string().into_bytes(),
);
// Open a local connection on port 25
let mut mailer =
SmtpClient::new_unencrypted_localhost().unwrap().transport();
// Send the email
let result = mailer.send(email);
assert!(result.is_ok());
}
```
Expand All @@ -61,7 +61,7 @@ fn main() {
"id1".to_string(),
"Hello world".to_string().into_bytes(),
);
let email_2 = SendableEmail::new(
Envelope::new(
Some(EmailAddress::new("user@localhost".to_string()).unwrap()),
Expand All @@ -70,7 +70,7 @@ fn main() {
"id2".to_string(),
"Hello world a second time".to_string().into_bytes(),
);
// Connect to a remote server on a custom port
let mut mailer = SmtpClient::new_simple("server.tld").unwrap()
// Set the name sent during EHLO/HELO, default is `localhost`
Expand All @@ -83,14 +83,14 @@ fn main() {
.authentication_mechanism(Mechanism::Plain)
// Enable connection reuse
.connection_reuse(ConnectionReuseParameters::ReuseUnlimited).transport();
let result_1 = mailer.send(email_1);
assert!(result_1.is_ok());
// The second email will use the same connection
let result_2 = mailer.send(email_2);
assert!(result_2.is_ok());
// Explicitly close the SMTP transaction as we enabled connection reuse
mailer.close();
}
Expand All @@ -99,6 +99,8 @@ fn main() {
You can specify custom TLS settings:

```rust,no_run
# #[cfg(feature = "native-tls")]
# {
extern crate native_tls;
extern crate lettre;
Expand Down Expand Up @@ -144,6 +146,7 @@ fn main() {
mailer.close();
}
# }
```

#### Lower level
Expand Down

0 comments on commit e51c53b

Please sign in to comment.