summaryrefslogtreecommitdiffstats
path: root/src/actions/seq.rs
blob: 7cdfe90b29c200010e49184142e7564a1a1d9d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::seqfile;
use crate::utils::stdioutils;
use crate::KhResult;

pub fn action_seq() -> KhResult<()> {
  if !stdioutils::is_stdin_tty() {
    write_stdin_to_seqfile()?;
  } else {
    //println!("stdin is tty")
  }

  if !stdioutils::is_stdout_tty() || stdioutils::is_stdin_tty() {
    write_seqfile_to_stdout();
  }

  Ok(())
}

fn write_stdin_to_seqfile() -> KhResult<()> {
  let mut lines = stdioutils::read_lines_from_stdin()?.join("\n");
  lines.push_str("\n");

  seqfile::write_to_seqfile(&lines)?;

  Ok(())
}

fn write_seqfile_to_stdout() {
  if let Ok(sequence) = seqfile::read_seqfile() {
    for line in sequence {
      khprintln!("{}", line);
    }
  }
}

#[cfg(test)]
mod integration {
  use super::*;

  use assert_fs::prelude::*;
  use predicates::prelude::*;
  use crate::testutils;
  use crate::utils::stdioutils;

  #[test]
  fn test_with_stdin() {
    let testdir = testutils::prepare_testdir_empty();
    stdioutils::test_stdin_write("hi\nthere");

    action_seq().unwrap();

    testdir.child(".khaleesi/seq").assert("hi\nthere\n");
  }

  #[test]
  fn test_no_stdin() {
    let testdir = testutils::prepare_testdir("testdir_with_seq");

    action_seq().unwrap();
    let out = stdioutils::test_stdout_clear();

    let predicate = predicate::str::similar(out);
    testdir.child(".khaleesi/seq").assert(predicate);
  }

  #[test]
  fn test_with_stdin_stdout() {
    let testdir = testutils::prepare_testdir_empty();
    stdioutils::test_stdin_write("hi\nthere");
    stdioutils::test_stdout_set_tty(false);

    action_seq().unwrap();
    let out = stdioutils::test_stdout_clear();

    testdir.child(".khaleesi/seq").assert("hi\nthere\n");
    assert_eq!("hi\nthere\n", out);
  }
}