Skip to content

Commit

Permalink
imp(New): Expand the new command's arguments
Browse files Browse the repository at this point in the history
Snapshot respects -f force overwrites on existing files. Expand the usage to new because I expected it to work and it didn't.
  • Loading branch information
brianp committed Dec 14, 2019
1 parent 95ab410 commit 9f1f04e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 26 deletions.
53 changes: 36 additions & 17 deletions new/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub fn exec(args: Args) -> Result<(), String> {
check_first_run(&project_paths.project_directory)?;

let template = modified_template(TEMPLATE, &project_paths.project_file);
write_template(&template, &project_paths.project_file).unwrap();
write_template(&template, &project_paths.project_file, args.flag_f)?;

println!(
"\u{270C} The template file {} has been written to {}\nHappy tmuxing!",
&project_paths.project_file.display(),
Expand All @@ -46,15 +47,20 @@ fn modified_template(template: &str, file: &PathBuf) -> String {
template.replace("{file}", file.to_str().unwrap())
}

fn write_template(template: &str, path: &PathBuf) -> Result<(), String> {
let path_str = path.to_str().unwrap();
fn write_template<S>(template: S, path: &PathBuf, force: bool) -> Result<(), String>
where
S: Into<String>,
{
let path_str = path.to_str().expect("Path could not be opened");
let mut file = OpenOptions::new()
.write(true)
.create_new(true)
.truncate(force)
.create(force)
.create_new(!force)
.open(path)
.map_err(|e| format!("Could not create the file {}. Error: {}", &path_str, e))?;

file.write_all(template.as_bytes()).map_err(|e| {
file.write_all(template.into().as_bytes()).map_err(|e| {
format!(
"Could not write contents of template to the file {}. Error {}",
&path_str, e
Expand All @@ -72,7 +78,6 @@ mod test {
use common::rand_names;
use std::fs::File;
use std::fs;
use std::io::Read;
use super::*;

#[test]
Expand Down Expand Up @@ -118,7 +123,7 @@ mod test {
#[test]
fn expect_ok_result_when_path_exists() {
let path = rand_names::project_file_with_dir("/tmp");
let result = write_template(&"test template".to_string(), &path);
let result = write_template(&"test template".to_string(), &path, false);
let _ = fs::remove_file(path);
println!("{:?}", result);
assert!(result.is_ok());
Expand All @@ -127,14 +132,14 @@ mod test {
#[test]
fn expect_err_result_when_path_does_not_exist() {
let path = rand_names::project_file_path();
let result = write_template(&"test template".to_string(), &path);
let result = write_template(&"test template".to_string(), &path, false);
assert!(result.is_err());
}

#[test]
fn expect_new_file_to_exist() {
let path = rand_names::project_file_with_dir("/tmp");
let _ = write_template(&"test template".to_string(), &path);
let _ = write_template(&"test template".to_string(), &path, false);
let result = &path.exists();
let _ = fs::remove_file(&path);
assert!(result);
Expand All @@ -143,7 +148,7 @@ mod test {
#[test]
fn expect_new_file_not_to_exist() {
let path = rand_names::project_file_path();
let _ = write_template(&"test template".to_string(), &path);
let _ = write_template(&"test template".to_string(), &path, false);
assert!(!path.exists());
}

Expand All @@ -156,14 +161,28 @@ mod test {
let _ = buffer.sync_all();

// Attempt to create the same named file with new content
let _ = write_template(&"new_content".to_string(), &path);
let _ = write_template(&"new_content".to_string(), &path, false);

let content = fs::read_to_string(&path).unwrap();

assert_eq!(content, "original content");
let _ = fs::remove_file(&path);
}

#[test]
fn expect_truncation_or_overwrite() {
// Write a file with content
let path = rand_names::project_file_with_dir("/tmp");
let mut buffer = File::create(&path).unwrap();
let _ = buffer.write(b"original content");
let _ = buffer.sync_all();

// Attempt to create the same named file with new content
let _ = write_template(&"new content".to_string(), &path, true);

// Read the file content
let mut f = File::open(&path).unwrap();
let mut s = String::new();
let _ = f.read_to_string(&mut s);
let content = fs::read_to_string(&path).unwrap();

assert_eq!(s, "original content");
assert_eq!(content, "new content");
let _ = fs::remove_file(&path);
}

Expand All @@ -173,7 +192,7 @@ mod test {
let mut buffer = File::create(&path).unwrap();
let _ = buffer.write(b"original content");
let _ = buffer.sync_all();
let result = write_template(&"new_content".to_string(), &path);
let result = write_template(&"new content".to_string(), &path, false);

assert!(result.is_err());
let _ = fs::remove_file(&path);
Expand Down
56 changes: 47 additions & 9 deletions new/tests/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,66 @@ mod test {
use std::fs;
use std::path::Path;

pub fn new(project_path: &Path) -> Result<(), String> {
fn cleanup(config_path: &Path) {
let _ = fs::remove_file(config_path);
let _ = fs::remove_dir(config_path.parent().unwrap());
}

#[test]
fn creates_new_file() {
let project_path = rand_names::project_file_path();

let args = Args {
flag_p: Some(project_path.parent().unwrap().display().to_string()),
arg_project: project_path.file_name().unwrap().to_str().unwrap().to_string(),
cmd_new: true,
flag_p: Some(project_path.parent().unwrap().display().to_string()),
..Default::default()
};

new::exec(args)
assert!(new::exec(args).is_ok());
assert!(&project_path.exists());

cleanup(&project_path);
}

fn cleanup(config_path: &Path) {
let _ = fs::remove_file(config_path);
let _ = fs::remove_dir(config_path.parent().unwrap());
#[test]
fn overwrites_existing_file() {
let project_path = rand_names::project_file_path();
let _ = fs::create_dir(project_path.parent().as_ref().unwrap());
let _ = fs::File::create(&project_path);

let args = Args {
arg_project: project_path.file_name().unwrap().to_str().unwrap().to_string(),
cmd_new: true,
flag_f: true,
flag_p: Some(project_path.parent().unwrap().display().to_string()),
..Default::default()
};

assert!(new::exec(args).is_ok());

let contents = fs::read_to_string(&project_path).unwrap();

let name = format!("# {}", project_path.display());
assert!(contents.contains(&name));

cleanup(&project_path);
}

#[test]
fn creates_new_file() {
fn fails_to_write_over_existing_file() {
let project_path = rand_names::project_file_path();
let _ = fs::create_dir(project_path.parent().as_ref().unwrap());
let _ = fs::File::create(&project_path);

assert!(new(&project_path).is_ok());
assert!(&project_path.exists());
let args = Args {
arg_project: project_path.file_name().unwrap().to_str().unwrap().to_string(),
cmd_new: true,
flag_p: Some(project_path.parent().unwrap().display().to_string()),
..Default::default()
};

assert!(new::exec(args).is_err());

cleanup(&project_path);
}
Expand Down

0 comments on commit 9f1f04e

Please # to comment.