Skip to content

Commit

Permalink
Bump to v5 (#52)
Browse files Browse the repository at this point in the history
bumps to v5, updates contributor list
  • Loading branch information
conradkleinespel authored Aug 8, 2020
1 parent ce2297c commit c786de1
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 88 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "rpassword"
version = "4.0.5"
version = "5.0.0"
authors = ["Conrad Kleinespel <[email protected]>"]
description = "Read passwords in console applications."
license = "Apache-2.0"
Expand All @@ -15,7 +15,4 @@ keywords = ["read", "password", "security", "pass", "getpass"]
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["std", "winnt", "fileapi", "processenv", "winbase", "handleapi", "consoleapi", "minwindef", "wincon"] }

[features]
enhanced_mock = []
winapi = { version = "0.3", features = ["std", "winnt", "fileapi", "processenv", "winbase", "handleapi", "consoleapi", "minwindef", "wincon"] }
23 changes: 5 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add `rpassword` as a dependency in Cargo.toml:

```toml
[dependencies]
rpassword = "4.0"
rpassword = "5.0"
```

Use `rpassword` within your code:
Expand All @@ -28,7 +28,7 @@ Use `rpassword` within your code:
extern crate rpassword;

fn main() {
// Prompt for a password on TTY (safest but not default for backwards compatibility)
// Prompt for a password on TTY (safest but not always most practical when integrating with other tools or unit testing)
let pass = rpassword::read_password_from_tty(Some("Password: ")).unwrap();
println!("Your password is {}", pass);

Expand All @@ -48,22 +48,6 @@ fn main() {

The full API documentation is available at [https://docs.rs/rpassword](https://docs.rs/rpassword).

## Optional feature

The optional feature **enhanced_mock** can be enabled to change `read_password_with_reader` signature from
```
pub fn read_password_with_reader<T>(source: Option<T>) -> ::std::io::Result<String>
where
T: ::std::io::BufRead
```
to
```
pub fn read_password_with_reader<T>(source: Option<&mut T>) -> ::std::io::Result<String>
where
T: ::std::io::BufRead
```
allowing to call `read_password_with_reader` multiple times with the same reader.

## Contributors

We welcome contribution from everyone. Feel free to open an issue or a pull request at any time.
Expand All @@ -75,10 +59,13 @@ Here's a list of existing `rpassword` contributors:
* [@DaveLancaster](https://github.com/DaveLancaster)
* [@dcuddeback](https://github.com/dcuddeback)
* [@Draphar](https://github.com/Draphar)
* [@dvermd](https://github.com/dvermd)
* [@equalsraf](https://github.com/equalsraf)
* [@Heliozoa](https://github.com/Heliozoa)
* [@JanLikar](https://github.com/JanLikar)
* [@joshuef](https://github.com/joshuef)
* [@longshorej](https://github.com/longshorej)
* [@nicokoch](https://github.com/nicokoch)
* [@petevine](https://github.com/petevine)
* [@psych0d0g](https://github.com/psych0d0g)
* [@retep998](https://github.com/retep998)
Expand Down
55 changes: 2 additions & 53 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,55 +237,7 @@ use unix::{read_password_from_stdin, display_on_tty};
use windows::{read_password_from_stdin, display_on_tty};

/// Reads a password from anything that implements BufRead
#[cfg(not(feature = "enhanced_mock"))]
mod legacy_mock {
use super::*;

/// Reads a password from STDIN
pub fn read_password() -> ::std::io::Result<String> {
read_password_with_reader(None::<::std::io::Empty>)
}

/// Reads a password from anything that implements BufRead
pub fn read_password_with_reader<T>(source: Option<T>) -> ::std::io::Result<String>
where
T: ::std::io::BufRead,
{
match source {
Some(mut reader) => {
let mut password = ZeroOnDrop::new();
reader.read_line(&mut password)?;
fixes_newline(&mut password);
Ok(password.into_inner())
},
None => read_password_from_stdin(false),
}
}

#[cfg(test)]
mod tests {
use std::io::Cursor;

fn mock_input_crlf() -> Cursor<&'static [u8]> {
Cursor::new(&b"A mocked response.\r\n\r\n"[..])
}

fn mock_input_lf() -> Cursor<&'static [u8]> {
Cursor::new(&b"A mocked response.\n"[..])
}

#[test]
fn can_read_from_redirected_input() {
let response = ::read_password_with_reader(Some(mock_input_crlf())).unwrap();
assert_eq!(response, "A mocked response.");
let response = ::read_password_with_reader(Some(mock_input_lf())).unwrap();
assert_eq!(response, "A mocked response.");
}
}
}

#[cfg(feature = "enhanced_mock")]
mod enhanced_mock {
mod mock {
use super::*;

/// Reads a password from STDIN
Expand Down Expand Up @@ -342,10 +294,7 @@ mod enhanced_mock {
}
}

#[cfg(feature = "enhanced_mock")]
pub use enhanced_mock::{read_password, read_password_with_reader};
#[cfg(not(feature = "enhanced_mock"))]
pub use legacy_mock::{read_password, read_password_with_reader};
pub use mock::{read_password, read_password_with_reader};

/// Reads a password from the terminal
pub fn read_password_from_tty(prompt: Option<&str>)
Expand Down
12 changes: 0 additions & 12 deletions tests/no-terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@ fn mock_input_lf() -> Cursor<&'static [u8]> {
Cursor::new(&b"A mocked response.\nAnother mocked response.\n"[..])
}

#[cfg(not(feature = "enhanced_mock"))]
#[test]
fn can_read_from_redirected_input() {
close_stdin();

let response = ::read_password_with_reader(Some(mock_input_crlf())).unwrap();
assert_eq!(response, "A mocked response.");
let response = ::read_password_with_reader(Some(mock_input_lf())).unwrap();
assert_eq!(response, "A mocked response.");
}

#[cfg(feature = "enhanced_mock")]
#[test]
fn can_read_from_redirected_input_many_times() {
close_stdin();
Expand Down

0 comments on commit c786de1

Please sign in to comment.