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

fix: default_value_os_t #3834

Merged
merged 2 commits into from Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion clap_derive/src/attrs.rs
Expand Up @@ -594,7 +594,7 @@ impl Attrs {
quote_spanned!(ident.span()=> {
static DEFAULT_VALUE: clap::once_cell::sync::Lazy<::std::ffi::OsString> = clap::once_cell::sync::Lazy::new(|| {
let val: #ty = #val;
::std::ffi::OsString = val.into()
::std::ffi::OsString::from(val)
});
&*DEFAULT_VALUE
})
Expand Down
26 changes: 26 additions & 0 deletions tests/derive/default_value.rs
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use clap::{CommandFactory, Parser};

use crate::utils;
Expand Down Expand Up @@ -44,6 +46,30 @@ fn auto_default_value_t() {
assert!(help.contains("[default: 0]"));
}

#[test]
fn default_value_os_t() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(value_parser, default_value_os_t = PathBuf::from("abc.def"))]
arg: PathBuf,
}
assert_eq!(
Opt {
arg: PathBuf::from("abc.def")
},
Opt::try_parse_from(&["test"]).unwrap()
);
assert_eq!(
Opt {
arg: PathBuf::from("ghi")
},
Opt::try_parse_from(&["test", "ghi"]).unwrap()
);

let help = utils::get_long_help::<Opt>();
assert!(help.contains("[default: abc.def]"));
}

#[test]
fn detect_os_variant() {
#![allow(unused_parens)] // needed for `as_ref` call
Expand Down