Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate directory for output option #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::Path;
use std::process;
use std::fs;
// Modules
use molehill::default_template;
use molehill::template;
Expand Down Expand Up @@ -34,8 +35,13 @@ fn main() {
let output = matches.value_of("output").unwrap();

if !(Path::new(output.clone()).is_dir()) {
eprintln!("Specify directory path for `-o` / `--output` option.");
process::exit(exitcode::USAGE);
match fs::create_dir_all(output) {
Ok(()) => println!("{} is created", output),
Err(e) => {
eprintln!("Error: {}", e);
process::exit(exitcode::IOERR);
}
}
}

if let Some(template) = matches.value_of("template") {
Expand Down
21 changes: 7 additions & 14 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn invvalid_template_option_short() -> Result<(), Box<dyn Error>> {
}

#[test]
fn invvalid_template_option_long() -> Result<(), Box<dyn Error>> {
fn invalid_template_option_long() -> Result<(), Box<dyn Error>> {
let mut cmd = Command::cargo_bin("molehill")?;

cmd.arg("--template").arg("path/to/file.txt");
Expand All @@ -36,23 +36,16 @@ fn invvalid_template_option_long() -> Result<(), Box<dyn Error>> {
Ok(())
}

#[test]
fn invalid_output_option_short() -> Result<(), Box<dyn Error>> {
let mut cmd = Command::cargo_bin("molehill")?;

cmd.arg("-o").arg("path/to/file.txt");
cmd.assert().failure()
.stderr(predicate::str::contains("Specify directory path for `-o` / `--output` option."));
Ok(())
}

#[test]
fn invalid_output_option_long() -> Result<(), Box<dyn Error>> {
let mut cmd = Command::cargo_bin("molehill")?;
let pathname = "path/to/dir";

cmd.arg("--output").arg("path/to/file.txt");
cmd.assert().failure()
.stderr(predicate::str::contains("Specify directory path for `-o` / `--output` option."));
cmd.arg("--output").arg(pathname);
cmd.assert().success()
.stdout(predicate::str::contains(pathname));

clean_dir("path");
Ok(())
}

Expand Down