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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further testing to oof cli #38

Merged
merged 4 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 19 additions & 28 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub fn parse_args() -> crate::Result<ParsedArgs> {
match &mut parsed_args.command {
Command::Compress { files, .. } | Command::Decompress { files, .. } => {
*files = canonicalize_files(&files)?;
}
_ => {}
},
_ => {},
}
Ok(parsed_args)
}
Expand Down Expand Up @@ -72,7 +72,7 @@ fn canonicalize(path: impl AsRef<Path>) -> crate::Result<PathBuf> {
} else {
Err(io_err.into())
}
}
},
}
}

Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn parse_args_from(mut args: Vec<OsString>) -> crate::Result<ParsedArgs> {

let command = Command::Compress { files, compressed_output_path };
ParsedArgs { command, flags }
}
},
// Defaults to decompression when there is no subcommand
None => {
flags_info.push(arg_flag!('o', "output"));
Expand All @@ -121,16 +121,16 @@ pub fn parse_args_from(mut args: Vec<OsString>) -> crate::Result<ParsedArgs> {
}

// Parse flags
let (files, mut flags) = oof::filter_flags(args, &flags_info)?;
let (files, flags) = oof::filter_flags(args, &flags_info)?;
let files = files.into_iter().map(PathBuf::from).collect();

let output_folder = flags.take_arg("output").map(PathBuf::from);
let output_folder = flags.arg("output").map(PathBuf::from);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabricio7p I have now fixed the --output error, we were using take_arg which removes the flag from our oof::Flags struct, that's why we couldn't find that later.

Not, by using .arg() instead, we clone (kindof) the content, and now the output_folder can be found in our Command::Decompress as well as oof::Flags.

I adapted the tests, and now they are passing correctly, sorry for the late response.


// TODO: ensure all files are decompressible

let command = Command::Decompress { files, output_folder };
ParsedArgs { command, flags }
}
},
_ => unreachable!("You should match each subcommand passed."),
};

Expand All @@ -157,20 +157,14 @@ mod tests {
assert_eq!(test_cli("--help").unwrap().command, Command::ShowHelp);
assert_eq!(test_cli("--version").unwrap().command, Command::ShowVersion);
assert_eq!(test_cli("--version").unwrap().flags, oof::Flags::default());
assert_eq!(
test_cli("foo.zip bar.zip").unwrap().command,
Command::Decompress {
files: vec!["foo.zip".into(), "bar.zip".into()],
output_folder: None
}
);
assert_eq!(
test_cli("compress foo bar baz.zip").unwrap().command,
Command::Compress {
files: vec!["foo".into(), "bar".into()],
compressed_output_path: "baz.zip".into()
}
);
assert_eq!(test_cli("foo.zip bar.zip").unwrap().command, Command::Decompress {
files: vec!["foo.zip".into(), "bar.zip".into()],
output_folder: None
});
assert_eq!(test_cli("compress foo bar baz.zip").unwrap().command, Command::Compress {
files: vec!["foo".into(), "bar".into()],
compressed_output_path: "baz.zip".into()
});
assert_eq!(test_cli("compress").unwrap_err(), crate::Error::MissingArgumentsForCompression);
}

Expand All @@ -186,12 +180,9 @@ mod tests {
// pub argument_flags: BTreeMap<&'static str, OsString>,
// }

assert_eq!(
test_cli("foo bar --output hey.zip").unwrap().flags,
oof::Flags {
boolean_flags: vec!["yes"].into_iter().collect(),
argument_flags: vec![("--output", OsString::from("hey"))].into_iter().collect(),
}
);
assert_eq!(test_cli("foo --yes bar --output folder").unwrap().flags, oof::Flags {
boolean_flags: vec!["yes"].into_iter().collect(),
argument_flags: vec![("output", OsString::from("folder"))].into_iter().collect(),
});
}
}
2 changes: 0 additions & 2 deletions src/oof/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ impl Flags {
pub fn new() -> Self {
Self::default()
}
}

impl Flags {
pub fn is_present(&self, flag_name: &str) -> bool {
self.boolean_flags.contains(flag_name) || self.argument_flags.contains_key(flag_name)
}
Expand Down