~gbmor/clinte

2cb85a4f5922be57a43470bde828d8f92138de40 — Ben Morrison 1 year, 1 month ago ec59c5f
testing create/delete
2 files changed, 38 insertions(+), 3 deletions(-)

M src/ed.rs
M src/posts.rs
M src/ed.rs => src/ed.rs +7 -1
@@ 1,10 1,15 @@
use std::env;
use std::fs;

#[cfg(not(test))]
use std::env;
#[cfg(not(test))]
use std::process;

use chrono::prelude::*;

#[cfg(not(test))]
use crate::error;

use crate::user;

fn create_tmp_file() -> Result<String, std::io::Error> {


@@ 16,6 21,7 @@ fn create_tmp_file() -> Result<String, std::io::Error> {
    }
}

#[cfg(not(test))]
pub fn call(body: &str) -> String {
    // If they don't have $EDITOR set, just default to nano
    // instead of assuming vim or emacs.

M src/posts.rs => src/posts.rs +31 -2
@@ 1,10 1,12 @@
use std::io;

use crate::db;
use crate::ed;
use crate::error;
use crate::user;

#[cfg(not(test))]
use crate::ed;

// Make sure nobody encodes narsty characters
// into a message to negatively affect other
// users


@@ 22,7 24,13 @@ pub fn create() -> error::Result<()> {
    println!();
    println!("Title of the new post: ");

    #[cfg(test)]
    let title = String::from("TEST_TITLE");

    #[cfg(not(test))]
    let mut title = String::new();

    #[cfg(not(test))]
    io::stdin().read_line(&mut title)?;

    let title = str_to_utf8(title.trim());


@@ 34,14 42,20 @@ pub fn create() -> error::Result<()> {

    println!();

    #[cfg(not(test))]
    let body_raw = str_to_utf8(&ed::call(""));

    #[cfg(not(test))]
    let body = if body_raw.len() > 500 {
        &body_raw[..500]
    } else {
        &body_raw
    };
    let trimmed_body = body.trim();

    #[cfg(test)]
    let body = String::from("TEST_BODY");

    let trimmed_body = body.trim();
    let user = &*user::NAME;

    let mut all = db::Posts::get_all(db::PATH);


@@ 216,4 230,19 @@ mod tests {
        assert_eq!(post.body, "TEST_BODY");
        fs::rename("clinte_bak.json", db::PATH).unwrap();
    }

    #[test]
    fn test_create_delete() {
        fs::copy(db::PATH, "clinte_bak.json").unwrap();
        create().unwrap();
        let all = db::Posts::get_all(db::PATH);
        let post = all.get(1);

        assert_eq!(post.title, "TEST_TITLE");
        assert_eq!(post.body, "TEST_BODY");

        delete_handler(2).unwrap();

        fs::rename("clinte_bak.json", db::PATH).unwrap();
    }
}