mod create {
mod when_file_exists {
mod when_not_truncate {
use std::fs::{self,File};
use exists;
use file::create;
#[test]
fn it_returns_ok_with_none() {
let path = "/tmp/no_truncate.txt";
let file = File::create(path);
assert!(file.is_ok());
assert!(exists(path));
let file = create(path, false);
assert!(file.is_ok());
assert!(file.unwrap().is_none());
assert!(fs::remove_file(path).is_ok());
}
}
mod when_truncate {
use std::fs::{self,File};
use exists;
use file::create;
#[test]
fn it_returns_ok_with_file() {
let path = "/tmp/truncate.txt";
let file = File::create(path);
assert!(file.is_ok());
assert!(exists(path));
let file = create(path, true);
assert!(file.is_ok());
assert!(file.unwrap().is_some());
assert!(fs::remove_file(path).is_ok());
}
}
}
mod when_file_dne {
mod when_bad_permissions {
use file::create;
#[test]
fn it_returns_err() {
assert!(create("/tst.txt", true).is_err());
}
}
mod when_dir_dne {
use dir::delete;
use exists;
use file::create;
#[test]
fn it_returns_ok_and_creates_dir_and_file() {
let dir = "/tmp/dne";
assert!(!exists(dir));
let file = "/tmp/dne/.test";
assert!(create(file, true).is_ok());
assert!(exists(dir));
assert!(exists(file));
assert!(delete(dir).is_ok());
}
}
use std::fs;
use exists;
use file::create;
#[test]
fn it_returns_ok_with_file() {
let path = "/tmp/new_file.txt";
let file = create(path, true);
assert!(file.is_ok());
assert!(file.unwrap().is_some());
assert!(exists(path));
assert!(fs::remove_file(path).is_ok());
}
}
}
mod delete {
mod when_file_dne {
use exists;
use file::delete;
#[test]
fn it_returns_ok() {
let path = "/dne.txt";
assert!(!exists(path));
assert!(delete(path).is_ok());
}
}
use std::fs::File;
use exists;
use file::delete;
#[test]
fn it_returns_ok_and_deletes_file() {
let path = "/tmp/.delete";
let file = File::create(path);
assert!(file.is_ok(), file.err().unwrap().to_string());
assert!(exists(path));
assert!(delete(path).is_ok());
assert!(!exists(path));
}
}
mod read {
mod when_file_dne {
use exists;
use file::read;
#[test]
fn it_returns_err() {
let path = "/dne.txt";
assert!(!exists(path));
assert!(read(path).is_err());
}
}
use std::fs::File;
use std::io::Write;
use file::{delete,read};
#[test]
fn if_returns_ok_with_content() {
let path = "/tmp/.read";
let file = File::create(path);
assert!(file.is_ok(), file.err().unwrap().to_string());
let content = "content";
assert!(file.unwrap().write_all(content.as_bytes()).is_ok());
let read = read(path);
assert!(read.is_ok());
assert_eq!(content, read.unwrap());
assert!(delete(path).is_ok())
}
}
mod write {
mod when_file_dne {
use exists;
use file::{delete,read,write};
#[test]
fn it_returns_ok_and_create_file_with_content() {
let path = "/tmp/.dne";
assert!(!exists(path));
let content = "content";
assert!(write(path, content, false).is_ok());
assert!(exists(path));
let read = read(path);
assert!(read.is_ok());
assert_eq!(content, read.unwrap());
assert!(delete(path).is_ok());
}
}
mod when_content_empty {
use exists;
use file::write;
#[test]
fn it_returns_ok_and_does_not_create_file() {
let path = "/tmp/.write";
assert!(!exists(path));
assert!(write(path, "", false).is_ok());
assert!(!exists(path));
}
}
mod when_truncate_true {
use exists;
use file::{delete,read,write};
#[test]
fn it_returns_ok_and_overwrites_content() {
let path = "/tmp/.write";
let content = "content";
assert!(write(path, content, false).is_ok());
assert!(exists(path));
let mut output = read(path);
assert!(output.is_ok());
assert_eq!(content, output.unwrap());
let overwrite = "overwrite";
assert!(write(path, overwrite, true).is_ok());
output = read(path);
assert!(output.is_ok());
assert_eq!(overwrite, output.unwrap());
assert!(delete(path).is_ok());
}
}
}