summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorVincent Breitmoser <look@my.amazin.horse>2019-01-20 13:45:52 +0100
committerVincent Breitmoser <look@my.amazin.horse>2019-01-20 14:07:24 +0100
commit82a6b730f657f6f66a6bd8caae0c87ae28dea558 (patch)
treebb53acaccef06a1f87b38a40d7f240408c870f99 /src/utils
parent7e84f2e6db1028f60193ecd40ee935fe629902cb (diff)
introduce stdioutils, move test fixtures over there
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/fileutil.rs34
-rw-r--r--src/utils/mod.rs2
-rw-r--r--src/utils/stdioutils.rs124
3 files changed, 125 insertions, 35 deletions
diff --git a/src/utils/fileutil.rs b/src/utils/fileutil.rs
index fca810e..ab90a55 100644
--- a/src/utils/fileutil.rs
+++ b/src/utils/fileutil.rs
@@ -53,33 +53,6 @@ pub fn read_lines_from_file(filepath: &Path) -> io::Result<impl Iterator<Item =
lines.map(|result| result.into_iter())
}
-pub fn read_single_char(mut source: impl BufRead) -> io::Result<char> {
- let mut buf = String::new();
- source.read_line(&mut buf)?;
-
- buf.chars().next().ok_or_else(|| io::Error::new(io::ErrorKind::Other, "calendar has no path"))
-}
-
-pub fn read_single_char_from_stdin() -> io::Result<char> {
- let stdin = std::io::stdin();
- let stdinlock = stdin.lock();
- read_single_char(stdinlock)
-}
-
-#[cfg(not(test))]
-pub fn read_lines_from_stdin() -> io::Result<Vec<String>> {
- let stdin = io::stdin();
- let lines = stdin.lock().lines();
- lines.collect()
-}
-
-#[cfg(test)]
-pub fn read_lines_from_stdin() -> io::Result<Vec<String>> {
- use testutils;
- let lines = testutils::test_stdin_clear();
- Ok(lines)
-}
-
pub fn read_file_to_string(path: &Path) -> io::Result<String> {
let mut file = fs::File::open(&path)?;
let mut contents = String::new();
@@ -117,11 +90,4 @@ mod tests {
write_file(file.path(), "z\n").unwrap();
file.assert("z\n");
}
-
- #[test]
- fn read_single_char_test() {
- let source = "ab".as_bytes();
- let read_char = read_single_char(source).unwrap();
- assert_eq!('a', read_char);
- }
}
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 8244c95..b4b17e2 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -2,4 +2,4 @@ pub mod dateutil;
pub mod lock;
pub mod fileutil;
pub mod misc;
-
+pub mod stdioutils;
diff --git a/src/utils/stdioutils.rs b/src/utils/stdioutils.rs
new file mode 100644
index 0000000..963cdc0
--- /dev/null
+++ b/src/utils/stdioutils.rs
@@ -0,0 +1,124 @@
+use std::io;
+use std::io::BufRead;
+
+pub fn read_single_char_from_stdin() -> io::Result<char> {
+ let stdin = io::stdin();
+ let stdinlock = stdin.lock();
+ read_single_char(stdinlock)
+}
+
+pub fn read_single_char(mut source: impl BufRead) -> io::Result<char> {
+ let mut buf = String::new();
+ source.read_line(&mut buf)?;
+
+ buf.chars().next().ok_or_else(|| io::Error::new(io::ErrorKind::Other, "calendar has no path"))
+}
+
+#[cfg(not(test))]
+pub use self::production::*;
+#[cfg(test)]
+pub use self::test::*;
+#[cfg(test)]
+pub use self::fixtures::*;
+
+#[cfg(not(test))]
+mod production {
+ use super::*;
+
+ pub fn read_lines_from_stdin() -> io::Result<Vec<String>> {
+ let stdin = io::stdin();
+ let lines = stdin.lock().lines();
+ lines.collect()
+ }
+
+ pub fn is_stdin_tty() -> bool {
+ atty::is(atty::Stream::Stdin)
+ }
+
+ pub fn is_stdout_tty() -> bool {
+ atty::is(atty::Stream::Stdout)
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ pub fn read_lines_from_stdin() -> io::Result<Vec<String>> {
+ let lines = fixtures::test_stdin_clear();
+ Ok(lines)
+ }
+
+ pub fn is_stdin_tty() -> bool {
+ fixtures::test_stdin_is_tty()
+ }
+
+ pub fn is_stdout_tty() -> bool {
+ fixtures::test_stdout_is_tty()
+ }
+
+}
+
+#[cfg(test)]
+pub mod fixtures {
+ use std::cell::RefCell;
+ thread_local! {
+ pub static STDOUT_BUF: RefCell<String> = RefCell::new(String::new());
+ pub static STDIN_BUF: RefCell<String> = RefCell::new(String::new());
+ pub static STDIN_TTY: RefCell<bool> = RefCell::new(true);
+ pub static STDOUT_TTY: RefCell<bool> = RefCell::new(true);
+ }
+
+ pub fn test_stdout_write(line: &str) {
+ STDOUT_BUF.with(|cell| cell.borrow_mut().push_str(&line));
+ }
+
+ pub fn test_stdout_clear() -> String {
+ STDOUT_BUF.with(|cell| {
+ let result = cell.borrow().clone();
+ *cell.borrow_mut() = String::new();
+ result
+ })
+ }
+
+ pub fn test_stdout_set_tty(istty: bool) {
+ STDOUT_TTY.with(|cell| { *cell.borrow_mut() = istty } );
+ }
+
+ pub fn test_stdout_is_tty() -> bool {
+ STDOUT_TTY.with(|cell| { *cell.borrow() } )
+ }
+
+ pub fn test_stdin_write(text: &str) {
+ test_stdin_set_tty(false);
+ STDIN_BUF.with(|cell| cell.borrow_mut().push_str(&text));
+ }
+
+ pub fn test_stdin_clear() -> Vec<String> {
+ STDIN_BUF.with(|cell| {
+ let result = cell.borrow().lines().map(|line| line.to_owned()).collect();
+ *cell.borrow_mut() = String::new();
+ result
+ })
+ }
+
+ pub fn test_stdin_set_tty(istty: bool) {
+ STDIN_TTY.with(|cell| { *cell.borrow_mut() = istty } );
+ }
+
+ pub fn test_stdin_is_tty() -> bool {
+ STDIN_TTY.with(|cell| { *cell.borrow() } )
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn read_single_char_test() {
+ let source = "ab".as_bytes();
+ let read_char = read_single_char(source).unwrap();
+ assert_eq!('a', read_char);
+ }
+}