Skip to content

Commit

Permalink
refactor around determine_output_dir()
Browse files Browse the repository at this point in the history
- fix typo in test case name
- change argument type
   - Before: (Option<&PathBuf>, &String, BuildTarget) -> PathBuf
   - After: (Option<&PathBuf>, &str, BuildTarget) -> PathBuf
  • Loading branch information
cat-in-136 committed Aug 19, 2023
1 parent b96b5d7 commit f1f73d0
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ fn collect_metadata(args: &Cli) -> Vec<config::ExtraMetadataSource> {
.collect::<Vec<_>>()
}

fn determine_output_dir(
output: Option<&PathBuf>,
file_name: &str,
build_target: BuildTarget,
) -> PathBuf {
match output.as_ref().map(PathBuf::from) {
Some(path) if path.is_dir() => path.join(file_name),
Some(path) => path,
None => build_target.target_path("generate-rpm").join(file_name),
}
}

fn run() -> Result<(), Error> {
let mut args = std::env::args();
let args = if let Some("generate-rpm") = args.nth(1).as_deref() {
Expand Down Expand Up @@ -91,18 +103,6 @@ fn run() -> Result<(), Error> {
Ok(())
}

fn determine_output_dir(
output: Option<&PathBuf>,
file_name: &String,
build_target: BuildTarget,
) -> PathBuf {
match output.as_ref().map(PathBuf::from) {
Some(path) if path.is_dir() => path.join(file_name),
Some(path) => path,
None => build_target.target_path("generate-rpm").join(file_name),
}
}

fn main() {
if let Err(err) = run() {
eprintln!("{err}");
Expand All @@ -118,25 +118,25 @@ mod tests {
// 2. Output is a file
// 3. Output is not specified
#[test]
fn test_ouput_is_dir() {
fn test_output_is_dir() {
let tempdir = tempfile::tempdir().unwrap();
let pathbufbinding = &tempdir.path().to_path_buf();

let output = Some(pathbufbinding);
let file_name = String::from("test.rpm");
let file_name = "test.rpm";
let build_target = BuildTarget::new(&crate::cli::Cli::default());

let target_file_name = determine_output_dir(output, &file_name, build_target);
assert_eq!(target_file_name, tempdir.path().join("test.rpm"));
}
#[test]
fn test_ouput_is_file() {
fn test_output_is_file() {
let tempdir = tempfile::tempdir().unwrap();
let pathbufbinding = &tempdir.path().to_path_buf();
let temppath = pathbufbinding.join("foo.rpm");

let output = Some(&temppath);
let file_name = String::from("test.rpm");
let file_name = "test.rpm";
let build_target = BuildTarget::new(&crate::cli::Cli::default());

let target_file_name = determine_output_dir(output, &file_name, build_target);
Expand All @@ -146,7 +146,7 @@ mod tests {
#[test]
fn test_no_output_specified() {
let output = None;
let file_name = String::from("test.rpm");
let file_name = "test.rpm";
let build_target = BuildTarget::new(&crate::cli::Cli::default());

let target_file_name = determine_output_dir(output, &file_name, build_target);
Expand Down

0 comments on commit f1f73d0

Please sign in to comment.